forked from ChrisVeigl/BrainBay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ob_evaluator.cpp
225 lines (189 loc) · 6.12 KB
/
ob_evaluator.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* -----------------------------------------------------------------------------
BrainBay - Evaluator - Object
Provides an interface to the GNU LibMatheval- Library for Expression Evaluation
Authors: Jeremy Wilkerson, Chris Veigl, Aleksandar B. Samardz (libmatheval)
Link to GNU-Libmatheval: http://www.gnu.org/software/libmatheval
Description:
String representation of function is allowed to consist of decimal constants,
variables, elementary functions, unary and binary operations.
Variable name is any combination of alphanumericals and _ characters beginning
with a non-digit that is not elementary function name.
Supported elementary functions are
(names that should be used are given in parenthesis):
exponential (exp), logarithmic (log), square root (sqrt), sine (sin), cosine (cos),
tangent (tan), cotangent (cot), secant (sec), cosecant (csc),
inverse sine (asin), inverse cosine (acos), inverse tangent (atan),
inverse cotangent (acot), inverse secant (asec), inverse cosecant (acsc),
hyperbolic sine (sinh), cosine (cosh), hyperbolic tangent (tanh),
hyperbolic cotangent (coth), hyperbolic secant (sech), hyperbolic cosecant (csch),
hyperbolic inverse sine (asinh), hyperbolic inverse cosine (acosh),
hyperbolic inverse tangent (atanh), hyperbolic inverse cotangent (acoth),
hyperbolic inverse secant (asech), hyperbolic inverse cosecant (acsch),
absolute value (abs), Heaviside step function (step) with value 1 defined
for x = 0 and Dirac delta function with infinity (delta) and
not-a-number (nandelta) values defined for x = 0.
Supported unary operation is unary minus ('-').
Supported binary operations are:
addition ('+'), subtraction ('+'), multiplication ('*'),
division multiplication ('/') and exponentiation ('^').
Usual mathematical rules regarding operation precedence apply.
Parenthesis ('(' and ')') could be used to change priority order.
Blanks and tab characters are allowed in string representing function;
newline characters must not appear in this string.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; See the
GNU General Public License for more details.
----------------------------------------------------------------------------- */
#include "brainBay.h"
#include "ob_evaluator.h"
#include "matheval.h"
EVALOBJ::EVALOBJ(int num) : BASE_CL()
{
outports = 1;
inports=1;
width = 70;
strcpy(in_ports[0].in_name,"A");
strcpy(in_ports[1].in_name,"B");
strcpy(in_ports[2].in_name,"C");
strcpy(in_ports[3].in_name,"D");
strcpy(in_ports[4].in_name,"E");
strcpy(in_ports[5].in_name,"F");
strcpy(out_ports[0].out_name,"out");
inputA = 0;
inputB = 0;
inputC = 0;
inputD = 0;
inputE = 0;
inputF = 0;
expression = NULL; evaluator = NULL;
setexp=FALSE;
setExpression("A");
}
void EVALOBJ::update_inports(void)
{
int i,z,m;
m=-1;
for (i=0;i<GLOBAL.objects;i++)
for (z=0;objects[i]->out[z].to_port!=-1;z++)
if ((objects[objects[i]->out[z].to_object]==this)&&(objects[i]->out[z].to_port>m)) m=objects[i]->out[z].to_port;
inports=m+2;
if (inports>6) inports=6;
height=CON_START+inports*CON_HEIGHT+5;
InvalidateRect(ghWndMain,NULL,TRUE);
}
void EVALOBJ::make_dialog(void)
{
display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_EVALBOX, ghWndStatusbox, (DLGPROC)EvalDlgHandler));
}
void EVALOBJ::load(HANDLE hFile)
{
char expr[MAXEXPRLENGTH];
load_object_basics(this);
load_property("expression",P_STRING,expr);
setExpression(expr);
}
void EVALOBJ::save(HANDLE hFile)
{
save_object_basics(hFile, this);
save_property(hFile,"expression",P_STRING,expression);
}
void EVALOBJ::incoming_data(int port, float value)
{
if (value!=INVALID_VALUE)
switch (port)
{
case 0:
inputA = value;
break;
case 1:
inputB = value;
break;
case 2:
inputC = value;
break;
case 3:
inputD = value;
break;
case 4:
inputE = value;
break;
case 5:
inputF = value;
break;
}
}
void EVALOBJ::setExpression(char *expr)
{
int len = strlen(expr);
if (expression != NULL)
delete expression;
expression = new char[len+1];
strcpy(expression, expr);
setexp=TRUE;
if (evaluator != NULL)
evaluator_destroy(evaluator);
evaluator = evaluator_create(expr);
setexp=FALSE;
if (evaluator == NULL)
report_error("Unable to parse expression");
}
void EVALOBJ::work(void)
{
static char *names[] = {"A", "B", "C", "D", "E", "F"};
static double values[NUMINPUTS];
double result=INVALID_VALUE;
if ((evaluator != NULL)&&(!setexp))
{
values[0] = inputA;
values[1] = inputB;
values[2] = inputC;
values[3] = inputD;
values[4] = inputE;
values[5] = inputF;
result = evaluator_evaluate(evaluator, NUMINPUTS, names, values);
}
pass_values(0, (float)result);
}
EVALOBJ::~EVALOBJ()
{
if (evaluator != NULL) evaluator_destroy(evaluator);
if (expression != NULL) delete expression;
}
LRESULT CALLBACK EvalDlgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool init;
EVALOBJ * st;
st = (EVALOBJ *) actobject;
if ((st==NULL)||(st->type!=OB_EVAL)) return(FALSE);
switch( message )
{
case WM_INITDIALOG:
init = true;
if (st->expression != NULL)
SetDlgItemText(hDlg, IDC_EVALEXPRESSION, st->expression);
init = false;
case WM_CLOSE:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_EVALAPPLY:
char strExpr[MAXEXPRLENGTH+1];
GetDlgItemText(hDlg, IDC_EVALEXPRESSION, strExpr, MAXEXPRLENGTH);
st->setExpression(strExpr);
strncpy(st->tag, st->expression,12);
st->tag[12]='.';st->tag[13]='.';st->tag[14]=0;
InvalidateRect(ghWndDesign,NULL,TRUE);
break;
}
return TRUE;
break;
case WM_SIZE:
case WM_MOVE: update_toolbox_position(hDlg);
break;
}
return FALSE;
}