Revision f835cc50
Added by Leszek Koltunski over 1 year ago
src/main/java/org/distorted/library/mesh/MeshMultigon.java | ||
---|---|---|
36 | 36 |
{ |
37 | 37 |
private static final float MAX_ERROR = 0.000001f; |
38 | 38 |
private float[][] mOuterVertices; |
39 |
private float[][] mOuterVectors; |
|
40 |
private int mNumOuter; |
|
39 | 41 |
|
40 | 42 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
41 | 43 |
|
... | ... | |
170 | 172 |
|
171 | 173 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
172 | 174 |
|
173 |
private void computeOuter(float[][] vertices) |
|
175 |
private void computeOuterVectors() |
|
176 |
{ |
|
177 |
mOuterVectors = new float[mNumOuter][]; |
|
178 |
|
|
179 |
for(int curr=0; curr<mNumOuter; curr++) |
|
180 |
{ |
|
181 |
int prev = curr==0 ? mNumOuter-1 : curr-1; |
|
182 |
int next = curr==mNumOuter-1 ? 0 : curr+1; |
|
183 |
|
|
184 |
float[] vP = mOuterVertices[prev]; |
|
185 |
float[] vN = mOuterVertices[next]; |
|
186 |
|
|
187 |
float dx = vP[0]-vN[0]; |
|
188 |
float dy = vP[1]-vN[1]; |
|
189 |
|
|
190 |
mOuterVectors[curr] = new float[] { dy,-dx }; |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
195 |
|
|
196 |
private void computeOuterVertices(float[][] vertices) |
|
174 | 197 |
{ |
175 | 198 |
ArrayList<float[]> tmp = new ArrayList<>(); |
176 | 199 |
|
... | ... | |
188 | 211 |
} |
189 | 212 |
while( !isSame(next[0]-first[0],next[1]-first[1]) ); |
190 | 213 |
|
191 |
int num = tmp.size();
|
|
192 |
mOuterVertices = new float[num][];
|
|
193 |
for(int i=0; i<num; i++) mOuterVertices[i] = tmp.remove(0);
|
|
214 |
mNumOuter = tmp.size();
|
|
215 |
mOuterVertices = new float[mNumOuter][];
|
|
216 |
for(int i=0; i<mNumOuter; i++) mOuterVertices[i] = tmp.remove(0);
|
|
194 | 217 |
} |
195 | 218 |
|
196 | 219 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
197 | 220 |
|
198 |
private boolean doesntBelongToOuter(float x, float y)
|
|
221 |
private int belongsToOuter(float x, float y)
|
|
199 | 222 |
{ |
200 |
for( float[] v : mOuterVertices ) |
|
201 |
if( isSame(x-v[0],y-v[1]) ) return false; |
|
223 |
for( int i=0; i<mNumOuter; i++ ) |
|
224 |
{ |
|
225 |
float[] v = mOuterVertices[i]; |
|
226 |
if( isSame(x-v[0],y-v[1]) ) return i; |
|
227 |
} |
|
202 | 228 |
|
203 |
return true;
|
|
229 |
return -1;
|
|
204 | 230 |
} |
205 | 231 |
|
206 | 232 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
207 | 233 |
|
208 | 234 |
private boolean[][] computeVertsUp(float[][] vertices) |
209 | 235 |
{ |
210 |
computeOuter(vertices); |
|
211 |
|
|
212 | 236 |
int num = vertices.length; |
213 | 237 |
boolean[][] up = new boolean[num][]; |
214 | 238 |
|
... | ... | |
217 | 241 |
float[] v = vertices[i]; |
218 | 242 |
int len = v.length/2; |
219 | 243 |
up[i] = new boolean[len]; |
220 |
for(int j=0; j<len; j++) up[i][j] = doesntBelongToOuter(v[2*j],v[2*j+1]); |
|
244 |
|
|
245 |
for(int j=0; j<len; j++) |
|
246 |
{ |
|
247 |
int vert = belongsToOuter(v[2*j],v[2*j+1]); |
|
248 |
up[i][j] = (vert==-1); |
|
249 |
} |
|
221 | 250 |
} |
222 | 251 |
|
223 | 252 |
return up; |
... | ... | |
266 | 295 |
|
267 | 296 |
if( eupc<0 ) |
268 | 297 |
{ |
269 |
int eupp = edges[prev]; |
|
270 |
int eupn = edges[next]; |
|
271 |
|
|
272 | 298 |
vL[0] = v[2*curr]; |
273 | 299 |
vL[1] = v[2*curr+1]; |
274 | 300 |
vR[0] = v[2*next]; |
... | ... | |
276 | 302 |
vT[0] = centers[component][0]; |
277 | 303 |
vT[1] = centers[component][1]; |
278 | 304 |
|
279 |
if( eupp<0 ) |
|
280 |
{ |
|
281 |
normL[0]=vL[0]-vT[0]; |
|
282 |
normL[1]=vL[1]-vT[1]; |
|
283 |
} |
|
284 |
else |
|
285 |
{ |
|
286 |
normL[0]= v[2*curr ] - v[2*prev ]; |
|
287 |
normL[1]= v[2*curr+1] - v[2*prev+1]; |
|
288 |
} |
|
305 |
int outerL = belongsToOuter(vL[0],vL[1]); |
|
306 |
int outerR = belongsToOuter(vR[0],vR[1]); |
|
289 | 307 |
|
290 |
if( eupn<0 ) |
|
291 |
{ |
|
292 |
normR[0]=vR[0]-vT[0]; |
|
293 |
normR[1]=vR[1]-vT[1]; |
|
294 |
} |
|
295 |
else |
|
296 |
{ |
|
297 |
int nnxt= next==len-1 ? 0 : next+1; |
|
298 |
normR[0]= v[2*next ] - v[2*nnxt ]; |
|
299 |
normR[1]= v[2*next+1] - v[2*nnxt+1]; |
|
300 |
} |
|
308 |
float[] vl = mOuterVectors[outerL]; |
|
309 |
float[] vr = mOuterVectors[outerR]; |
|
310 |
|
|
311 |
normL[0] = -vl[0]; |
|
312 |
normL[1] = -vl[1]; |
|
313 |
normR[0] = -vr[0]; |
|
314 |
normR[1] = -vr[1]; |
|
301 | 315 |
|
302 | 316 |
return MeshBandedTriangle.MODE_NORMAL; |
303 | 317 |
} |
... | ... | |
323 | 337 |
} |
324 | 338 |
else |
325 | 339 |
{ |
326 |
float dx=v[2*next ]-v[2*curr ]; |
|
327 |
float dy=v[2*next+1]-v[2*curr+1]; |
|
340 |
int outerT = belongsToOuter(vT[0],vT[1]); |
|
341 |
float[] vt = mOuterVectors[outerT]; |
|
342 |
float dx = vt[0]; |
|
343 |
float dy = vt[1]; |
|
328 | 344 |
|
329 | 345 |
normL[0]=dx; |
330 | 346 |
normL[1]=dy; |
... | ... | |
357 | 373 |
for(float[] vertex : vertices) numTriangles+=vertex.length/2; |
358 | 374 |
MeshBandedTriangle[] triangles = new MeshBandedTriangle[numTriangles]; |
359 | 375 |
|
376 |
computeOuterVertices(vertices); |
|
377 |
computeOuterVectors(); |
|
378 |
|
|
360 | 379 |
int[][] edgesUp = computeEdgesUp(vertices); |
361 | 380 |
boolean[][] vertsUp = computeVertsUp(vertices); |
362 | 381 |
float[][] centers = computeCenters(vertices); |
Also available in: Unified diff
Improve MeshMultigon (for the BandagedPyraminx case!)