summaryrefslogtreecommitdiffstats
path: root/Project/MathExpression.cpp
diff options
context:
space:
mode:
authorThales Lima Oliveira <thaleslima.ufu@gmail.com>2017-12-21 02:14:45 -0200
committerThales Lima Oliveira <thaleslima.ufu@gmail.com>2017-12-21 02:14:45 -0200
commitb32c2281cff97d1902f963b782c1b10342945f77 (patch)
treedd64655d86f9b59b882720938ae002a31f1835cd /Project/MathExpression.cpp
parentc6d815b6b11504be68d7a97df63cfef749624418 (diff)
downloadPSP.git-b32c2281cff97d1902f963b782c1b10342945f77.tar.gz
PSP.git-b32c2281cff97d1902f963b782c1b10342945f77.tar.xz
PSP.git-b32c2281cff97d1902f963b782c1b10342945f77.zip
Math expression solver implemented
Diffstat (limited to 'Project/MathExpression.cpp')
-rw-r--r--Project/MathExpression.cpp14
1 files changed, 9 insertions, 5 deletions
diff --git a/Project/MathExpression.cpp b/Project/MathExpression.cpp
index 5bb69d6..920d168 100644
--- a/Project/MathExpression.cpp
+++ b/Project/MathExpression.cpp
@@ -155,12 +155,14 @@ void MathExpression::Rotate(bool clockwise)
bool MathExpression::Solve(double input, double timeStep, double currentTime)
{
// Get the input vector from connection lines (can't use default (one) input argument)
- std::vector<double> inputVector;
+ m_inputValues[0] = currentTime;
+ m_inputValues[1] = timeStep;
+ int i = 2;
for(auto itN = m_nodeList.begin(), itNEnd = m_nodeList.end(); itN != itNEnd; ++itN) {
Node* node = *itN;
if(node->GetNodeType() != Node::NODE_OUT) {
if(!node->IsConnected()) {
- inputVector.push_back(0.0); // Node not connected means zero value as input.
+ m_inputValues[i] = 0.0; // Node not connected means zero value as input.
} else {
for(auto itC = m_childList.begin(), itCEnd = m_childList.end(); itC != itCEnd; ++itC) {
ConnectionLine* cLine = static_cast<ConnectionLine*>(*itC);
@@ -168,17 +170,20 @@ bool MathExpression::Solve(double input, double timeStep, double currentTime)
for(auto itCN = nodeList.begin(), itCNEnd = nodeList.end(); itCN != itCNEnd; ++itCN) {
Node* childNode = *itCN;
if(childNode == node) {
- inputVector.push_back(cLine->GetValue());
+ m_inputValues[i] = cLine->GetValue();
break;
}
}
}
}
+ ++i;
}
}
- if(m_variablesVector.size() != inputVector.size()) return false;
// Solve the math expression using fparser
+ double result = m_fparser.Eval(m_inputValues);
+ if(m_fparser.EvalError() != 0) return false;
+ m_output = result;
return true;
}
@@ -318,6 +323,5 @@ bool MathExpression::Initialize()
// Optimize only once to gain performance.
m_fparser.Optimize();
-
return true;
}