V-ART
meshobject.h
Go to the documentation of this file.
1 
5 #ifndef VART_MESHOBJECT_H
6 #define VART_MESHOBJECT_H
7 
8 #include "vart/mesh.h"
9 #include "vart/point4d.h"
10 #include "vart/graphicobj.h"
11 #include "vart/transform.h"
12 #include <vector>
13 #include <list>
14 #include <map>
15 
16 namespace VART {
25  class MeshObject : public GraphicObj {
27  friend std::ostream& operator<<(std::ostream& output, const MeshObject& m);
28 
29  public:
30  // PUBLIC METHODS
31  MeshObject();
32  MeshObject(const MeshObject& obj);
33  MeshObject& operator=(const MeshObject& obj);
34 
37  virtual VART::SceneNode * Copy();
38 
40  void SetMaterial(const Material& mat);
41 
44  void MakeBox(double minX, double maxX, double minY, double maxY, double minZ, double maxZ);
45 
51  void GetYProjection(std::list<Point4D>* resultPtr, double height=0) const;
52 
60  void Optimize();
61 
66  void Clear();
67 
73  void SetVertices(const std::vector<Point4D>& vertexVec);
74 
80  void SetNormals(const std::vector<Point4D>& normalVec);
81 
91  void SetVertices(const char* vertexStr);
92 
96  void SetVertex(unsigned int index, const Point4D& newValue);
97 
99  const std::vector<double>& GetVerticesCoordinates() { return vertCoordVec; }
100 
112  void AddFace(const char* indexStr);
113 
119  void AddMesh(const Mesh& m);
120 
122  virtual void ComputeBoundingBox();
123  void ComputeBoundingBox(const Transform& trans, BoundingBox* bbPtr);
124 
129  void ComputeSubBBoxes( const Transform& trans, int subdivisions );
130 
132  std::vector<VART::BoundingBox> GetSubBBoxes() { return subBBoxes; };
133 
134  virtual TypeID GetID() const { return MESH_OBJECT; }
135 
139  void MergeWith(const MeshObject& obj);
140 
142  void ApplyTransform(const Transform& trans);
143 
144  //~ /// \brief Splits the object along some plane parallel to XZ.
145  //~ /// \param y [in] Position of the cutting plane.
146  //~ /// \param ptObjAbove [out] Parts that lie below the cutting plane.
147  //~ /// \param ptObjBelow [out] Parts that lie above the cutting plane.
148  //~ ///
149  //~ /// Splits an object into two, along a cutting plane. The two resulting objects
150  //~ /// are returned by filling the objects pointed by ptObjAbove and ptObjBelow, which
151  //~ /// must not contain previous geometry (use Clear()). If the cutting plane does not
152  //~ /// intersect the object, one of the resulting objects will be empty (check with
153  //~ /// IsEmpty()).
154  //~ void YSplit(double y, MeshObject* ptObjBelow, MeshObject* ptObjAbove) const;
155 
160  bool IsEmpty() { return meshList.empty(); }
161 
163  Point4D GetVertex(unsigned int pos);
164 
168  void SmallerVertex(Point4D* resultPtr);
169 
175 
180  void ComputeVertexNormals();
181 
182  // STATIC PUBLIC METHODS
190  static void ComputeTriangleNormal(const Point4D& v1, const Point4D& v2,
191  const Point4D& v3, Point4D* resultPtr);
192 
205  static bool ReadFromOBJ(const std::string& filename, std::list<MeshObject*>* resultPtr);
206 
208  unsigned int NumFaces();
209 
210  // STATIC PUBLIC ATTRIBUTES
212  static float sizeOfNormals;
213 
214  protected:
215  // PROTECTED NESTED CLASSES
217  public:
218  VertexTriplet(unsigned int vi, unsigned int ti, unsigned int ni)
219  :vertexIndex(vi), textIndex(ti), normIndex(ni) {}
220  bool operator < (VertexTriplet indexes) const {
221  if (vertexIndex != indexes.vertexIndex)
222  return vertexIndex < indexes.vertexIndex;
223  if (textIndex != indexes.textIndex)
224  return textIndex < indexes.textIndex;
225  else
226  return normIndex < indexes.normIndex;
227  }
228  unsigned int vertexIndex;
229  unsigned int textIndex;
230  unsigned int normIndex;
231  };
232 
233  // PROTECTED METHODS
234  virtual bool DrawInstanceOGL() const;
235 
239  void AddNormal(unsigned int idx, const Point4D& vec);
240 
244  Point4D Vertex(unsigned int i) const {
245  return Point4D(vertCoordVec[i*3], vertCoordVec[i*3+1], vertCoordVec[i*3+2]); }
246 
248  void NormalizeAllNormals();
249 
250  // PROTECTED ATTRIBUTES
255  std::vector<Point4D> vertVec;
256 
262  std::vector<double> vertCoordVec;
263 
268  std::vector<Point4D> normVec;
269 
274  std::vector<double> normCoordVec;
275 
280  std::vector<float> textCoordVec;
281 
283  std::list<Mesh> meshList;
284 
285  // PROTECTED STATIC METHODS
287  static void ReadVertex(std::istringstream& iss, unsigned int* vi, unsigned int* ti, unsigned int* ni);
288 
289  static void ReadMaterialTable(const std::string& filename,
290  std::map<std::string,Material>* matMapPtr);
291 
292  static void ReadVerticesLine(std::istringstream& input,
293  std::list<VertexTriplet>* resultPtr);
294 
295 
296  private:
297  // PRIVATE METHODS
302  void subDivideBBox( VART::BoundingBox motherBox, int subdivisions, std::vector<VART::Point4D> pointList );
303 
305  void computeNewSubBBox( VART::BoundingBox oldBox, int subdivisions, std::vector<VART::Point4D> pointList);
306 
307  // PRIVATE ATRIBUTES
311  std::vector<VART::BoundingBox> subBBoxes;
312 
313  }; // end class declaration
314 } // end namespace
315 
316 #endif
Base class for objects that compose a scene graph.
Definition: scenenode.h:25
Point4D GetVertex(unsigned int pos)
Returns a copy of a vertex.
Definition: meshobject.cpp:144
static void ReadMaterialTable(const std::string &filename, std::map< std::string, Material > *matMapPtr)
Points and vectors using homogeneous coordinates.
Definition: point4d.h:22
void AddNormal(unsigned int idx, const Point4D &vec)
Adds a vector to a vertex normal.
Definition: meshobject.cpp:158
Header file for V-ART class "GraphicObj".
Point4D Vertex(unsigned int i) const
Returns a vertex as a Point4D.
Definition: meshobject.h:244
static void ComputeTriangleNormal(const Point4D &v1, const Point4D &v2, const Point4D &v3, Point4D *resultPtr)
Computes the normal of a triangle.
Definition: meshobject.cpp:685
void AddFace(const char *indexStr)
Adds a face (a mesh of a single polygon) based on previously set vertices.
Definition: meshobject.cpp:244
std::list< Mesh > meshList
List of Meshes.
Definition: meshobject.h:283
Header file for V-ART class "Mesh".
friend std::ostream & operator<<(std::ostream &output, const MeshObject &m)
Output operator.
void GetYProjection(std::list< Point4D > *resultPtr, double height=0) const
Computes the projection of all vertices along the Y axis.
Definition: meshobject.cpp:344
Axis aligned bounding box.
Definition: boundingbox.h:23
void SetVertices(const std::vector< Point4D > &vertexVec)
Sets the vector of vertices.
A mesh is part of an graphical object, in which faces are made of vertices according to some rule of ...
Definition: mesh.h:30
std::vector< VART::BoundingBox > GetSubBBoxes()
returns the list of subdivided bounding boxes.
Definition: meshobject.h:132
Point4D GetVertexMedia()
Return the Aritmethic mean of vertexes values.
Definition: meshobject.cpp:221
virtual TypeID GetID() const
Returns type identification of the node.
Definition: meshobject.h:134
bool IsEmpty()
Checks whether the object contains geometry data.
Definition: meshobject.h:160
std::vector< double > normCoordVec
Vector of all vertex normals (their coordinates in sequence).
Definition: meshobject.h:274
void NormalizeAllNormals()
Normalizes all vertex normals.
Geometric transformations.
Definition: transform.h:24
void SmallerVertex(Point4D *resultPtr)
Computes and returns the smaller vertex.
Definition: meshobject.cpp:192
virtual void ComputeBoundingBox()
Computes the bounding box.
Definition: meshobject.cpp:375
An scene node that is associated with a shape.
Definition: graphicobj.h:18
void Clear()
Erases internal structures.
Definition: meshobject.cpp:59
Header file for V-ART class "Transform".
Material properties for graphical objects.
Definition: material.h:16
std::vector< float > textCoordVec
Vector of all texture coordinates.
Definition: meshobject.h:280
void SetMaterial(const Material &mat)
Assigns a material to all meshes of the mesh object.
Definition: meshobject.cpp:69
VertexTriplet(unsigned int vi, unsigned int ti, unsigned int ni)
Definition: meshobject.h:218
virtual bool DrawInstanceOGL() const
Non-recursive drawing - should be overriden by every derived class.
Definition: meshobject.cpp:693
void ComputeSubBBoxes(const Transform &trans, int subdivisions)
Computes de SubBBoxes and stores them.
Definition: meshobject.cpp:431
void AddMesh(const Mesh &m)
Adds a copy of the mesh to the object.
Definition: meshobject.cpp:270
unsigned int NumFaces()
Computes the number of faces.
Definition: meshobject.cpp:168
void ApplyTransform(const Transform &trans)
Apply Transformation to all vertices.
Definition: meshobject.cpp:658
static float sizeOfNormals
Size of normals for rendering (in world coordinates).
Definition: meshobject.h:212
void SetVertex(unsigned int index, const Point4D &newValue)
Changes one vertex.
Definition: meshobject.cpp:129
void SetNormals(const std::vector< Point4D > &normalVec)
Sets the vector of normals.
Definition: meshobject.cpp:91
static void ReadVertex(std::istringstream &iss, unsigned int *vi, unsigned int *ti, unsigned int *ni)
Reads a vertex description from a face on a OBJ file.
bool operator<(VertexTriplet indexes) const
Definition: meshobject.h:220
std::vector< double > vertCoordVec
Vector of all vertex coordinates (their coordinates in sequence).
Definition: meshobject.h:262
std::vector< Point4D > normVec
Vector of all normals.
Definition: meshobject.h:268
const std::vector< double > & GetVerticesCoordinates()
Returns the coordinates of the vertices in the object.
Definition: meshobject.h:99
static void ReadVerticesLine(std::istringstream &input, std::list< VertexTriplet > *resultPtr)
Header file for V-ART class "Point4D".
void Optimize()
Optimize object for display.
Definition: meshobject.cpp:369
std::vector< Point4D > vertVec
Vector of all vertices.
Definition: meshobject.h:255
static bool ReadFromOBJ(const std::string &filename, std::list< MeshObject * > *resultPtr)
Read MeshObjects from a Wavefront OBJ file.
Definition: meshobject.cpp:775
void MakeBox(double minX, double maxX, double minY, double maxY, double minZ, double maxZ)
Creates a box aligned with the 3 reference planes (XY, XZ and YZ).
Definition: meshobject.cpp:275
void MergeWith(const MeshObject &obj)
Merges one mesh object with another.
Definition: meshobject.cpp:633
virtual VART::SceneNode * Copy()
Returns a copy of an MeshObject. Every derived class must reimplements this method, to avoid errors with VART::SceneNode::RecursiveCopy.
Definition: meshobject.cpp:54
void ComputeVertexNormals()
Computes the normal of every vertex.
Definition: meshobject.cpp:475
Graphical object made of polygon meshes.
Definition: meshobject.h:25
MeshObject & operator=(const MeshObject &obj)
Definition: meshobject.cpp:42