math.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * math.cpp - math operation nodes
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <cmath>
  19. #include <boost/math/constants/constants.hpp>
  20. #include <2geom/point.h>
  21. #include <core/node_info.h>
  22. #include <core/node/node.h>
  23. #include <core/node/property.h>
  24. using boost::math::double_constants::pi;
  25. namespace rainynite::core {
  26. namespace nodes {
  27. class Add : public Node<double> {
  28. public:
  29. Add() {
  30. init<double>(a, 0);
  31. init<double>(b, 0);
  32. }
  33. public:
  34. double get(shared_ptr<Context> ctx) const override {
  35. try {
  36. return get_a()->get(ctx) + get_b()->get(ctx);
  37. } catch (...) {
  38. return {};
  39. }
  40. }
  41. private:
  42. NODE_PROPERTY(a, double);
  43. NODE_PROPERTY(b, double);
  44. };
  45. REGISTER_NODE(Add);
  46. // TODO: generic Add node
  47. class PointAdd : public Node<Geom::Point> {
  48. public:
  49. PointAdd() {
  50. init<Geom::Point>(a, {});
  51. init<Geom::Point>(b, {});
  52. }
  53. public:
  54. Geom::Point get(shared_ptr<Context> ctx) const override {
  55. try {
  56. return get_a()->get(ctx) + get_b()->get(ctx);
  57. } catch (...) {
  58. return {};
  59. }
  60. }
  61. private:
  62. NODE_PROPERTY(a, Geom::Point);
  63. NODE_PROPERTY(b, Geom::Point);
  64. };
  65. REGISTER_NODE(PointAdd);
  66. class Multiply : public Node<double> {
  67. public:
  68. Multiply() {
  69. init<double>(a, 0);
  70. init<double>(b, 0);
  71. }
  72. public:
  73. double get(shared_ptr<Context> ctx) const override {
  74. try {
  75. return get_a()->get(ctx) * get_b()->get(ctx);
  76. } catch (...) {
  77. return {};
  78. }
  79. }
  80. private:
  81. NODE_PROPERTY(a, double);
  82. NODE_PROPERTY(b, double);
  83. };
  84. REGISTER_NODE(Multiply);
  85. class Sin : public Node<double> {
  86. public:
  87. Sin() {
  88. init<double>(turns, 0);
  89. }
  90. public:
  91. double get(shared_ptr<Context> ctx) const override {
  92. try {
  93. return std::sin(get_turns()->get(ctx) * pi * 2);
  94. } catch (...) {
  95. return {};
  96. }
  97. }
  98. private:
  99. NODE_PROPERTY(turns, double);
  100. };
  101. REGISTER_NODE(Sin);
  102. } // namespace nodes
  103. } // namespace rainynite::core