PolyLineT.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 7053 $                                                       *
00038  *   $Author: bommes $                                                      *
00039  *   $Date: 2009-09-11 11:52:15 +0200 (Fr, 11. Sep 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 //=============================================================================
00045 //
00046 // CLASS PolyLineT
00047 //
00048 // Author:  David Bommes <bommes@cs.rwth-aachen.de>
00049 //
00050 //=============================================================================
00051 
00052 
00053 #ifndef ACG_POLYLINET_HH
00054 #define ACG_POLYLINET_HH
00055 
00056 
00057 //== INCLUDES =================================================================
00058 #include <vector>
00059 #include <iostream>
00060 #include "../../Config/ACGDefines.hh"
00061 
00062 //== FORWARDDECLARATIONS ======================================================
00063 
00064 //== NAMESPACES ===============================================================
00065 
00066 namespace ACG {
00067 
00068 //== CLASS DEFINITION =========================================================
00069 
00070 
00071 
00072 
00080 template <class PointT>
00081 class PolyLineT
00082 {
00083 public:
00084 
00085   // internal relevant Types
00086   typedef PointT                      Point;
00087   typedef typename Point::value_type  Scalar;
00088 
00090   PolyLineT(bool _closed = false );
00091 
00093   PolyLineT(const PolyLineT& _line);
00094 
00096   ~PolyLineT() {}
00097 
00098   // acces functions for closed_
00099   bool is_closed() const         { return closed_;}
00100   void set_closed(const bool _c) { closed_ = _c;}
00101 
00102   // get number of vertices
00103   unsigned int n_vertices() const { return points_.size(); }
00104 
00105   // get number of edges
00106   unsigned int n_edges() const { if( n_vertices() == 0) return 0;
00107                                  else return points_.size()-1 + (unsigned int)closed_; }
00108 
00109   // clear the current polyline
00110   void clear();
00111 
00112   // resize current polyline
00113   void resize( unsigned int _n);
00114 
00115 
00116   // add a point
00117   void add_point(const Point& _p);
00118 
00119   // insert _p at _idx into polyline
00120   void insert_point(int _idx, const Point& _p);
00121 
00122   // delete point at _idx
00123   void delete_point(int _idx);
00124 
00125 
00126   // get a point of the polyline
00127         Point& point(unsigned int _i)       { return points_[_i];}
00128   const Point& point(unsigned int _i) const { return points_[_i];}
00129 
00130   // get the points of the polyline
00131         std::vector<Point>& points()       { return points_;}
00132   const std::vector<Point>& points() const { return points_;}
00133 
00134   // get first point of the polyline ( no range check!!!)
00135         Point& front()       { return points_[0];}
00136   const Point& front() const { return points_[0];}
00137   // get last point of the polyline ( no range check!!!)
00138         Point& back()       { return points_[n_vertices()-1];}
00139   const Point& back() const { return points_[n_vertices()-1];}
00140 
00141   // TODO
00142   // void push_back (const Point& _p)
00143   // void push_front(const Point& _p)
00144   // void pop_back (const Point& _p)
00145   // void pop_front(const Point& _p)
00146 
00147   // compute the length of the polyline (in future cached method)
00148   Scalar length() const;
00149 
00150 
00151   // controle interval lengths
00152   void subdivide( Scalar _largest  );
00153   void collapse ( Scalar _smallest );
00154 
00155   void subdivide_old( Scalar _largest  );
00156   void collapse_old ( Scalar _smallest );
00157 
00158   // invert polyline that first vertex becomes last
00159   void invert();
00160 
00161   // append _pl to polyline
00162   void append(const PolyLineT<PointT>& _pl);
00163 
00164   // prepend _pl to polyline
00165   void prepend(const PolyLineT<PointT>& _pl);
00166 
00167   // split polyline at vertex with index _split_idx
00168   void split_closed( unsigned int _split_idx);
00169 
00170   // split polyline at vertex with index _split_idx
00171   void split( unsigned int _split_idx, PolyLineT<PointT>& _new_pl);
00172 
00173   // smooth polyline
00174   void smooth_uniform_laplace();
00175   void smooth_uniform_laplace2();
00176   void smooth_uniform_laplace3();
00177 
00178   // project polyline points to nearest surface points (use spatial search!!!)
00179   template <class MeshT, class SpatialSearchT>
00180   void project_to_mesh( const MeshT& _mesh, SpatialSearchT * _ssearch = 0);
00181 
00182   // project polyline points to nearest surface points (use spatial search!!!)
00183   template <class MeshT, class SpatialSearchT>
00184   void project_to_mesh( const std::vector<MeshT*> _mesh,
00185                         std::vector<SpatialSearchT*>* _ssearch = 0);
00186 
00187   // project polyline points to nearest surface points (use spatial search!!!)
00188   template <class MeshT, class SpatialSearchT>
00189   void resample_on_mesh_edges( MeshT& _mesh, SpatialSearchT * _ssearch = 0);
00190 
00191 #ifdef USE_PHYSIM
00192   // add projected polyline edges and vertices to mesh and select them
00193   template <class MeshT, class SpatialSearchT>
00194   void integrate_into_mesh( MeshT& _mesh, SpatialSearchT * _ssearch = 0);
00195 #endif
00196 
00197   // for non-closed polylines add a sample at the boundary 
00198   // if start or end vertex lie in a boundary face and facehandles are valid
00199   template <class MeshT>
00200   void add_boundary_points( MeshT& _mesh);
00201 
00202   // conversion methods PolyLine <-> LineNode
00203   template <class LineNodeT>
00204   LineNodeT* get_line_node( LineNodeT*& _line_node, int _mode = 0);
00205 
00206   template <class LineNodeT>
00207   void set_line_node( LineNodeT*& _line_node, int _mode = 0);
00208 
00209   // print information string
00210   void print() const;
00211 
00212   // storage
00213   void load( const char* _filename);
00214   void save( const char* _filename) const;
00215 
00216   // ############################### Standard Property Handling #############################
00217 
00218   // request properties
00219   void request_vertex_normals()    { request_prop( ref_count_vnormals_   , vnormals_);}
00220   void request_vertex_colors()     { request_prop( ref_count_vcolors_    , vcolors_ );}
00221   void request_vertex_scalars()    { request_prop( ref_count_vscalars_   , vscalars_);}
00222   void request_vertex_selections() { request_prop( ref_count_vselections_, vselections_);}
00223   void request_vertex_vhandles()   { request_prop( ref_count_vvhandles_  , vvhandles_);}
00224   void request_vertex_ehandles()   { request_prop( ref_count_vehandles_  , vehandles_);}
00225   void request_vertex_fhandles()   { request_prop( ref_count_vfhandles_  , vfhandles_);}
00226 
00227   void request_edge_normals()      { request_prop( ref_count_enormals_   , enormals_);}
00228   void request_edge_colors()       { request_prop( ref_count_ecolors_    , ecolors_ );}
00229   void request_edge_scalars()      { request_prop( ref_count_escalars_   , escalars_);}
00230   void request_edge_selections()   { request_prop( ref_count_eselections_, eselections_);}
00231 
00232   // release properties
00233   void release_vertex_normals()    { release_prop( ref_count_vnormals_   , vnormals_);}
00234   void release_vertex_colors()     { release_prop( ref_count_vcolors_    , vcolors_ );}
00235   void release_vertex_scalars()    { release_prop( ref_count_vscalars_   , vscalars_);}
00236   void release_vertex_selections() { release_prop( ref_count_vselections_, vselections_);}
00237   void release_vertex_vhandles()   { release_prop( ref_count_vvhandles_  , vvhandles_);}
00238   void release_vertex_ehandles()   { release_prop( ref_count_vehandles_  , vehandles_);}
00239   void release_vertex_fhandles()   { release_prop( ref_count_vfhandles_  , vfhandles_);}
00240 
00241   void release_edge_normals()      { release_prop( ref_count_enormals_   , enormals_);}
00242   void release_edge_colors()       { release_prop( ref_count_ecolors_    , ecolors_ );}
00243   void release_edge_scalars()      { release_prop( ref_count_escalars_   , escalars_);}
00244   void release_edge_selections()   { release_prop( ref_count_eselections_, eselections_);}
00245 
00246   // property availability
00247   bool vertex_normals_available()    const {return bool(ref_count_vnormals_);}
00248   bool vertex_colors_available()     const {return bool(ref_count_vcolors_);}
00249   bool vertex_scalars_available()    const {return bool(ref_count_vscalars_);}
00250   bool vertex_selections_available() const {return bool(ref_count_vselections_);}
00251   bool vertex_vhandles_available() const {return bool(ref_count_vvhandles_);}
00252   bool vertex_ehandles_available() const {return bool(ref_count_vehandles_);}
00253   bool vertex_fhandles_available() const {return bool(ref_count_vfhandles_);}
00254 
00255   bool edge_normals_available()    const {return bool(ref_count_enormals_);}
00256   bool edge_colors_available()     const {return bool(ref_count_ecolors_);}
00257   bool edge_scalars_available()    const {return bool(ref_count_escalars_);}
00258   bool edge_selections_available() const {return bool(ref_count_eselections_);}
00259 
00260   // property access ( no range or availability check! )
00261         Point& vertex_normal(unsigned int _i)       { return vnormals_[_i];}
00262   const Point& vertex_normal(unsigned int _i) const { return vnormals_[_i];}
00263 
00264         Point & vertex_color(unsigned int _i)       { return vcolors_[_i];}
00265   const Point & vertex_color(unsigned int _i) const { return vcolors_[_i];}
00266 
00267         Scalar& vertex_scalar(unsigned int _i)       { return vscalars_[_i];}
00268   const Scalar& vertex_scalar(unsigned int _i) const { return vscalars_[_i];}
00269 
00270         unsigned char& vertex_selection(unsigned int _i)       {return vselections_[_i];}
00271   const unsigned char& vertex_selection(unsigned int _i) const {return vselections_[_i];}
00272 
00273         int& vertex_vhandle(unsigned int _i)       {return vvhandles_[_i];}
00274   const int& vertex_vhandle(unsigned int _i) const {return vvhandles_[_i];}
00275 
00276         int& vertex_ehandle(unsigned int _i)       {return vehandles_[_i];}
00277   const int& vertex_ehandle(unsigned int _i) const {return vehandles_[_i];}
00278 
00279         int& vertex_fhandle(unsigned int _i)       {return vfhandles_[_i];}
00280   const int& vertex_fhandle(unsigned int _i) const {return vfhandles_[_i];}
00281 
00282         Point& edge_normal(unsigned int _i)       { return enormals_[_i];}
00283   const Point& edge_normal(unsigned int _i) const { return enormals_[_i];}
00284 
00285         Point & edge_color(unsigned int _i)       { return ecolors_[_i];}
00286   const Point & edge_color(unsigned int _i) const { return ecolors_[_i];}
00287 
00288         Scalar& edge_scalar(unsigned int _i)       { return escalars_[_i];}
00289   const Scalar& edge_scalar(unsigned int _i) const { return escalars_[_i];}
00290 
00291         unsigned char& edge_selection(unsigned int _i)       {return eselections_[_i];}
00292   const unsigned char& edge_selection(unsigned int _i) const {return eselections_[_i];}
00293 
00294 
00295   // ############################### Helper Functions  #############################
00296 
00297   // copy this[_j] = _pl[_i] WITH all vertex properties
00298   void copy_vertex_complete(const PolyLineT<PointT>& _pl, unsigned int _i, unsigned int _j);
00299 
00300 
00301   // copy this[_j] = _pl[_i] WITH all edge properties
00302   void copy_edge_complete(const PolyLineT<PointT>& _pl, unsigned int _i, unsigned int _j);
00303 
00304 private:
00305 
00306   template <class MeshT, class SpatialSearchT>
00307   Point find_nearest_point( const MeshT&                _mesh,
00308                             const Point&                _point,
00309                             typename MeshT::FaceHandle &_fh,
00310                             SpatialSearchT *            _ssearch = 0,
00311                             double*                     _dbest   = 0);
00312 
00313 
00314   template <class PropT>
00315   void request_prop( unsigned int& _ref_count, PropT& _prop);
00316   template <class PropT>
00317   void release_prop( unsigned int& _ref_count, PropT& _prop);
00318 
00319   template <class IPoint>
00320   bool plane_line_intersection( const IPoint& _p_plane,
00321                                 const IPoint& _n_plane,
00322                                 const IPoint& _p0,
00323                                 const IPoint& _p1,
00324                                       IPoint& _p_int);
00325 
00326   template<class MeshT>
00327   void edge_points_in_segment( const MeshT& _mesh,
00328                                const Point& _p0,
00329                                const Point& _p1,
00330                                const typename MeshT::FaceHandle _fh0,
00331                                const typename MeshT::FaceHandle _fh1,
00332                                std::vector<Point> &                     _points,
00333                                std::vector<typename MeshT::EdgeHandle>& _ehandles );
00334 
00335 
00336 private:
00337 
00338   // list of points
00339   std::vector<Point> points_;
00340 
00341   // connect first and last point?
00342   bool closed_;
00343 
00344   // ############################### Standard Property Handling #############################
00345 
00346   // list of vertex properties
00347   std::vector<Point>  vnormals_;
00348   std::vector<Point>  vcolors_;
00349   std::vector<Scalar> vscalars_;
00350   std::vector<unsigned char>   vselections_;
00351   std::vector<int>    vvhandles_;
00352   std::vector<int>    vehandles_;
00353   std::vector<int>    vfhandles_;
00354 
00355   std::vector<Point>  enormals_;
00356   std::vector<Point>  ecolors_;
00357   std::vector<Scalar> escalars_;
00358   std::vector<unsigned char>   eselections_;
00359 
00360 
00361   // property reference counter
00362   unsigned int ref_count_vnormals_;
00363   unsigned int ref_count_vcolors_;
00364   unsigned int ref_count_vscalars_;
00365   unsigned int ref_count_vselections_;
00366   unsigned int ref_count_vvhandles_;
00367   unsigned int ref_count_vehandles_;
00368   unsigned int ref_count_vfhandles_;
00369 
00370   unsigned int ref_count_enormals_;
00371   unsigned int ref_count_ecolors_;
00372   unsigned int ref_count_escalars_;
00373   unsigned int ref_count_eselections_;
00374 };
00375 
00376 
00377 //=============================================================================
00378 } // namespace ACG
00379 //=============================================================================
00380 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_POLYLINET_C)
00381 #define ACG_POLYLINET_TEMPLATES
00382 #include "PolyLineT.cc"
00383 #endif
00384 //=============================================================================
00385 #endif // ACG_POLYLINET_HH defined
00386 //=============================================================================
00387 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .