SourceMemory.h

Go to the documentation of this file.
00001 // $Id$
00002 
00003 #ifndef __SOURCEMEMORY_H__
00004 #define __SOURCEMEMORY_H__
00005 
00006 #include <stdlib.h>
00007 #include "Source.h"
00008 #include "Pixels.h"
00009 
00010 #undef DEBUG
00011 #define DEBUG 0
00012 #include "libDebug.h"
00013 
00014 //============================================================================
00015 //============================================================================
00016 template<class DATA, int DIMENSIONALITY>
00017 class SourceMemoryBaseOf: public SourceOf<DATA, DIMENSIONALITY, int>
00018 {
00019   typedef int PRECISION;
00020   typedef PointOf<DIMENSIONALITY, int> POINT;
00021 
00022 protected:
00023   typedef PointOf<DIMENSIONALITY, int> type_point;
00024 
00025   PointOf<DIMENSIONALITY, int> m_ptSize;
00026   DATA *m_vdata;
00027 
00028 public:
00029   //--------------------------------------------------------------------------
00030   //--------------------------------------------------------------------------
00031   SourceMemoryBaseOf() :
00032     SourceOf<DATA, DIMENSIONALITY, int> (), m_ptSize(0), m_vdata(NULL)
00033   {
00034   }
00035 
00036   virtual ~SourceMemoryBaseOf()
00037   {
00038     if (this->AboutToDie())
00039       {
00040       Free();
00041       }
00042   }
00043 
00044   virtual String Describe() const
00045   {
00046     return (_LINE + "SourceMemory(" + DIMENSIONALITY + "D)" + LINE_
00047         + this->DescribeCommon());
00048   }
00049 
00050   //--------------------------------------------------------------------------
00051   //--------------------------------------------------------------------------
00052   const PointOf<DIMENSIONALITY, int>& Size() const
00053   {
00054     return m_ptSize;
00055   }
00056 
00057   //--------------------------------------------------------------------------
00058   //--------------------------------------------------------------------------
00059   PointOf<DIMENSIONALITY, int> Bound(const PointOf<DIMENSIONALITY, int>& pt) const
00060   {
00061     return pt.Bound(PointOf<DIMENSIONALITY, int> (0), m_ptSize - 1);
00062   }
00063 
00064   //--------------------------------------------------------------------------
00065   //--------------------------------------------------------------------------
00066   const DATA *ReadData() const
00067   {
00068     return m_vdata;
00069   }
00070 
00071   //--------------------------------------------------------------------------
00072   //--------------------------------------------------------------------------
00073   DATA *WriteData()
00074   {
00075     return m_vdata;
00076   }
00077 
00078   //--------------------------------------------------------------------------
00079   //--------------------------------------------------------------------------
00080   virtual void Allocate(const PointOf<DIMENSIONALITY, int>& ptSize)
00081   {
00082     ASSERTf(Counted::References() == 1, "Shared object being reallocated?");
00083     if (m_ptSize.CVolume() == ptSize.CVolume())
00084       {
00085       m_ptSize = ptSize;
00086       D1("KEEPING MEMORY %ld", sizeof(DATA) * this->CSize());
00087       }
00088     else
00089       {
00090       Free();
00091       m_ptSize = ptSize;
00092       D1("ALLOCATING MEMORY %ld", sizeof(DATA) * this->CSize());
00093       m_vdata = new DATA[this->CSize()];
00094       }
00095   }
00096 
00097   //--------------------------------------------------------------------------
00098   //--------------------------------------------------------------------------
00099   void Free()
00100   {
00101     if (m_vdata != NULL)
00102       {
00103       D1("DELETEING MEMORY %ld", sizeof(DATA) * this->CSize());
00104       delete[] m_vdata;
00105       }
00106   }
00107 
00108   //--------------------------------------------------------------------------
00109   //--------------------------------------------------------------------------
00110   virtual void Fill(const DATA& data)
00111   {
00112     int c = this->CSize();
00113     for (int n = 0; n < c; n++)
00114       {
00115       m_vdata[n] = data;
00116       }
00117   }
00118 
00119   /*
00120    //--------------------------------------------------------------------------
00121    //--------------------------------------------------------------------------
00122    // each point dimensionality will probably want to override this,
00123    // but a default implementation is possible
00124    void GetPoint(DATA& dataOut, const PointOf<DIMENSIONALITY,int>& ptIn) const
00125    {
00126    WARNING("probably dont want to use this");
00127    PointOf<DIMENSIONALITY,int> pt=ptIn.Bound(
00128    PointOf<DIMENSIONALITY,int>(0),this->m_ptSize-1
00129    );
00130 
00131    int cDim=DIMENSIONALITY;
00132 
00133    int nLoc=0;
00134    for (int nDim=0; nDim<cDim; nDim++){
00135    nLoc=int(nLoc*this->CSize(nDim) + pt.Dim(nDim));
00136    }
00137 
00138    dataOut=this->m_vdata[nLoc];
00139    }
00140 
00141    //--------------------------------------------------------------------------
00142    //--------------------------------------------------------------------------
00143    // each point dimensionality will probably want to override this,
00144    // but a default implementation is possible
00145    void SetPoint(const PointOf<DIMENSIONALITY,int>& ptIn, const DATA& data)
00146    {
00147    WARNING("probably dont want to use this");
00148    PointOf<DIMENSIONALITY,int> pt=ptIn.Bound(
00149    PointOf<DIMENSIONALITY,int>(0),this->m_ptSize-1
00150    );
00151 
00152    int cDim=DIMENSIONALITY;
00153 
00154    int nLoc=0;
00155    for (int nDim=0; nDim<cDim; nDim++){
00156    nLoc=int(nLoc*this->CSize(nDim) + pt.Dim(nDim));
00157    }
00158 
00159    this->m_vdata[nLoc]=data;
00160    }
00161    */
00162 
00163   static String SerializationId()
00164   {
00165     return String("SourceMemory(") + DIMENSIONALITY + "D)";
00166   }
00167 
00168   void Serialize(Stream& st) const
00169   {
00170     ::Serialize(st, m_ptSize);
00171     st.Append((byte*) m_vdata, this->CSize() * sizeof(DATA));
00172   }
00173 
00174   void Deserialize(Stream& st)
00175   {
00176     type_point pt;
00177     ::Deserialize(st, pt);
00178     Allocate(pt);
00179     st.Read((byte*) m_vdata, this->CSize() * sizeof(DATA));
00180   }
00181 };
00182 
00183 //============================================================================
00184 //============================================================================
00185 template<class DATA, int DIMENSIONALITY>
00186 class SourceMemoryOf: public SourceMemoryBaseOf<DATA, DIMENSIONALITY>
00187 {
00188   void RegisterDataAsParameters(RegistryOfParameters& reg)
00189   {
00190     ERROR("NOT IMPLEMENTED");
00191   }
00192 };
00193 
00194 //============================================================================
00195 //============================================================================
00196 template<class DATA>
00197 class SourceMemoryOf<DATA, 2> : public SourceMemoryBaseOf<DATA, 2>
00198 {
00199   typedef int PRECISION;
00200 public:
00201 SOURCE_ACTUALS_2D  ;
00202 
00203   //--------------------------------------------------------------------------
00204   //--------------------------------------------------------------------------
00205   void Get(DATA& dataOut, const int& nX, const int& nY) const
00206     {
00207     const int& cX=this->m_ptSize.X();
00208 
00209     dataOut= this->m_vdata[nY*cX + nX];
00210     }
00211 
00212   //--------------------------------------------------------------------------
00213   //--------------------------------------------------------------------------
00214   void Set(const int& nX, const int& nY, const DATA& data)
00215     {
00216     const int& cX=this->m_ptSize.X();
00217 
00218     this->m_vdata[nY*cX + nX]=data;
00219     }
00220 
00221   void RegisterDataAsParameters(RegistryOfParameters& reg);
00222 
00223   };
00224 
00225 //============================================================================
00226 //============================================================================
00227 template<class DATA>
00228 class SourceMemoryOf<DATA, 3> : public SourceMemoryBaseOf<DATA, 3>
00229 {
00230   typedef int PRECISION;
00231 public:
00232 SOURCE_ACTUALS_3D  ;
00233 
00234   //--------------------------------------------------------------------------
00235   //--------------------------------------------------------------------------
00236   void Get(DATA& dataOut, const int& nX, const int& nY, const int& nZ) const
00237     {
00238     const int& cX=this->m_ptSize.X();
00239     const int& cY=this->m_ptSize.Y();
00240 
00241     dataOut= this->m_vdata[(nZ*cY+nY)*cX + nX];
00242     }
00243 
00244   //--------------------------------------------------------------------------
00245   //--------------------------------------------------------------------------
00246   void Set(const int& nX, const int& nY, const int& nZ, const DATA& data)
00247     {
00248     const int& cX=this->m_ptSize.X();
00249     const int& cY=this->m_ptSize.Y();
00250 
00251     this->m_vdata[(nZ*cY+nY)*cX + nX]=data;
00252     }
00253 
00254   void RegisterDataAsParameters(RegistryOfParameters& reg);
00255   };
00256 
00257 //--------------------------------------------------------------------------
00258 //--------------------------------------------------------------------------
00259 template<>
00260 inline void SourceMemoryOf<Point2DReal, 2>::RegisterDataAsParameters(RegistryOfParameters& reg)
00261 {
00262   int c = this->CSize();
00263   for (int n = 0; n < c; n++)
00264     {
00265     reg.Register(&(this->m_vdata[n].X()));
00266     reg.Register(&(this->m_vdata[n].Y()));
00267     }
00268 }
00269 
00270 //--------------------------------------------------------------------------
00271 //--------------------------------------------------------------------------
00272 template<>
00273 inline void SourceMemoryOf<Point3DReal, 3>::RegisterDataAsParameters(RegistryOfParameters& reg)
00274 {
00275   int c = this->CSize();
00276   for (int n = 0; n < c; n++)
00277     {
00278     reg.Register(&(this->m_vdata[n].X()));
00279     reg.Register(&(this->m_vdata[n].Y()));
00280     reg.Register(&(this->m_vdata[n].Z()));
00281     }
00282 }
00283 
00284 //--------------------------------------------------------------------------
00285 //--------------------------------------------------------------------------
00286 template<>
00287 inline void SourceMemoryOf<Real, 2>::RegisterDataAsParameters(RegistryOfParameters& reg)
00288 {
00289   int c = this->CSize();
00290   for (int n = 0; n < c; n++)
00291     {
00292     reg.Register(&(this->m_vdata[n]));
00293     }
00294 }
00295 
00296 //--------------------------------------------------------------------------
00297 //--------------------------------------------------------------------------
00298 template<>
00299 inline void SourceMemoryOf<Real, 3>::RegisterDataAsParameters(RegistryOfParameters& reg)
00300 {
00301   int c = this->CSize();
00302   for (int n = 0; n < c; n++)
00303     {
00304     reg.Register(&(this->m_vdata[n]));
00305     }
00306 }
00307 
00308 //--------------------------------------------------------------------------
00309 //--------------------------------------------------------------------------
00310 template<class DATA>
00311 inline void SourceMemoryOf<DATA, 2>::RegisterDataAsParameters(RegistryOfParameters& reg)
00312 {
00313   ERROR("NOT IMPLEMENTED");
00314 }
00315 
00316 //--------------------------------------------------------------------------
00317 //--------------------------------------------------------------------------
00318 template<class DATA>
00319 inline void SourceMemoryOf<DATA, 3>::RegisterDataAsParameters(RegistryOfParameters& reg)
00320 {
00321   ERROR("NOT IMPLEMENTED");
00322 }
00323 
00324 //==========================================================================
00325 //==========================================================================
00326 // HELPERS (ASSISTANTS)
00327 //==========================================================================
00328 //==========================================================================
00329 
00330 //--------------------------------------------------------------------------
00331 //--------------------------------------------------------------------------
00332 template<class SOURCE, int DIMENSIONALITY>
00333 SOURCE* Allocate(const PointOf<DIMENSIONALITY, int>& ptSize, SOURCE* psm)
00334 {
00335   psm->Allocate(ptSize);
00336   return psm;
00337 }
00338 
00339 //--------------------------------------------------------------------------
00340 //--------------------------------------------------------------------------
00341 template<class SOURCE, class DATA>
00342 SOURCE* Fill(const DATA& data, SOURCE *psm)
00343 {
00344   psm->Fill(data);
00345   return psm;
00346 }
00347 
00348 #endif // __SOURCEMEMORY_H__

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1