Main Page   Class Hierarchy   Compound List   File List   Compound Members  

geMesh.h

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 2002-2003 David Pritchard <drpritch@alumni.uwaterloo.ca>
00003 // 
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either
00007 // version 2 of the License, or (at your option) any later
00008 // version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 #ifndef freecloth_geom_geMesh_h
00020 #define freecloth_geom_geMesh_h
00021 
00022 #ifndef freecloth_geom_package_h
00023 #include <freecloth/geom/package.h>
00024 #endif
00025 
00026 #ifndef freecloth_base_vector
00027 #include <freecloth/base/vector>
00028 #endif
00029 
00030 #ifndef freecloth_resmgt_rcBase_h
00031 #include <freecloth/resmgt/rcBase.h>
00032 #endif
00033 
00034 #ifndef freecloth_geom_gePoint_h
00035 #include <freecloth/geom/gePoint.h>
00036 #endif
00037 
00038 #ifndef freecloth_geom_geMeshTypes_h
00039 #include <freecloth/geom/geMeshTypes.h>
00040 #endif
00041 
00042 FREECLOTH_NAMESPACE_START
00043 
00044 ////////////////////////////////////////////////////////////////////////////////
00045 // FORWARD DECLARATIONS
00046 //
00047 
00048 class GeMeshBuilder;
00049 class GeVector;
00050 
00051 ////////////////////////////////////////////////////////////////////////////////
00052 /*!
00053  * \class GeMesh freecloth/geom/geMesh.h
00054  * \brief A class for triangle meshes.
00055  *
00056  * A mesh is represented by vertices, texture vertices and faces. Vertices
00057  * and texture vertices are stored in arrays, and faces use array indices
00058  * (VertexId, TextureVertexId) to refer to vertices. A mesh with no
00059  * texture vertices is considered to have no texture.
00060  *
00061  * Must be constructed by GeMeshBuilder. Allows changes to geometry
00062  * (vertices, texture vertices) after construction, but does not allow
00063  * changes to topology (faces).
00064  *
00065  * Retrieval and storage of faces is a little odd. Faces are stored as
00066  * GeMesh::Face objects, containing only face data. Clients are presented
00067  * with the alternate GeMesh::FaceWrapper class, which is given access to
00068  * vertex and texture vertex data; this is more useful for client use.
00069  * (Design pattern: facade.) A special iterator class allows iteration over
00070  * the faces using FaceWrappers.
00071  *
00072  * A winged-edge representation can be built with GeMeshWingedEdge. This
00073  * representation allows better traversal of the mesh topology, but has
00074  * too much overhead for everyday use.
00075  */
00076 
00077 // Note - RCBase *must* be first parent class here... weird compiler
00078 // bugs due to multiple inheritance otherwise. FIXME: which platform?
00079 class GeMesh : public RCBase, public GeMeshTypes {
00080 
00081 public:
00082 
00083     // ----- classes -----
00084 
00085     //! Custom ConstIterator for faces.
00086     class FaceConstIterator;
00087     //! Facade for faces.
00088     class FaceWrapper;
00089     
00090     // ----- types and enumerations -----
00091     
00092     //! Facade for faces.
00093     typedef FaceWrapper FaceType;
00094 
00095     // ----- member functions -----
00096     
00097     // Default copy constructor is fine.
00098     // Default assignment operator is fine.
00099     
00100     UInt32 getNbFaces() const;
00101     FaceType getFace( FaceId ) const;
00102     FaceConstIterator beginFace() const;
00103     FaceConstIterator endFace() const;
00104 
00105     UInt32 getNbVertices() const;
00106     const VertexType& getVertex( VertexId ) const;
00107     VertexType& getVertex( VertexId );
00108     const VertexType* getVertexArray() const;
00109     VertexIterator beginVertex();
00110     VertexIterator endVertex();
00111     VertexConstIterator beginVertex() const;
00112     VertexConstIterator endVertex() const;
00113 
00114     bool hasTexture() const;
00115     UInt32 getNbTextureVertices() const;
00116     const TextureVertexType& getTextureVertex( TextureVertexId ) const;
00117     TextureVertexType& getTextureVertex( TextureVertexId );
00118     const TextureVertexType* getTextureVertexArray() const;
00119     TextureVertexIterator beginTextureVertex();
00120     TextureVertexIterator endTextureVertex();
00121     TextureVertexConstIterator beginTextureVertex() const;
00122     TextureVertexConstIterator endTextureVertex() const;
00123 
00124 
00125 private:
00126 
00127     // ----- friends -----
00128 
00129     friend class FaceWrapper;
00130     friend class GeMeshBuilder;
00131 
00132     // ----- classes -----
00133 
00134     class Face;
00135 
00136     // ----- types and enumerations -----
00137     
00138     typedef std::vector<Face> FaceContainer;
00139 
00140     // ----- member functions -----
00141     GeMesh();
00142 
00143     // ----- data members -----
00144     
00145     FaceContainer           _faces;
00146     VertexContainer         _vertices;
00147     TextureVertexContainer  _textureVertices;
00148 };
00149 
00150 
00151 ////////////////////////////////////////////////////////////////////////////////
00152 /*!
00153  * \class GeMesh::Face freecloth/geom/geMesh.h
00154  * \brief A private data structure used to store information associated with
00155  * mesh faces.
00156  *
00157  * This class has no knowledge of other parts of the mesh (vertices,
00158  * texture vertices), and hence can only perform a very limited number of
00159  * operations.
00160  *
00161  * Vertices are assumed to be in counter-clockwise order.
00162  */
00163 class GeMesh::Face : public GeMeshTypes {
00164 
00165 public:
00166     // ----- types and enumerations -----
00167     enum {
00168         //! Maximum number of vertices in a face.
00169         NB_VERTICES = 3
00170     };
00171 
00172     // ----- member functions -----
00173     Face(
00174         VertexId, VertexId, VertexId,
00175         TextureVertexId, TextureVertexId, TextureVertexId
00176     );
00177     // Default copy constructor is fine.
00178     // Default assignment operator is fine.
00179     UInt32 getNbVertices() const;
00180     VertexId getVertexId( FaceVertexId ) const;
00181     TextureVertexId getTextureVertexId( FaceVertexId ) const;
00182 
00183 private:
00184 
00185     // ----- friends -----
00186     
00187     friend class GeMesh;
00188 
00189     // ----- data members -----
00190 
00191     VertexId        _vertexIds[ NB_VERTICES ];
00192     TextureVertexId _textureVertexIds[ NB_VERTICES ];
00193 };
00194 
00195 
00196 ////////////////////////////////////////////////////////////////////////////////
00197 /*!
00198  * \class GeMesh::FaceWrapper freecloth/geom/geMesh.h
00199  * \brief Facade class for access to face data, while drawing on information
00200  * from the vertex and texture vertex arrays.
00201  *
00202  * Each face contains indices into the vertex and texture vertex arrays.
00203  * From the vertices, other information can be calculated.
00204  *
00205  * At present, all faces are triangular.
00206  *
00207  * \pattern Facade
00208  */
00209 class GeMesh::FaceWrapper : public GeMeshTypes {
00210 
00211 public:
00212     // ----- types and enumerations -----
00213 
00214     enum {
00215         //! Maximum number of vertices in a face.
00216         NB_VERTICES = 3
00217     };
00218 
00219     // ----- member functions -----
00220     
00221     //! Construct invalid wrapper - mostly used by iterator classes.
00222     FaceWrapper();
00223     FaceWrapper( const GeMesh&, FaceId );
00224     // Default copy constructor is fine.
00225     // Default assignment operator is fine.
00226 
00227     FaceId getFaceId() const;
00228     UInt32 getNbVertices() const;
00229     VertexId getVertexId( FaceVertexId ) const;
00230     const VertexType& getVertex( FaceVertexId ) const;
00231     TextureVertexId getTextureVertexId( FaceVertexId ) const;
00232     const TextureVertexType& getTextureVertex( FaceVertexId ) const;
00233 
00234     //! Calculate area of face.
00235     Float calcArea() const;
00236     //! Calculate the normal for the face. Guaranteed to be a unit vector.
00237     GeVector calcNormal() const;
00238     //! Calculate the barycentric co-ordinates of p.
00239     //!
00240     //! Barycentric co-ordinates express p as a linear combination of the
00241     //! vertices of the face.
00242     GePoint calcBarycentric( const GePoint& p ) const;
00243 
00244 private:
00245     // ----- friends -----
00246     friend class GeMesh::FaceConstIterator;
00247     friend class GeMeshBuilder;
00248 
00249     // ----- static member functions -----
00250     // FIXME: move to somewhere else... public GePoint member?
00251     static GePoint calcBarycentric(
00252         const GePoint& p,
00253         const GePoint& v1,
00254         const GePoint& v2,
00255         const GePoint& v3
00256     );
00257 
00258     // ----- member functions -----
00259 
00260     FaceWrapper( const GeMesh*, FaceId );
00261     bool isValid() const;
00262 
00263     // ----- data members -----
00264     const GeMesh*   _mesh;
00265     FaceId          _faceId;
00266 };
00267 
00268 
00269 
00270 ////////////////////////////////////////////////////////////////////////////////
00271 /*!
00272  * \class GeMesh::FaceConstIterator freecloth/geom/geMesh.h
00273  * \brief Iterator for faces, using the FaceWrapper facade.
00274  *
00275  * Essentially, this is just like vector<Face>::const_iterator, but returns
00276  * a FaceWrapper object through the * and -> operators.
00277  * 
00278  * \pattern Iterator
00279  */
00280 class GeMesh::FaceConstIterator : public GeMeshTypes {
00281 
00282 public:
00283     // ----- static member functions -----
00284     // named constructor
00285     static FaceConstIterator begin( const GeMesh& );
00286     static FaceConstIterator end( const GeMesh& );
00287 
00288     // ----- member functions -----
00289     FaceConstIterator();
00290     FaceConstIterator( const GeMesh&, FaceId );
00291     // default copy constructor is fine.
00292 
00293     FaceConstIterator& operator++();
00294     FaceConstIterator operator++( int );
00295     FaceConstIterator& operator--();
00296     FaceConstIterator operator--( int );
00297 
00298     bool operator == ( const FaceConstIterator& ) const;
00299     bool operator != ( const FaceConstIterator& ) const;
00300 
00301     const FaceWrapper& operator*() const;
00302     const FaceWrapper* operator->() const;
00303 
00304 private:
00305     // ----- friends -----
00306     friend class GeMesh;
00307     friend class GeMeshBuilder;
00308 
00309     // ----- member functions -----
00310 
00311     //! Private constructor used by the public named constructors. 
00312     explicit FaceConstIterator( const FaceWrapper& );
00313 
00314     // ----- data members -----
00315     
00316     //! We have to keep a wrapper as a member for the -> operator to work.
00317     GeMesh::FaceWrapper _wrapper;
00318 };
00319 
00320 ////////////////////////////////////////////////////////////////////////////////
00321 // GLOBAL FUNCTIONS
00322 //
00323 std::ostream& operator<<( std::ostream&, const GeMesh& );
00324 std::ostream& operator<<( std::ostream&, const GeMesh::FaceWrapper& );
00325 
00326 
00327 FREECLOTH_NAMESPACE_END
00328 
00329 #include <freecloth/geom/geMesh.inline.h>
00330 
00331 #endif

Generated on Fri May 2 13:04:25 2003 for Freecloth by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002