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

mitkPointSet.h

00001 /*=========================================================================
00002 
00003   Program:   3DMed
00004   Date:      $Date: 2014-02-25 18:30:00 +0800 $
00005   Version:   $Version: 4.6.0 $
00006   Copyright: MIPG, Institute of Automation, Chinese Academy of Sciences
00007 
00008 =========================================================================*/
00009 
00010 
00011 #ifndef __mitkPointSet_h
00012 #define __mitkPointSet_h
00013 
00014 #include "mitkObject.h"
00015 #include "mitkMesh.h"
00016 #include "mitkGlobalRegistrationFramework.h"
00017 
00018 
00019 template<class T> class mitkPoint;
00020 template<class T> class mitkBoundingBox;
00021 
00027 class MITK_REGISTRATION_API mitkPointSet
00028 {
00029 public:
00033     mitkPointSet(); 
00034 
00038     virtual ~mitkPointSet();
00039 
00046     mitkPointSet(unsigned int numberOfPoints,unsigned int pointSetDimension,double fillElement=0);
00047 
00048     mitkPointSet(unsigned int num, unsigned int dim, double* p);
00049 
00054     mitkPointSet(mitkPointSet* pointSet); 
00055 
00061     void GetPoint(unsigned int i, ScalarParameterType* p);
00062     void GetPoint(unsigned int i, ScalarIndexType* p);
00063     void GetPoint(unsigned int i, ScalarPixelType* p);
00064     void GetPoint(unsigned int i, mitkPoint<double> & p);
00065     void GetPoint(unsigned int i, VectorParameterType & p);
00066     void GetPoint(unsigned int i, VectorIndexType & p);
00067     void GetPoint(unsigned int i, VectorPixelType & p);
00068 
00074     double GetPoint(unsigned int i, unsigned int dim);
00075 
00081     void SetPoint(unsigned int i, ScalarParameterType* p);
00082     void SetPoint(unsigned int i, ScalarIndexType* p);
00083     void SetPoint(unsigned int i, ScalarPixelType* p);
00084     void SetPoint(unsigned int i, mitkPoint<double> & p);
00085     void SetPoint(unsigned int i, VectorParameterType & p);
00086     void SetPoint(unsigned int i, VectorIndexType & p);
00087     void SetPoint(unsigned int i, VectorPixelType & p);
00088     
00094     vector<double>* GetPointsInSequence(vector<int>* sequence);
00095 
00100     vector<double>* GetPointData();
00101 
00106     double* GetPointDataPtr() {return m_PointSet;}  
00107 
00113     void SetPointData(vector<double>* pointSet, unsigned int dimension);
00114 
00121     void SetPointData(double* pointSet, unsigned int dimension, unsigned int numberOfPoints);
00122 
00128     vector<double>* GetPointsInDimension(unsigned int sDimension);
00129 
00135     void SetPointsInDimension(vector<double>* points, unsigned int sDimension);
00136     
00142     void SetPointsInDimension(double fillElement, unsigned int sDimension);
00143 
00148     void SetPointData(mitkMesh* mesh);
00149     
00154     void GetPointData(mitkMesh* mesh);
00155 
00161     void GetWeight(unsigned int i, vector<double>* w);
00162 
00168     void SetWeight(unsigned int i, vector<double>* w);
00169 
00175     vector<double>* GetWeightsInSequence(vector<int>* sequence);
00176 
00182     void SetWeightData(vector<double>* weights, unsigned int dimension);
00183 
00190     void SetWeightData(double* weights, unsigned int dimension, unsigned int numberOfPoints);
00191 
00196     vector<double>* GetWeightData();
00197 
00202     double* GetWeightDataPtr() {return m_Weights;}
00203 
00208     void SetPointSetMask(vector<int>* pointSetMask);
00209 
00214     vector<int>* GetPointSetMask() {return m_PointSetMask;}
00215 
00220     void SetPointSetMaskFlag(bool maskFlag) {m_FlagUseMask = maskFlag;}
00221 
00226     bool GetPointSetMaskFlag() {return m_FlagUseMask;}
00227 
00234     void ReadPointDataFromFile(char *inFileName,int pointSetDimension,int dataType);
00235     
00240     void WritePointDataToFile(char* outFileName);
00241 
00246     unsigned int GetNumberOfPoints();
00247 
00252     void SetNumberOfPoints(unsigned int num) {m_NumberOfPoints = num;}
00253 
00258     int GetPointSetDimension() {return m_PointSetDimension;}
00259 
00264     void SetPointSetDimension(unsigned int pointSetDimension) {m_PointSetDimension = pointSetDimension;}
00265 
00270     int GetWeightsDimension() {return m_WeightsDimension;}
00271 
00281     void GetBoundingBox(double b[6]);
00282 
00292     void GetBoundingBox(float b[6]);
00293 
00300     void GetCentroid(double p[3]);
00301 
00308     virtual void GetNearestPoint(double* query_point, double* result_point, int &i);
00309 
00310     virtual void GetNearestIndex(double* query_point, int &i);
00311 
00318     virtual void GetKNearestPoints(double* query_point, unsigned int k, vector<int> &indices);
00319 
00326     virtual void GetKNearestPoints(vector<double>* query_point, unsigned int k, vector<int> &indices);
00327 
00333     virtual void GetNearestPoints(mitkPointSet* query_pointset, vector<int> &indices); 
00334 
00340     virtual void GetPointsInBoundingBox(mitkBoundingBox<double>& box, vector<int> &indices);
00341 
00348     virtual void GetPointsInRadius(double* query_point, double radius, vector<int> &indices);
00349 
00354     void Fill(double fillElement);
00355 
00359     bool RemoveDuplicates();
00360 
00364     void ConvertPointSet2Dto3D();
00365 
00366     //int GetPointLabel(double x,double y,double z);
00367 
00371     virtual void Initialize();
00372 
00376     virtual void Clear();
00377     
00382     bool IsPointSetEmpty();
00383     
00388     bool IsWeightsEmpty();
00389 
00394     bool IsInitialized() {return m_FlagInitialized;}
00395 
00396     void SetInitializedFlag(bool f) { m_FlagInitialized = f; }
00397 
00402     void ShallowCopy(mitkPointSet* pointSet);
00403 
00408     void DeepCopy(mitkPointSet* pointSet);
00409 
00414     void Merge(mitkPointSet* pointSet);
00415 
00422     void Reserve(unsigned int s, unsigned int d = 3);
00423 
00428     void SetOriginAndDirection(double o[3], int d[3]);
00429 
00430     void SetSpacing(double s[3]);
00431 
00432     double & operator()(unsigned int i, unsigned int d)
00433     {
00434         return m_PointSet[i * m_PointSetDimension + d];
00435     }
00436 
00437 protected:
00438     void Allocate();
00439     void AllocateWeights();
00440 
00441     void _init();
00442 
00443     unsigned int                m_PointSetDimension;
00444     unsigned int                m_WeightsDimension;
00445     unsigned int                m_NumberOfPoints;
00446 
00447     vector<int>*                m_PointSetMask;
00448     double*                     m_PointSet;
00449     double*                     m_Weights;
00450 
00451     double                      m_Centroid[3];
00452     double                      m_BoundingBox[6];
00453 
00454     double                      m_Spacings[3];
00455     double                      m_Origins[3];
00456     int                         m_Directions[3];
00457 
00458     bool                        m_FlagComputeCentroid;
00459     bool                        m_FlagComputeBoundingBox;
00460     bool                        m_FlagUseMask;
00461     bool                        m_FlagInitialized;
00462 
00463 private:
00464     mitkPointSet(const mitkPointSet&);
00465     void operator=(const mitkPointSet&);
00466 };
00467 
00468 //----------------------------------------------------------------------------
00469 template<class T>
00470 class mitkPoint
00471 {
00472 public:
00473     mitkPoint() :m_dim(0), m_point_data(0) {}
00474 
00475     ~mitkPoint() {m_point_data.clear();}
00476 
00477     mitkPoint(unsigned int dim)
00478     {
00479         m_dim = dim;
00480         m_point_data.resize(m_dim);
00481     }
00482 
00483     mitkPoint(unsigned int pointDimension, T value)
00484     {
00485         m_dim = pointDimension;
00486         m_point_data.assign(m_dim,value);
00487     }
00488 
00489 /*  mitkPoint(T x, T y)
00490     {
00491         m_dim = 2;
00492         m_point_data.resize(m_dim);
00493         m_point_data[0] = x;
00494         m_point_data[1] = y;
00495     }
00496 */
00497     mitkPoint(T x, T y, T z)
00498     {
00499         m_dim = 3;
00500         m_point_data.resize(m_dim);
00501         m_point_data[0] = x;
00502         m_point_data[1] = y;
00503         m_point_data[2] = z;
00504     }
00505 
00506     mitkPoint(T a, T b, T c, T d)
00507     {
00508         m_dim = 4;
00509         m_point_data.resize(m_dim);
00510         m_point_data[0] = a; m_point_data[1] = b; m_point_data[2] = c; m_point_data[3] = d;
00511     }
00512 
00513     mitkPoint(unsigned int pointDimension, T* value)
00514     {
00515         m_dim = pointDimension;
00516         m_point_data.resize(m_dim);
00517         for(int i=0;i<m_dim;i++) m_point_data[i] = value[i];
00518     }
00519 
00520     mitkPoint(const vector<T>& value)
00521     {   
00522         m_dim = value.size();
00523         m_point_data.resize(m_dim);
00524         m_point_data = value;
00525     }
00526 
00527     mitkPoint(const mitkPoint& p)
00528     {
00529         m_dim = p.m_dim;
00530         m_point_data = p.m_point_data;
00531     }
00532 
00533     mitkPoint<T>& operator= (const mitkPoint<T>& p)
00534     {
00535         m_dim = p.m_dim;
00536         m_point_data = p.m_point_data;
00537         return *this;
00538     }
00539     
00540     mitkPoint<T>& operator= (const vector<T>& p)
00541     {
00542         if(m_dim == p.size()) m_point_data = p;
00543         return *this;
00544     }
00545 
00546     mitkPoint<T>& operator= (const T p[3])
00547     {
00548         m_dim = 3;
00549         m_point_data.resize(3);
00550         for(int i=0;i<3;i++) m_point_data[i] = p[i];
00551         return *this;
00552     }
00553 
00554     mitkPoint<T> operator+ (const mitkPoint<T>& p)
00555     {
00556         if(p.m_dim != m_dim)    return *this;
00557 
00558         mitkPoint<T> point(m_dim);
00559         for(int i=0;i<m_dim;i++)
00560         {
00561             point.m_point_data[i] = m_point_data[i] + p.m_point_data[i];
00562         }
00563         return point;
00564     }
00565 
00566     mitkPoint<T> operator- (const mitkPoint<T>& p)
00567     {
00568         if(p.m_dim != m_dim)    return *this;
00569 
00570         mitkPoint<T> point(m_dim);
00571         for(unsigned int i=0;i<m_dim;i++)
00572         {
00573             point.m_point_data[i] = m_point_data[i] - p.m_point_data[i];
00574         }
00575         return point;
00576     }
00577 
00578     mitkPoint<T> operator* (const mitkPoint<T>& p)
00579     {
00580         if(p.m_dim != m_dim)    return *this;
00581 
00582         mitkPoint<T> point(m_dim);
00583         for(int i=0;i<m_dim;i++)
00584         {
00585             point.m_point_data[i] = m_point_data[i] * p.m_point_data[i];
00586         }
00587         return point;
00588     }
00589 
00590     mitkPoint<T> operator/ (const mitkPoint<T>& p)
00591     {
00592         if(p.m_dim != m_dim)    return *this;
00593 
00594         mitkPoint<T> point(m_dim);
00595         for(unsigned int i=0;i<m_dim;i++)
00596         {
00597 
00598             point.m_point_data[i] = p.m_point_data[i] == 0 ? 0 : m_point_data[i] / p.m_point_data[i];
00599         }
00600         return point;
00601     }
00602 
00603     bool operator== (const mitkPoint<T>& p)
00604     {
00605         if(p.m_dim != m_dim)    return 0;
00606 
00607         bool m = true;
00608         for(unsigned int i=0;i<m_dim;i++) m &= m_point_data[i] == p.m_point_data[i];
00609         return m;
00610     }
00611 
00612     bool operator!= (const mitkPoint<T>& p)
00613     {
00614         if(p.m_dim != m_dim)    return 0;
00615 
00616         bool m = true;
00617         for(int i=0;i<m_dim;i++) m &= m_point_data[i] != p.m_point_data[i];
00618         return m;
00619     }
00620 
00621     T operator[] (unsigned int i) const
00622     {
00623         return m_point_data[i];
00624     }
00625 
00626     unsigned int size()
00627     {
00628         return m_dim;
00629     }
00630 
00631     void resize(unsigned int s) 
00632     {
00633         m_point_data.resize(s);
00634         m_dim = s;
00635     }
00636 
00637     void set(unsigned int i, const T & value)
00638     {
00639         if(i>=0 && i<m_dim)
00640             m_point_data[i] = value;
00641     }
00642 
00643     T get_norm() const
00644     {
00645         T sum = 0;
00646         for(unsigned int i=0;i<m_dim;i++)
00647         {
00648             const T v = m_point_data[i];
00649             sum += v * v;
00650         }
00651         return sqrt(double(sum));
00652     }
00653 
00654     vector<T>       m_point_data;
00655     unsigned int    m_dim;
00656 };
00657 //----------------------------------------------------------------------------
00658 template<class T>
00659 class mitkBoundingBox
00660 {
00661 public:
00662     mitkBoundingBox() :m_dim(3),m_bound_data(0) {}
00663 
00664     ~mitkBoundingBox() {}
00665 
00666     //construct with elements directly
00667     mitkBoundingBox(T x0, T x1, T y0, T y1)
00668     {
00669         m_dim = 2;
00670         m_bound_data.resize(m_dim*2);
00671         m_bound_data[0] = x0;
00672         m_bound_data[1] = x1;
00673         m_bound_data[2] = y0;
00674         m_bound_data[3] = y1;
00675     }
00676 
00677     //construct with elements directly
00678     mitkBoundingBox(T x0, T x1, T y0, T y1, T z0, T z1)
00679     {
00680         m_dim = 3;
00681         m_bound_data.resize(m_dim*2);
00682         m_bound_data[0] = x0;
00683         m_bound_data[1] = x1;
00684         m_bound_data[2] = y0;
00685         m_bound_data[3] = y1;
00686         m_bound_data[4] = z0;
00687         m_bound_data[5] = z1;
00688     }
00689     
00690     //construct with arrays
00691     mitkBoundingBox(T* min, T* max, unsigned int dim)
00692     {
00693         m_dim = dim;
00694         m_bound_data.resize(m_dim*2);
00695         for(unsigned int i=0;i<m_dim;i++)
00696         {
00697             m_bound_data[2*i]   = min[i];
00698             m_bound_data[2*i+1] = max[i];
00699         }
00700     }
00701     
00702     //construct with vectors
00703     mitkBoundingBox(vector<T>* min, vector<T>* max)
00704     {
00705         m_dim = min->size();
00706         m_bound_data.resize(m_dim*2);
00707         for(int i=0;i<m_dim;i++)
00708         {
00709             m_bound_data[2*i]   = (*min)[i];
00710             m_bound_data[2*i+1] = (*max)[i];
00711         }
00712     }
00713     
00714     //construct with scalar values
00715     mitkBoundingBox(T min, T max, unsigned int dim)
00716     {
00717         m_dim = dim;
00718         m_bound_data.resize(m_dim*2);
00719         for(unsigned int i=0;i<m_dim;i++)
00720         {
00721             m_bound_data[2*i]   = min;
00722             m_bound_data[2*i+1] = max;
00723         }
00724     }
00725     
00726     //construct with mitkBoundingBox
00727     mitkBoundingBox(const mitkBoundingBox& b)
00728     {
00729         m_dim = b.m_dim;
00730         m_bound_data.resize(m_dim*2);
00731         m_bound_data = b.m_bound_data;
00732     }
00733     
00734     //construct with an array contains min & max
00735     mitkBoundingBox(T* b, unsigned int dim)
00736     {
00737         m_dim = dim;
00738         m_bound_data.resize(dim*2);
00739         for(int i=0;i<m_bound_data.size();i++) m_bound_data[i] = b[i];
00740     }
00741 
00742     mitkBoundingBox& operator= (const mitkBoundingBox& b)
00743     {
00744         m_dim = b.m_dim;
00745         m_bound_data = b.m_bound_data;
00746         return *this;
00747     }
00748 
00749     //T& operator[] (unsigned int i)
00750     //{
00751     //  return m_bound_data[i];
00752     //}
00753 
00754     void GetHighBound(T* bmax) const
00755     {
00756         for(unsigned int i=0;i<m_dim;i++) bmax[i] = m_bound_data[2*i+1];
00757     }
00758 
00759     void GetLowBound(T* bmin) const
00760     {
00761         for(unsigned int i=0;i<m_dim;i++) bmin[i] = m_bound_data[2*i];
00762     }
00763 
00764     void SetHighBound(T* max) 
00765     {
00766         for(int i=0;i<m_dim;i++) m_bound_data[i*2+1] = max[i];
00767     }
00768 
00769     void SetLowBound(T* min)
00770     {
00771         for(int i=0;i<m_dim;i++) m_bound_data[i*2] = min[i];
00772     }
00773     unsigned int GetDimension() const {return m_dim;}
00774 
00775     vector<T>       m_bound_data;
00776     unsigned int    m_dim;
00777 };
00778 //----------------------------------------------------------------------------
00779 template<class T1, class T2>
00780 inline T2 mitk_point_dist(T1* p1, T2* p2, unsigned int dim) //distance - point to point
00781 {
00782     T2 sum_sq = 0;
00783     for(unsigned int i=0; i < dim; i++ ) 
00784     {
00785         T2 tmp = (*p1)[i] - (*p2)[i];
00786         sum_sq += tmp * tmp;
00787     }
00788     sum_sq = sqrt(sum_sq);
00789     return sum_sq;
00790 }
00791 //----------------------------------------------------------------------------
00792 template<class T>
00793 inline T mitk_point_dist_sq(T* p1, T* p2, unsigned int dim) //distance^2 - point to point
00794 {
00795     T sum_sq = 0;
00796     for(unsigned int i=0; i < dim; i++ ) 
00797     {
00798         T tmp = p1[i] - p2[i];
00799         sum_sq += tmp * tmp;
00800     }
00801     return sum_sq;
00802 }
00803 //----------------------------------------------------------------------------
00804 template<class T>
00805 inline T mitk_point_dist_sq(vector<T>* p1, T* p2, unsigned int dim) //distance^2 - point to point
00806 {
00807     T sum_sq = 0;
00808     for(unsigned int i=0; i < dim; i++ ) 
00809     {
00810         T tmp = (*p1)[i] - p2[i];
00811         sum_sq += tmp * tmp;
00812     }
00813     return sum_sq;
00814 }
00815 //----------------------------------------------------------------------------
00816 template<class T>
00817 inline T mitk_point_dist_sq(vector<T>* p, const mitkBoundingBox<T>& b, bool& inside)  //distance^2 - point to boundingbox
00818 {   
00819     T sum_sq = 0;
00820     unsigned int dim = b.GetDimension();
00821     T x0[3], x1[3];
00822 
00823     b.GetLowBound(x0);
00824     b.GetHighBound(x1);
00825 
00826     inside = true;
00827 
00828     for( unsigned int i=0; i<dim; i++ )
00829     {
00830         if( (*p)[i] < x0[i] ) 
00831         {
00832             T tmp = x0[i] - (*p)[i];
00833             sum_sq += tmp * tmp;
00834             inside = false;
00835         }
00836         else if( (*p)[i] > x1[i] ) 
00837         {
00838             T tmp = x1[i] - (*p)[i];
00839             sum_sq += tmp * tmp;
00840             inside = false;
00841         }  
00842     }
00843 
00844     return sum_sq;
00845 }
00846 //----------------------------------------------------------------------------
00847 template<class T>
00848 inline T mitk_point_dist_sq(T* p, const mitkBoundingBox<T>& b, bool& inside)  //distance^2 - point to boundingbox
00849 {   
00850     T sum_sq = 0;
00851     unsigned int dim = b.GetDimension();
00852     T x0[3], x1[3];
00853 
00854     b.GetLowBound(x0);
00855     b.GetHighBound(x1);
00856 
00857     inside = true;
00858 
00859     for( unsigned int i=0; i<dim; i++ )
00860     {
00861         if( p[i] < x0[i] ) 
00862         {
00863             T tmp = x0[i] - p[i];
00864             sum_sq += tmp * tmp;
00865             inside = false;
00866         }
00867         else if( p[i] > x1[i] ) 
00868         {
00869             T tmp = x1[i] - p[i];
00870             sum_sq += tmp * tmp;
00871             inside = false;
00872         }  
00873     }
00874 
00875     return sum_sq;
00876 }
00877 //----------------------------------------------------------------------------
00878 
00879 
00880 //#define DEFINED_mitkPointSet
00881 
00882 #endif
00883 

Generated on Tue Feb 25 15:00:37 2014 for MITK (Medical Imaging ToolKit) by  doxygen 1.4.3