SourceCombinePointwiseOperator.h

Go to the documentation of this file.
00001 // $Id$
00002 
00003 #ifndef __SOURCECOMBINEPOINTWISEOPERATOR_H__
00004 #define __SOURCECOMBINEPOINTWISEOPERATOR_H__
00005 
00006 #include "SourceGenerics.h"
00007 #include "SourceCombine.h"
00008 #include "Promotion.h"
00009 #include "libUtility.h"
00010 
00011 //============================================================================
00012 // base class
00013 //============================================================================
00014 template<class DATA, int DIMENSIONALITY, class PRECISION, class SOURCE>
00015 class SourceCombinePointwiseOperatorBaseOf: public SourceCombineOf<DATA,
00016     DIMENSIONALITY, PRECISION, SOURCE>
00017 {
00018 protected:
00019   typedef typename SOURCE::type_data DATA_SOURCE;
00020 
00021   mutable DATA_SOURCE* m_vdata;
00022   mutable int m_cdata;
00023 public:
00024   SourceCombinePointwiseOperatorBaseOf() :
00025     SourceCombineOf<DATA, DIMENSIONALITY, PRECISION, SOURCE> ()
00026   {
00027     m_vdata = NULL;
00028     m_cdata = 0;
00029   }
00030 
00031   virtual ~SourceCombinePointwiseOperatorBaseOf()
00032   {
00033     SafeDeleteArray(m_vdata);
00034   }
00035 
00036   virtual String Describe() const
00037   {
00038     return (_LINE + "SourceCombinePointwiseOperatorOf(" + DIMENSIONALITY + "D)"
00039         + LINE_ + this->DescribeCommon());
00040   }
00041 
00042   virtual void PrepareForAccessAction() const
00043   {
00044     SourceCombineOf<DATA, DIMENSIONALITY, PRECISION, SOURCE>::PrepareForAccessAction();
00045     if (m_cdata != this->CSources())
00046       {
00047       SafeDeleteArray(m_vdata);
00048       m_cdata = this->CSources();
00049       m_vdata = new DATA_SOURCE[m_cdata];
00050       }
00051   }
00052 
00053 };
00054 
00055 //============================================================================
00056 //============================================================================
00057 template<class OPERATOR, int DIMENSIONALITY, class PRECISION, class SOURCE>
00058 class SourceCombinePointwiseOperatorOf: public SourceCombineOf<
00059     typename OPERATOR::type_data, DIMENSIONALITY, PRECISION, SOURCE>
00060 {
00061 };
00062 
00063 //============================================================================
00064 // 2D
00065 //============================================================================
00066 template<class OPERATOR, class PRECISION, class SOURCE>
00067 class SourceCombinePointwiseOperatorOf<OPERATOR, 2, PRECISION, SOURCE> : public SourceCombinePointwiseOperatorBaseOf<
00068     typename OPERATOR::type_data, 2, PRECISION, SOURCE>
00069 {
00070   typedef typename OPERATOR::type_data DATA;
00071 public:
00072 SOURCE_ACTUALS_2D  ;
00073 
00074   void Get(DATA& dataOut, const PRECISION &rX, const PRECISION &rY) const
00075     {
00076     for (int n=0; n<this->m_cdata; n++)
00077       {
00078       ::Get(*this->PSource(n), this->m_vdata[n], rX, rY);
00079       }
00080 
00081     OPERATOR::FromSource(dataOut, this->m_vdata, this->m_cdata);
00082     }
00083 
00084   void Set(const PRECISION &rX, const PRECISION &rY, const DATA& data)
00085     {
00086     OPERATOR::ToSource(this->m_vdata, this->m_cdata, data);
00087 
00088     for (int n=0; n<this->m_cdata; n++)
00089       {
00090       ::Set(*this->PSource(n),rX, rY, this->m_vdata[n]);
00091       }
00092     }
00093   };
00094 
00095 //============================================================================
00096 // 3d
00097 //============================================================================
00098 template<class OPERATOR, class PRECISION, class SOURCE>
00099 class SourceCombinePointwiseOperatorOf<OPERATOR, 3, PRECISION, SOURCE> : public SourceCombinePointwiseOperatorBaseOf<
00100     typename OPERATOR::type_data, 3, PRECISION, SOURCE>
00101 {
00102   typedef typename OPERATOR::type_data DATA;
00103 public:
00104 SOURCE_ACTUALS_3D  ;
00105 
00106   void Get(DATA& dataOut, const PRECISION &rX, const PRECISION &rY, const PRECISION &rZ) const
00107     {
00108     for (int n=0; n<this->m_cdata; n++)
00109       {
00110       ::Get(*this->PSource(n), this->m_vdata[n], rX, rY, rZ);
00111       }
00112 
00113     OPERATOR::FromSource(dataOut, this->m_vdata, this->m_cdata);
00114     }
00115 
00116   void Set(const PRECISION &rX, const PRECISION &rY, const PRECISION &rZ, const DATA& data)
00117     {
00118     OPERATOR::ToSource(this->m_vdata, this->m_cdata, data);
00119 
00120     for (int n=0; n<this->m_cdata; n++)
00121       {
00122       ::Set(*this->PSource(n),rX, rY, rZ, this->m_vdata[n]);
00123       }
00124     }
00125 
00126   };
00127 
00128 //============================================================================
00129 // OperatorAverage
00130 //============================================================================
00131 template<class DATA_OUT, class DATA_SOURCE>
00132 class OperatorAverageOf
00133 {
00134 public:
00135   typedef DATA_OUT type_data;
00136 
00137   static void FromSource(DATA_OUT& dataOut, const DATA_SOURCE* vdata, const int& cdata)
00138   {
00139     RANGEPROMOTION(DATA_SOURCE) dataT(0);
00140 
00141     for (int n = 0; n < cdata; n++)
00142       {
00143       dataT = dataT + vdata[n];
00144       }
00145     dataOut = dataT / cdata;
00146   }
00147 
00148   static void ToSource(DATA_SOURCE* vdata, const int& cdata, const DATA_OUT& dataOut)
00149   {
00150     for (int n = 0; n < cdata; n++)
00151       {
00152       vdata[n] = dataOut;
00153       }
00154   }
00155 };
00156 
00157 template<class DATA, int DIMENSIONALITY, class PRECISION, class SOURCE>
00158 class TypeSourceCombineAverageOf
00159 {
00160 public:
00161   typedef SourceCombinePointwiseOperatorOf<OperatorAverageOf<DATA,
00162       typename SOURCE::type_data> , DIMENSIONALITY, PRECISION, SOURCE> type;
00163 };
00164 
00165 #define SourceCombineAverageOf(_data,_dims,_precision,_src) \
00166     typename TypeSourceCombineAverageOf<_data,_dims,_precision,_src>::type
00167 
00168 //============================================================================
00169 // OperatorSum
00170 //============================================================================
00171 template<class DATA_OUT, class DATA_SOURCE>
00172 class OperatorSumOf
00173 {
00174 public:
00175   typedef DATA_OUT type_data;
00176 
00177   static void FromSource(DATA_OUT& dataOut, const DATA_SOURCE* vdata, const int& cdata)
00178   {
00179     dataOut = 0;
00180 
00181     for (int n = 0; n < cdata; n++)
00182       {
00183       dataOut = dataOut + vdata[n];
00184       }
00185   }
00186 
00187   static void ToSource(DATA_SOURCE* vdata, const int& cdata, const DATA_OUT& dataOut)
00188   {
00189     for (int n = 0; n < cdata; n++)
00190       {
00191       vdata[n] = dataOut / cdata;
00192       }
00193   }
00194 };
00195 
00196 template<class DATA, int DIMENSIONALITY, class PRECISION, class SOURCE>
00197 class TypeSourceCombineSumOf
00198 {
00199 public:
00200   typedef SourceCombinePointwiseOperatorOf<OperatorSumOf<DATA,
00201       typename SOURCE::type_data> , DIMENSIONALITY, PRECISION, SOURCE> type;
00202 };
00203 
00204 #define SourceCombineSumOf(_data,_dims,_precision,_src) \
00205     typename TypeSourceCombineSumOf<_data,_dims,_precision,_src>::type
00206 
00207 //============================================================================
00208 // OperatorDifference
00209 //============================================================================
00210 template<class DATA_OUT, class DATA_SOURCE>
00211 class OperatorDifferenceOf
00212 {
00213 public:
00214   typedef DATA_OUT type_data;
00215 
00216   static void FromSource(DATA_OUT& dataOut, const DATA_SOURCE* vdata, const int& cdata)
00217   {
00218     dataOut = vdata[0];
00219 
00220     for (int n = 1; n < cdata; n++)
00221       {
00222       dataOut = dataOut - vdata[n];
00223       }
00224     dataOut = dataOut + MidValue(DATA_SOURCE);
00225   }
00226 
00227   static void ToSource(DATA_SOURCE* vdata, const int& cdata, const DATA_OUT& dataOut)
00228   {
00229     vdata[0] = dataOut;
00230     for (int n = 1; n < cdata; n++)
00231       {
00232       vdata[n] = MidValue(DATA_SOURCE) / cdata;
00233       }
00234   }
00235 };
00236 
00237 template<class DATA, int DIMENSIONALITY, class PRECISION, class SOURCE>
00238 class TypeSourceCombineDifferenceOf
00239 {
00240 public:
00241   typedef SourceCombinePointwiseOperatorOf<OperatorDifferenceOf<DATA,
00242       typename SOURCE::type_data> , DIMENSIONALITY, PRECISION, SOURCE> type;
00243 };
00244 
00245 #define SourceCombineDifferenceOf(_data,_dims,_precision,_src) \
00246     typename TypeSourceCombineDifferenceOf<_data,_dims,_precision,_src>::type
00247 
00248 //============================================================================
00249 // OperatorIndexOfMaximum
00250 //============================================================================
00251 template<class DATA_OUT, class DATA_SOURCE>
00252 class OperatorIndexOfMaximumOf
00253 {
00254 public:
00255   typedef DATA_OUT type_data;
00256 
00257   static void FromSource(DATA_OUT& dataOut, const DATA_SOURCE* vdata, const int& cdata)
00258   {
00259     DATA_SOURCE dataT = vdata[0];
00260     dataOut = 0;
00261 
00262     for (int n = 1; n < cdata; n++)
00263       {
00264       if (dataT < vdata[n])
00265         {
00266         dataOut = n;
00267         dataT = vdata[n];
00268         }
00269       }
00270   }
00271 
00272   static void ToSource(DATA_SOURCE* vdata, const int& cdata, const DATA_OUT& dataOut)
00273   {
00274     ERROR("not implemented");
00275   }
00276 };
00277 
00278 template<class DATA, int DIMENSIONALITY, class PRECISION, class SOURCE>
00279 class TypeSourceCombineIndexOfMaximumOf
00280 {
00281 public:
00282   typedef SourceCombinePointwiseOperatorOf<OperatorIndexOfMaximumOf<DATA,
00283       typename SOURCE::type_data> , DIMENSIONALITY, PRECISION, SOURCE> type;
00284 };
00285 
00286 #define SourceCombineIndexOfMaximumOf(_data,_dims,_precision,_src) \
00287     typename TypeSourceCombineIndexOfMaximumOf<_data,_dims,_precision,_src>::type
00288 
00289 #define COMMA ,
00290 //============================================================================
00291 // Average
00292 //============================================================================
00293 MAKE_COMBINE_ASSISTANT(
00294     Average,
00295     SourceCombinePointwiseOperatorOf,
00296     OperatorAverageOf<TypeOfData(SOURCE) COMMA TypeOfData(SOURCE)>,
00297     TypeOfDimensionality(SOURCE),
00298     TypeOfPrecision(SOURCE),
00299       {}
00300 );
00301 
00302 //============================================================================
00303 // Sum
00304 //============================================================================
00305 MAKE_COMBINE_ASSISTANT(
00306     Sum,
00307     SourceCombinePointwiseOperatorOf,
00308     OperatorSumOf<TypeOfData(SOURCE) COMMA TypeOfData(SOURCE)>,
00309     TypeOfDimensionality(SOURCE),
00310     TypeOfPrecision(SOURCE),
00311       {}
00312 );
00313 
00314 //============================================================================
00315 // Difference
00316 //============================================================================
00317 MAKE_COMBINE_ASSISTANT(
00318     Difference,
00319     SourceCombinePointwiseOperatorOf,
00320     OperatorDifferenceOf<TypeOfData(SOURCE) COMMA TypeOfData(SOURCE)>,
00321     TypeOfDimensionality(SOURCE),
00322     TypeOfPrecision(SOURCE),
00323       {}
00324 );
00325 
00326 #endif //__SOURCECOMBINEPOINTWISEOPERATOR_H__
00327 

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1