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; |
34 | AXIS m_axis; |
35 | short m_index; |
36 | }; |
37 | #ifdef VHACD_DEBUG_MESH |
38 | struct Material { |
39 | |
40 | Vec3<double> m_diffuseColor; |
41 | double m_ambientIntensity; |
42 | Vec3<double> m_specularColor; |
43 | Vec3<double> m_emissiveColor; |
44 | double m_shininess; |
45 | double m_transparency; |
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 | |
64 | //! Triangular mesh data structure |
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 | |
116 | //! Constructor. |
117 | Mesh(); |
118 | //! Destructor. |
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 |