ModuleMath.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "ModuleBase.h"
  2. #include <iostream>
  3. #include <cmath>
  4. #include <json.hpp>
  5. namespace mdd {
  6. class ModuleMath : public ModuleBase {
  7. private:
  8. const std::map<std::string, std::function<double(double, double)>> _ops = {
  9. {"add", [](double a, double b) {return a + b; }},
  10. {"subtract", [](double a, double b) {return a - b; }},
  11. {"multiply", [](double a, double b) {return a * b; }},
  12. {"divide", [](double a, double b) {return a / b; }},
  13. {"power", [](double a, double b) {return pow(a, b); }},
  14. {"logarithm", [](double a, double b) {return log(b) / log(a); }},
  15. {"minimum", [](double a, double b) {return std::min(a, b); }},
  16. {"maximum", [](double a, double b) {return std::max(a, b); }},
  17. {"lesser", [](double a, double b) {return a > b; }},
  18. {"greater", [](double a, double b) {return a < b; }}
  19. };
  20. std::function<double(double, double)> _operation;
  21. std::vector<double> applyOperation(const std::vector<double>& val1, const std::vector<double>& val2);
  22. public:
  23. ModuleMath();
  24. bool configure(const std::string& config) override;
  25. std::string getConfiguration();
  26. state update() override;
  27. };
  28. ModuleMath::ModuleMath()
  29. : ModuleBase(R"JSON(
  30. [{
  31. "name":"operation",
  32. "value":"add",
  33. "options": [
  34. "add",
  35. "subtract",
  36. "multiply",
  37. "divide",
  38. "power",
  39. "logarithm",
  40. "minimum",
  41. "maximum",
  42. "lesser",
  43. "greater"
  44. ]
  45. }])JSON")
  46. {
  47. _ops.at("add");
  48. _operation = _ops.find("add")->second;
  49. std::vector<double> default_val = {1};
  50. inputs.push_back(std::make_shared<Input>(this, "Value", 0, default_val));
  51. inputs.push_back(std::make_shared<Input>(this, "Value", 1, default_val));
  52. outputs.push_back(std::make_shared<Output>(this, "Value", 0, default_val));
  53. setType("Math");
  54. }
  55. bool ModuleMath::configure(const std::string& config)
  56. {
  57. json config_parsed = json::parse(config);
  58. for (size_t i = 0; i < config_parsed.size(); i++)
  59. {
  60. if (config_parsed[i].contains("name"))
  61. {
  62. if (config_parsed[i]["name"].get<std::string>() == "operation")
  63. {
  64. const std::string op = config_parsed[i]["value"].get<std::string>();
  65. auto it = _ops.find(op);
  66. if (it != _ops.end())
  67. {
  68. _operation = it->second;
  69. return true;
  70. }
  71. }
  72. }
  73. }
  74. return false;
  75. }
  76. std::string ModuleMath::getConfiguration() {
  77. json ret = json::parse(ModuleBase::getConfiguration());
  78. for (auto it = _ops.begin(); it != _ops.end(); it++)
  79. {
  80. if (it->second(256,2) == _operation(256, 2)) {
  81. ret[0]["value"] = it->first;
  82. break;
  83. }
  84. }
  85. return ret.dump();
  86. }
  87. std::vector<double> ModuleMath::applyOperation(const std::vector<double>&val1, const std::vector<double>&val2) {
  88. std::vector<double> ret; //= json::array();
  89. size_t length= std::max(val1.size(), val2.size());
  90. for (int i = 0; i < length; i++) {
  91. if (val1.size() == val2.size()) {
  92. ret.push_back(_operation(val1[i], val2[i]));
  93. }
  94. else if (1 == val1.size()) {
  95. ret.push_back(_operation(val1[0], val2[i]));
  96. }
  97. else if (1 == val2.size()) {
  98. ret.push_back(_operation(val1[i], val2[0]));
  99. }
  100. }
  101. return ret;
  102. }
  103. state ModuleMath::update() {
  104. std::vector<double> ret = applyOperation(getInput(0)->getValue(), getInput(1)->getValue());
  105. return getOutput(0)->setValue(ret);
  106. }
  107. }
  108. //gdb
  109. //catch throw
  110. //run