ArithmeticPrimFunc.h

Go to the documentation of this file.
00001 // Copyright (C) 2005 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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     // the second parameter can either be a float or T (the same type as the first parameter)
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

Generated on Mon Feb 11 06:54:25 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1