ML Reference
MeVis/Foundation/Sources/ML/include/mlSubImageBox.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //-------------------------------------------------------------------------
00010 //-------------------------------------------------------------------------
00011 #ifndef __mlSubImageBox_H
00012 #define __mlSubImageBox_H
00013 
00014 //ML-includes
00015 #ifndef __mlInitSystemML_H
00016 #include "mlInitSystemML.h"
00017 #endif
00018 #ifndef __mlImageVector_H
00019 #include "mlImageVector.h"
00020 #endif
00021 
00022 ML_START_NAMESPACE
00023 
00024 //-------------------------------------------------------------------------
00050 //-------------------------------------------------------------------------
00051 template<typename intT> class TSubImageBox
00052 {
00053   
00054 public:
00056   typedef TImageVector<intT> VectorType;
00057 
00062   VectorType v1;
00063   
00068   VectorType v2;
00069   
00070   //------------------------------------------------------
00072 
00073   //------------------------------------------------------
00076   inline TSubImageBox() : v1(0), v2(-1)
00077   {
00078     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox( )" );
00079   }
00080   
00084   inline TSubImageBox(const VectorType& vector1, const VectorType& vector2) : v1(vector1), v2(vector2)
00085   {
00086     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox( )" );
00087   }
00088   
00091   inline TSubImageBox(const VectorType extent) : v1(0), v2(extent-1)                         
00092   {
00093     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox( )" );
00094   }
00096   
00097   
00098   
00099   //------------------------------------------------------
00101 
00102   //------------------------------------------------------
00104   bool operator==(const TSubImageBox<intT>& box) const 
00105   { 
00106     return (v1==box.v1) && (v2==box.v2); 
00107   }
00108   
00110   bool operator!=(const TSubImageBox<intT>& box) const 
00111   { 
00112     return (v1!=box.v1) || (v2!=box.v2); 
00113   }
00114   
00118   bool isEmpty() const
00119   { 
00120     return (v1.x>v2.x) || (v1.y>v2.y) || (v1.z>v2.z) || (v1.c>v2.c) ||  (v1.t>v2.t) || (v1.u>v2.u);  
00121   }
00122   
00125   intT getNumVoxels() const
00126   {
00127     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox::getNumVoxels( )" );
00128     
00129     if (isEmpty())
00130     { 
00131       return 0; 
00132     } 
00133     else 
00134     { 
00135       return VectorType::compMul((v2-v1)+VectorType(1)); 
00136     }
00137   }
00138   
00141   VectorType getExtent() const 
00142   {
00143     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox::getExtent( )" );
00144     
00145     if (isEmpty())
00146     { 
00147       return VectorType(0); 
00148     } 
00149     else 
00150     { 
00151       return (v2-v1)+VectorType(1); 
00152     }
00153   }
00154   
00155   
00158   void correct();
00160   
00161   
00162   
00163   //------------------------------------------------------
00165 
00166   //------------------------------------------------------
00171   static TSubImageBox<intT> intersect(const TSubImageBox<intT>& box1, const TSubImageBox<intT>& box2);
00172   
00176   static TSubImageBox<intT> merge(const TSubImageBox<intT>& box1, const TSubImageBox<intT>& box2);
00177   
00180   void makeEmpty()                                         
00181   {
00182     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox::makeEmpty( )" );
00183     
00184     v1.set(0); 
00185     v2.set(-1); 
00186   }
00187   
00189   TSubImageBox<intT> intersect(const TSubImageBox<intT>& box) const
00190   {
00191     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox::intersect( )" );
00192     
00193     return TSubImageBox<intT>::intersect(*this, box); 
00194   }
00195   
00197   TSubImageBox<intT> merge(const TSubImageBox<intT>& box) const
00198   {
00199     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox::merge( )" );
00200     
00201     return TSubImageBox<intT>::merge(*this, box); 
00202   }
00203   
00206   VectorType clamp(const VectorType& position) const;
00207   
00209   inline bool contains (const VectorType& position) const    
00210   {
00211     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox::contains ( )" );
00212     
00213     return (position >= v1) && (position <= v2); 
00214   }
00215   
00217   inline void translate(const VectorType& offsetVector)          
00218   {
00219     ML_TRACE_IN_TIME_CRITICAL( "TSubImageBox::translate( )" );
00220     
00221     v1 = v1+offsetVector; 
00222     v2 = v2+offsetVector;              
00223   }
00224   
00228   static void get3DCorners(const TSubImageBox<intT>& box, VectorType corners[8])
00229   {
00230     ML_TRACE_IN( "TSubImageBox::get3DCorners( )" );
00231     ML_TRY
00232     {
00233       if (box.isEmpty()){ 
00234         for (int c=0; c < 8; c++){ corners[c] = ImageVector(0); }
00235       }
00236       else{
00237         corners[0] = box.v1;
00238         corners[1] = ImageVector(box.v1.x, box.v1.y, box.v2.z,0,0,0);
00239         corners[2] = ImageVector(box.v1.x, box.v2.y, box.v1.z,0,0,0);
00240         corners[3] = ImageVector(box.v1.x, box.v2.y, box.v2.z,0,0,0);
00241         corners[4] = ImageVector(box.v2.x, box.v1.y, box.v1.z,0,0,0);
00242         corners[5] = ImageVector(box.v2.x, box.v1.y, box.v2.z,0,0,0);
00243         corners[6] = ImageVector(box.v2.x, box.v2.y, box.v1.z,0,0,0);
00244         corners[7] = box.v2;
00245       }
00246     }
00247     ML_CATCH_RETHROW;
00248   }
00249   
00251   
00252   
00253 #ifdef ML_DEPRECATED
00254   
00256 
00257   
00258 public:
00259   
00262   inline ML_DEPRECATED intT getSize() const { return getNumVoxels(); }
00265   inline ML_DEPRECATED VectorType getExt() const { return getExtent(); }
00266   
00268   
00269 #endif // ML_DEPRECATED  
00270   
00271 };
00272 
00273 #ifdef ML_DEPRECATED
00274 
00275 
00276 
00277 
00278 #define TSubImgBox TSubImageBox
00279 
00280 #endif // ML_DEPRECATED
00281 
00282 
00283 ML_END_NAMESPACE
00284 
00285 //-----------------------------------------------------------------------------------
00286 //   Stream output for std::ostream
00287 //-----------------------------------------------------------------------------------
00288 namespace std {
00289   
00291   template <typename intT>
00292   inline ostream& operator<<(ostream& s, const ML_NAMESPACE::TSubImageBox<intT> &box)
00293   {
00294     return s << box.v1 << " " << box.v2;
00295   }
00296   
00297 }
00298 
00299 
00300 ML_START_NAMESPACE
00301 
00303 typedef TSubImageBox<MLint>   SubImageBox;
00304 
00305 #ifdef ML_DEPRECATED
00306 
00307 
00308 
00309 
00310 ML_DEPRECATED typedef TSubImageBox<MLint16> SubImgBox16;
00311 
00314 ML_DEPRECATED typedef TSubImageBox<MLint32> SubImgBox32;
00315 
00318 ML_DEPRECATED typedef TSubImageBox<MLint64> SubImgBox64;
00319 
00322 ML_DEPRECATED typedef SubImageBox SubImgBox;
00323 
00325 #endif // ML_DEPRECATED
00326 
00327 
00328 //------------------------------------------------------
00329 //   IMPLEMENTATION: Now only internal stuff follows
00330 //------------------------------------------------------
00331 
00332 // Swaps all components where v1.* > v2.*. I.e., an empty vector becomes non
00333 // empty if corresponding components are not equal.
00334 template <typename intT>
00335 void TSubImageBox<intT>::correct()
00336 {
00337   // Move smaller components into v1 and bigger ones into v2
00338   VectorType V1(v1);
00339   VectorType V2(v2);
00340   if (V1.x<V2.x){ v1.x=V1.x; v2.x=V2.x; } else{ v1.x=V2.x; v2.x=V1.x; }
00341   if (V1.y<V2.y){ v1.y=V1.y; v2.y=V2.y; } else{ v1.y=V2.y; v2.y=V1.y; }
00342   if (V1.z<V2.z){ v1.z=V1.z; v2.z=V2.z; } else{ v1.z=V2.z; v2.z=V1.z; }
00343   if (V1.c<V2.c){ v1.c=V1.c; v2.c=V2.c; } else{ v1.c=V2.c; v2.c=V1.c; }
00344   if (V1.t<V2.t){ v1.t=V1.t; v2.t=V2.t; } else{ v1.t=V2.t; v2.t=V1.t; }
00345   if (V1.u<V2.u){ v1.u=V1.u; v2.u=V2.u; } else{ v1.u=V2.u; v2.u=V1.u; }
00346 }
00347 
00348 // Returns the overlapping region of subimage regions loc1 and loc2.
00349 // A result with (v1.x>v2.x || ... || v1.z>v1.z) is interpreted as empty!
00350 // See also isEmpty(). This is especially true if that already 
00351 // holds for loc1 or loc2.
00352 template <typename intT>
00353 TSubImageBox<intT> TSubImageBox<intT>::intersect(const TSubImageBox<intT> &loc1, const TSubImageBox<intT> &loc2)
00354 {
00355   ML_TRACE_IN( "TSubImageBox<intT>::intersect( )" );
00356   
00357   TSubImageBox<intT> result;
00358   ML_TRY
00359   {
00360     result.v1 = VectorType::compMax(loc1.v1,loc2.v1);
00361     result.v2 = VectorType::compMin(loc1.v2,loc2.v2);
00362     return result;
00363   }
00364   ML_CATCH_RETHROW;
00365 }
00366 
00370 template <typename intT>
00371 TSubImageBox<intT> TSubImageBox<intT>::merge(const TSubImageBox<intT> &loc1, const TSubImageBox<intT> &loc2)
00372 {
00373   ML_TRACE_IN( "TSubImageBox<intT>::merge( )" );
00374   
00375   ML_TRY
00376   {
00377     if (loc1.isEmpty()){ return loc2; } else
00378       if (loc2.isEmpty()){ return loc1; } else
00379       {
00380         // Both regions are not empty; determine surrounding box
00381         // in all dimensions.
00382         return TSubImageBox<intT>(VectorType::compMin(loc1.v1,loc2.v1),
00383                                   VectorType::compMax(loc1.v2,loc2.v2));
00384       }
00385   }
00386   ML_CATCH_RETHROW;
00387 }
00388 
00389 
00390 // Clamps the position pos to the nearest position inside the SubImageBox.
00391 // If the SubImageBox is empty then the result vector contains undefined values.
00392 template <typename intT>
00393 TImageVector<intT> TSubImageBox<intT>::clamp(const VectorType &pos) const
00394 {
00395   ML_TRACE_IN( "TSubImageBox<intT>::clamp()" );
00396   
00397   return VectorType::compMax(VectorType::compMin(pos, v2), v1);
00398 }
00399 
00400 ML_END_NAMESPACE
00401 
00402 #endif //of __mlSubImageBox_H
00403 
00404