00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00019 #include<ImLib3D/Image3D.hpp>
00020 #include<ImLib3D/Image3Dlinear.hpp>
00021 #ifndef _Mask3D_hpp
00022 #define _Mask3D_hpp
00023
00024
00026
00027 template<class MaskType>
00028 class MaskConcept
00029 {
00030 inline MaskType * mask() {return (MaskType *)(this);}
00031 inline const MaskType *cmask() const {return (MaskType *)(this);}
00032 public:
00034 void Invert()
00035 {
00036 typename MaskType::iteratorFast p;
00037 for (p=mask()->begin(); p!=mask()->end(); p++)
00038 {
00039 if (!(*p)) { (*p) = 1;}
00040 else { (*p) = 0;}
00041 }
00042 }
00044 void Normalize()
00045 {
00046 typename MaskType::iteratorFast p;
00047 for (p=mask()->begin(); p!=mask()->end(); p++)
00048 {
00049 if (*p){(*p) = 1;}
00050 }
00051 }
00052
00054 void Select(byte v)
00055 {
00056 typename MaskType::iteratorFast p;
00057 for (p=mask()->begin(); p!=mask()->end(); p++)
00058 {
00059 if (*p==v){(*p) = 1;}else{*p=0;}
00060 }
00061 }
00063 template<class ImArgType>
00064 void ApplyOn(ImArgType &ima) const
00065 {
00066 if(ima.Size()!=cmask()->Size())
00067 {ThrowError("MaskIP3D::onceptOn different sizes");}
00068 typename MaskType::const_iteratorFast p;
00069 typename ImArgType::iteratorFast sp;
00070 for (p=cmask()->begin(),sp=ima.begin(); p!=cmask()->end(); p++,sp++)
00071 {
00072 if (!(*p)) { (*sp) = Zero<typename ImArgType::value_type>();}
00073 }
00074 }
00075 double Sum() const
00076 {
00077 double sum=0;
00078 for(typename MaskType::const_iteratorFast p=cmask()->begin(); p!=cmask()->end(); p++ )
00079 {
00080 sum+=*p;
00081 }
00082 return sum;
00083 }
00085 Vect3Di VectFromOffset(int position) const
00086 {
00087 int counter=0;
00088 typename MaskType::const_iteratorXYZ it;
00089 for (it=cmask()->begin(); it!=cmask()->end(); it++)
00090 {
00091 if (*it)
00092 {
00093 counter++;
00094 if (counter == position)
00095 return Vect3Di(it.x, it.y, it.z);
00096 }
00097 }
00098 ThrowError("Mask3d::RealPosFromRelativePos position outside mask : %d\n", position);
00099 return Vect3Di(0, 0, 0);
00100 }
00102 RectZone3Di FindBoundingBox() const
00103 {
00104 RectZone3Di box(cmask()->Height(), cmask()->Width(), cmask()->Depth(), 0, 0, 0);
00105 typename MaskType::const_iteratorXYZ p;
00106 bool noValues = true;
00107 for (p=cmask()->begin(); p!=cmask()->end(); p++)
00108 {
00109 if ((*p)>0)
00110 {
00111 if (p.x < box.x0)
00112 box.x0 = p.x;
00113 if (p.y < box.y0)
00114 box.y0 = p.y;
00115 if (p.z < box.z0)
00116 box.z0 = p.z;
00117 noValues = false;
00118 }
00119 }
00120 int n = cmask()->GetNVoxels();
00121 p=cmask()->begin(); p+=n-1;
00122 for (; p!=cmask()->begin(); p--)
00123 {
00124 if ((*p)>0)
00125 {
00126 if (p.x > box.x1)
00127 box.x1 = p.x;
00128 if (p.y > box.y1)
00129 box.y1 = p.y;
00130 if (p.z > box.z1)
00131 box.z1 = p.z;
00132 noValues = false;
00133 }
00134 }
00135 if (noValues)
00136 {
00137 box.x0 = 0; box.y0 = 0; box.z0 = 0;
00138 }
00139 return box;
00140 }
00141
00142 };
00143
00144
00146
00149 class Mask3D : public Image3Dlinear<byte> , public MaskConcept<Mask3D>
00150 {
00151 typedef Image3Dlinear<byte> _Parent;
00152 public:
00153 virtual string GetTypeName() const;
00154 virtual void Write(ImLib3DFile *file,xmlpp::Element *parentNode=NULL,xmlpp::Element *node=NULL) const
00155 {
00156 node=_Parent::CreateWriteNode(file,parentNode,node);
00157 if(!file->HasAttribute(node,"Type")){node->set_attribute ("Type",GetTypeName());}
00158 _Parent::Write(file,NULL,node);
00159 }
00160
00161 template<class OtherIm3DValue >
00162 Mask3D & operator=(const Image3Dlinear<OtherIm3DValue> & imOrig)
00163 {
00164 Container3D<byte>::operator=(imOrig);
00165 return *this;
00166 }
00167
00168 double Sum() const {return MaskConcept<Mask3D>::Sum();}
00169
00170
00171 Mask3D():Image3Dlinear<byte>(){;}
00172 Mask3D(const Size3D &size) :
00173 Image3Dlinear<byte>(size)
00174 {}
00175 Mask3D(const Mask3D &other):
00176 Image3Dlinear<byte>(other)
00177 {}
00178 Mask3D(int _width, int _height, int _depth) :
00179 Image3Dlinear<byte>( _width, _height, _depth )
00180 {}
00181 Mask3D(const string& fname) :
00182 Image3Dlinear<byte>()
00183 {Streamable::ReadFromFile(fname);}
00184 Mask3D(const char * fname) :
00185 Image3Dlinear<byte>()
00186 {Streamable::ReadFromFile(fname);}
00187 Mask3D( char * fname) :
00188 Image3Dlinear<byte>()
00189 {Streamable::ReadFromFile(fname);}
00190 template<class OtherImageType >
00191 Mask3D(const OtherImageType & other):
00192 Image3Dlinear<byte>(other)
00193 {}
00194 };
00195
00196
00197
00198 inline byte Image3D::Mask(const Vect3Di &P) const {return (*properties.mask)(P);}
00199 inline byte &Image3D::Mask(const Vect3Di &P) {return (*properties.mask)(P);}
00200 inline byte Image3D::Mask(int x,int y,int z) const {return (*properties.mask)(x,y,z);}
00201 inline byte &Image3D::Mask(int x,int y,int z) {return (*properties.mask)(x,y,z);}
00202 inline void Image3D::RemoveMask()
00203 {
00204 if(!HasMask()){ThrowError("Container3D::RemoveMask: no mask");}
00205 delete properties.mask;
00206 properties.mask = NULL;
00207 }
00208
00209 template<> inline string TypeName<Mask3D>(){return "Mask3D";}
00210
00211 inline string Mask3D::GetTypeName() const {return TypeName<Mask3D>();}
00212
00213 #endif // _Mask3D_hpp
00214