1 #ifndef COIN_EVALUATOR_H 2 #define COIN_EVALUATOR_H 3 4 /**************************************************************************\ 5 * Copyright (c) Kongsberg Oil & Gas Technologies AS 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are 10 * met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Neither the name of the copyright holder nor the names of its 20 * contributors may be used to endorse or promote products derived from 21 * this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 \**************************************************************************/ 35 36 #ifndef COIN_INTERNAL 37 #error this is a private header file 38 #endif /* !COIN_INTERNAL */ 39 40 41 /* 42 * expression parser/evaluator for SoCalculator expressions. 43 * 44 * I chose to implement this "module" in pure C, since I think 45 * mixing flex/bison code and C++ is a bad idea. 46 * 47 * The bison parser builds a tree structure which is traversed 48 * each time the expression needs to be evaluated. I guess 49 * the inputs will change more often than the expression, so I 50 * think this is better than parsing the expression every time. 51 * 52 * Call so_eval_parse() to build the tree structure. This method 53 * returns NULL if an error occured. The actual error message 54 * can be found by using so_eval_error(). 55 * 56 * Call so_eval_evaluate() to evaluate the expression. You should 57 * supply callback pointers to read and write the registers. 58 * so_eval_evaluate() should never fail. If you supply an illegal 59 * value to some function, it will be clamped or the result will 60 * be set to some (hopefully) useful value. 61 * pederb, 20000307 62 */ 63 #ifdef __cplusplus 64 extern "C" { 65 #endif /* __cplusplus */ 66 67 /* the expression node structure */ 68 typedef struct so_eval_node { 69 int id; /* node id */ 70 float value; /* used only by value nodes */ 71 char regname[4]; /* register/field name in SoCalculator */ 72 int regidx; /* index into vector register */ 73 struct so_eval_node *child1, *child2, *child3; 74 } so_eval_node; 75 76 /* callbacks are needed to read/write values from/to fields. 77 When returning a float field value, write to data[0]. When 78 returning a SbVec3f, set data[0], data[1] and data[2] */ 79 typedef struct { 80 void (*readfieldcb)(const char *reg, float *data, void *userdata); 81 82 /* 'component' only applies when setting vectors. If component < 0, copy 83 all three components from data, otherwise only copy data[0] into the 84 specified component (data only contains a single float */ 85 void (*writefieldcb)(const char *reg, float *data, int component, void *userdata); 86 void *userdata; 87 } so_eval_cbdata; 88 89 /* create a tree structure from expression string */ 90 so_eval_node *so_eval_parse(const char *buffer); /* defined in epsilon.y */ 91 92 /* free memory used by tree structure */ 93 void so_eval_delete(so_eval_node *node); 94 95 /* evaluates the tree structure */ 96 void so_eval_evaluate(so_eval_node *node, const so_eval_cbdata *cbdata); 97 98 /* returns current error message, or NULL if none. 99 check this after calling so_eval_parse() */ 100 char * so_eval_error(void); /* defined in epsilon.y */ 101 102 /* methods to create misc nodes */ 103 so_eval_node *so_eval_create_unary(int id, so_eval_node *topnode); 104 so_eval_node *so_eval_create_binary(int id, so_eval_node *lhs, so_eval_node *rhs); 105 so_eval_node *so_eval_create_ternary(int id, so_eval_node *cond, 106 so_eval_node *branch1, so_eval_node *branch2); 107 so_eval_node *so_eval_create_reg(const char *regname); 108 so_eval_node *so_eval_create_reg_comp(const char *regname, int index); 109 so_eval_node *so_eval_create_flt_val(float val); 110 111 112 /* node ids */ 113 enum { 114 ID_ADD, 115 ID_ADD_VEC, 116 ID_SUB, 117 ID_SUB_VEC, 118 ID_MUL, 119 ID_DIV, 120 ID_NEG, 121 ID_NEG_VEC, 122 ID_AND, 123 ID_OR, 124 ID_LEQ, 125 ID_GEQ, 126 ID_EQ, 127 ID_NEQ, 128 ID_COS, 129 ID_SIN, 130 ID_TAN, 131 ID_ACOS, 132 ID_ASIN, 133 ID_ATAN, 134 ID_ATAN2, 135 ID_COSH, 136 ID_SINH, 137 ID_TANH, 138 ID_SQRT, 139 ID_EXP, 140 ID_LOG, 141 ID_LOG10, 142 ID_CEIL, 143 ID_FLOOR, 144 ID_FABS, 145 ID_FMOD, 146 ID_RAND, 147 ID_CROSS, 148 ID_DOT, 149 ID_LEN, 150 ID_NORMALIZE, 151 ID_TEST_FLT, 152 ID_TEST_VEC, 153 ID_VEC3F, 154 ID_FLT_REG, 155 ID_VEC_REG, 156 ID_VEC_REG_COMP, 157 ID_FLT_COND, 158 ID_VEC_COND, 159 ID_VALUE, 160 ID_ASSIGN_FLT, 161 ID_ASSIGN_VEC, 162 ID_SEPARATOR, 163 ID_NOT, 164 ID_LT, 165 ID_GT, 166 ID_POW, 167 ID_MUL_VEC_FLT, 168 ID_DIV_VEC_FLT 169 }; 170 171 /* lexical tokens */ 172 /* do not uncomment. Bison will define these 173 enum { 174 LEX_FLTFUNC, 175 LEX_COMPARE, 176 LEX_AND, 177 LEX_OR, 178 LEX_ATAN2, 179 LEX_POW, 180 LEX_FMOD, 181 LEX_VEC3F, 182 LEX_VALUE, 183 LEX_TMP_FLT_REG, 184 LEX_OUT_FLT_REG, 185 LEX_IN_FLT_REG, 186 LEX_TMP_VEC_REG, 187 LEX_OUT_VEC_REG, 188 LEX_IN_VEC_REG, 189 LEX_LEN, 190 LEX_ERROR 191 }; 192 */ 193 194 195 #ifdef __cplusplus 196 }; /* extern "C" */ 197 #endif /* __cplusplus */ 198 199 #endif /* COIN_EVALUATOR_H */ 200