philsupertramp/game-math
Loading...
Searching...
No Matches
Operator.h
Go to the documentation of this file.
1#pragma once
2#include "MathNode.h"
3#include <cassert>
4#include <cmath>
5#include <iostream>
6#include <map>
7#include <memory>
8#include <unordered_set>
9
10
22};
23
28 TYPE_ADDITION = 0, // ConnectionType_Dual
29 TYPE_SUBTRACTION = 1, // ConnectionType_Dual
30 TYPE_MULTIPLICATION = 2, // ConnectionType_Dual
31 TYPE_DIVISION = 3, // ConnectionType_Dual
32 TYPE_POWER = 4, // ConnectionType_Dual
33 TYPE_PARENTHESES_OPEN = 5, // ConnectionType_None
34 TYPE_PARENTHESES_CLOSE = 6, // ConnectionType_None
35};
36
40enum OperatorValue : char {
47 // l / r
55};
56
60class Operator : public MathNode
61{
62public:
64 std::function<double(double, double)> op;
73 Operator(const std::string& name, std::function<double(double, double)> fun, OperatorPriority operatorPriority)
74 : MathNode(name)
75 , op(std::move(fun))
76 , priority(operatorPriority) {
77 type = MathNodeType::NodeType_Operator;
78 }
79
84 Operator(const Operator& other)
85 : MathNode(other) {
86 op = other.op;
87 priority = other.priority;
88 }
89
94 [[nodiscard]] double Evaluate() const override {
95 if(priority == OPClassParentheses) { return left->Evaluate(); }
96 assert(left != nullptr && right != nullptr);
97 return op(left->Evaluate(), right->Evaluate());
98 }
99};
100
106std::shared_ptr<Operator> GenerateOperator(OperatorType type);
107
108
117class Function : public Operator
118{
119public:
125 Function(const std::string& val, const std::function<double(double)>& fun)
126 : Operator(
127 val, [fun](double a, [[maybe_unused]] double b) { return fun(a); }, OperatorPriority::OPClassFunction) {
128 connectionType = NodeConnectionType::ConnectionType_Left;
129 type = MathNodeType::NodeType_Functional;
130 }
131
139 Function(const std::string& val, const std::function<double(double, double)>& fun)
141 connectionType = NodeConnectionType::ConnectionType_Dual;
142 type = MathNodeType::NodeType_Functional;
143 std::cerr << "This can't be parsed yet!!!!!!" << std::endl;
144 }
145
146 [[nodiscard]] double Evaluate() const override {
147 if(connectionType == NodeConnectionType::ConnectionType_Left) return op(left->Evaluate(), 0.0);
153 return NAN;
154 }
155
156 // TODO: add beautified string representation
157};
161const std::map<std::string, std::shared_ptr<Function>> DefaultFunctions = {
162 { "sqrt", std::make_shared<Function>("sqrt", [](double a) { return sqrt(a); }) },
163 { "log", std::make_shared<Function>("log", [](double a) { return log(a); }) },
164 { "sin", std::make_shared<Function>("sin", [](double a) { return sin(a); }) },
165 { "cos", std::make_shared<Function>("cos", [](double a) { return cos(a); }) },
166};
172bool isFunction(const std::string& in);
173
std::shared_ptr< Operator > GenerateOperator(OperatorType type)
OperatorPriority
Definition: Operator.h:15
@ OPClassLine
Definition: Operator.h:17
@ OPClassUnknown
Definition: Operator.h:16
@ OPClassPow
Definition: Operator.h:19
@ OPClassFunction
Definition: Operator.h:21
@ OPClassParentheses
Definition: Operator.h:20
@ OPClassDot
Definition: Operator.h:18
OperatorType
Definition: Operator.h:27
@ TYPE_POWER
Definition: Operator.h:32
@ TYPE_MULTIPLICATION
Definition: Operator.h:30
@ TYPE_DIVISION
Definition: Operator.h:31
@ TYPE_SUBTRACTION
Definition: Operator.h:29
@ TYPE_ADDITION
Definition: Operator.h:28
@ TYPE_PARENTHESES_CLOSE
Definition: Operator.h:34
@ TYPE_PARENTHESES_OPEN
Definition: Operator.h:33
const std::map< std::string, std::shared_ptr< Function > > DefaultFunctions
Definition: Operator.h:161
OperatorValue
Definition: Operator.h:40
@ VALUE_ADDITION
l + r
Definition: Operator.h:42
@ VALUE_POWER
l^r
Definition: Operator.h:50
@ VALUE_PARENTHESES_CLOSE
)
Definition: Operator.h:54
@ VALUE_MULTIPLICATION
l * r
Definition: Operator.h:46
@ VALUE_PARENTHESES_OPEN
(
Definition: Operator.h:52
@ VALUE_DIVISION
Definition: Operator.h:48
@ VALUE_SUBTRACTION
l - r
Definition: Operator.h:44
bool isFunction(const std::string &in)
Definition: Operator.h:118
double Evaluate() const override
Definition: Operator.h:146
Function(const std::string &val, const std::function< double(double, double)> &fun)
Definition: Operator.h:139
Function(const std::string &val, const std::function< double(double)> &fun)
Definition: Operator.h:125
Definition: MathNode.h:43
std::shared_ptr< MathNode > left
next node on left side
Definition: MathNode.h:54
MathNodeType type
node type representation
Definition: MathNode.h:50
std::shared_ptr< MathNode > right
next node on right side
Definition: MathNode.h:56
NodeConnectionType connectionType
type of connection allowed for this node
Definition: MathNode.h:52
Definition: Operator.h:61
std::function< double(double, double)> op
Operator implementation, this will be evaluated during Evaluate calls.
Definition: Operator.h:64
OperatorPriority priority
Operator priority, used to create AST.
Definition: Operator.h:66
double Evaluate() const override
Definition: Operator.h:94
Operator(const Operator &other)
Definition: Operator.h:84
Operator(const std::string &name, std::function< double(double, double)> fun, OperatorPriority operatorPriority)
Definition: Operator.h:73
Definition: vec3.h:360