00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #include "PrimitiveFunction.h"
00018 #include "Primitive.h"
00019 #include "SceneGraph.h"
00020 
00021 #ifndef N_ARITHMETIC_PRIMITIVE_FUNCTION
00022 #define N_ARITHMETIC_PRIMITIVE_FUNCTION
00023 
00024 using namespace std;
00025 
00026 namespace Fluxus
00027 {
00028 
00033 class ArithmeticPrimFunc : public PrimitiveFunction
00034 {
00035 public:
00036     ArithmeticPrimFunc();
00037     ~ArithmeticPrimFunc();
00038 
00039     virtual void Run(Primitive &prim, const SceneGraph &world);
00040 
00041 private:
00042 
00047     PData *OperatorFirst(const string &op, PData* first, PData *second);
00048     template<class T>
00049     PData *OperatorSecond(const string &op, TypedPData<T>* first, PData *second);
00050     template<class T, class S>
00051     PData *OperatorThird(const string &op, TypedPData<T>* first, TypedPData<S>* second);
00052 
00053     PData *OperatorFloatFirst(const string &op, PData* first, float second);
00054     template<class T>
00055     PData *OperatorFloatSecond(const string &op, TypedPData<T>* first, float second);
00057 };
00058 
00059 template<class T>
00060 PData *ArithmeticPrimFunc::OperatorSecond(const string &op, TypedPData<T>* first, PData *second)
00061 {
00062     
00063     TypedPData<T> *data = dynamic_cast<TypedPData<T>*>(second); 
00064     if (data) return OperatorThird<T,T>(op,first,data);
00065     else
00066     {
00067         TypedPData<float> *data = dynamic_cast<TypedPData<float>*>(second);
00068         if (data) return OperatorThird<T,float>(op,first,data);
00069     }
00070     return NULL;
00071 }
00072 
00073 template<class T, class S>
00074 PData *ArithmeticPrimFunc::OperatorThird(const string &op, TypedPData<T>* first, TypedPData<S>* second)
00075 {
00076     if (op=="add") 
00077     {   
00078         TypedPData<T> *tmp=new TypedPData<T>(first->Size());
00079         for (unsigned int n=0; n<first->Size(); n++)
00080         {
00081             tmp->m_Data[n]=first->m_Data[n]+second->m_Data[n];
00082         }
00083         return tmp;
00084     }
00085     else if (op=="sub") 
00086     {   
00087         TypedPData<T> *tmp=new TypedPData<T>(first->Size());
00088         for (unsigned int n=0; n<first->Size(); n++)
00089         {
00090             tmp->m_Data[n]=first->m_Data[n]-second->m_Data[n];
00091         }
00092         return tmp;
00093     }
00094     else if (op=="mul") 
00095     {   
00096         TypedPData<T> *tmp=new TypedPData<T>(first->Size());
00097         for (unsigned int n=0; n<first->Size(); n++)
00098         {
00099             tmp->m_Data[n]=first->m_Data[n]*second->m_Data[n];
00100         }
00101         return tmp;
00102     }
00103     else if (op=="div") 
00104     {   
00105         TypedPData<T> *tmp=new TypedPData<T>(first->Size());
00106         for (unsigned int n=0; n<first->Size(); n++)
00107         {
00108             tmp->m_Data[n]=first->m_Data[n]/second->m_Data[n];
00109         }
00110         return tmp;
00111     }
00112     
00113     return NULL;
00114 }
00115 
00116 template<class T>
00117 PData *ArithmeticPrimFunc::OperatorFloatSecond(const string &op, TypedPData<T>* first, float second)
00118 {
00119     if (op=="add") 
00120     {   
00121         TypedPData<T> *tmp=new TypedPData<T>(first->Size());
00122         for (unsigned int n=0; n<first->Size(); n++)
00123         {
00124             tmp->m_Data[n]=first->m_Data[n]+second;
00125         }
00126         return tmp;
00127     }
00128     else if (op=="sub") 
00129     {   
00130         TypedPData<T> *tmp=new TypedPData<T>(first->Size());
00131         for (unsigned int n=0; n<first->Size(); n++)
00132         {
00133             tmp->m_Data[n]=first->m_Data[n]-second;
00134         }
00135         return tmp;
00136     }
00137     else if (op=="mul") 
00138     {   
00139         TypedPData<T> *tmp=new TypedPData<T>(first->Size());
00140         for (unsigned int n=0; n<first->Size(); n++)
00141         {
00142             tmp->m_Data[n]=first->m_Data[n]*second;
00143         }
00144         return tmp;
00145     }
00146     else if (op=="div") 
00147     {   
00148         TypedPData<T> *tmp=new TypedPData<T>(first->Size());
00149         for (unsigned int n=0; n<first->Size(); n++)
00150         {
00151             tmp->m_Data[n]=first->m_Data[n]/second;
00152         }
00153         return tmp;
00154     }
00155     
00156     return NULL;
00157 }
00158 
00159 
00160 }
00161 
00162 #endif