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 | #include "vhacdManifoldMesh.h" |
16 | namespace VHACD { |
17 | TMMVertex::TMMVertex(void) |
18 | { |
19 | Initialize(); |
20 | } |
21 | void TMMVertex::Initialize() |
22 | { |
23 | m_name = 0; |
24 | m_id = 0; |
25 | m_duplicate = 0; |
26 | m_onHull = false; |
27 | m_tag = false; |
28 | } |
29 | |
30 | TMMVertex::~TMMVertex(void) |
31 | { |
32 | } |
33 | TMMEdge::TMMEdge(void) |
34 | { |
35 | Initialize(); |
36 | } |
37 | void TMMEdge::Initialize() |
38 | { |
39 | m_id = 0; |
40 | m_triangles[0] = m_triangles[1] = m_newFace = 0; |
41 | m_vertices[0] = m_vertices[1] = 0; |
42 | } |
43 | TMMEdge::~TMMEdge(void) |
44 | { |
45 | } |
46 | void TMMTriangle::Initialize() |
47 | { |
48 | m_id = 0; |
49 | for (int32_t i = 0; i < 3; i++) { |
50 | m_edges[i] = 0; |
51 | m_vertices[0] = 0; |
52 | } |
53 | m_visible = false; |
54 | } |
55 | TMMTriangle::TMMTriangle(void) |
56 | { |
57 | Initialize(); |
58 | } |
59 | TMMTriangle::~TMMTriangle(void) |
60 | { |
61 | } |
62 | TMMesh::TMMesh() |
63 | { |
64 | } |
65 | TMMesh::~TMMesh(void) |
66 | { |
67 | } |
68 | void TMMesh::GetIFS(Vec3<double>* const points, Vec3<int32_t>* const triangles) |
69 | { |
70 | size_t nV = m_vertices.GetSize(); |
71 | size_t nT = m_triangles.GetSize(); |
72 | |
73 | for (size_t v = 0; v < nV; v++) { |
74 | points[v] = m_vertices.GetData().m_pos; |
75 | m_vertices.GetData().m_id = v; |
76 | m_vertices.Next(); |
77 | } |
78 | for (size_t f = 0; f < nT; f++) { |
79 | TMMTriangle& currentTriangle = m_triangles.GetData(); |
80 | triangles[f].X() = static_cast<int32_t>(currentTriangle.m_vertices[0]->GetData().m_id); |
81 | triangles[f].Y() = static_cast<int32_t>(currentTriangle.m_vertices[1]->GetData().m_id); |
82 | triangles[f].Z() = static_cast<int32_t>(currentTriangle.m_vertices[2]->GetData().m_id); |
83 | m_triangles.Next(); |
84 | } |
85 | } |
86 | void TMMesh::Clear() |
87 | { |
88 | m_vertices.Clear(); |
89 | m_edges.Clear(); |
90 | m_triangles.Clear(); |
91 | } |
92 | void TMMesh::Copy(TMMesh& mesh) |
93 | { |
94 | Clear(); |
95 | // updating the id's |
96 | size_t nV = mesh.m_vertices.GetSize(); |
97 | size_t nE = mesh.m_edges.GetSize(); |
98 | size_t nT = mesh.m_triangles.GetSize(); |
99 | for (size_t v = 0; v < nV; v++) { |
100 | mesh.m_vertices.GetData().m_id = v; |
101 | mesh.m_vertices.Next(); |
102 | } |
103 | for (size_t e = 0; e < nE; e++) { |
104 | mesh.m_edges.GetData().m_id = e; |
105 | mesh.m_edges.Next(); |
106 | } |
107 | for (size_t f = 0; f < nT; f++) { |
108 | mesh.m_triangles.GetData().m_id = f; |
109 | mesh.m_triangles.Next(); |
110 | } |
111 | // copying data |
112 | m_vertices = mesh.m_vertices; |
113 | m_edges = mesh.m_edges; |
114 | m_triangles = mesh.m_triangles; |
115 | |
116 | // generate mapping |
117 | CircularListElement<TMMVertex>** vertexMap = new CircularListElement<TMMVertex>*[nV]; |
118 | CircularListElement<TMMEdge>** edgeMap = new CircularListElement<TMMEdge>*[nE]; |
119 | CircularListElement<TMMTriangle>** triangleMap = new CircularListElement<TMMTriangle>*[nT]; |
120 | for (size_t v = 0; v < nV; v++) { |
121 | vertexMap[v] = m_vertices.GetHead(); |
122 | m_vertices.Next(); |
123 | } |
124 | for (size_t e = 0; e < nE; e++) { |
125 | edgeMap[e] = m_edges.GetHead(); |
126 | m_edges.Next(); |
127 | } |
128 | for (size_t f = 0; f < nT; f++) { |
129 | triangleMap[f] = m_triangles.GetHead(); |
130 | m_triangles.Next(); |
131 | } |
132 | |
133 | // updating pointers |
134 | for (size_t v = 0; v < nV; v++) { |
135 | if (vertexMap[v]->GetData().m_duplicate) { |
136 | vertexMap[v]->GetData().m_duplicate = edgeMap[vertexMap[v]->GetData().m_duplicate->GetData().m_id]; |
137 | } |
138 | } |
139 | for (size_t e = 0; e < nE; e++) { |
140 | if (edgeMap[e]->GetData().m_newFace) { |
141 | edgeMap[e]->GetData().m_newFace = triangleMap[edgeMap[e]->GetData().m_newFace->GetData().m_id]; |
142 | } |
143 | if (nT > 0) { |
144 | for (int32_t f = 0; f < 2; f++) { |
145 | if (edgeMap[e]->GetData().m_triangles[f]) { |
146 | edgeMap[e]->GetData().m_triangles[f] = triangleMap[edgeMap[e]->GetData().m_triangles[f]->GetData().m_id]; |
147 | } |
148 | } |
149 | } |
150 | for (int32_t v = 0; v < 2; v++) { |
151 | if (edgeMap[e]->GetData().m_vertices[v]) { |
152 | edgeMap[e]->GetData().m_vertices[v] = vertexMap[edgeMap[e]->GetData().m_vertices[v]->GetData().m_id]; |
153 | } |
154 | } |
155 | } |
156 | for (size_t f = 0; f < nT; f++) { |
157 | if (nE > 0) { |
158 | for (int32_t e = 0; e < 3; e++) { |
159 | if (triangleMap[f]->GetData().m_edges[e]) { |
160 | triangleMap[f]->GetData().m_edges[e] = edgeMap[triangleMap[f]->GetData().m_edges[e]->GetData().m_id]; |
161 | } |
162 | } |
163 | } |
164 | for (int32_t v = 0; v < 3; v++) { |
165 | if (triangleMap[f]->GetData().m_vertices[v]) { |
166 | triangleMap[f]->GetData().m_vertices[v] = vertexMap[triangleMap[f]->GetData().m_vertices[v]->GetData().m_id]; |
167 | } |
168 | } |
169 | } |
170 | delete[] vertexMap; |
171 | delete[] edgeMap; |
172 | delete[] triangleMap; |
173 | } |
174 | bool TMMesh::CheckConsistancy() |
175 | { |
176 | size_t nE = m_edges.GetSize(); |
177 | size_t nT = m_triangles.GetSize(); |
178 | for (size_t e = 0; e < nE; e++) { |
179 | for (int32_t f = 0; f < 2; f++) { |
180 | if (!m_edges.GetHead()->GetData().m_triangles[f]) { |
181 | return false; |
182 | } |
183 | } |
184 | m_edges.Next(); |
185 | } |
186 | for (size_t f = 0; f < nT; f++) { |
187 | for (int32_t e = 0; e < 3; e++) { |
188 | int32_t found = 0; |
189 | for (int32_t k = 0; k < 2; k++) { |
190 | if (m_triangles.GetHead()->GetData().m_edges[e]->GetData().m_triangles[k] == m_triangles.GetHead()) { |
191 | found++; |
192 | } |
193 | } |
194 | if (found != 1) { |
195 | return false; |
196 | } |
197 | } |
198 | m_triangles.Next(); |
199 | } |
200 | return true; |
201 | } |
202 | } |