-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmtrk_equations.cpp
126 lines (98 loc) · 2.34 KB
/
mtrk_equations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "mtrk_common.h"
#include "mtrk_equations.h"
#include "mtrk_api.h"
using namespace SEQ_NAMESPACE;
mtrk_state* stateInstance=0;
mtrk_equations::mtrk_equations()
{
parsedFunctions=0;
count=0;
equations=0;
}
mtrk_equations::~mtrk_equations()
{
clear();
}
void mtrk_equations::setStateInstance(mtrk_state* pointer)
{
stateInstance=pointer;
}
void mtrk_equations::clear()
{
if (parsedFunctions!=0)
{
for (int i=0; i<count; i++)
{
if (parsedFunctions[i]!=0)
{
delete parsedFunctions[i];
parsedFunctions[i]=0;
}
}
delete[] parsedFunctions;
parsedFunctions=0;
}
equations=0;
}
double getCounter(const double* p)
{
int index=int(p[0]);
if ((index<0) || (index>=MTRK_DEFS_COUNTERS))
{
return -1;
}
return stateInstance->counters[index];
}
double getFloat(const double* p)
{
int index=int(p[0]);
if ((index<0) || (index>=MTRK_DEFS_FLOATS))
{
return -1;
}
return stateInstance->floats[index];
}
bool mtrk_equations::prepare(cJSON* section)
{
clear();
cJSON* entry=0;
cJSON_ArrayForEach(entry, section)
{
count++;
}
parsedFunctions=new FunctionParser*[count];
for (int i=0; i<count; i++)
{
parsedFunctions[i]=new FunctionParser;
}
int index=0;
entry=0;
cJSON_ArrayForEach(entry, section)
{
MTRK_GETITEM(entry, MTRK_PROPERTIES_EQUATION, equation)
std::string equation_str(equation->valuestring);
parsedFunctions[index]->AddFunction("ctr", getCounter, 1);
parsedFunctions[index]->AddFunction("flt", getFloat, 1);
parsedFunctions[index]->Parse(equation_str, "x");
cJSON_AddNumberToObject(entry, MTRK_PROPERTIES_MEMINDEX, index);
index++;
}
MTRK_LOG("Equation: " << count)
equations=section;
return true;
}
double mtrk_equations::evaluate(char* name)
{
cJSON* equation = cJSON_GetObjectItemCaseSensitive(equations, name);
if (equation==NULL)
{
return 0.;
}
cJSON* index = cJSON_GetObjectItemCaseSensitive(equation, MTRK_PROPERTIES_MEMINDEX);
if (index==NULL)
{
return 0.;
}
double variables[1] = { 1.0 };
return parsedFunctions[index->valueint]->Eval(variables);
}