File: vhacdMesh.cpp

package info (click to toggle)
bullet 3.06%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,012 kB
  • sloc: cpp: 243,705; lisp: 12,017; ansic: 11,175; python: 626; makefile: 133; sh: 75
file content (358 lines) | stat: -rw-r--r-- 9,764 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
 All rights reserved.
 
 
 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 
 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.
 
 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
 
 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.
 */
#define _CRT_SECURE_NO_WARNINGS

#include "LinearMath/btConvexHullComputer.h"
#include "vhacdMesh.h"
#include <fstream>
#include <iosfwd>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

namespace VHACD
{
Mesh::Mesh()
{
	m_diag = 1.0;
}
Mesh::~Mesh()
{
}
double Mesh::ComputeVolume() const
{
	const size_t nV = GetNPoints();
	const size_t nT = GetNTriangles();
	if (nV == 0 || nT == 0)
	{
		return 0.0;
	}

	Vec3<double> bary(0.0, 0.0, 0.0);
	for (size_t v = 0; v < nV; v++)
	{
		bary += GetPoint(v);
	}
	bary /= static_cast<double>(nV);

	Vec3<double> ver0, ver1, ver2;
	double totalVolume = 0.0;
	for (int t = 0; t < nT; t++)
	{
		const Vec3<int>& tri = GetTriangle(t);
		ver0 = GetPoint(tri[0]);
		ver1 = GetPoint(tri[1]);
		ver2 = GetPoint(tri[2]);
		totalVolume += ComputeVolume4(ver0, ver1, ver2, bary);
	}
	return totalVolume / 6.0;
}

void Mesh::ComputeConvexHull(const double* const pts,
							 const size_t nPts)
{
	ResizePoints(0);
	ResizeTriangles(0);
	btConvexHullComputer ch;
	ch.compute(pts, 3 * sizeof(double), (int)nPts, -1.0, -1.0);
	for (int v = 0; v < ch.vertices.size(); v++)
	{
		AddPoint(Vec3<double>(ch.vertices[v].getX(), ch.vertices[v].getY(), ch.vertices[v].getZ()));
	}
	const int nt = ch.faces.size();
	for (int t = 0; t < nt; ++t)
	{
		const btConvexHullComputer::Edge* sourceEdge = &(ch.edges[ch.faces[t]]);
		int a = sourceEdge->getSourceVertex();
		int b = sourceEdge->getTargetVertex();
		const btConvexHullComputer::Edge* edge = sourceEdge->getNextEdgeOfFace();
		int c = edge->getTargetVertex();
		while (c != a)
		{
			AddTriangle(Vec3<int>(a, b, c));
			edge = edge->getNextEdgeOfFace();
			b = c;
			c = edge->getTargetVertex();
		}
	}
}
void Mesh::Clip(const Plane& plane,
				SArray<Vec3<double> >& positivePart,
				SArray<Vec3<double> >& negativePart) const
{
	const size_t nV = GetNPoints();
	if (nV == 0)
	{
		return;
	}
	double d;
	for (size_t v = 0; v < nV; v++)
	{
		const Vec3<double>& pt = GetPoint(v);
		d = plane.m_a * pt[0] + plane.m_b * pt[1] + plane.m_c * pt[2] + plane.m_d;
		if (d > 0.0)
		{
			positivePart.PushBack(pt);
		}
		else if (d < 0.0)
		{
			negativePart.PushBack(pt);
		}
		else
		{
			positivePart.PushBack(pt);
			negativePart.PushBack(pt);
		}
	}
}
bool Mesh::IsInside(const Vec3<double>& pt) const
{
	const size_t nV = GetNPoints();
	const size_t nT = GetNTriangles();
	if (nV == 0 || nT == 0)
	{
		return false;
	}
	Vec3<double> ver0, ver1, ver2;
	double volume;
	for (int t = 0; t < nT; t++)
	{
		const Vec3<int>& tri = GetTriangle(t);
		ver0 = GetPoint(tri[0]);
		ver1 = GetPoint(tri[1]);
		ver2 = GetPoint(tri[2]);
		volume = ComputeVolume4(ver0, ver1, ver2, pt);
		if (volume < 0.0)
		{
			return false;
		}
	}
	return true;
}
double Mesh::ComputeDiagBB()
{
	const size_t nPoints = GetNPoints();
	if (nPoints == 0)
		return 0.0;
	Vec3<double> minBB = m_points[0];
	Vec3<double> maxBB = m_points[0];
	double x, y, z;
	for (size_t v = 1; v < nPoints; v++)
	{
		x = m_points[v][0];
		y = m_points[v][1];
		z = m_points[v][2];
		if (x < minBB[0])
			minBB[0] = x;
		else if (x > maxBB[0])
			maxBB[0] = x;
		if (y < minBB[1])
			minBB[1] = y;
		else if (y > maxBB[1])
			maxBB[1] = y;
		if (z < minBB[2])
			minBB[2] = z;
		else if (z > maxBB[2])
			maxBB[2] = z;
	}
	return (m_diag = (maxBB - minBB).GetNorm());
}

#ifdef VHACD_DEBUG_MESH
bool Mesh::SaveVRML2(const std::string& fileName) const
{
	std::ofstream fout(fileName.c_str());
	if (fout.is_open())
	{
		const Material material;

		if (SaveVRML2(fout, material))
		{
			fout.close();
			return true;
		}
		return false;
	}
	return false;
}
bool Mesh::SaveVRML2(std::ofstream& fout, const Material& material) const
{
	if (fout.is_open())
	{
		fout.setf(std::ios::fixed, std::ios::floatfield);
		fout.setf(std::ios::showpoint);
		fout.precision(6);
		size_t nV = m_points.Size();
		size_t nT = m_triangles.Size();
		fout << "#VRML V2.0 utf8" << std::endl;
		fout << "" << std::endl;
		fout << "# Vertices: " << nV << std::endl;
		fout << "# Triangles: " << nT << std::endl;
		fout << "" << std::endl;
		fout << "Group {" << std::endl;
		fout << "    children [" << std::endl;
		fout << "        Shape {" << std::endl;
		fout << "            appearance Appearance {" << std::endl;
		fout << "                material Material {" << std::endl;
		fout << "                    diffuseColor " << material.m_diffuseColor[0] << " "
			 << material.m_diffuseColor[1] << " "
			 << material.m_diffuseColor[2] << std::endl;
		fout << "                    ambientIntensity " << material.m_ambientIntensity << std::endl;
		fout << "                    specularColor " << material.m_specularColor[0] << " "
			 << material.m_specularColor[1] << " "
			 << material.m_specularColor[2] << std::endl;
		fout << "                    emissiveColor " << material.m_emissiveColor[0] << " "
			 << material.m_emissiveColor[1] << " "
			 << material.m_emissiveColor[2] << std::endl;
		fout << "                    shininess " << material.m_shininess << std::endl;
		fout << "                    transparency " << material.m_transparency << std::endl;
		fout << "                }" << std::endl;
		fout << "            }" << std::endl;
		fout << "            geometry IndexedFaceSet {" << std::endl;
		fout << "                ccw TRUE" << std::endl;
		fout << "                solid TRUE" << std::endl;
		fout << "                convex TRUE" << std::endl;
		if (nV > 0)
		{
			fout << "                coord DEF co Coordinate {" << std::endl;
			fout << "                    point [" << std::endl;
			for (size_t v = 0; v < nV; v++)
			{
				fout << "                        " << m_points[v][0] << " "
					 << m_points[v][1] << " "
					 << m_points[v][2] << "," << std::endl;
			}
			fout << "                    ]" << std::endl;
			fout << "                }" << std::endl;
		}
		if (nT > 0)
		{
			fout << "                coordIndex [ " << std::endl;
			for (size_t f = 0; f < nT; f++)
			{
				fout << "                        " << m_triangles[f][0] << ", "
					 << m_triangles[f][1] << ", "
					 << m_triangles[f][2] << ", -1," << std::endl;
			}
			fout << "                ]" << std::endl;
		}
		fout << "            }" << std::endl;
		fout << "        }" << std::endl;
		fout << "    ]" << std::endl;
		fout << "}" << std::endl;
		return true;
	}
	return false;
}
bool Mesh::SaveOFF(const std::string& fileName) const
{
	std::ofstream fout(fileName.c_str());
	if (fout.is_open())
	{
		size_t nV = m_points.Size();
		size_t nT = m_triangles.Size();
		fout << "OFF" << std::endl;
		fout << nV << " " << nT << " " << 0 << std::endl;
		for (size_t v = 0; v < nV; v++)
		{
			fout << m_points[v][0] << " "
				 << m_points[v][1] << " "
				 << m_points[v][2] << std::endl;
		}
		for (size_t f = 0; f < nT; f++)
		{
			fout << "3 " << m_triangles[f][0] << " "
				 << m_triangles[f][1] << " "
				 << m_triangles[f][2] << std::endl;
		}
		fout.close();
		return true;
	}
	return false;
}

bool Mesh::LoadOFF(const std::string& fileName, bool invert)
{
	FILE* fid = fopen(fileName.c_str(), "r");
	if (fid)
	{
		const std::string strOFF("OFF");
		char temp[1024];
		fscanf(fid, "%s", temp);
		if (std::string(temp) != strOFF)
		{
			fclose(fid);
			return false;
		}
		else
		{
			int nv = 0;
			int nf = 0;
			int ne = 0;
			fscanf(fid, "%i", &nv);
			fscanf(fid, "%i", &nf);
			fscanf(fid, "%i", &ne);
			m_points.Resize(nv);
			m_triangles.Resize(nf);
			Vec3<double> coord;
			float x, y, z;
			for (int p = 0; p < nv; p++)
			{
				fscanf(fid, "%f", &x);
				fscanf(fid, "%f", &y);
				fscanf(fid, "%f", &z);
				m_points[p][0] = x;
				m_points[p][1] = y;
				m_points[p][2] = z;
			}
			int i, j, k, s;
			for (int t = 0; t < nf; ++t)
			{
				fscanf(fid, "%i", &s);
				if (s == 3)
				{
					fscanf(fid, "%i", &i);
					fscanf(fid, "%i", &j);
					fscanf(fid, "%i", &k);
					m_triangles[t][0] = i;
					if (invert)
					{
						m_triangles[t][1] = k;
						m_triangles[t][2] = j;
					}
					else
					{
						m_triangles[t][1] = j;
						m_triangles[t][2] = k;
					}
				}
				else  // Fix me: support only triangular meshes
				{
					for (int h = 0; h < s; ++h)
						fscanf(fid, "%i", &s);
				}
			}
			fclose(fid);
		}
	}
	else
	{
		return false;
	}
	return true;
}
#endif  // VHACD_DEBUG_MESH
}  // namespace VHACD