Simbody  3.8
PolygonalMesh.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_POLYGONAL_MESH_H_
2 #define SimTK_SimTKCOMMON_POLYGONAL_MESH_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2008-14 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: Michael Sherman, Ayman Habib *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
27 #include "SimTKcommon/Simmatrix.h"
29 
30 namespace SimTK {
31 
32 class PolygonalMesh;
33 class PolygonalMeshImpl;
34 
35 // We only want the template instantiation to occur once. This symbol is defined
36 // in the SimTKcommon compilation unit that defines the PolygonalMesh class but
37 // should not be defined any other time.
38 #ifndef SimTK_SIMTKCOMMON_DEFINING_POLYGONALMESH
39  extern template class PIMPLHandle<PolygonalMesh, PolygonalMeshImpl, true>;
40 #endif
41 
80 public:
83 
96  int resolution = 1);
97 
130  static PolygonalMesh createBrickMesh(const Vec3& halfDims,
131  int resolution = 1);
132 
162  Real radius,
163  Real halfLength,
164  int resolution=1);
165 
168  void clear();
169 
171  int getNumFaces() const;
173  int getNumVertices() const;
174 
176  bool hasNormals() const;
178  bool hasNormalsAtVertices() const;
180  bool hasNormalsAtFaces() const;
181 
183  bool hasTextureCoordinates() const;
190 
195  const Vec3& getVertexPosition(int vertex) const;
196 
201  const UnitVec3 getVertexNormal(int faceIndex, int vertexIndex) const;
202 
206  const UnitVec3& getVertexNormal(int vertex) const;
207 
212  const Vec2 getVertexTextureCoordinate(int faceIndex,
213  int vertexIndex) const;
214 
217  int getNumVerticesForFace(int face) const;
218 
225  int getFaceVertex(int face, int vertex) const;
226 
231  int addVertex(const Vec3& position);
232 
240  int addNormal(const UnitVec3& normal);
241 
246  int addTextureCoordinate(const Vec2& textureCoord);
247 
256  int addFace(const Array_<int>& vertices);
257 
258  int addFaceWithNormals(const Array_<int>& vertices,
259  const Array_<int>& normalIndices);
260 
261  void addFaceTextureCoordinates(const Array_<int>& textureIndices);
262 
271 
279 
291  void loadFile(const String& pathname);
292 
299  void loadObjFile(const String& pathname);
300 
306  void loadObjFile(std::istream& file);
307 
314  void loadVtpFile(const String& pathname);
315 
326  void loadStlFile(const String& pathname);
327 
328  private:
329  explicit PolygonalMesh(PolygonalMeshImpl* impl) : HandleBase(impl) {}
330  void initializeHandleIfEmpty();
331  void enforceMeshConsistency();
332 
333 };
334 
335 } // namespace SimTK
336 
337 #endif // SimTK_SimTKCOMMON_POLYGONAL_MESH_H_
This header provides declarations of the user-visible portion of the PIMPLHandle template classes tha...
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:224
This is the header which should be included in user programs that would like to make use of all the S...
This class provides a description of a mesh made of polygonal faces (not limited to triangles).
Definition: PolygonalMesh.h:79
static PolygonalMesh createSphereMesh(Real radius, int resolution=1)
Create a sphere-shaped mesh, with roughly uniform mesh elements.
void loadFile(const String &pathname)
Attempt to interpret the given file as a mesh file, with the format determined from the file name ext...
static PolygonalMesh createBrickMesh(const Vec3 &halfDims, int resolution=1)
Create a brick-shaped mesh.
static PolygonalMesh createCylinderMesh(const UnitVec3 &axis, Real radius, Real halfLength, int resolution=1)
Create a cylinder-shaped mesh, with the long axis in a given direction.
const Vec2 getVertexTextureCoordinate(int faceIndex, int vertexIndex) const
Get the texture coordinate of a vertex in the mesh, vertex specified by face/vertex.
void loadStlFile(const String &pathname)
Load an STL file, adding the vertices, faces , normals it contains to this mesh, and ignoring anythin...
void loadObjFile(std::istream &file)
Alternate signature for Wavefront OBJ format that takes an already-open istream rather than a pathnam...
int getNumVerticesForFace(int face) const
Get the number of vertices that make up a particular face.
bool hasTextureCoordinates() const
Check whether the PolygonalMesh contains Texture information.at ALL faces or vertices.
const UnitVec3 & getVertexNormal(int vertex) const
Get the normal of a vertex in the mesh.
int addFace(const Array_< int > &vertices)
Add a face to the mesh.
int getFaceVertex(int face, int vertex) const
Get the index of one of the vertices of a face.
PolygonalMesh & scaleMesh(Real scale)
Scale a mesh by multiplying every vertex by a fixed value.
void clear()
Restore this PolygonalMesh to its default-constructed state, meaning that it will contain no vertices...
const UnitVec3 getVertexNormal(int faceIndex, int vertexIndex) const
Get the normal of a vertex in the mesh, vertex specified by face/vertex.
int addFaceWithNormals(const Array_< int > &vertices, const Array_< int > &normalIndices)
void addFaceTextureCoordinates(const Array_< int > &textureIndices)
bool hasNormalsAtVertices() const
Check whether the PolygonalMesh contains Normals information at ALL vertices.
PolygonalMesh & transformMesh(const Transform &X_AM)
Transform a mesh by applying the given Transform to every vertex, leaving the mesh permanently change...
bool hasNormalsAtFaces() const
Check whether the PolygonalMesh contains Normals information.at ALL faces.
void loadVtpFile(const String &pathname)
Load a VTK PolyData (.vtp) file, adding the vertices, faces, normals and texture coordinates it conta...
int addTextureCoordinate(const Vec2 &textureCoord)
Add texture coordinate to the mesh.
void loadObjFile(const String &pathname)
Load a Wavefront OBJ (.obj) file, adding the vertices, faces, normals and texture coordinates it cont...
int getNumVertices() const
Get the number of vertices in the mesh.
PolygonalMesh()
Create an empty PolygonalMesh, with no vertices or faces.
Definition: PolygonalMesh.h:82
int addNormal(const UnitVec3 &normal)
Add vertex normal to the mesh.
bool hasNormals() const
Check whether the PolygonalMesh contains Normals information.
const Vec3 & getVertexPosition(int vertex) const
Get the position of a vertex in the mesh.
bool hasTextureCoordinatesAtVertices() const
Check whether the PolygonalMesh contains Texture information at every vertex, ready for visualization...
bool hasTextureCoordinatesAtFaces() const
Check whether the PolygonalMesh contains Texture information at every face/vertex client needs to con...
int getNumFaces() const
Get the number of faces in the mesh.
int addVertex(const Vec3 &position)
Add a vertex to the mesh.
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:65
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:607