1 #include "float_math.h"
2 #include "meshvolume.h"
3 
4 /*----------------------------------------------------------------------
5 		Copyright (c) 2004 Open Dynamics Framework Group
6 					www.physicstools.org
7 		All rights reserved.
8 
9 		Redistribution and use in source and binary forms, with or without modification, are permitted provided
10 		that the following conditions are met:
11 
12 		Redistributions of source code must retain the above copyright notice, this list of conditions
13 		and the following disclaimer.
14 
15 		Redistributions in binary form must reproduce the above copyright notice,
16 		this list of conditions and the following disclaimer in the documentation
17 		and/or other materials provided with the distribution.
18 
19 		Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
20 		be used to endorse or promote products derived from this software without specific prior written permission.
21 
22 		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 		INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 		DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 		EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 		IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 		THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 -----------------------------------------------------------------------*/
30 
31 // http://codesuppository.blogspot.com
32 //
33 // mailto: jratcliff@infiniplex.net
34 //
35 // http://www.amillionpixels.us
36 //
37 
det(const float * p1,const float * p2,const float * p3)38 inline float det(const float *p1, const float *p2, const float *p3)
39 {
40 	return p1[0] * p2[1] * p3[2] + p2[0] * p3[1] * p1[2] + p3[0] * p1[1] * p2[2] - p1[0] * p3[1] * p2[2] - p2[0] * p1[1] * p3[2] - p3[0] * p2[1] * p1[2];
41 }
42 
computeMeshVolume(const float * vertices,unsigned int tcount,const unsigned int * indices)43 float computeMeshVolume(const float *vertices, unsigned int tcount, const unsigned int *indices)
44 {
45 	float volume = 0;
46 
47 	for (unsigned int i = 0; i < tcount; i++, indices += 3)
48 	{
49 		const float *p1 = &vertices[indices[0] * 3];
50 		const float *p2 = &vertices[indices[1] * 3];
51 		const float *p3 = &vertices[indices[2] * 3];
52 
53 		volume += det(p1, p2, p3);  // compute the volume of the tetrahedran relative to the origin.
54 	}
55 
56 	volume *= (1.0f / 6.0f);
57 	if (volume < 0)
58 		volume *= -1;
59 	return volume;
60 }
61 
CrossProduct(const float * a,const float * b,float * cross)62 inline void CrossProduct(const float *a, const float *b, float *cross)
63 {
64 	cross[0] = a[1] * b[2] - a[2] * b[1];
65 	cross[1] = a[2] * b[0] - a[0] * b[2];
66 	cross[2] = a[0] * b[1] - a[1] * b[0];
67 }
68 
DotProduct(const float * a,const float * b)69 inline float DotProduct(const float *a, const float *b)
70 {
71 	return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
72 }
73 
tetVolume(const float * p0,const float * p1,const float * p2,const float * p3)74 inline float tetVolume(const float *p0, const float *p1, const float *p2, const float *p3)
75 {
76 	float a[3];
77 	float b[3];
78 	float c[3];
79 
80 	a[0] = p1[0] - p0[0];
81 	a[1] = p1[1] - p0[1];
82 	a[2] = p1[2] - p0[2];
83 
84 	b[0] = p2[0] - p0[0];
85 	b[1] = p2[1] - p0[1];
86 	b[2] = p2[2] - p0[2];
87 
88 	c[0] = p3[0] - p0[0];
89 	c[1] = p3[1] - p0[1];
90 	c[2] = p3[2] - p0[2];
91 
92 	float cross[3];
93 
94 	CrossProduct(b, c, cross);
95 
96 	float volume = DotProduct(a, cross);
97 
98 	if (volume < 0)
99 		return -volume;
100 
101 	return volume;
102 }
103 
det(const float * p0,const float * p1,const float * p2,const float * p3)104 inline float det(const float *p0, const float *p1, const float *p2, const float *p3)
105 {
106 	return p1[0] * p2[1] * p3[2] + p2[0] * p3[1] * p1[2] + p3[0] * p1[1] * p2[2] - p1[0] * p3[1] * p2[2] - p2[0] * p1[1] * p3[2] - p3[0] * p2[1] * p1[2];
107 }
108 
computeMeshVolume2(const float * vertices,unsigned int tcount,const unsigned int * indices)109 float computeMeshVolume2(const float *vertices, unsigned int tcount, const unsigned int *indices)
110 {
111 	float volume = 0;
112 
113 	const float *p0 = vertices;
114 	for (unsigned int i = 0; i < tcount; i++, indices += 3)
115 	{
116 		const float *p1 = &vertices[indices[0] * 3];
117 		const float *p2 = &vertices[indices[1] * 3];
118 		const float *p3 = &vertices[indices[2] * 3];
119 
120 		volume += tetVolume(p0, p1, p2, p3);  // compute the volume of the tetrahdren relative to the root vertice
121 	}
122 
123 	return volume * (1.0f / 6.0f);
124 }
125