-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.js
240 lines (157 loc) · 6.46 KB
/
models.js
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
//////////////////////////////////////////////////////////////////////////////
//
// Functions for processing triangle mesh models
//
// NEW VERSION - Handling just vertex coordinates
//
// J. Madeira - Oct. 2015 + Nov. 2018
//
//////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------
//
// Recursive triangle subdivision, using the midpoints of edges
//
function recSubdivisionMidPoint( v1, v2, v3,
coordsArray,
recursionDepth ) {
// Recursive midpoint subdivision of one triangle
if( recursionDepth == 0 ) {
// Storing coordinates and colors in the destination arrays
coordsArray.push( v1[0], v1[1], v1[2] );
coordsArray.push( v2[0], v2[1], v2[2] );
coordsArray.push( v3[0], v3[1], v3[2] );
}
else {
// Compute the midpoints and proceed recursively
var mid12 = computeMidPoint( v1, v2 );
var mid23 = computeMidPoint( v2, v3 );
var mid31 = computeMidPoint( v3, v1 );
// 4 recursive calls
recSubdivisionMidPoint( v1, mid12, mid31,
coordsArray, recursionDepth - 1 );
recSubdivisionMidPoint( v2, mid23, mid12,
coordsArray, recursionDepth - 1 );
recSubdivisionMidPoint( v3, mid31, mid23,
coordsArray, recursionDepth - 1 );
recSubdivisionMidPoint( mid12, mid23, mid31,
coordsArray, recursionDepth - 1 );
}
}
//----------------------------------------------------------------------------
function midPointRefinement( coordsArray,
recursionDepth ) {
// Mesh refinement - Higher-level function
// Each triangle is subdivided into 4 smaller ones - Lower-level recursive function
// Vertices are duplicated, whenever needed !!
// recursionDepth controls the final number of triangles and vertices
var origArrayLength = coordsArray.length;
// Copying
var origCoords = coordsArray.slice();
// Clearing the arrays
coordsArray.splice( 0, origArrayLength );
var origIndex;
// Each triangle is recursively subdivided into 4 triangles
// Iterate through the original triangular faces
for( origIndex = 0; origIndex < origArrayLength; origIndex += 9 )
{
/* Call the recursive subdivision function */
recSubdivisionMidPoint( origCoords.slice( origIndex, origIndex + 3 ),
origCoords.slice( origIndex + 3, origIndex + 6 ),
origCoords.slice( origIndex + 6, origIndex + 9 ),
coordsArray,
recursionDepth );
}
}
//----------------------------------------------------------------------------
//// Recursive triangle subdivision, using the triangle centroid
//
function recSubdivisionCentroid( v1, v2, v3,
coordsArray,
recursionDepth ) {
// Recursive centroid subdivision of one triangle
if( recursionDepth == 0 ) {
// Storing coordinates and colors in the destination arrays
coordsArray.push( v1[0], v1[1], v1[2] );
coordsArray.push( v2[0], v2[1], v2[2] );
coordsArray.push( v3[0], v3[1], v3[2] );
}
else {
// Compute the centroid and proceed recursively
var centroid = computeCentroid( v1, v2, v3 );
// 3 recursive calls
recSubdivisionCentroid( v1, v2, centroid,
coordsArray, recursionDepth - 1 );
recSubdivisionCentroid( v2, v3, centroid,
coordsArray, recursionDepth - 1 );
recSubdivisionCentroid( v3, v1, centroid,
coordsArray, recursionDepth - 1 );
}
}
//----------------------------------------------------------------------------
function centroidRefinement( coordsArray,
recursionDepth ) {
// Mesh refinement - Higher-level function
// Each triangle is subdivided into 3 smaller ones - Lower-level recursive function
// Vertices are duplicated, whenever needed !!
// recursionDepth controls the final number of triangles and vertices
var origArrayLength = coordsArray.length;
// Copying
var origCoords = coordsArray.slice();
// Clearing the arrays
coordsArray.splice( 0, origArrayLength );
var origIndex;
// Each triangle is recursively subdivided into 3 triangles
// Iterate through the original triangular faces
for( origIndex = 0; origIndex < origArrayLength; origIndex += 9 )
{
/* Call the recursive subdivision function */
recSubdivisionCentroid( origCoords.slice( origIndex, origIndex + 3 ),
origCoords.slice( origIndex + 3, origIndex + 6 ),
origCoords.slice( origIndex + 6, origIndex + 9 ),
coordsArray,
recursionDepth );
}
}
//----------------------------------------------------------------------------
//
// Moving vertices to the spherical surface of radius 1
//
function moveToSphericalSurface( coordsArray ) {
// Each vertex is moved to the spherecial surface of radius 1
// and centered at (0,0,0)
// This is done by handling each vertex as if it were a prosition vector,
// and normalizing
var arrayLength = coordsArray.length;
for( origIndex = 0; origIndex < arrayLength; origIndex += 3 )
{
var v = coordsArray.slice( origIndex, origIndex + 3 );
normalize( v );
var i;
for( i = 0; i < 3; i++ ) {
coordsArray[origIndex + i] = v[i];
}
}
}
//----------------------------------------------------------------------------
//
// NEW --- Computing the triangle unit normal vector
//
// And associating to every vertex
//
function computeVertexNormals( coordsArray, normalsArray ) {
// Clearing the new normals array
normalsArray.splice( 0, normalsArray.length );
// Taking 3 vertices from the coordinates array
for( var index = 0; index < coordsArray.length; index += 9 )
{
// Compute unit normal vector for each triangle
var normalVector = computeNormalVector( coordsArray.slice(index, index + 3),
coordsArray.slice(index + 3, index + 6),
coordsArray.slice(index + 6, index + 9) );
// Store the unit normal vector for each vertex
for( var j = 0; j < 3; j++ )
{
normalsArray.push( normalVector[0], normalVector[1], normalVector[2] );
}
}
}