MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLCSO/CSOTools/CSOMarchingSquares.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00009 //----------------------------------------------------------------------------------
00010 
00011 #ifndef __MarchingSquares_H
00012 #define __MarchingSquares_H
00013 
00014 #include "MLCSOIncludes.h"
00015 #include "CSOMarchingSquaresCell.h"
00016 #include "CSOFunction.h"
00017 
00018 ML_START_NAMESPACE
00019 
00020 
00022 
00025 class MLCSO_EXPORT CSOMarchingSquares
00026 {
00027 
00028 public:
00029 
00031   struct AmbiguousCell { 
00032     CSOMarchingSquaresCell::vecPoint2DIter start;  
00033     CSOMarchingSquaresCell::vecPoint2DIter end;    
00034     CSOMarchingSquaresCell                 cell;   
00035 
00037     AmbiguousCell() {} 
00039     AmbiguousCell(CSOMarchingSquaresCell::vecPoint2DIter s, CSOMarchingSquaresCell::vecPoint2DIter e, CSOMarchingSquaresCell c) : start(s), end(e), cell(c) {}
00040   };
00041 
00042   typedef std::map<int, AmbiguousCell> mapAmbigousCell;     
00043   typedef mapAmbigousCell::iterator    mapAmbigousCellIter; 
00044 
00045   typedef std::map<int, CSOMarchingSquaresCell>        mapCell;      
00046   typedef mapCell::iterator            mapCellIter;  
00047 
00049   CSOMarchingSquares();
00051   ~CSOMarchingSquares();
00052 
00053   
00054 public:
00055 
00057   void reset();
00059   void setInterpolation(bool use);
00061   void setImage(float* image, int imgSizeX, int imgSizeY);
00065   void setFunction(CSOFunction* implicitFunction, const Matrix4& voxelToWorldMatrix, int startX, int startY, int imgSizeX, int imgSizeY, int voxelZPosition);
00066   
00070   void findIsoLine(int startPosX, int startPosY, float isoValue, bool takeShortestPath, CSOMarchingSquaresCell::vecPoint2D& positions);
00072   void findAllIsoLines(float isoValue, bool takeShortestPath, std::vector<CSOMarchingSquaresCell::vecPoint2D>& vecPositions);
00074   bool fillCSO(CSO* cso, CSOMarchingSquaresCell::vecPoint2D& positions, float posZ, PagedImage* pImg,
00075                bool useSmoothing, float smoothingFactor, int numSmoothPasses, int smoothRange, CSOSmoothingModes smoothingMode) const;
00076   
00077 protected:
00078 
00080   void _findStartPosition(int& startX, int& startY);
00082   bool _findNearestIsoCell(int voxelPosX, int voxelPosY, CSOMarchingSquaresCell& cell);
00084   bool _findNearestIsoCell(int voxelPos[2], CSOMarchingSquaresCell& cell);
00087   void _trackIsoCell(CSOMarchingSquaresCell startCell, int fromDir, CSOMarchingSquaresCell::vecPoint2D& positions, mapCell* pVisitedCells = NULL);
00089   void _createCell(int topLeftVoxel[2], CSOMarchingSquaresCell& cell);
00093   int  _walkToCell(const CSOMarchingSquaresCell& fromCell, int fromDir, CSOMarchingSquaresCell& toCell);
00095   int _getPossibleEnterDirection(const CSOMarchingSquaresCell& cell) const;
00097   unsigned int  _getKey(const CSOMarchingSquaresCell& cell) const;
00099   unsigned int _getKey(int x, int y) const;
00100   
00102   float _getValueAt(int x, int y);
00103   
00104 protected:
00105 
00107   float*      _image;
00109   int         _imageSizeX;
00111   int         _imageSizeY;
00112   
00114   int _startX;
00116   int _startY;
00117   
00119   float       _isoValue;
00121   CSOFunction* _function;
00123   int _voxelZPosition;
00125   Matrix4 _voxelToWorldMatrix;
00126   
00128   bool        _bInterpolatePoints;
00131   mapAmbigousCell _mapAmbiguities;
00132   
00134   std::map <int, double> _existingValues;
00135 };
00136 
00138 
00139 inline void CSOMarchingSquares::setImage(float* image, int imgSizeX, int imgSizeY)
00140 {
00141   _image = image;
00142   _imageSizeX = imgSizeX;
00143   _imageSizeY = imgSizeY;
00144   _startX = 0;
00145   _startY = 0;
00146   
00147   _function = NULL;
00148   _voxelZPosition = 0; 
00149 }
00150 
00152 
00153 inline void CSOMarchingSquares::setFunction(CSOFunction* implicitFunction,
00154                                             const Matrix4& voxelToWorldMatrix,
00155                                             int startX, int startY,
00156                                             int imgSizeX, int imgSizeY, 
00157                                             int voxelZPosition)
00158 {
00159   _image      = NULL;
00160   
00161   _imageSizeX = imgSizeX;
00162   _imageSizeY = imgSizeY;
00163   
00164   _startX = startX;
00165   _startY = startY;
00166   
00167   _function           = implicitFunction;
00168   _voxelToWorldMatrix = voxelToWorldMatrix;
00169   
00170   _voxelZPosition     = voxelZPosition;    
00171 }
00172 
00174 
00175 inline void CSOMarchingSquares::setInterpolation(bool useInterpolation)
00176 {
00177   _bInterpolatePoints = useInterpolation;
00178 }
00179 
00181 
00182 inline unsigned int CSOMarchingSquares::_getKey(const CSOMarchingSquaresCell& cell) const
00183 {
00184   return _getKey(cell._topLeftVoxel[0], cell._topLeftVoxel[1]);
00185 }
00186 
00188 
00189 inline unsigned int CSOMarchingSquares::_getKey(int x, int y) const
00190 {
00191   return 1 + x-_startX + ((1 + y-_startY) * (_imageSizeX+_startX+1));
00192 }
00193 
00195 
00196 inline int CSOMarchingSquares::_getPossibleEnterDirection(const CSOMarchingSquaresCell& cell) const
00197 {
00198   int fromDir = cell.getToDirection(1);
00199   if (0 == fromDir) {
00200     fromDir = cell.getToDirection(2);
00201     if (0 == fromDir) {
00202       fromDir = cell.getToDirection(4);
00203       if (0 == fromDir) {
00204         fromDir = cell.getToDirection(8);
00205       }
00206     }
00207   }
00208   return fromDir;
00209 }
00210 
00212 
00213 
00214 ML_END_NAMESPACE
00215 
00216 #endif // __MarchingSquares_H
00217 
00218