PointCloud.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools version 1.1
00003 //
00004 // GoTools module: CORE
00005 //
00006 // Copyright (C) 2000-2007 SINTEF ICT, Applied Mathematics, Norway.
00007 //
00008 // This program is free software; you can redistribute it and/or          
00009 // modify it under the terms of the GNU General Public License            
00010 // as published by the Free Software Foundation version 2 of the License. 
00011 //
00012 // This program is distributed in the hope that it will be useful,        
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00015 // GNU General Public License for more details.                           
00016 //
00017 // You should have received a copy of the GNU General Public License      
00018 // along with this program; if not, write to the Free Software            
00019 // Foundation, Inc.,                                                      
00020 // 59 Temple Place - Suite 330,                                           
00021 // Boston, MA  02111-1307, USA.                                           
00022 //
00023 // Contact information: E-mail: tor.dokken@sintef.no                      
00024 // SINTEF ICT, Department of Applied Mathematics,                         
00025 // P.O. Box 124 Blindern,                                                 
00026 // 0314 Oslo, Norway.                                                     
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //===========================================================================
00033 #ifndef _GOPOINTCLOUD_H
00034 #define _GOPOINTCLOUD_H
00035 
00036 
00037 #include "errormacros.h"
00038 #include "GeomObject.h"
00039 #include "Array.h"
00040 #include <vector>
00041 
00042 
00043 namespace Go {
00046 
00047 
00048 // NB! This class is now a template. The dimension is the template
00049 // parameter. Typedefs are provided for ease of use. Thus, what used
00050 // to be a "PointCloud" is now a "PointCloud3D". @@@jbt
00051 
00055 template <int Dim>
00056 class PointCloud : public GeomObject {
00057 public:
00060     PointCloud()
00061     {};
00062 
00065 
00071     template <typename ForwardIterator>
00072     PointCloud(ForwardIterator start, int numpoints)
00073         : points_(numpoints)
00074     {
00075         std::copy(start, start + Dim * numpoints, points_[0].begin());
00076     }
00077 
00079     PointCloud(std::vector<Array<double, Dim> >& points)
00080         : points_(points)
00081     {}
00082 
00084     virtual ~PointCloud()
00085     {}
00086 
00087     // inherited from GeomObject
00088     virtual BoundingBox boundingBox() const
00089     {
00090         BoundingBox box;
00091         box.setFromArray(points_[0].begin(), points_.back().end(), Dim);
00092         return box;
00093     }
00094 
00096     virtual int dimension() const
00097     { return Dim; }
00098 
00099     // inherited from GeomObject
00100     virtual ClassType instanceType() const
00101     { return PointCloud::classType(); }
00102     
00103     // inherited from GeomObject
00104     static ClassType classType()
00105     { return Class_PointCloud; }
00106 
00107     // inherited from GeomObject
00108 // #ifdef _MSC_VER
00109 // #if _MSC_VER < 1300
00110 //     virtual GeomObject* clone() const
00111 //     { return new PointCloud(*this); }
00112 // #else
00113 //     virtual PointCloud* clone() const
00114 //     { return new PointCloud(*this); }
00115 // #endif // _MSC_VER < 1300
00116 // #else
00117     virtual PointCloud* clone() const
00118     { return new PointCloud(*this); }
00119 // #endif
00120 
00123     int numPoints() const
00124     { return points_.size(); }
00125     
00130     Array<double, Dim>& point(int i) 
00131     { return points_[i]; }
00132 
00137     const Array<double, Dim>& point(int i) const
00138     { return points_[i]; }
00139 
00143     double* rawData()
00144     { return points_[0].begin(); }
00145 
00149     const double* rawData() const
00150     { return points_[0].begin(); }
00151 
00154     const std::vector<Array<double, Dim> >& pointVector()
00155     { return points_; }
00156 
00157     // inherited from Streamable
00158     virtual void read (std::istream& is)
00159     {
00160         bool is_good = is.good();
00161         if (!is_good) {
00162             THROW("Invalid geometry file!");
00163         }
00164         int nump;
00165         is >> nump;
00166         ALWAYS_ERROR_IF(nump < 1, "Less than one point in cloud.");
00167         is_good = is.good();
00168         if (!is_good) {
00169             THROW("Invalid geometry file!");
00170         }
00171         points_.resize(nump);
00172         for (int i = 0; i < nump; ++i) {
00173             for (int d = 0; d < Dim; ++d) {
00174                 is >> points_[i][d];
00175             }
00176         }
00177 
00178         is_good = is.good();
00179         if (!is_good) {
00180             THROW("Invalid geometry file!");
00181         }
00182     }
00183 
00184     // inherited from Streamable
00185     virtual void write (std::ostream& os) const
00186     {
00187         os << std::setprecision(15);
00188 
00189         int nump = points_.size();
00190         os << nump << '\n';
00191         for(int i = 0; i < nump; ++i) {
00192             os << points_[i][0];
00193             for (int d = 1; d < Dim; ++d) {
00194                 os << ' ' << points_[i][d];
00195             }
00196             os << '\n';
00197         }
00198         os << std::endl;
00199     }
00200 
00201 private:
00202     std::vector<Array<double, Dim> > points_;
00203 
00204 };
00205 
00206 
00207 typedef PointCloud<3> PointCloud3D;
00208 typedef PointCloud<4> PointCloud4D;
00209 
00210 
00212 } // namespace Go
00213 
00214 
00215 
00216 
00217 #endif // _GOPOINTCLOUD_H
00218 
00219 

Generated on Mon Jun 11 14:48:18 2007 for GoTools Core Library by  doxygen 1.5.1