00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef MAKEDEPEND
00041 #include <math.h>
00042 #endif
00043 #include "SeVec3d.h"
00044 #include "SeExpression.h"
00045 #include "SeExprNode.h"
00046 #include "SeExprFunc.h"
00047
00048
00049 SeExprNode::SeExprNode(const SeExpression* expr)
00050 : _expr(expr), _parent(0), _isVec(0)
00051 {
00052 }
00053
00054
00055 SeExprNode::SeExprNode(const SeExpression* expr, SeExprNode* a)
00056 : _expr(expr), _parent(0), _isVec(0)
00057 {
00058 _children.reserve(1);
00059 addChild(a);
00060 }
00061
00062
00063 SeExprNode::SeExprNode(const SeExpression* expr, SeExprNode* a, SeExprNode* b)
00064 : _expr(expr), _parent(0), _isVec(0)
00065 {
00066 _children.reserve(2);
00067 addChild(a);
00068 addChild(b);
00069 }
00070
00071
00072 SeExprNode::SeExprNode(const SeExpression* expr, SeExprNode* a, SeExprNode* b,
00073 SeExprNode* c)
00074 : _expr(expr), _parent(0), _isVec(0)
00075 {
00076 _children.reserve(3);
00077 addChild(a);
00078 addChild(b);
00079 addChild(c);
00080 }
00081
00082
00083 SeExprNode::~SeExprNode()
00084 {
00085
00086 std::vector<SeExprNode*>::iterator iter;
00087 for (iter = _children.begin(); iter != _children.end(); iter++)
00088 delete *iter;
00089 }
00090
00091
00092 void
00093 SeExprNode::addChild(SeExprNode* child)
00094 {
00095 _children.push_back(child);
00096 child->_parent = this;
00097 }
00098
00099
00100 void
00101 SeExprNode::addChildren(SeExprNode* surrogate)
00102 {
00103 std::vector<SeExprNode*>::iterator iter;
00104 for (iter = surrogate->_children.begin();
00105 iter != surrogate->_children.end();
00106 iter++)
00107 {
00108 addChild(*iter);
00109 }
00110 surrogate->_children.clear();
00111 delete surrogate;
00112 }
00113
00114
00115 bool
00116 SeExprNode::prep(bool wantVec)
00117 {
00118
00119
00120 bool valid=true;
00121 std::vector<SeExprNode*>::iterator iter;
00122 _isVec = 0;
00123 for (iter = _children.begin(); iter != _children.end(); iter++) {
00124 SeExprNode* child = *iter;
00125 if (!child->prep(wantVec)) valid=false;
00126 if (child->isVec()) _isVec = 1;
00127 }
00128 return valid;
00129 }
00130
00131
00132 void
00133 SeExprNode::eval(SeVec3d& result) const
00134 {
00135
00136
00137
00138 SeVec3d val;
00139 for (int i = 0; i < numChildren(); i++)
00140 child(i)->eval(val);
00141 result = 0.0;
00142 }
00143
00144
00145 bool
00146 SeExprBlockNode::prep(bool wantVec)
00147 {
00148
00149 bool valid=true;
00150
00151 if (!child(0)->prep(1)) valid=false;
00152
00153
00154 if (!child(1)->prep(wantVec)) valid=false;
00155
00156 _isVec = child(1)->isVec();
00157 return valid;
00158 }
00159
00160
00161 void
00162 SeExprBlockNode::eval(SeVec3d& result) const
00163 {
00164
00165 SeVec3d val;
00166 child(0)->eval(val);
00167 child(1)->eval(result);
00168 }
00169
00170
00171 bool
00172 SeExprIfThenElseNode::prep(bool )
00173 {
00174 bool valid=true;
00175
00176 if (!child(0)->prep(0)) valid=false;
00177
00178
00179 if (!child(1)->prep(1)) valid=false;
00180 if (!child(2)->prep(1)) valid=false;
00181 _isVec = 0;
00182 return valid;
00183 }
00184
00185
00186 void
00187 SeExprIfThenElseNode::eval(SeVec3d& result) const
00188 {
00189
00190 SeVec3d val;
00191 child(0)->eval(val);
00192 if (val[0])
00193 child(1)->eval(val);
00194 else
00195 child(2)->eval(val);
00196 result = 0.0;
00197 }
00198
00199
00200 bool
00201 SeExprAssignNode::prep(bool )
00202 {
00203
00204 if (!child(0)->prep(1)) return 0;
00205 _isVec = child(0)->isVec();
00206
00207
00208 _var = _expr->getLocalVar(_name);
00209 if (_isVec) _var->setIsVec();
00210 return 1;
00211 }
00212
00213
00214 void
00215 SeExprAssignNode::eval(SeVec3d& result) const
00216 {
00217 if (_var) {
00218
00219 const SeExprNode* node = child(0);
00220 node->eval(_var->val);
00221 if (_var->isVec() && !node->isVec())
00222 _var->val[1] = _var->val[2] = _var->val[0];
00223 }
00224 else result = 0.0;
00225 }
00226
00227
00228 bool
00229 SeExprVecNode::prep(bool wantVec)
00230 {
00231
00232 if (!SeExprNode::prep(0)) return 0;
00233 _isVec = wantVec;
00234 return 1;
00235 }
00236
00237
00238 void
00239 SeExprVecNode::eval(SeVec3d& result) const
00240 {
00241 if (_isVec) {
00242 SeVec3d v;
00243 child(0)->eval(v); result[0] = v[0];
00244 child(1)->eval(v); result[1] = v[0];
00245 child(2)->eval(v); result[2] = v[0];
00246 } else {
00247 child(0)->eval(result);
00248 }
00249 }
00250
00251
00252 bool
00253 SeExprCondNode::prep(bool wantVec)
00254 {
00255 bool valid=true;
00256
00257 if (!child(0)->prep(0)) valid=false;
00258 if (!child(1)->prep(wantVec)) valid=false;
00259 if (!child(2)->prep(wantVec)) valid=false;
00260 _isVec = wantVec && (child(1)->isVec() || child(2)->isVec());
00261 return valid;
00262 }
00263
00264
00265 void
00266 SeExprCondNode::eval(SeVec3d& result) const
00267 {
00268 SeVec3d v;
00269 child(0)->eval(v);
00270 const SeExprNode* node = v[0] ? child(1) : child(2);
00271 node->eval(result);
00272 if (_isVec && !node->isVec())
00273 result[1] = result[2] = result[0];
00274 }
00275
00276
00277 bool
00278 SeExprAndNode::prep(bool )
00279 {
00280
00281 if (!SeExprNode::prep(0)) return 0;
00282 _isVec = 0;
00283 return 1;
00284 }
00285
00286
00287 void
00288 SeExprAndNode::eval(SeVec3d& result) const
00289 {
00290
00291 SeVec3d a, b;
00292 child(0)->eval(a);
00293 if (!a[0]) {
00294 result[0] = 0;
00295 } else {
00296 child(1)->eval(b);
00297 result[0] = (b[0] != 0.0);
00298 }
00299 }
00300
00301
00302 bool
00303 SeExprOrNode::prep(bool )
00304 {
00305
00306 if (!SeExprNode::prep(0)) return 0;
00307 _isVec = 0;
00308 return 1;
00309 }
00310
00311
00312 void
00313 SeExprOrNode::eval(SeVec3d& result) const
00314 {
00315
00316 SeVec3d a, b;
00317 child(0)->eval(a);
00318 if (a[0]) {
00319 result[0] = 1;
00320 } else {
00321 child(1)->eval(b);
00322 result[0] = (b[0] != 0.0);
00323 }
00324 }
00325
00326
00327 bool
00328 SeExprSubscriptNode::prep(bool )
00329 {
00330 bool valid=true;
00331
00332
00333 if (!child(0)->prep(1)) valid=false;
00334 if (!child(1)->prep(0)) valid=false;
00335 _isVec = 0;
00336 return valid;
00337 }
00338
00339
00340 void
00341 SeExprSubscriptNode::eval(SeVec3d& result) const
00342 {
00343 const SeExprNode* child0 = child(0);
00344 const SeExprNode* child1 = child(1);
00345 SeVec3d a, b;
00346 child0->eval(a);
00347 child1->eval(b);
00348 int index = int(b[0]);
00349
00350 if (child0->isVec()) {
00351 switch(index) {
00352 case 0: result[0] = a[0]; break;
00353 case 1: result[0] = a[1]; break;
00354 case 2: result[0] = a[2]; break;
00355 default: result[0] = 0; break;
00356 }
00357 } else {
00358 switch(index) {
00359 case 0:
00360 case 1:
00361 case 2: result[0] = a[0]; break;
00362 default: result[0] = 0; break;
00363 }
00364 }
00365 }
00366
00367
00368 void
00369 SeExprNegNode::eval(SeVec3d& result) const
00370 {
00371 SeVec3d a;
00372 const SeExprNode* child0 = child(0);
00373 child0->eval(a);
00374 result[0] = -a[0];
00375 if (_isVec) {
00376 result[1] = -a[1];
00377 result[2] = -a[2];
00378 }
00379 }
00380
00381
00382 void
00383 SeExprInvertNode::eval(SeVec3d& result) const
00384 {
00385 SeVec3d a;
00386 const SeExprNode* child0 = child(0);
00387 child0->eval(a);
00388 result[0] = 1-a[0];
00389 if (_isVec) {
00390 result[1] = 1-a[1];
00391 result[2] = 1-a[2];
00392 }
00393 }
00394
00395
00396 void
00397 SeExprNotNode::eval(SeVec3d& result) const
00398 {
00399 SeVec3d a;
00400 const SeExprNode* child0 = child(0);
00401 child0->eval(a);
00402 result[0] = !a[0];
00403 if (_isVec) {
00404 result[1] = !a[1];
00405 result[2] = !a[2];
00406 }
00407 }
00408
00409
00410 bool
00411 SeExprCompareEqNode::prep(bool wantVec)
00412 {
00413 wantVec = true;
00414 if (!SeExprNode::prep(wantVec)) return 0;
00415 _isVec = 0;
00416 return 1;
00417 }
00418
00419
00420 bool
00421 SeExprCompareNode::prep(bool wantVec)
00422 {
00423
00424 wantVec = false;
00425 if (!SeExprNode::prep(wantVec)) return 0;
00426 _isVec = 0;
00427 return 1;
00428 }
00429
00430
00431 void
00432 SeExprEqNode::eval(SeVec3d& result) const
00433 {
00434 SeVec3d a, b;
00435 const SeExprNode* child0 = child(0);
00436 const SeExprNode* child1 = child(1);
00437 child0->eval(a);
00438 child1->eval(b);
00439
00440 if (!child0->isVec()) a[1] = a[2] = a[0];
00441 if (!child1->isVec()) b[1] = b[2] = b[0];
00442 result[0] = a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
00443 }
00444
00445
00446 void
00447 SeExprNeNode::eval(SeVec3d& result) const
00448 {
00449 SeVec3d a, b;
00450 const SeExprNode* child0 = child(0);
00451 const SeExprNode* child1 = child(1);
00452 child0->eval(a);
00453 child1->eval(b);
00454
00455 if (!child0->isVec()) a[1] = a[2] = a[0];
00456 if (!child1->isVec()) b[1] = b[2] = b[0];
00457 result[0] = a[0] != b[0] || a[1] != b[1] || a[2] != b[2];
00458 }
00459
00460
00461 void
00462 SeExprLtNode::eval(SeVec3d& result) const
00463 {
00464 SeVec3d a, b;
00465 const SeExprNode* child0 = child(0);
00466 const SeExprNode* child1 = child(1);
00467 child0->eval(a);
00468 child1->eval(b);
00469
00470 result[0] = a[0] < b[0];
00471 }
00472
00473
00474 void
00475 SeExprGtNode::eval(SeVec3d& result) const
00476 {
00477 SeVec3d a, b;
00478 const SeExprNode* child0 = child(0);
00479 const SeExprNode* child1 = child(1);
00480 child0->eval(a);
00481 child1->eval(b);
00482
00483 result[0] = a[0] > b[0];
00484 }
00485
00486
00487 void
00488 SeExprLeNode::eval(SeVec3d& result) const
00489 {
00490 SeVec3d a, b;
00491 const SeExprNode* child0 = child(0);
00492 const SeExprNode* child1 = child(1);
00493 child0->eval(a);
00494 child1->eval(b);
00495
00496 result[0] = a[0] <= b[0];
00497 }
00498
00499
00500 void
00501 SeExprGeNode::eval(SeVec3d& result) const
00502 {
00503 SeVec3d a, b;
00504 const SeExprNode* child0 = child(0);
00505 const SeExprNode* child1 = child(1);
00506 child0->eval(a);
00507 child1->eval(b);
00508
00509 result[0] = a[0] >= b[0];
00510 }
00511
00512
00513 void
00514 SeExprAddNode::eval(SeVec3d& result) const
00515 {
00516 const SeExprNode* child0 = child(0);
00517 const SeExprNode* child1 = child(1);
00518 SeVec3d a, b;
00519 child0->eval(a);
00520 child1->eval(b);
00521
00522 if (!_isVec) {
00523 result[0] = a[0] + b[0];
00524 }
00525 else {
00526
00527 if (!child0->isVec()) a[1] = a[2] = a[0];
00528 if (!child1->isVec()) b[1] = b[2] = b[0];
00529 result = a + b;
00530 }
00531 }
00532
00533
00534 void
00535 SeExprSubNode::eval(SeVec3d& result) const
00536 {
00537 const SeExprNode* child0 = child(0);
00538 const SeExprNode* child1 = child(1);
00539 SeVec3d a, b;
00540 child0->eval(a);
00541 child1->eval(b);
00542
00543 if (!_isVec) {
00544 result[0] = a[0] - b[0];
00545 }
00546 else {
00547
00548 if (!child0->isVec()) a[1] = a[2] = a[0];
00549 if (!child1->isVec()) b[1] = b[2] = b[0];
00550 result = a - b;
00551 }
00552 }
00553
00554
00555 void
00556 SeExprMulNode::eval(SeVec3d& result) const
00557 {
00558 const SeExprNode* child0 = child(0);
00559 const SeExprNode* child1 = child(1);
00560 SeVec3d a, b;
00561 child0->eval(a);
00562 child1->eval(b);
00563
00564 if (!_isVec) {
00565 result[0] = a[0] * b[0];
00566 }
00567 else {
00568
00569 if (!child0->isVec()) a[1] = a[2] = a[0];
00570 if (!child1->isVec()) b[1] = b[2] = b[0];
00571 result = a * b;
00572 }
00573 }
00574
00575
00576 void
00577 SeExprDivNode::eval(SeVec3d& result) const
00578 {
00579 const SeExprNode* child0 = child(0);
00580 const SeExprNode* child1 = child(1);
00581 SeVec3d a, b;
00582 child0->eval(a);
00583 child1->eval(b);
00584
00585 if (!_isVec) {
00586 result[0] = a[0] / b[0];
00587 }
00588 else {
00589
00590 if (!child0->isVec()) a[1] = a[2] = a[0];
00591 if (!child1->isVec()) b[1] = b[2] = b[0];
00592 result = a / b;
00593 }
00594 }
00595
00596
00597 static double niceMod(double a, double b)
00598 {
00599 if (b == 0) return 0;
00600 return a - floor(a/b)*b;
00601 }
00602
00603
00604 void
00605 SeExprModNode::eval(SeVec3d& result) const
00606 {
00607 const SeExprNode* child0 = child(0);
00608 const SeExprNode* child1 = child(1);
00609 SeVec3d a, b;
00610 child0->eval(a);
00611 child1->eval(b);
00612
00613 if (!_isVec) {
00614 result[0] = niceMod(a[0], b[0]);
00615 }
00616 else {
00617
00618 if (!child0->isVec()) a[1] = a[2] = a[0];
00619 if (!child1->isVec()) b[1] = b[2] = b[0];
00620 result[0] = niceMod(a[0], b[0]);
00621 result[1] = niceMod(a[1], b[1]);
00622 result[2] = niceMod(a[2], b[2]);
00623 }
00624 }
00625
00626
00627 void
00628 SeExprExpNode::eval(SeVec3d& result) const
00629 {
00630 const SeExprNode* child0 = child(0);
00631 const SeExprNode* child1 = child(1);
00632 SeVec3d a, b;
00633 child0->eval(a);
00634 child1->eval(b);
00635
00636 if (!_isVec) {
00637 result[0] = pow(a[0], b[0]);
00638 }
00639 else {
00640
00641 if (!child0->isVec()) a[1] = a[2] = a[0];
00642 if (!child1->isVec()) b[1] = b[2] = b[0];
00643 result[0] = pow(a[0], b[0]);
00644 result[1] = pow(a[1], b[1]);
00645 result[2] = pow(a[2], b[2]);
00646 }
00647 }
00648
00649
00650 bool
00651 SeExprVarNode::prep(bool )
00652 {
00653
00654 _var = _expr->resolveLocalVar(name());
00655 if (!_var) _var = _expr->resolveVar(name());
00656 if (!_var) {
00657 addError(std::string("No variable named $")+name());
00658 return 0;
00659 }
00660 _isVec = _var->isVec();
00661 return 1;
00662 }
00663
00664
00665 void
00666 SeExprVarNode::eval(SeVec3d& result) const
00667 {
00668 if (_var) _var->eval(this, result);
00669 else result = 0.0;
00670 }
00671
00672
00673 bool
00674 SeExprFuncNode::prep(bool wantVec)
00675 {
00676
00677 _func = _expr->resolveFunc(_name);
00678
00679 if (!_func) _func = SeExprFunc::lookup(_name);
00680 if (!_func) {
00681 addError("No function named "+_name);
00682 return 0;
00683 }
00684
00685 _nargs = numChildren();
00686 if (_nargs < _func->minArgs()) {
00687 addError("Too few args for function "+_name);
00688 return 0;
00689 }
00690 if (_nargs > _func->maxArgs() && _func->maxArgs() >= 0) {
00691 addError("Too many args for function "+_name);
00692 return 0;
00693 }
00694
00695
00696 _scalarArgs.resize(_nargs);
00697 _vecArgs.resize(_nargs);
00698
00699
00700 if (_func->type() == SeExprFunc::FUNCX) {
00701 _isVec = 1;
00702 if(!_func->funcx()->isThreadSafe()) _expr->setThreadUnsafe(_name);
00703 return _func->funcx()->prep(this, wantVec);
00704 }
00705
00706
00707
00708 if (!SeExprNode::prep(wantVec || _func->hasVecArgs())){ return 0; }
00709
00710
00711
00712
00713 _isVec = 0;
00714 if (wantVec) {
00715 if (_func->isVec()) _isVec = 1;
00716 else if (!_func->hasVecArgs()) {
00717 for (int i = 0; i < _nargs; i++)
00718 if (child(i)->isVec()) { _isVec = 1; break; }
00719 }
00720 }
00721 return 1;
00722 }
00723
00724 SeVec3d*
00725 SeExprFuncNode::evalArgs() const
00726 {
00727 SeVec3d* a = vecArgs();
00728 for (int i = 0; i < _nargs; i++) {
00729 const SeExprNode* child = SeExprNode::child(i);
00730 SeVec3d& A = a[i];
00731 child->eval(A);
00732 if (!child->isVec()) A[1] = A[2] = A[0];
00733 }
00734 return a;
00735 }
00736
00737 SeVec3d
00738 SeExprFuncNode::evalArg(int n) const
00739 {
00740 SeVec3d arg;
00741 const SeExprNode* child = SeExprNode::child(n);
00742 child->eval(arg);
00743 if (!child->isVec()) arg[1] = arg[2] = arg[0];
00744 return arg;
00745 }
00746
00747 bool
00748 SeExprFuncNode::isStrArg(int n) const
00749 {
00750 return n < _nargs && dynamic_cast<const SeExprStrNode*>(child(n)) != 0;
00751 }
00752
00753 std::string
00754 SeExprFuncNode::getStrArg(int n) const
00755 {
00756 if (n < _nargs)
00757 return static_cast<const SeExprStrNode*>(child(n))->str();
00758 return "";
00759 }
00760
00761
00762 void
00763 SeExprFuncNode::eval(SeVec3d& result) const
00764 {
00765 if (!_func) { result = 0.0; return; }
00766
00767
00768 if (_func->type() == SeExprFunc::FUNCX) {
00769 _func->funcx()->eval(this, result);
00770 return;
00771 }
00772
00773
00774 bool applyScalarToVec = _isVec && !_func->isVec();
00775 int niter = applyScalarToVec ? 3 : 1;
00776
00777
00778 SeVec3d* a = evalArgs();
00779 for (int i = 0; i < niter; i++) {
00780 switch (_func->type()) {
00781 default:
00782 result[i] = result[1] = result[2] = 0;
00783 break;
00784 case SeExprFunc::FUNC0:
00785 result[i] = _func->func0()();
00786 break;
00787 case SeExprFunc::FUNC1:
00788 result[i] = _func->func1()(a[0][i]);
00789 break;
00790 case SeExprFunc::FUNC2:
00791 result[i] = _func->func2()(a[0][i], a[1][i]);
00792 break;
00793 case SeExprFunc::FUNC3:
00794 result[i] = _func->func3()(a[0][i], a[1][i], a[2][i]);
00795 break;
00796 case SeExprFunc::FUNC4:
00797 result[i] = _func->func4()(a[0][i], a[1][i], a[2][i], a[3][i]);
00798 break;
00799 case SeExprFunc::FUNC5:
00800 result[i] = _func->func5()(a[0][i], a[1][i], a[2][i], a[3][i],
00801 a[4][i]);
00802 break;
00803 case SeExprFunc::FUNC6:
00804 result[i] = _func->func6()(a[0][i], a[1][i], a[2][i], a[3][i],
00805 a[4][i], a[5][i]);
00806 break;
00807 case SeExprFunc::FUNCN:
00808 {
00809 double* d = scalarArgs();
00810 for (int n = 0; n < _nargs; n++) d[n] = a[n][i];
00811 result[i] = _func->funcn()(_nargs, d);
00812 break;
00813 }
00814 case SeExprFunc::FUNC1V:
00815 result[i] = _func->func1v()(a[0]);
00816 break;
00817 case SeExprFunc::FUNC2V:
00818 result[i] = _func->func2v()(a[0], a[1]);
00819 break;
00820 case SeExprFunc::FUNCNV:
00821 result[i] = _func->funcnv()(_nargs, a);
00822 break;
00823 case SeExprFunc::FUNC1VV:
00824 result = _func->func1vv()(a[0]);
00825 break;
00826 case SeExprFunc::FUNC2VV:
00827 result = _func->func2vv()(a[0], a[1]);
00828 break;
00829 case SeExprFunc::FUNCNVV:
00830 result = _func->funcnvv()(_nargs, a);
00831 break;
00832 }
00833 }
00834
00835 }