00001
00014 #ifndef CMATHEXPRESSION_H
00015 #define CMATHEXPRESSION_H
00016
00017
00025 class CMathExpression
00026 {
00027 public:
00031 CMathExpression(const char * Equation = 0);
00032
00036 CMathExpression(const CMathExpression& copy)
00037 { m_pFormula = copy.m_pFormula->GetCopy(); }
00038
00042 const CMathExpression& operator=(const CMathExpression& copy)
00043 {
00044 if (this != ©)
00045 {
00046 if (m_pFormula)
00047 delete m_pFormula;
00048
00049 m_pFormula = copy.m_pFormula->GetCopy();
00050 }
00051 return *this;
00052 }
00053
00058 float GetValue(const CVector3f& Point) const
00059 {
00060
00061
00062 #if 0
00063 POP_ASSERT(m_pFormula != NULL, "Using a NULL pointer.");
00064 return (m_pFormula->Compute(Point));
00065 #else
00066 POP_ASSERT(m_pFunction != NULL, "Using a NULL pointer.");
00067 return ((*m_pFunction)(Point));
00068 #endif
00069 }
00070
00075 CMathExpression* GetDerivative(CValue::TYPE_VARIABLE Type) const
00076 {
00077 POP_ASSERT(m_pFormula != NULL, "Using a NULL pointer.");
00078 CMathExpression* ptrDerivative = new CMathExpression;
00079 ptrDerivative->m_pFormula = m_pFormula->Derive(Type);
00080 ptrDerivative->Compile();
00081 return ptrDerivative;
00082 }
00083
00088 void Print(std::ostream& o) const
00089 {
00090 POP_ASSERT(m_pFormula != NULL, "Using a NULL pointer.");
00091 m_pFormula->Print(o);
00092 }
00093
00097 std::string GetString() const
00098 {
00099 POP_ASSERT(m_pFormula != NULL, "Using a NULL pointer.");
00100 std::ostringstream o;
00101 m_pFormula->Print(o);
00102 return o.str();
00103 }
00104
00109 ~CMathExpression()
00110 {
00111 if (m_pFormula)
00112 delete m_pFormula;
00113 if (m_pFunction)
00114 delete [] (char *) m_pFunction;
00115 }
00116
00120 static bool Validate(const std::string& Formula, int *value = NULL,
00121 int min = -1, int max = -1);
00122
00123 private:
00124 CValue* m_pFormula;
00125 float (*m_pFunction)(const CVector3f&);
00133 static CValue* BuildFormula(const char* Formula, int min, int max);
00134
00139 void Compile(std::vector<char>& v) const;
00140
00145 void Compile()
00146 {
00147 if (m_pFunction)
00148 delete [] (char *) m_pFunction;
00149
00150
00151 std::vector<char> buffer;
00152 Compile(buffer);
00153 char * pFunc = new char[buffer.size()];
00154 memcpy(pFunc, &buffer[0], buffer.size()*sizeof(char));
00155 m_pFunction = (float (*)(const CVector3f&)) pFunc;
00156 }
00157 };
00158
00159 #endif // CMATHEXPRESSION_H
00160