vhacdMesh.h
Go to the documentation of this file.
1 /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
2  All rights reserved.
3 
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 
9  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 
11  3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  */
15 #pragma once
16 #ifndef VHACD_MESH_H
17 #define VHACD_MESH_H
18 #include "vhacdSArray.h"
19 #include "vhacdVector.h"
20 
21 #define VHACD_DEBUG_MESH
22 
23 namespace VHACD {
24 enum AXIS {
25  AXIS_X = 0,
26  AXIS_Y = 1,
27  AXIS_Z = 2
28 };
29 struct Plane {
30  double m_a;
31  double m_b;
32  double m_c;
33  double m_d;
35  short m_index;
36 };
37 #ifdef VHACD_DEBUG_MESH
38 struct Material {
39 
44  double m_shininess;
46  Material(void)
47  {
48  m_diffuseColor.X() = 0.5;
49  m_diffuseColor.Y() = 0.5;
50  m_diffuseColor.Z() = 0.5;
51  m_specularColor.X() = 0.5;
52  m_specularColor.Y() = 0.5;
53  m_specularColor.Z() = 0.5;
54  m_ambientIntensity = 0.4;
55  m_emissiveColor.X() = 0.0;
56  m_emissiveColor.Y() = 0.0;
57  m_emissiveColor.Z() = 0.0;
58  m_shininess = 0.4;
59  m_transparency = 0.0;
60  };
61 };
62 #endif // VHACD_DEBUG_MESH
63 
65 class Mesh {
66 public:
67  void AddPoint(const Vec3<double>& pt) { m_points.PushBack(pt); };
68  void SetPoint(size_t index, const Vec3<double>& pt) { m_points[index] = pt; };
69  const Vec3<double>& GetPoint(size_t index) const { return m_points[index]; };
70  Vec3<double>& GetPoint(size_t index) { return m_points[index]; };
71  size_t GetNPoints() const { return m_points.Size(); };
72  double* GetPoints() { return (double*)m_points.Data(); } // ugly
73  const double* const GetPoints() const { return (double*)m_points.Data(); } // ugly
74  const Vec3<double>* const GetPointsBuffer() const { return m_points.Data(); } //
75  Vec3<double>* const GetPointsBuffer() { return m_points.Data(); } //
76  void AddTriangle(const Vec3<int32_t>& tri) { m_triangles.PushBack(tri); };
77  void SetTriangle(size_t index, const Vec3<int32_t>& tri) { m_triangles[index] = tri; };
78  const Vec3<int32_t>& GetTriangle(size_t index) const { return m_triangles[index]; };
79  Vec3<int32_t>& GetTriangle(size_t index) { return m_triangles[index]; };
80  size_t GetNTriangles() const { return m_triangles.Size(); };
81  int32_t* GetTriangles() { return (int32_t*)m_triangles.Data(); } // ugly
82  const int32_t* const GetTriangles() const { return (int32_t*)m_triangles.Data(); } // ugly
83  const Vec3<int32_t>* const GetTrianglesBuffer() const { return m_triangles.Data(); }
84  Vec3<int32_t>* const GetTrianglesBuffer() { return m_triangles.Data(); }
85  const Vec3<double>& GetCenter() const { return m_center; }
86  const Vec3<double>& GetMinBB() const { return m_minBB; }
87  const Vec3<double>& GetMaxBB() const { return m_maxBB; }
88  void ClearPoints() { m_points.Clear(); }
89  void ClearTriangles() { m_triangles.Clear(); }
90  void Clear()
91  {
92  ClearPoints();
93  ClearTriangles();
94  }
95  void ResizePoints(size_t nPts) { m_points.Resize(nPts); }
96  void ResizeTriangles(size_t nTri) { m_triangles.Resize(nTri); }
97  void CopyPoints(SArray<Vec3<double> >& points) const { points = m_points; }
98  double GetDiagBB() const { return m_diag; }
99  double ComputeVolume() const;
100  void ComputeConvexHull(const double* const pts,
101  const size_t nPts);
102  void Clip(const Plane& plane,
103  SArray<Vec3<double> >& positivePart,
104  SArray<Vec3<double> >& negativePart) const;
105  bool IsInside(const Vec3<double>& pt) const;
106  double ComputeDiagBB();
107  Vec3<double> &ComputeCenter(void);
108 
109 #ifdef VHACD_DEBUG_MESH
110  bool LoadOFF(const std::string& fileName, bool invert);
111  bool SaveVRML2(const std::string& fileName) const;
112  bool SaveVRML2(std::ofstream& fout, const Material& material) const;
113  bool SaveOFF(const std::string& fileName) const;
114 #endif // VHACD_DEBUG_MESH
115 
117  Mesh();
119  ~Mesh(void);
120 
121 private:
122  SArray<Vec3<double> > m_points;
123  SArray<Vec3<int32_t> > m_triangles;
124  Vec3<double> m_minBB;
125  Vec3<double> m_maxBB;
126  Vec3<double> m_center;
127  double m_diag;
128 };
129 }
130 #endif
void SetTriangle(size_t index, const Vec3< int32_t > &tri)
Definition: vhacdMesh.h:77
void AddPoint(const Vec3< double > &pt)
Definition: vhacdMesh.h:67
Definition: vhacdMesh.h:26
void CopyPoints(SArray< Vec3< double > > &points) const
Definition: vhacdMesh.h:97
const int32_t *const GetTriangles() const
Definition: vhacdMesh.h:82
void SetPoint(size_t index, const Vec3< double > &pt)
Definition: vhacdMesh.h:68
Vec3< double > m_specularColor
Definition: vhacdMesh.h:42
const Vec3< int32_t > & GetTriangle(size_t index) const
Definition: vhacdMesh.h:78
Definition: vhacdMesh.h:29
Vec3< double > & GetPoint(size_t index)
Definition: vhacdMesh.h:70
size_t GetNTriangles() const
Definition: vhacdMesh.h:80
const Vec3< double > & GetCenter() const
Definition: vhacdMesh.h:85
Definition: vhacdMesh.h:38
Vec3< double > *const GetPointsBuffer()
Definition: vhacdMesh.h:75
SArray.
Definition: vhacdSArray.h:27
Vec3< int32_t > *const GetTrianglesBuffer()
Definition: vhacdMesh.h:84
double m_b
Definition: vhacdMesh.h:31
Definition: vhacdMesh.h:27
Triangular mesh data structure.
Definition: vhacdMesh.h:65
Vec3< double > m_diffuseColor
Definition: vhacdMesh.h:40
AXIS m_axis
Definition: vhacdMesh.h:34
void ResizeTriangles(size_t nTri)
Definition: vhacdMesh.h:96
void ClearPoints()
Definition: vhacdMesh.h:88
const Vec3< double > *const GetPointsBuffer() const
Definition: vhacdMesh.h:74
int32_t * GetTriangles()
Definition: vhacdMesh.h:81
const Vec3< double > & GetMaxBB() const
Definition: vhacdMesh.h:87
double * GetPoints()
Definition: vhacdMesh.h:72
double m_shininess
Definition: vhacdMesh.h:44
double m_d
Definition: vhacdMesh.h:33
const double *const GetPoints() const
Definition: vhacdMesh.h:73
void ResizePoints(size_t nPts)
Definition: vhacdMesh.h:95
Definition: vhacdCircularList.h:19
const Vec3< int32_t > *const GetTrianglesBuffer() const
Definition: vhacdMesh.h:83
double GetDiagBB() const
Definition: vhacdMesh.h:98
double m_ambientIntensity
Definition: vhacdMesh.h:41
double m_transparency
Definition: vhacdMesh.h:45
AXIS
Definition: vhacdMesh.h:24
Vec3< double > m_emissiveColor
Definition: vhacdMesh.h:43
size_t GetNPoints() const
Definition: vhacdMesh.h:71
void ClearTriangles()
Definition: vhacdMesh.h:89
Definition: vhacdMesh.h:25
Vec3< int32_t > & GetTriangle(size_t index)
Definition: vhacdMesh.h:79
short m_index
Definition: vhacdMesh.h:35
double m_a
Definition: vhacdMesh.h:30
Material(void)
Definition: vhacdMesh.h:46
const Vec3< double > & GetMinBB() const
Definition: vhacdMesh.h:86
double m_c
Definition: vhacdMesh.h:32
void Clear()
Definition: vhacdMesh.h:90
void AddTriangle(const Vec3< int32_t > &tri)
Definition: vhacdMesh.h:76
const Vec3< double > & GetPoint(size_t index) const
Definition: vhacdMesh.h:69