00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00020 #ifndef _Container3D_hxx
00021 #define _Container3D_hxx
00022
00023
00024
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
00049
00050 if(!file->HasAttribute(node,"Type")){node->set_attribute("Type",GetTypeName());}
00051
00052 if(!file->HasChild(node,"ImageData"))
00053 {
00054 xmlpp::Element *dataN=node->add_child("ImageData");
00055 file->AddBinaryNode(dataN,GetBinarySize());
00056
00057 WriteBinary(file);
00058 }
00059
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
00072 string type=file->GetAttribute(node,"Type");
00073 if(type!=GetTypeName()){ThrowError("Container3D::Read: Expected type:\"%s\" found type:\"%s\"",GetTypeName(),type);}
00074
00075
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
00121 Image3D::operator=(*dynamic_cast<const Image3D *>(&other));
00122
00123 if(IsSameType<Im3DValue,float >() || IsSameType<Im3DValue,double>() ||
00124 IsSameType<Im3DValue,Vect3Df>() || IsSameType<Im3DValue,int >() ||
00125 IsSameType<Im3DValue,byte >() )
00126 {
00127
00128 memcpy((void *)GetData(),(void *)other.GetData(),sizeof(Im3DValue)*GetNVoxels());
00129 }
00130 else
00131 {
00132
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
00151 Image3D::operator=(other);
00152
00153
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
00193
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