/vol/vipdata/irtk/geometry++/include/irtkPointSet.h

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkPointSet.h 2 2008-12-23 12:40:14Z dr $
00005   Copyright : Imperial College, Department of Computing
00006               Visual Information Processing (VIP), 2008 onwards
00007   Date      : $Date: 2008-12-23 12:40:14 +0000 (Tue, 23 Dec 2008) $
00008   Version   : $Revision: 2 $
00009   Changes   : $Author: dr $
00010 
00011 =========================================================================*/
00012 
00013 #ifndef _IRTKPOINTSET_H
00014 
00015 #define _IRTKPOINTSET_H
00016 
00017 #define POINTSET_SIZE 4096
00018 
00025 class irtkPointSet : public irtkObject
00026 {
00027 
00028 private:
00029 
00031   int _m;
00032 
00034   int _n;
00035 
00037   irtkPoint *_data;
00038 
00039 public:
00040 
00041   //
00042   // Constructors and destructor
00043   //
00044 
00046   irtkPointSet();
00047 
00049   irtkPointSet(const irtkPointSet &);
00050 
00052   virtual ~irtkPointSet();
00053 
00054   //
00055   // Size get acccessor and clearing of irtkPointSet
00056   //
00057 
00059   int  Size() const;
00060 
00062   void Clear();
00063 
00064   //
00065   // Operators for access
00066   //
00067 
00069   irtkPoint   &operator()(int);
00070 
00072   irtkPoint    operator()(int) const;
00073 
00075   irtkPointSet operator()(int, int) const;
00076 
00077   //
00078   // Operators for irtkPointSet arithmetic
00079   //
00080 
00081   // irtkPointSet operation for =
00082   irtkPointSet &operator=(const irtkPointSet &);
00083 
00084   // irtkPointSet operator for Point adding
00085   irtkPointSet& operator+=(const irtkPoint&);
00086 
00087   // irtkPointSet operator for Point substraction
00088   irtkPointSet& operator-=(const irtkPoint&);
00089 
00090   // irtkPointSet operator for irtkPointSet adding
00091   irtkPointSet& operator+=(const irtkPointSet&);
00092 
00093   // irtkPointSet operator for irtkPointSet substraction
00094   irtkPointSet& operator-=(const irtkPointSet&);
00095 
00097   virtual irtkPoint CenterOfGravity() const;
00098 
00100   virtual void BoundingBox(irtkPoint &, irtkPoint &) const;
00101 
00102   //
00103   // Explicit methods to add or delete points
00104   //
00105 
00107   void Add(const irtkPoint&);
00108 
00110   void Del(const irtkPoint&);
00111 
00113   void Add(const irtkPointSet &);
00114 
00116   void Del(const irtkPointSet &);
00117 
00119   void Add(double *);
00120 
00122   void Del(double *);
00123 
00124   //
00125   // irtkPointSet in- and output
00126   //
00127 
00129   friend ostream& operator<< (ostream&, const irtkPointSet&);
00130 
00132   friend istream& operator>> (istream&, irtkPointSet&);
00133 
00135   void Read (char *);
00136 
00138   void Write(char *);
00139 
00141   void ReadVTK (char *);
00142 
00144   void WriteVTK(char *);
00145 
00146   //
00147   // Misc. functions
00148   //
00149 
00151   irtkPoint StandardDeviationEllipsoid() const;
00152 
00154   int IsInside(double, double) const;
00155 
00156 };
00157 
00158 inline irtkPointSet::irtkPointSet()
00159 {
00160   // Initialize
00161   _n = 0;
00162   _m = POINTSET_SIZE;
00163   _data = new irtkPoint[POINTSET_SIZE];
00164 }
00165 
00166 inline irtkPointSet::irtkPointSet(const irtkPointSet &pset)
00167 {
00168   int i;
00169 
00170   // Initialize
00171   _n = 0;
00172   _m = 0;
00173   _data = NULL;
00174 
00175   // Allocate memory
00176   if (pset._m  > 0) {
00177     _data = new irtkPoint[pset._m];
00178     _m = pset._m;
00179     _n = pset._n;
00180   }
00181 
00182   // Copy points
00183   for (i = 0; i < _n; i++) {
00184     _data[i] = pset._data[i];
00185   }
00186 }
00187 
00188 inline irtkPointSet::~irtkPointSet()
00189 {
00190   if (_m > 0) {
00191     delete []_data;
00192     _m = 0;
00193     _n = 0;
00194   }
00195   _m = 0;
00196   _n = 0;
00197   _data = NULL;
00198 }
00199 
00200 inline irtkPoint& irtkPointSet::operator()(int j)
00201 {
00202 #ifdef NO_BOUNDS
00203   return _data[j];
00204 #else
00205   if ((j >= 0) && (j < _n)) {
00206     return _data[j];
00207   } else {
00208     cerr << "irtkPointSet::operator(int) parameter out of range\n";
00209     exit(1);
00210   }
00211 #endif
00212 }
00213 
00214 inline irtkPoint  irtkPointSet::operator()(int j) const
00215 {
00216 #ifdef NO_BOUNDS
00217   return _data[j];
00218 #else
00219   if ((j >= 0) && (j < _n)) {
00220     return _data[j];
00221   } else {
00222     cerr << "irtkPointSet::operator(int) parameter out of range\n";
00223     exit(1);
00224   }
00225 #endif
00226 }
00227 
00228 inline irtkPointSet& irtkPointSet::operator+=(const irtkPoint &p)
00229 {
00230   this->Add(p);
00231   return *this;
00232 }
00233 
00234 inline irtkPointSet& irtkPointSet::operator-=(const irtkPoint &p)
00235 {
00236   this->Del(p);
00237   return *this;
00238 }
00239 
00240 inline irtkPointSet& irtkPointSet::operator+=(const irtkPointSet &pset)
00241 {
00242   this->Add(pset);
00243   return *this;
00244 }
00245 
00246 inline irtkPointSet& irtkPointSet::operator-=(const irtkPointSet &pset)
00247 {
00248   this->Del(pset);
00249   return *this;
00250 }
00251 
00252 inline irtkPointSet irtkPointSet::operator()(int j, int k) const
00253 {
00254   int i;
00255   irtkPointSet pset;
00256 
00257 #ifdef NO_BOUNDS
00258   for (i = j; i < k; j++) {
00259     pset += _data[i];
00260   }
00261   return pset;
00262 #else
00263   if ((j >= 0) && (k < _n)) {
00264     for (i = j; i < k; j++) {
00265       pset += _data[i];
00266     }
00267     return pset;
00268   } else {
00269     cerr << "irtkPointSet::operator(int, int) parameter out of range\n";
00270     exit(1);
00271   }
00272   return pset;
00273 #endif
00274 }
00275 
00276 inline int irtkPointSet::Size() const
00277 {
00278   return _n;
00279 }
00280 
00281 inline void irtkPointSet::Add(double *p)
00282 {
00283   this->Add(irtkPoint(p[0], p[1], p[2]));
00284 }
00285 
00286 inline void irtkPointSet::Del(double *p)
00287 {
00288   this->Del(irtkPoint(p[0], p[1], p[2]));
00289 }
00290 
00291 #endif
00292