philsupertramp/game-math
Loading...
Searching...
No Matches
Operand.h
Go to the documentation of this file.
1#pragma once
2
3
4#include "MathNode.h"
5#include <cmath>
6#include <cstdlib>
7#include <map>
8#include <memory>
9#include <string>
10#include <unordered_set>
15class Operand : public MathNode
16{
17public:
22 explicit Operand(const std::string& name)
23 : MathNode(name) {
24 connectionType = NodeConnectionType::ConnectionType_None;
25 }
26};
27
33class Symbolic : public Operand
34{
35public:
37 double evaluationValue = 1.0;
38
43 explicit Symbolic(const std::string& name, double defaultValue = 0.0)
44 : Operand(name)
45 , evaluationValue(defaultValue) {
46 type = MathNodeType::NodeType_Symbolic;
47 }
48
53 Symbolic(const Symbolic& other)
54 : Operand(other) {
55 type = other.type;
57 }
58
63 [[nodiscard]] double Evaluate() const override { return isNegative ? evaluationValue * (-1.0) : evaluationValue; }
64};
65
66static std::map<std::string, std::shared_ptr<Symbolic>> DefaultSymbols = {
67 { "pi", std::make_shared<Symbolic>("pi", 3.1415926535897932384626433832795028841971693993751058209749445923078164) },
68 { "e", std::make_shared<Symbolic>("e", 2.71828182845904523536028747135266249775724709369995957496696762772407663) },
69};
70
71bool isConstant(const std::string& in);
72bool hasSymbol(const std::vector<std::shared_ptr<Symbolic>>& container, const std::shared_ptr<Symbolic>& sym);
73
77class Number : public Operand
78{
81
82public:
87 explicit Number(const std::string& val)
88 : Operand(val) {
89 type = MathNodeType::NodeType_Numeric;
90 numericValue = strtod(value, nullptr);
91 isNegative = (numericValue > 0) ? false : ((numericValue < 0) ? true : false);
93 std::string realValue = std::to_string(numericValue);
94 value = new char[realValue.size() + 1];
95 std::copy(realValue.begin(), realValue.end(), value);
96 value[realValue.size()] = '\0'; // don't forget the terminating 0
97 }
98
103 [[nodiscard]] double Evaluate() const override { return isNegative ? numericValue * (-1.0) : numericValue; }
104};
105
static std::map< std::string, std::shared_ptr< Symbolic > > DefaultSymbols
Definition: Operand.h:66
bool isConstant(const std::string &in)
bool hasSymbol(const std::vector< std::shared_ptr< Symbolic > > &container, const std::shared_ptr< Symbolic > &sym)
Definition: MathNode.h:43
bool isNegative
helper to determine whether a node is negated or not
Definition: MathNode.h:46
char * value
char representation of value
Definition: MathNode.h:58
MathNodeType type
node type representation
Definition: MathNode.h:50
NodeConnectionType connectionType
type of connection allowed for this node
Definition: MathNode.h:52
Definition: Operand.h:78
Number(const std::string &val)
Definition: Operand.h:87
double Evaluate() const override
Definition: Operand.h:103
double numericValue
real numerical value
Definition: Operand.h:80
Definition: Operand.h:16
Operand(const std::string &name)
Definition: Operand.h:22
Definition: Operand.h:34
double Evaluate() const override
Definition: Operand.h:63
Symbolic(const Symbolic &other)
Definition: Operand.h:53
Symbolic(const std::string &name, double defaultValue=0.0)
Definition: Operand.h:43
double evaluationValue
value which replaces symbol during evaluation
Definition: Operand.h:37