Project

General

Profile

BitmapToolkit Scol plugin
MathToolkit.cpp
Go to the documentation of this file.
1#include "Prerequisites.h"
2#include "Smoothers.h"
3
11
12int Random(int mi, int mx)
13{
14 int d = mx - mi + 1;
15 return static_cast<int>(((static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0)) * d) + mi);
16}
17
18const BtQuaternion BtQuaternion::IDENTITY(1.0f, 0.0f, 0.0f, 0.0f);
19
20// Smoother destruction callback
21int destroySmootherObj(mmachine m, SCOL_PTR_TYPE handsys, int obj)
22{
23 ISmoother* smoother = MMgetPointer<ISmoother*>(m, MTOP(obj));
24 SAFE_DELETE(smoother);
25 MMsetPointer<ISmoother*>(m, MTOP(obj), 0);
26
27 MMechostr(MSKDEBUG, "ObjSmoother destroyed.\n");
28 return 0;
29}
30
31
32// Push value to a smoother from interface
33void pushValue(ISmoother* smoother, float value1, float value2, float value3)
34{
35 int type = smoother->getSmootherType();
36 switch (type)
37 {
39 static_cast<TSmoother<float>*>(smoother)->pushValue(value1);
40 break;
42 static_cast<TSmoother<Vector2>*>(smoother)->pushValue(Vector2(value1, value2));
43 break;
45 static_cast<TSmoother<Vector3>*>(smoother)->pushValue(Vector3(value1, value2, value3));
46 break;
47 default:
48 break;
49 }
50}
51
52// Get smoothed value from a smoother interface
53void getSmoothedValue(ISmoother* smoother, float& value1, float& value2, float& value3)
54{
55 int type = smoother->getSmootherType();
56 switch (type)
57 {
59 {
60 value1 = static_cast<TSmoother<float>*>(smoother)->getSmoothedValue();
61 value2 = 0.0f;
62 value3 = 0.0f;
63 }
64 break;
66 {
67 Vector2 val = static_cast<TSmoother<Vector2>*>(smoother)->getSmoothedValue();
68 value1 = val.x;
69 value2 = val.y;
70 value3 = 0.0f;
71 }
72 break;
74 {
75 Vector3 val = static_cast<TSmoother<Vector3>*>(smoother)->getSmoothedValue();
76 value1 = val.x;
77 value2 = val.y;
78 value3 = val.z;
79 }
80 break;
81 default:
82 break;
83 }
84}
85
86// Get extrapolated value from a smoother interface
87void getExtrapolatedValue(ISmoother* smoother, float& value1, float& value2, float& value3)
88{
89 int type = smoother->getSmootherType();
90 switch (type)
91 {
93 {
94 value1 = static_cast<TSmoother<float>*>(smoother)->getExtrapolatedValue();
95 value2 = 0.0f;
96 value3 = 0.0f;
97 }
98 break;
100 {
101 Vector2 val = static_cast<TSmoother<Vector2>*>(smoother)->getExtrapolatedValue();
102 value1 = val.x;
103 value2 = val.y;
104 value3 = 0.0f;
105 }
106 break;
108 {
109 Vector3 val = static_cast<TSmoother<Vector3>*>(smoother)->getExtrapolatedValue();
110 value1 = val.x;
111 value2 = val.y;
112 value3 = val.z;
113 }
114 break;
115 default:
116 break;
117 }
118}
119
132int _CRsmoother(mmachine m)
133{
134 MMechostr(MSKDEBUG, "_CRsmoother\n");
135
136 int ibeta = MMpull(m);
137 int ialpha = MMpull(m);
138 int imethod = MMpull(m);
139 int itype = MMpull(m);
140
141 if ((ibeta == NIL) || (ialpha == NIL) || (imethod == NIL) || (itype == NIL))
142 {
143 MMechostr(MSKDEBUG, "_CRsmoother: Some arguments are invalid\n");
144 MMset(m, 0, NIL);
145 return 0;
146 }
147
148 float beta = MTOF(ibeta);
149 float alpha = MTOF(ialpha);
150 int method = MTOI(imethod);
151 int type = MTOI(itype);
152
153 ISmoother* smoother = 0;
154
155 if (method != SMOOTHER_METHOD_DOUBLEEXP && method != SMOOTHER_METHOD_LINEAR)
156 {
157 MMechostr(MSKDEBUG, "_CRsmoother: Method is not valid ! Please choose between SMOOTHER_METHOD_DOUBLEEXP and SMOOTHER_METHOD_LINEAR\n");
158 MMset(m, 0, NIL);
159 return 0;
160 }
161
162 switch (type)
163 {
165 {
166 if (method == SMOOTHER_METHOD_DOUBLEEXP)
167 smoother = new DoubleExponentialSmoother<float>(alpha, beta);
168 else if (method == SMOOTHER_METHOD_LINEAR)
169 smoother = new LinearSmoother<float>(alpha);
170 }
171 break;
173 {
174 if (method == SMOOTHER_METHOD_DOUBLEEXP)
175 smoother = new DoubleExponentialSmoother<Vector2>(alpha, beta);
176 else if (method == SMOOTHER_METHOD_LINEAR)
177 smoother = new LinearSmoother<Vector2>(alpha);
178 }
179 break;
181 {
182 if (method == SMOOTHER_METHOD_DOUBLEEXP)
183 smoother = new DoubleExponentialSmoother<Vector3>(alpha, beta);
184 else if (method == SMOOTHER_METHOD_LINEAR)
185 smoother = new LinearSmoother<Vector3>(alpha);
186 }
187 break;
188 default:
189 MMechostr(MSKDEBUG, "_CRsmoother: Type is not valid ! Please choose between SMOOTHER_TYPE_FLOAT, SMOOTHER_TYPE_VEC2 and SMOOTHER_TYPE_VEC3\n");
190 MMset(m, 0, NIL);
191 return 0;
192 break;
193 }
194
195 if (!smoother)
196 {
197 MMechostr(MSKDEBUG, "_CRsmoother: Couldn't malloc smoother\n");
198 MMset(m, 0, NIL);
199 return 0;
200 }
201
202 if ((MMpushPointer(m, smoother) != 0))
203 {
204 SAFE_DELETE(smoother);
205 MMset(m, 0, NIL);
206 return MERRMEM;
207 }
208
209 return OBJcreate(m, OBJSMOOTHERSCOL, SCOL_PTR smoother, NIL, 0);
210}
211
220int _CLRsmoother(mmachine m)
221{
222 MMechostr(MSKDEBUG, "_CLRsmoother");
223 int ismoother = MMget(m, 0);
224 if (ismoother == NIL)
225 {
226 MMechostr(MSKDEBUG, "_CLRsmoother: error ! smoother is NIL !\n");
227 MMset(m, 0, NIL);
228 return 0;
229 }
230
231 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
232 if (smootherOBJ == NULL)
233 {
234 MMset(m, 0, NIL);
235 return 0;
236 }
237
238 smootherOBJ->clearValues();
239
240 return 0;
241}
242
251int _DSsmoother(mmachine m)
252{
253 MMechostr(MSKDEBUG, "_DSsmoother\n");
254
255 int obj = MMget(m, 0);
256 if (obj == NIL)
257 {
258 MMset(m, 0, NIL);
259 return 0;
260 }
261
262 OBJdelTM(m, OBJSMOOTHERSCOL, obj);
263
264 MMset(m, 0, ITOM(0));
265 return 0;
266}
267
277int _PUSHsmootherValue1(mmachine m)
278{
279 int value = MMpull(m);
280 int ismoother = MMget(m, 0);
281 if (value == NIL || ismoother == NIL)
282 {
283 MMechostr(MSKDEBUG, "_CLRsmoother: error ! value or smoother is NIL !\n");
284 MMset(m, 0, NIL);
285 return 0;
286 }
287
288 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
289 if (smootherOBJ == NULL)
290 {
291 MMset(m, 0, NIL);
292 return 0;
293 }
294
295 pushValue(smootherOBJ, MTOF(value), 0.0f, 0.0f);
296 return 0;
297}
298
309int _PUSHsmootherValue2(mmachine m)
310{
311 int value2 = MMpull(m);
312 int value1 = MMpull(m);
313 int ismoother = MMget(m, 0);
314 if (value1 == NIL || value2 == NIL || ismoother == NIL)
315 {
316 MMechostr(MSKDEBUG, "_CLRsmoother: error ! value or smoother is NIL !\n");
317 MMset(m, 0, NIL);
318 return 0;
319 }
320
321 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
322 if (smootherOBJ == NULL)
323 {
324 MMset(m, 0, NIL);
325 return 0;
326 }
327
328 pushValue(smootherOBJ, MTOF(value1), MTOF(value2), 0.0f);
329 return 0;
330}
331
343int _PUSHsmootherValue3(mmachine m)
344{
345 int value3 = MMpull(m);
346 int value2 = MMpull(m);
347 int value1 = MMpull(m);
348 int ismoother = MMget(m, 0);
349 if (value1 == NIL || value2 == NIL || value3 == NIL || ismoother == NIL)
350 {
351 MMechostr(MSKDEBUG, "_CLRsmoother: error ! value or smoother is NIL !\n");
352 MMset(m, 0, NIL);
353 return 0;
354 }
355
356 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
357 if (smootherOBJ == NULL)
358 {
359 MMset(m, 0, NIL);
360 return 0;
361 }
362
363 pushValue(smootherOBJ, MTOF(value1), MTOF(value2), MTOF(value3));
364 return 0;
365}
366
376{
377 int ismoother = MMget(m, 0);
378 if (ismoother == NIL)
379 {
380 MMechostr(MSKDEBUG, "_CLRsmoother: error ! smoother is NIL !\n");
381 MMset(m, 0, NIL);
382 return 0;
383 }
384
385 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
386 if (smootherOBJ == NULL)
387 {
388 MMset(m, 0, NIL);
389 return 0;
390 }
391
392 float value1 = 0.0f, value2 = 0.0f, value3 = 0.0f;
393 getSmoothedValue(smootherOBJ, value1, value2, value3);
394 MMset(m, 0, FTOM(value1));
395 return 0;
396}
397
407{
408 int ismoother = MMget(m, 0);
409 if (ismoother == NIL)
410 {
411 MMechostr(MSKDEBUG, "_CLRsmoother: error ! smoother is NIL !\n");
412 MMset(m, 0, NIL);
413 return 0;
414 }
415
416 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
417 if (smootherOBJ == NULL)
418 {
419 MMset(m, 0, NIL);
420 return 0;
421 }
422
423 float value1 = 0.0f, value2 = 0.0f, value3 = 0.0f;
424 getSmoothedValue(smootherOBJ, value1, value2, value3);
425 int tupleSize = MMmalloc(m, 2, TYPETAB);
426 if (tupleSize == NIL)
427 {
428 MMset(m, 0, NIL);
429 return 0;
430 }
431
432 MMstore(m, tupleSize, 0, FTOM(value1));
433 MMstore(m, tupleSize, 1, FTOM(value2));
434 MMset(m, 0, PTOM(tupleSize));
435 return 0;
436}
437
447{
448 int ismoother = MMget(m, 0);
449 if (ismoother == NIL)
450 {
451 MMechostr(MSKDEBUG, "_CLRsmoother: error ! smoother is NIL !\n");
452 MMset(m, 0, NIL);
453 return 0;
454 }
455
456 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
457 if (smootherOBJ == NULL)
458 {
459 MMset(m, 0, NIL);
460 return 0;
461 }
462
463 float value1 = 0.0f, value2 = 0.0f, value3 = 0.0f;
464 getSmoothedValue(smootherOBJ, value1, value2, value3);
465
466 int tupleSize = MMmalloc(m, 3, TYPETAB);
467 if (tupleSize == NIL)
468 {
469 MMset(m, 0, NIL);
470 return 0;
471 }
472
473 MMstore(m, tupleSize, 0, FTOM(value1));
474 MMstore(m, tupleSize, 1, FTOM(value2));
475 MMstore(m, tupleSize, 2, FTOM(value3));
476 MMset(m, 0, PTOM(tupleSize));
477 return 0;
478}
479
489{
490 int ismoother = MMget(m, 0);
491 if (ismoother == NIL)
492 {
493 MMechostr(MSKDEBUG, "_CLRsmoother: error ! smoother is NIL !\n");
494 MMset(m, 0, NIL);
495 return 0;
496 }
497
498 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
499 if (smootherOBJ == NULL)
500 {
501 MMset(m, 0, NIL);
502 return 0;
503 }
504
505 float value1 = 0.0f, value2 = 0.0f, value3 = 0.0f;
506 getExtrapolatedValue(smootherOBJ, value1, value2, value3);
507 MMset(m, 0, FTOM(value1));
508 return 0;
509}
510
520{
521 int ismoother = MMget(m, 0);
522 if (ismoother == NIL)
523 {
524 MMechostr(MSKDEBUG, "_CLRsmoother: error ! smoother is NIL !\n");
525 MMset(m, 0, NIL);
526 return 0;
527 }
528
529 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
530 if (smootherOBJ == NULL)
531 {
532 MMset(m, 0, NIL);
533 return 0;
534 }
535
536 float value1 = 0.0f, value2 = 0.0f, value3 = 0.0f;
537 getExtrapolatedValue(smootherOBJ, value1, value2, value3);
538 int tupleSize = MMmalloc(m, 2, TYPETAB);
539 if (tupleSize == NIL)
540 {
541 MMset(m, 0, NIL);
542 return 0;
543 }
544
545 MMstore(m, tupleSize, 0, FTOM(value1));
546 MMstore(m, tupleSize, 1, FTOM(value2));
547 MMset(m, 0, PTOM(tupleSize));
548 return 0;
549}
550
560{
561 int ismoother = MMget(m, 0);
562 if (ismoother == NIL)
563 {
564 MMechostr(MSKDEBUG, "_CLRsmoother: error ! smoother is NIL !\n");
565 MMset(m, 0, NIL);
566 return 0;
567 }
568
569 ISmoother* smootherOBJ = MMgetPointer<ISmoother*>(m, MTOP(ismoother));
570 if (smootherOBJ == NULL)
571 {
572 MMset(m, 0, NIL);
573 return 0;
574 }
575
576 float value1 = 0.0f, value2 = 0.0f, value3 = 0.0f;
577 getExtrapolatedValue(smootherOBJ, value1, value2, value3);
578 int tupleSize = MMmalloc(m, 3, TYPETAB);
579 if (tupleSize == NIL)
580 {
581 MMset(m, 0, NIL);
582 return 0;
583 }
584
585 MMstore(m, tupleSize, 0, FTOM(value1));
586 MMstore(m, tupleSize, 1, FTOM(value2));
587 MMstore(m, tupleSize, 2, FTOM(value3));
588 MMset(m, 0, PTOM(tupleSize));
589 return 0;
590}
591
592NativeDefinition base_math_tk[] = {
593 { "ObjSmoother", TYPTYPE, NULL, NULL },
594 { "SMOOTHER_TYPE_UNDEFINED", TYPVAR, "I", SCOL_TYPTYPE(SMOOTHER_TYPE_UNDEFINED) },
595 { "SMOOTHER_TYPE_FLOAT", TYPVAR, "I", SCOL_TYPTYPE(SMOOTHER_TYPE_FLOAT) },
596 { "SMOOTHER_TYPE_VEC2", TYPVAR, "I", SCOL_TYPTYPE(SMOOTHER_TYPE_VEC2) },
597 { "SMOOTHER_TYPE_VEC3", TYPVAR, "I", SCOL_TYPTYPE(SMOOTHER_TYPE_VEC3) },
598
599 { "SMOOTHER_METHOD_UNDEFINED", TYPVAR, "I", SCOL_TYPTYPE(SMOOTHER_METHOD_UNDEFINED) },
600 { "SMOOTHER_METHOD_LINEAR", TYPVAR, "I", SCOL_TYPTYPE(SMOOTHER_METHOD_LINEAR) },
601 { "SMOOTHER_METHOD_DOUBLEEXP", TYPVAR, "I", SCOL_TYPTYPE(SMOOTHER_METHOD_DOUBLEEXP) },
602
603 { "_CRsmoother", 5, "fun [Chn I I F F] ObjSmoother", _CRsmoother },
604 { "_CLRsmoother", 1, "fun [ObjSmoother] ObjSmoother", _CLRsmoother },
605 { "_DSsmoother", 1, "fun [ObjSmoother] I", _DSsmoother },
606
607 { "_PUSHsmootherValue1", 2, "fun [ObjSmoother F] ObjSmoother", _PUSHsmootherValue1 },
608 { "_PUSHsmootherValue2", 3, "fun [ObjSmoother F F] ObjSmoother", _PUSHsmootherValue2 },
609 { "_PUSHsmootherValue3", 4, "fun [ObjSmoother F F F] ObjSmoother", _PUSHsmootherValue3 },
610
611 { "_GETsmootherSmoothedValue1", 1, "fun [ObjSmoother] F", _GETsmootherSmoothedValue1 },
612 { "_GETsmootherSmoothedValue2", 1, "fun [ObjSmoother] [F F]", _GETsmootherSmoothedValue2 },
613 { "_GETsmootherSmoothedValue3", 1, "fun [ObjSmoother] [F F F]", _GETsmootherSmoothedValue3 },
614
615 { "_GETsmootherExtrapolatedValue1", 1, "fun [ObjSmoother] F", _GETsmootherExtrapolatedValue1 },
616 { "_GETsmootherExtrapolatedValue2", 1, "fun [ObjSmoother] [F F]", _GETsmootherExtrapolatedValue2 },
617 { "_GETsmootherExtrapolatedValue3", 1, "fun [ObjSmoother] [F F F]", _GETsmootherExtrapolatedValue3 }
618};
619
624int LoadMathToolkit(mmachine m)
625{
626 int k;
627 MMechostr(MSKDEBUG, " > Loading MathToolkit\n");
628 OBJSMOOTHERSCOL = OBJregister(0, 0, destroySmootherObj, "OBJSMOOTHERSCOL");
629 k = PKhardpak2(m, "math_toolkit.pkg", sizeof(base_math_tk) / sizeof(base_math_tk[0]), base_math_tk);
630 MMechostr(MSKDEBUG, " > Successfully Loaded\n\n");
631 return k;
632}
void getExtrapolatedValue(ISmoother *smoother, float &value1, float &value2, float &value3)
int destroySmootherObj(mmachine m, SCOL_PTR_TYPE handsys, int obj)
int LoadMathToolkit(mmachine m)
Load the packages in Scol virtual machine.
void pushValue(ISmoother *smoother, float value1, float value2, float value3)
int Random(int mi, int mx)
int OBJSMOOTHERSCOL
void getSmoothedValue(ISmoother *smoother, float &value1, float &value2, float &value3)
NativeDefinition base_math_tk[]
#define SMOOTHER_TYPE_VEC2
Definition Smoothers.h:9
#define SMOOTHER_TYPE_VEC3
Definition Smoothers.h:10
#define SMOOTHER_TYPE_FLOAT
Definition Smoothers.h:8
#define SMOOTHER_TYPE_UNDEFINED
Definition Smoothers.h:7
#define SMOOTHER_METHOD_LINEAR
Definition Smoothers.h:13
#define SMOOTHER_METHOD_UNDEFINED
Definition Smoothers.h:12
#define SMOOTHER_METHOD_DOUBLEEXP
Definition Smoothers.h:14
static const BtQuaternion IDENTITY
Double Exponential smoother.
Definition Smoothers.h:77
Smoother interface.
Definition Smoothers.h:20
virtual void clearValues()=0
virtual int getSmootherType()=0
Linear smoother.
Definition Smoothers.h:54
int _PUSHsmootherValue3(mmachine m)
_PUSHsmootherValue3 : Push a new vector3 value to ObjSmoother
int _GETsmootherExtrapolatedValue3(mmachine m)
_GETsmootherExtrapolatedValue3 : Get an extrapolated value from smoother as a tuple of 3 float
int _CLRsmoother(mmachine m)
_CLRsmoother : Clears smoother state
int _PUSHsmootherValue1(mmachine m)
_PUSHsmootherValue1 : Push a new value to ObjSmoother
int _PUSHsmootherValue2(mmachine m)
_PUSHsmootherValue2 : Push a new vector2 value to ObjSmoother
int _GETsmootherSmoothedValue2(mmachine m)
_GETsmootherSmoothedValue2 : Get last smoothed value from smoother as a tuple of 2 float
int _GETsmootherSmoothedValue1(mmachine m)
_GETsmootherSmoothedValue1 : Get last smoothed value from smoother as simple float
int _GETsmootherExtrapolatedValue2(mmachine m)
_GETsmootherExtrapolatedValue2 : Get an extrapolated value from smoother as a tuple of 2 float
int _GETsmootherExtrapolatedValue1(mmachine m)
_GETsmootherExtrapolatedValue1 : Get an extrapolated value from smoother as simple float
int _CRsmoother(mmachine m)
_CRsmoother : Create a new Smoother object
int _GETsmootherSmoothedValue3(mmachine m)
_GETsmootherSmoothedValue3 : Get last smoothed value from smoother as a tuple of 3 float
int _DSsmoother(mmachine m)
_DSsmoother : Destroys a Smoother object