Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Container3D.hxx

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00020 #ifndef _Container3D_hxx
00021 #define _Container3D_hxx
00022 
00023 // *******************************************************
00024 // template implementations for Container3D
00025 // *******************************************************
00026 
00027 template<class Im3DValue>
00028 bool 
00029 Container3D<Im3DValue>::WarnOnUnkownType() const
00030 {
00031     if(!(IsSameType<Im3DValue,float  >() || IsSameType<Im3DValue,double   >() || 
00032          IsSameType<Im3DValue,Vect3Df>() || IsSameType<Im3DValue,byte     >() || 
00033          IsSameType<Im3DValue,int    >() || IsSameType<Im3DValue,short int>()))
00034     {
00035         mprintf("Warning: Container3D value type is unknown, using default routines:typename:%s\n",TypeName<Im3DValue>().c_str());
00036         return true;
00037     }
00038     return false;
00039 }
00040 
00041 
00042 
00043 template<class Im3DValue>
00044 void 
00045 Container3D<Im3DValue>::Write(ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node) const
00046 {
00047     node=CreateWriteNode(file,parentNode,node);
00048     // **** Optionally add unset image properties ***
00049     // set image type (Image3Df, Field3D etc....) 
00050     if(!file->HasAttribute(node,"Type")){node->set_attribute("Type",GetTypeName());}
00051     // store Data (binary) information
00052     if(!file->HasChild(node,"ImageData"))
00053     {
00054         xmlpp::Element *dataN=node->add_child("ImageData");
00055         file->AddBinaryNode(dataN,GetBinarySize());
00056         // write image's binary block 
00057         WriteBinary(file);
00058     }      
00059     // write parents data (image size and properties)
00060     Image3D::Write(file,NULL,node);
00061 }
00062 template<class Im3DValue>
00063 void 
00064 Container3D<Im3DValue>::Read(ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node)
00065 {
00066     node=CreateReadNode(file,parentNode,node);
00067     Image3D::Read(file,NULL,node);
00068 
00069     if(file->debug){cout << "Container3D::Read: " << endl;file->ShowXML(node);}
00070     
00071     //type checking
00072     string type=file->GetAttribute(node,"Type");
00073     if(type!=GetTypeName()){ThrowError("Container3D::Read: Expected type:\"%s\" found type:\"%s\"",GetTypeName(),type);}
00074 
00075     //image data
00076     xmlpp::Element *dataN=file->GetChild(node,"ImageData");
00077     file->ReadBinaryNode(dataN);
00078     ReadBinary(file);
00079     if(!file->CheckBinaryReadComplete())
00080     {ThrowError("XMLBinaryFile::BinaryRead bad read size (maybe wrong image type?) ");}
00081 }
00082 
00083 
00084 template<class Im3DValue>
00085 void
00086 Container3D<Im3DValue>::WriteBinary(ImLib3DFile *file) const
00087 {
00088     file->template BinaryWrite<Im3DValue,Im3DValue *>(imageData,imageData+GetNVoxels());
00089 }
00090 template<class Im3DValue>
00091 void
00092 Container3D<Im3DValue>::ReadBinary(ImLib3DFile *file)
00093 {
00094     file->template BinaryRead<Im3DValue>(imageData,imageData+GetNVoxels());
00095 }
00096 
00097 // *******************************************************
00098 template<class Im3DValue>
00099 void 
00100 Container3D<Im3DValue>::Resize(const Size3D &newSize)
00101 {
00102     if(GetData() && newSize==Size()){return;}
00103     Image3D::Resize(newSize);
00104     DeAllocate();
00105     nvoxels=width*height*depth;
00106     widthXheight=width*height;
00107     if(nvoxels>0){Allocate();}
00108     if(nvoxels<0){ThrowError("Container3D::Resize size<0??");}
00109     if(HasMask()){Mask().Resize(newSize);}
00110 }
00111 
00112 
00113 // *******************************************************
00114 template<class Im3DValue>
00115 Container3D<Im3DValue>& 
00116 Container3D<Im3DValue>::operator=(const Container3D<Im3DValue> & other)
00117 {
00118     if (this == &other){return *this;}
00119     if(other.Size()!=Size()){Resize(other.Size());}
00120     // call parent operator =
00121     Image3D::operator=(*dynamic_cast<const Image3D *>(&other));
00122     // now copy the actual image data   
00123     if(IsSameType<Im3DValue,float  >() || IsSameType<Im3DValue,double>() || 
00124        IsSameType<Im3DValue,Vect3Df>() || IsSameType<Im3DValue,int   >() || 
00125        IsSameType<Im3DValue,byte   >() )
00126     {
00127         // memcpy is 2xfaster and works safely on these types
00128         memcpy((void *)GetData(),(void *)other.GetData(),sizeof(Im3DValue)*GetNVoxels());
00129     }
00130     else
00131     {
00132         // use fast iterators for unknown types
00133         const_iteratorFast pOrig;
00134         iteratorFast p;
00135         for (p = begin(),pOrig=other.begin(); p != end(); p++,pOrig++)
00136         {
00137             *p = *pOrig;
00138         }       
00139     }
00140     
00141     return *this;
00142 }
00143 
00144 // *******************************************************
00145 template<class Im3DValue>
00146 template<class OtherImageType>
00147 Container3D<Im3DValue>& 
00148 Container3D<Im3DValue>::operator=(const OtherImageType & other)
00149 {
00150     // copy information in parent class
00151     Image3D::operator=(other);
00152 
00153     // now copy data, using cast
00154     typename OtherImageType::const_iteratorFast pOrig;
00155     iteratorFast p;
00156     for (p = begin(),pOrig=other.begin(); p != end(); p++,pOrig++)
00157     {
00158         *p = static_cast<Im3DValue>(*pOrig);
00159     }       
00160         
00161     return *this;
00162 }
00163 
00164 // *******************************************************
00165 template<class Im3DValue>
00166 bool 
00167 Container3D<Im3DValue>::operator==(const Container3D<Im3DValue> & other) const
00168 {
00169     if(other.Size()!=Size()){return false;}
00170 
00171     const_iteratorFast pOrig;
00172     const_iteratorXYZ p;
00173     for (p = begin(),pOrig=other.begin(); p != end(); p++,pOrig++)
00174     {
00175         if(!((*p) == (*pOrig))){return false;}
00176     }       
00177     
00178     if(!(properties==other.properties)){return false;}
00179     
00180     return true;
00181 }
00182 
00183 
00184 // *******************************************************
00185 template<class Im3DValue>
00186 void
00187 Container3D<Im3DValue>:: Allocate()
00188 {
00189     DeAllocate();
00190     try
00191     {
00192 //              mprintf("Container3D::Allocate: nvoxels:%d sizeof element:%d\n",nvoxels,sizeof(Im3DValue));
00193 //              mprintf("Container3D::Allocate: %d %d %d\n",Width(),Height(),Depth());
00194         imageData=new Im3DValue[GetNVoxels()];      
00195         if(!imageData)
00196         {
00197             ThrowError("Container3D::Allocate: allocate failed. nvoxels:%d sizeof element:%d\n",GetNVoxels(),sizeof(Im3DValue));
00198         }
00199         imageDataEnd=imageData+GetNVoxels();
00200     }
00201     catch(...)
00202     {
00203         ThrowError("Container3D::Allocate: allocate failed. nvoxels:%d sizeof element:%d\n",GetNVoxels(),sizeof(Im3DValue));
00204     }
00205 }
00206 
00207 // *******************************************************
00208 template<class Im3DValue>
00209 void 
00210 Container3D<Im3DValue>::DeAllocate()
00211 {
00212     if(imageData){delete [] imageData;}
00213     imageData=NULL;
00214     imageDataEnd=NULL;
00215 }
00216 
00217 // *******************************************************
00218 template<class Im3DValue>
00219 Container3D<Im3DValue>::~Container3D()
00220 {
00221     DeAllocate();
00222 }
00223 
00224 // *******************************************************
00225 template<class Im3DValue>
00226 Container3D<Im3DValue>::Container3D(int _width, int _height, int _depth) : 
00227     Image3D(_width,_height,_depth),
00228     imageData(NULL),imageDataEnd(NULL)
00229 {
00230     Resize(Size3D(_width,_height,_depth));
00231 }
00232 // *******************************************************
00233 template<class Im3DValue>
00234 Container3D<Im3DValue>::Container3D():
00235     Image3D(),
00236     imageData(NULL),imageDataEnd(NULL)
00237 {
00238     Resize(Size3D(0,0,0));
00239 }
00240 
00241 // *******************************************************
00242 template<class Im3DValue>
00243 Container3D<Im3DValue>::Container3D(const Size3D &size):
00244     Image3D(size),
00245     imageData(NULL),imageDataEnd(NULL)
00246 {
00247     Resize(size);
00248 }
00249 
00250 // *******************************************************
00251 template<class Im3DValue>
00252 Container3D<Im3DValue>::Container3D(const Container3D & other) :
00253     Image3D(other),
00254     imageData(NULL),imageDataEnd(NULL)
00255 {
00256     Resize(other.Size());
00257     (*this)=other;
00258 }
00259 
00260 #endif // _Container3D_hxx

Generated on Fri Jun 17 13:35:56 2005 for ImLib3D by  doxygen 1.4.2