1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.objectlib.helpers;
|
11
|
|
12
|
import org.distorted.library.effect.EffectName;
|
13
|
import org.distorted.library.effect.MatrixEffectMove;
|
14
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
15
|
import org.distorted.library.effect.MatrixEffectScale;
|
16
|
import org.distorted.library.effect.VertexEffect;
|
17
|
import org.distorted.library.main.QuatHelper;
|
18
|
import org.distorted.library.mesh.MeshBase;
|
19
|
import org.distorted.library.mesh.MeshJoined;
|
20
|
import org.distorted.library.mesh.MeshPolygon;
|
21
|
import org.distorted.library.type.Static3D;
|
22
|
import org.distorted.library.type.Static4D;
|
23
|
|
24
|
import java.util.ArrayList;
|
25
|
|
26
|
import static org.distorted.objectlib.main.TwistyObject.MESH_FAST;
|
27
|
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
|
31
|
public class FactoryCubit
|
32
|
{
|
33
|
private static FactoryCubit mThis;
|
34
|
|
35
|
private static final float MAX_CORE_DIFF = 0.01f;
|
36
|
|
37
|
private static final float[] mBuffer = new float[3];
|
38
|
private static final float[] mQuat1 = new float[4];
|
39
|
private static final float[] mQuat2 = new float[4];
|
40
|
private static final float[] mQuat3 = new float[4];
|
41
|
|
42
|
public static final String NAME = EffectName.DEFORM.name();
|
43
|
|
44
|
public static class StickerCoords
|
45
|
{
|
46
|
float[] vertices;
|
47
|
float scale;
|
48
|
boolean outer;
|
49
|
}
|
50
|
|
51
|
private static class FaceTransform
|
52
|
{
|
53
|
int face;
|
54
|
int numFaces;
|
55
|
|
56
|
int sticker;
|
57
|
float vx,vy,vz;
|
58
|
float scale;
|
59
|
float qx,qy,qz,qw;
|
60
|
}
|
61
|
|
62
|
private static final ArrayList<FaceTransform> mNewFaceTransf = new ArrayList<>();
|
63
|
private static final ArrayList<FaceTransform> mOldFaceTransf = new ArrayList<>();
|
64
|
private static final ArrayList<StickerCoords> mStickerCoords = new ArrayList<>();
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
private FactoryCubit()
|
69
|
{
|
70
|
|
71
|
}
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
// H - height of the band in the middle
|
75
|
// alpha - angle of the edge [0,90]
|
76
|
// dist - often in a polygon the distance from edge to center is not 1, but something else.
|
77
|
// This is the distance.
|
78
|
// K - where to begin the second, much more flat part of the band. [0,1]
|
79
|
// N - number of bands. N>=3
|
80
|
//
|
81
|
// theory: two distinct parts to the band:
|
82
|
// 1) (0,B) - steep
|
83
|
// 2) (B,1) - flat
|
84
|
//
|
85
|
// In first part, we have y = g(x) ; in second - y = g(f(x)) where
|
86
|
//
|
87
|
// g(x) = sqrt( R^2 - (x-D)^2 ) - R*cos(alpha)
|
88
|
// f(x) = ((D-B)/(1-B)*x + B*(1-D)/(1-B)
|
89
|
// h(x) = R*(sin(alpha) - sin(x))
|
90
|
// R = H/(1-cos(alpha))
|
91
|
// D = H*sin(alpha)
|
92
|
// B = h(K*alpha)
|
93
|
//
|
94
|
// The N points are taken at:
|
95
|
//
|
96
|
// 1) in the second part, there are K2 = (N-3)/3 such points
|
97
|
// 2) in the first - K1 = (N-3) - K2
|
98
|
// 3) also, the 3 points 0,B,1
|
99
|
//
|
100
|
// so we have the sequence A[i] of N points
|
101
|
//
|
102
|
// 0
|
103
|
// h((i+1)*(1-K)*alpha/(K1+1)) (i=0,1,...,K1-1)
|
104
|
// B
|
105
|
// (1-B)*(i+1)/(K2+1) + B (i=0,i,...,K2-1)
|
106
|
// 1
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
private float f(float D, float B, float x)
|
111
|
{
|
112
|
return ((D-B)*x + B*(1-D))/(1-B);
|
113
|
}
|
114
|
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
|
117
|
private float g(float R, float D, float x, float cosAlpha)
|
118
|
{
|
119
|
float d = x-D;
|
120
|
return (float)(Math.sqrt(R*R-d*d)-R*cosAlpha);
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
private float h(float R, float sinAlpha, float x)
|
126
|
{
|
127
|
return R*(sinAlpha-(float)Math.sin(x));
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
private boolean areColinear(float[][] vertices, int index1, int index2, int index3)
|
133
|
{
|
134
|
float x1 = vertices[index1][0];
|
135
|
float y1 = vertices[index1][1];
|
136
|
float z1 = vertices[index1][2];
|
137
|
float x2 = vertices[index2][0];
|
138
|
float y2 = vertices[index2][1];
|
139
|
float z2 = vertices[index2][2];
|
140
|
float x3 = vertices[index3][0];
|
141
|
float y3 = vertices[index3][1];
|
142
|
float z3 = vertices[index3][2];
|
143
|
|
144
|
float v1x = x2-x1;
|
145
|
float v1y = y2-y1;
|
146
|
float v1z = z2-z1;
|
147
|
float v2x = x3-x1;
|
148
|
float v2y = y3-y1;
|
149
|
float v2z = z3-z1;
|
150
|
|
151
|
double A = Math.sqrt( (v1x*v1x+v1y*v1y+v1z*v1z) / (v2x*v2x+v2y*v2y+v2z*v2z) );
|
152
|
|
153
|
return (v1x==A*v2x && v1y==A*v2y && v1z==A*v2z);
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
|
158
|
private void computeNormalVector(float[][] vertices, int index1, int index2, int index3)
|
159
|
{
|
160
|
float x1 = vertices[index1][0];
|
161
|
float y1 = vertices[index1][1];
|
162
|
float z1 = vertices[index1][2];
|
163
|
float x2 = vertices[index2][0];
|
164
|
float y2 = vertices[index2][1];
|
165
|
float z2 = vertices[index2][2];
|
166
|
float x3 = vertices[index3][0];
|
167
|
float y3 = vertices[index3][1];
|
168
|
float z3 = vertices[index3][2];
|
169
|
|
170
|
float v1x = x2-x1;
|
171
|
float v1y = y2-y1;
|
172
|
float v1z = z2-z1;
|
173
|
float v2x = x3-x1;
|
174
|
float v2y = y3-y1;
|
175
|
float v2z = z3-z1;
|
176
|
|
177
|
mBuffer[0] = v1y*v2z - v2y*v1z;
|
178
|
mBuffer[1] = v1z*v2x - v2z*v1x;
|
179
|
mBuffer[2] = v1x*v2y - v2x*v1y;
|
180
|
|
181
|
double len = mBuffer[0]*mBuffer[0] + mBuffer[1]*mBuffer[1] + mBuffer[2]*mBuffer[2];
|
182
|
len = Math.sqrt(len);
|
183
|
mBuffer[0] /= len;
|
184
|
mBuffer[1] /= len;
|
185
|
mBuffer[2] /= len;
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
|
190
|
private void fitInSquare(FaceTransform info, float[][] vert3D, boolean isOuter)
|
191
|
{
|
192
|
float minX = Float.MAX_VALUE;
|
193
|
float maxX =-Float.MAX_VALUE;
|
194
|
float minY = Float.MAX_VALUE;
|
195
|
float maxY =-Float.MAX_VALUE;
|
196
|
|
197
|
for (float[] vert : vert3D)
|
198
|
{
|
199
|
float x = vert[0];
|
200
|
float y = vert[1];
|
201
|
|
202
|
if (x > maxX) maxX = x;
|
203
|
if (x < minX) minX = x;
|
204
|
if (y > maxY) maxY = y;
|
205
|
if (y < minY) minY = y;
|
206
|
}
|
207
|
|
208
|
minX = minX<0 ? -minX:minX;
|
209
|
maxX = maxX<0 ? -maxX:maxX;
|
210
|
minY = minY<0 ? -minY:minY;
|
211
|
maxY = maxY<0 ? -maxY:maxY;
|
212
|
|
213
|
float max1 = Math.max(minX,minY);
|
214
|
float max2 = Math.max(maxX,maxY);
|
215
|
float max3 = Math.max(max1,max2);
|
216
|
|
217
|
info.scale = max3/0.5f;
|
218
|
|
219
|
int len = vert3D.length;
|
220
|
StickerCoords sInfo = new StickerCoords();
|
221
|
sInfo.outer = isOuter;
|
222
|
sInfo.scale = info.scale;
|
223
|
sInfo.vertices = new float[2*len];
|
224
|
|
225
|
for( int vertex=0; vertex<len; vertex++ )
|
226
|
{
|
227
|
sInfo.vertices[2*vertex ] = vert3D[vertex][0] / info.scale;
|
228
|
sInfo.vertices[2*vertex+1] = vert3D[vertex][1] / info.scale;
|
229
|
}
|
230
|
|
231
|
mStickerCoords.add(sInfo);
|
232
|
|
233
|
info.sticker = mStickerCoords.size() -1;
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
private FaceTransform constructNewTransform(final float[][] vert3D, boolean isOuter, int face, int numFaces)
|
239
|
{
|
240
|
FaceTransform ft = new FaceTransform();
|
241
|
ft.face = face;
|
242
|
ft.numFaces = numFaces;
|
243
|
|
244
|
// compute center of gravity
|
245
|
ft.vx = 0.0f;
|
246
|
ft.vy = 0.0f;
|
247
|
ft.vz = 0.0f;
|
248
|
int len = vert3D.length;
|
249
|
|
250
|
for (float[] vert : vert3D)
|
251
|
{
|
252
|
ft.vx += vert[0];
|
253
|
ft.vy += vert[1];
|
254
|
ft.vz += vert[2];
|
255
|
}
|
256
|
|
257
|
ft.vx /= len;
|
258
|
ft.vy /= len;
|
259
|
ft.vz /= len;
|
260
|
|
261
|
// move all vertices so that their center of gravity is at (0,0,0)
|
262
|
for (int i=0; i<len; i++)
|
263
|
{
|
264
|
vert3D[i][0] -= ft.vx;
|
265
|
vert3D[i][1] -= ft.vy;
|
266
|
vert3D[i][2] -= ft.vz;
|
267
|
}
|
268
|
|
269
|
// find 3 non-colinear vertices
|
270
|
int foundIndex = -1;
|
271
|
|
272
|
for(int vertex=2; vertex<len; vertex++)
|
273
|
{
|
274
|
if( !areColinear(vert3D,0,1,vertex) )
|
275
|
{
|
276
|
foundIndex = vertex;
|
277
|
break;
|
278
|
}
|
279
|
}
|
280
|
|
281
|
// compute the normal vector
|
282
|
if( foundIndex==-1 )
|
283
|
{
|
284
|
throw new RuntimeException("all vertices colinear");
|
285
|
}
|
286
|
|
287
|
computeNormalVector(vert3D,0,1,foundIndex);
|
288
|
|
289
|
// rotate so that the normal vector becomes (0,0,1)
|
290
|
float axisX, axisY, axisZ;
|
291
|
|
292
|
if( mBuffer[0]!=0.0f || mBuffer[1]!=0.0f )
|
293
|
{
|
294
|
axisX = -mBuffer[1];
|
295
|
axisY = mBuffer[0];
|
296
|
axisZ = 0.0f;
|
297
|
|
298
|
float axiLen = axisX*axisX + axisY*axisY;
|
299
|
axiLen = (float)Math.sqrt(axiLen);
|
300
|
axisX /= axiLen;
|
301
|
axisY /= axiLen;
|
302
|
axisZ /= axiLen;
|
303
|
}
|
304
|
else
|
305
|
{
|
306
|
axisX = 0.0f;
|
307
|
axisY = 1.0f;
|
308
|
axisZ = 0.0f;
|
309
|
}
|
310
|
|
311
|
float cosTheta = mBuffer[2];
|
312
|
float sinTheta = (float)Math.sqrt(1-cosTheta*cosTheta);
|
313
|
float sinHalfTheta = computeSinHalf(cosTheta);
|
314
|
float cosHalfTheta = computeCosHalf(sinTheta,cosTheta);
|
315
|
|
316
|
mQuat1[0] = axisX*sinHalfTheta;
|
317
|
mQuat1[1] = axisY*sinHalfTheta;
|
318
|
mQuat1[2] = axisZ*sinHalfTheta;
|
319
|
mQuat1[3] = cosHalfTheta;
|
320
|
mQuat2[0] =-axisX*sinHalfTheta;
|
321
|
mQuat2[1] =-axisY*sinHalfTheta;
|
322
|
mQuat2[2] =-axisZ*sinHalfTheta;
|
323
|
mQuat2[3] = cosHalfTheta;
|
324
|
|
325
|
for (float[] vert : vert3D)
|
326
|
{
|
327
|
QuatHelper.quatMultiply(mQuat3, mQuat1, vert );
|
328
|
QuatHelper.quatMultiply( vert, mQuat3, mQuat2);
|
329
|
}
|
330
|
|
331
|
// fit the whole thing in a square and remember the scale & 2D vertices
|
332
|
fitInSquare(ft, vert3D, isOuter);
|
333
|
|
334
|
// remember the rotation
|
335
|
ft.qx =-mQuat1[0];
|
336
|
ft.qy =-mQuat1[1];
|
337
|
ft.qz =-mQuat1[2];
|
338
|
ft.qw = mQuat1[3];
|
339
|
|
340
|
return ft;
|
341
|
}
|
342
|
|
343
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
344
|
|
345
|
private void rotateAllVertices(float[] result, int len, float[] vertices, float sin, float cos)
|
346
|
{
|
347
|
for(int i=0; i<len; i++)
|
348
|
{
|
349
|
result[2*i ] = vertices[2*i ]*cos - vertices[2*i+1]*sin;
|
350
|
result[2*i+1] = vertices[2*i ]*sin + vertices[2*i+1]*cos;
|
351
|
}
|
352
|
}
|
353
|
|
354
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
355
|
|
356
|
private float computeScale(float[] v1, float[] v2, int v1i, int v2i)
|
357
|
{
|
358
|
float v1x = v1[2*v1i];
|
359
|
float v1y = v1[2*v1i+1];
|
360
|
float v2x = v2[2*v2i];
|
361
|
float v2y = v2[2*v2i+1];
|
362
|
|
363
|
float lenSq1 = v1x*v1x + v1y*v1y;
|
364
|
float lenSq2 = v2x*v2x + v2y*v2y;
|
365
|
|
366
|
return (float)Math.sqrt(lenSq2/lenSq1);
|
367
|
}
|
368
|
|
369
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
370
|
// valid for 0<angle<2*PI
|
371
|
|
372
|
private float computeSinHalf(float cos)
|
373
|
{
|
374
|
return (float)Math.sqrt((1-cos)/2);
|
375
|
}
|
376
|
|
377
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
378
|
// valid for 0<angle<2*PI
|
379
|
|
380
|
private float computeCosHalf(float sin, float cos)
|
381
|
{
|
382
|
float cosHalf = (float)Math.sqrt((1+cos)/2);
|
383
|
return sin<0 ? -cosHalf : cosHalf;
|
384
|
}
|
385
|
|
386
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
387
|
|
388
|
private int computeRotatedIndex(int oldVertex, int len, int rotatedVertex)
|
389
|
{
|
390
|
int v = (rotatedVertex + oldVertex);
|
391
|
if( v>=len ) v-=len;
|
392
|
if( v< 0 ) v+=len;
|
393
|
|
394
|
return v;
|
395
|
}
|
396
|
|
397
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
398
|
|
399
|
private boolean isScaledVersionOf(float[] newVert, float[] oldVert, int len, int vertex)
|
400
|
{
|
401
|
int newZeroIndex = computeRotatedIndex(0,len,vertex);
|
402
|
float EPSILON = 0.001f;
|
403
|
float scale = computeScale(newVert,oldVert,newZeroIndex,0);
|
404
|
|
405
|
for(int i=1; i<len; i++)
|
406
|
{
|
407
|
int index = computeRotatedIndex(i,len,vertex);
|
408
|
|
409
|
float horz = oldVert[2*i ] - scale*newVert[2*index ];
|
410
|
float vert = oldVert[2*i+1] - scale*newVert[2*index+1];
|
411
|
|
412
|
if( horz>EPSILON || horz<-EPSILON || vert>EPSILON || vert<-EPSILON ) return false;
|
413
|
}
|
414
|
|
415
|
return true;
|
416
|
}
|
417
|
|
418
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
419
|
|
420
|
private void correctInfo(FaceTransform info, float scale, float sin, float cos, int oldSticker)
|
421
|
{
|
422
|
mStickerCoords.remove(info.sticker);
|
423
|
|
424
|
info.sticker = oldSticker;
|
425
|
info.scale *= scale;
|
426
|
|
427
|
mQuat1[0] = info.qx;
|
428
|
mQuat1[1] = info.qy;
|
429
|
mQuat1[2] = info.qz;
|
430
|
mQuat1[3] = info.qw;
|
431
|
|
432
|
float sinHalf = computeSinHalf(cos);
|
433
|
float cosHalf = computeCosHalf(sin,cos);
|
434
|
|
435
|
mQuat2[0] = 0.0f;
|
436
|
mQuat2[1] = 0.0f;
|
437
|
mQuat2[2] = sinHalf;
|
438
|
mQuat2[3] = cosHalf;
|
439
|
|
440
|
QuatHelper.quatMultiply( mQuat3, mQuat1, mQuat2 );
|
441
|
|
442
|
info.qx = mQuat3[0];
|
443
|
info.qy = mQuat3[1];
|
444
|
info.qz = mQuat3[2];
|
445
|
info.qw = mQuat3[3];
|
446
|
}
|
447
|
|
448
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
449
|
|
450
|
private void printVert(double[] buffer)
|
451
|
{
|
452
|
int len = buffer.length/2;
|
453
|
String str = "";
|
454
|
|
455
|
for(int i=0; i<len; i++)
|
456
|
{
|
457
|
str += (" ("+buffer[2*i]+" , "+buffer[2*i+1]+" ) ");
|
458
|
}
|
459
|
|
460
|
android.util.Log.d("D", str);
|
461
|
}
|
462
|
|
463
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
464
|
|
465
|
private boolean foundVertex(FaceTransform info, float[] buffer, int len, float[] newVert, float[] oldVert, int oldSticker)
|
466
|
{
|
467
|
int lenVertOld = oldVert.length/2;
|
468
|
float lenOld=0.0f, oldX=0.0f, oldY=0.0f;
|
469
|
|
470
|
for(int oldV=0; oldV<lenVertOld; oldV++)
|
471
|
{
|
472
|
oldX = oldVert[2*oldV];
|
473
|
oldY = oldVert[2*oldV+1];
|
474
|
lenOld = (float)Math.sqrt(oldX*oldX + oldY*oldY);
|
475
|
|
476
|
if( lenOld!=0 ) break;
|
477
|
}
|
478
|
|
479
|
for(int vertex=0; vertex<len; vertex++)
|
480
|
{
|
481
|
float newX = newVert[2*vertex ];
|
482
|
float newY = newVert[2*vertex+1];
|
483
|
float lenNew = (float)Math.sqrt(newX*newX + newY*newY);
|
484
|
|
485
|
if( lenNew!=0 )
|
486
|
{
|
487
|
float cos = (float)QuatHelper.computeCos( oldX, oldY, newX, newY, lenNew, lenOld);
|
488
|
float sin = (float)QuatHelper.computeSin( oldX, oldY, newX, newY, lenNew, lenOld);
|
489
|
|
490
|
rotateAllVertices(buffer,len,newVert,sin,cos);
|
491
|
|
492
|
if( isScaledVersionOf(buffer,oldVert,len,vertex) )
|
493
|
{
|
494
|
int newZeroIndex = computeRotatedIndex(0,len,vertex);
|
495
|
float scale = computeScale(oldVert,newVert,0,newZeroIndex);
|
496
|
correctInfo(info,scale,sin,cos,oldSticker);
|
497
|
return true;
|
498
|
}
|
499
|
}
|
500
|
}
|
501
|
|
502
|
return false;
|
503
|
}
|
504
|
|
505
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
506
|
|
507
|
private float computeCoreDistance(float[] verts)
|
508
|
{
|
509
|
float ret = 0.0f;
|
510
|
float centerX=0.0f,centerY=0.0f;
|
511
|
int len = verts.length/2;
|
512
|
|
513
|
for(int i=0; i<len; i++)
|
514
|
{
|
515
|
centerX += verts[2*i ];
|
516
|
centerY += verts[2*i+1];
|
517
|
}
|
518
|
|
519
|
centerX /= (2*len);
|
520
|
centerY /= (2*len);
|
521
|
|
522
|
for(int i=0; i<len; i++)
|
523
|
{
|
524
|
float distX = centerX-verts[2*i ];
|
525
|
float distY = centerY-verts[2*i+1];
|
526
|
ret += (float)Math.sqrt(distX*distX + distY*distY);
|
527
|
}
|
528
|
|
529
|
return ret;
|
530
|
}
|
531
|
|
532
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
533
|
|
534
|
private boolean successfullyCollapsedStickers(final FaceTransform newInfo, final FaceTransform oldInfo)
|
535
|
{
|
536
|
StickerCoords sNewInfo = mStickerCoords.get(newInfo.sticker);
|
537
|
StickerCoords sOldInfo = mStickerCoords.get(oldInfo.sticker);
|
538
|
|
539
|
float[] newVert = sNewInfo.vertices;
|
540
|
float[] oldVert = sOldInfo.vertices;
|
541
|
int oldLen = oldVert.length;
|
542
|
int newLen = newVert.length;
|
543
|
|
544
|
if( oldLen==newLen )
|
545
|
{
|
546
|
float coreDistOld = computeCoreDistance(oldVert); // the two stickers are at different scales,
|
547
|
float coreDistNew = computeCoreDistance(newVert); // so even if they are in fact the same, do not
|
548
|
float diff = (coreDistOld*oldInfo.scale)/(coreDistNew*newInfo.scale); // collapse them into one. Example: Master Skewb
|
549
|
if( diff<1.0-MAX_CORE_DIFF || diff>1.0+MAX_CORE_DIFF ) return false; // and two triangular stickers of different size.
|
550
|
|
551
|
int oldSticker = oldInfo.sticker;
|
552
|
float[] buffer1 = new float[oldLen];
|
553
|
|
554
|
if( foundVertex(newInfo, buffer1, oldLen/2, newVert, oldVert, oldSticker) )
|
555
|
{
|
556
|
if( sNewInfo.outer ) sOldInfo.outer = true;
|
557
|
return true;
|
558
|
}
|
559
|
}
|
560
|
|
561
|
return false;
|
562
|
}
|
563
|
|
564
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
565
|
|
566
|
private float[][] constructVert(float[][] vertices, int[] index)
|
567
|
{
|
568
|
int len = index.length;
|
569
|
float[][] ret = new float[len][4];
|
570
|
|
571
|
for(int i=0; i<len; i++)
|
572
|
{
|
573
|
float[] tmp = vertices[index[i]];
|
574
|
ret[i][0] = tmp[0];
|
575
|
ret[i][1] = tmp[1];
|
576
|
ret[i][2] = tmp[2];
|
577
|
ret[i][3] = 1.0f;
|
578
|
}
|
579
|
|
580
|
return ret;
|
581
|
}
|
582
|
|
583
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
584
|
|
585
|
private void applyVertexEffects(MeshBase mesh, ObjectVertexEffects effects, int meshState)
|
586
|
{
|
587
|
boolean[] uses = effects.getUses();
|
588
|
String[] names = effects.getNames();
|
589
|
float[][] variables = effects.getVariables();
|
590
|
float[][] centers = effects.getCenters();
|
591
|
float[][] regions = effects.getRegions();
|
592
|
int numEffects = uses.length;
|
593
|
|
594
|
for(int eff=0; eff<numEffects; eff++)
|
595
|
if( names[eff]!=null && (meshState==MESH_NICE || uses[eff]) )
|
596
|
{
|
597
|
VertexEffect effect = VertexEffect.constructEffect(names[eff],variables[eff],centers[eff],regions[eff]);
|
598
|
if( effect!=null ) mesh.apply(effect);
|
599
|
}
|
600
|
}
|
601
|
|
602
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
603
|
|
604
|
private void correctComponents(MeshBase mesh, int numComponents)
|
605
|
{
|
606
|
int numTexToBeAdded = numComponents-mesh.getNumTexComponents();
|
607
|
|
608
|
mesh.mergeEffComponents();
|
609
|
|
610
|
for(int i=0; i<numTexToBeAdded; i++ ) mesh.addEmptyTexComponent();
|
611
|
}
|
612
|
|
613
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
614
|
|
615
|
private void printTransform(FaceTransform f)
|
616
|
{
|
617
|
android.util.Log.e("D", "face="+f.face+" q=("+f.qx+", "+f.qy+", "+f.qz+", "+f.qw+") v=("
|
618
|
+f.vx+", "+f.vy+", "+f.vz+") scale="+f.scale+" sticker="+f.sticker);
|
619
|
}
|
620
|
|
621
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
622
|
|
623
|
private float[] computeBands(float H, int alpha, float dist, float K, int N)
|
624
|
{
|
625
|
float[] bands = new float[2*N];
|
626
|
|
627
|
bands[0] = 1.0f;
|
628
|
bands[1] = 0.0f;
|
629
|
|
630
|
float beta = (float)Math.atan(dist*Math.tan(Math.PI*alpha/180));
|
631
|
float sinBeta = (float)Math.sin(beta);
|
632
|
float cosBeta = (float)Math.cos(beta);
|
633
|
float R = cosBeta<1.0f ? H/(1.0f-cosBeta) : 0.0f;
|
634
|
float D = R*sinBeta;
|
635
|
float B = h(R,sinBeta,K*beta);
|
636
|
|
637
|
if( D>1.0f )
|
638
|
{
|
639
|
for(int i=1; i<N; i++)
|
640
|
{
|
641
|
bands[2*i ] = (float)(N-1-i)/(N-1);
|
642
|
bands[2*i+1] = H*(1-bands[2*i]);
|
643
|
}
|
644
|
}
|
645
|
else
|
646
|
{
|
647
|
int K2 = (int)((N-3)*K);
|
648
|
int K1 = (N-3)-K2;
|
649
|
|
650
|
for(int i=0; i<=K1; i++)
|
651
|
{
|
652
|
float angle = K*beta + (1-K)*beta*(K1-i)/(K1+1);
|
653
|
float x = h(R,sinBeta,angle);
|
654
|
bands[2*i+2] = 1.0f - x;
|
655
|
bands[2*i+3] = g(R,D,x,cosBeta);
|
656
|
}
|
657
|
|
658
|
for(int i=0; i<=K2; i++)
|
659
|
{
|
660
|
float x = (1-B)*(i+1)/(K2+1) + B;
|
661
|
bands[2*K1+2 + 2*i+2] = 1.0f - x;
|
662
|
bands[2*K1+2 + 2*i+3] = g(R,D,f(D,B,x),cosBeta);
|
663
|
}
|
664
|
}
|
665
|
|
666
|
bands[2*N-2] = 0.0f;
|
667
|
bands[2*N-1] = H;
|
668
|
|
669
|
return bands;
|
670
|
}
|
671
|
|
672
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
673
|
|
674
|
private void computeConvexityCenter(float[] out, float[] in, FaceTransform ft)
|
675
|
{
|
676
|
if( in==null )
|
677
|
{
|
678
|
out[0] = out[1] = 0.0f;
|
679
|
}
|
680
|
else
|
681
|
{
|
682
|
out[0] = in[0] - ft.vx;
|
683
|
out[1] = in[1] - ft.vy;
|
684
|
out[2] = in[2] - ft.vz;
|
685
|
out[3] = 1.0f;
|
686
|
|
687
|
mQuat1[0] =-ft.qx;
|
688
|
mQuat1[1] =-ft.qy;
|
689
|
mQuat1[2] =-ft.qz;
|
690
|
mQuat1[3] = ft.qw;
|
691
|
|
692
|
mQuat2[0] = -mQuat1[0];
|
693
|
mQuat2[1] = -mQuat1[1];
|
694
|
mQuat2[2] = -mQuat1[2];
|
695
|
mQuat2[3] = mQuat1[3];
|
696
|
|
697
|
QuatHelper.quatMultiply( mQuat3, mQuat1, out);
|
698
|
QuatHelper.quatMultiply( out, mQuat3, mQuat2);
|
699
|
|
700
|
out[0] /= ft.scale;
|
701
|
out[1] /= ft.scale;
|
702
|
out[2] /= ft.scale;
|
703
|
}
|
704
|
}
|
705
|
|
706
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
707
|
|
708
|
private void changeStickerPointers(int[][] table, int oldPointer, int newPointer)
|
709
|
{
|
710
|
int len = table.length;
|
711
|
|
712
|
for(int i=0; i<len; i++)
|
713
|
{
|
714
|
int lenInner = table[i].length;
|
715
|
|
716
|
for(int j=0; j<lenInner; j++)
|
717
|
if( table[i][j]==oldPointer ) table[i][j] = newPointer;
|
718
|
}
|
719
|
}
|
720
|
|
721
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
722
|
// INTERNAL API
|
723
|
|
724
|
public int printStickerCoords()
|
725
|
{
|
726
|
int stickers = mStickerCoords.size();
|
727
|
int ret = 0;
|
728
|
|
729
|
android.util.Log.d("D", "---- STICKER COORDS ----");
|
730
|
|
731
|
for(int s=0; s<stickers; s++)
|
732
|
{
|
733
|
StickerCoords info = mStickerCoords.get(s);
|
734
|
|
735
|
if( info.outer ) ret++;
|
736
|
|
737
|
String ver = (info.outer?"OUTER":"INNER")+" scale: "+info.scale+" { ";
|
738
|
int len = info.vertices.length/2;
|
739
|
|
740
|
for(int i =0; i<len; i++)
|
741
|
{
|
742
|
if( i!=0 ) ver += ", ";
|
743
|
ver += ( info.vertices[2*i]+"f, "+info.vertices[2*i+1]+"f");
|
744
|
}
|
745
|
|
746
|
ver += " }";
|
747
|
android.util.Log.d("D", ver);
|
748
|
}
|
749
|
|
750
|
android.util.Log.d("D", "---- END STICKER COORDS ----");
|
751
|
|
752
|
return ret;
|
753
|
}
|
754
|
|
755
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
756
|
|
757
|
public void printFaceTransform()
|
758
|
{
|
759
|
android.util.Log.d("D", "---- OLD FACE TRANSFORM ---");
|
760
|
|
761
|
int oldfaces = mOldFaceTransf.size();
|
762
|
|
763
|
for(int f=0; f<oldfaces; f++)
|
764
|
{
|
765
|
printTransform(mOldFaceTransf.get(f));
|
766
|
}
|
767
|
|
768
|
android.util.Log.d("D", "---- NEW FACE TRANSFORM ---");
|
769
|
|
770
|
int newfaces = mNewFaceTransf.size();
|
771
|
|
772
|
for(int f=0; f<newfaces; f++)
|
773
|
{
|
774
|
printTransform(mNewFaceTransf.get(f));
|
775
|
}
|
776
|
}
|
777
|
|
778
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
779
|
// PUBLIC API
|
780
|
|
781
|
public static FactoryCubit getInstance()
|
782
|
{
|
783
|
if( mThis==null ) mThis = new FactoryCubit();
|
784
|
|
785
|
return mThis;
|
786
|
}
|
787
|
|
788
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
789
|
|
790
|
public static ObjectVertexEffects generateVertexEffect( float[][] vertices,
|
791
|
float[][] corners, int[] cornerIndices,
|
792
|
float[][] centers, int[] centerIndices )
|
793
|
{
|
794
|
int numVerts = vertices.length;
|
795
|
String[] names = new String[numVerts];
|
796
|
float[][] vars = new float[numVerts][];
|
797
|
float[][] cents= new float[numVerts][];
|
798
|
float[][] regs = new float[numVerts][];
|
799
|
boolean[] uses = new boolean[numVerts];
|
800
|
|
801
|
for(int i=0; i<numVerts; i++)
|
802
|
{
|
803
|
int centerI = centerIndices[i];
|
804
|
int cornerI = cornerIndices[i];
|
805
|
|
806
|
if( centerI>=0 && cornerI>=0 )
|
807
|
{
|
808
|
float[] ce = centers[centerI];
|
809
|
float[] ve = vertices[i];
|
810
|
float S = corners[cornerI][0];
|
811
|
float R = corners[cornerI][1];
|
812
|
|
813
|
float CX = ve[0];
|
814
|
float CY = ve[1];
|
815
|
float CZ = ve[2];
|
816
|
float X = S*(ce[0]-CX);
|
817
|
float Y = S*(ce[1]-CY);
|
818
|
float Z = S*(ce[2]-CZ);
|
819
|
|
820
|
names[i]= NAME;
|
821
|
vars[i] = new float[] { 0, X,Y,Z, 1 };
|
822
|
cents[i]= new float[] { CX, CY, CZ };
|
823
|
regs[i] = new float[] { 0,0,0, R };
|
824
|
uses[i] = false;
|
825
|
}
|
826
|
}
|
827
|
|
828
|
return new ObjectVertexEffects(names,vars,cents,regs,uses);
|
829
|
}
|
830
|
|
831
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
832
|
|
833
|
public float[] getStickerScales()
|
834
|
{
|
835
|
int index=0,num=0,len = mStickerCoords.size();
|
836
|
|
837
|
for(int i=0; i<len; i++) if( mStickerCoords.get(i).outer ) num++;
|
838
|
|
839
|
if( num>0 )
|
840
|
{
|
841
|
float[] scales = new float[num];
|
842
|
|
843
|
for(int i=0; i<len; i++)
|
844
|
{
|
845
|
StickerCoords sticker = mStickerCoords.get(i);
|
846
|
if( sticker.outer ) scales[index++] = sticker.scale;
|
847
|
}
|
848
|
|
849
|
return scales;
|
850
|
}
|
851
|
|
852
|
return null;
|
853
|
}
|
854
|
|
855
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
856
|
|
857
|
public float[][] getStickerCoords()
|
858
|
{
|
859
|
int index=0,num=0,len = mStickerCoords.size();
|
860
|
|
861
|
for(int i=0; i<len; i++) if( mStickerCoords.get(i).outer ) num++;
|
862
|
|
863
|
if( num>0 )
|
864
|
{
|
865
|
float[][] coords = new float[num][];
|
866
|
|
867
|
for(int i=0; i<len; i++)
|
868
|
{
|
869
|
StickerCoords sticker = mStickerCoords.get(i);
|
870
|
if( sticker.outer ) coords[index++] = sticker.vertices;
|
871
|
}
|
872
|
|
873
|
return coords;
|
874
|
}
|
875
|
|
876
|
return null;
|
877
|
}
|
878
|
|
879
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
880
|
|
881
|
public int[][] getStickerVariants()
|
882
|
{
|
883
|
int numvariants = 1; // there's one in the 'new' array
|
884
|
|
885
|
int oldfaces = mOldFaceTransf.size();
|
886
|
|
887
|
for(int f=0; f<oldfaces; f++)
|
888
|
{
|
889
|
FaceTransform ft = mOldFaceTransf.get(f);
|
890
|
if( ft.face==0 ) numvariants++;
|
891
|
}
|
892
|
|
893
|
int[][] ret = new int[numvariants][];
|
894
|
int inner=0, index=-1;
|
895
|
|
896
|
for(int f=0; f<oldfaces; f++)
|
897
|
{
|
898
|
FaceTransform ft = mOldFaceTransf.get(f);
|
899
|
if( ft.face==0 )
|
900
|
{
|
901
|
index++;
|
902
|
inner=0;
|
903
|
ret[index] = new int[ft.numFaces];
|
904
|
}
|
905
|
|
906
|
ret[index][inner++] = ft.sticker;
|
907
|
}
|
908
|
|
909
|
int newfaces = mNewFaceTransf.size();
|
910
|
|
911
|
for(int f=0; f<newfaces; f++)
|
912
|
{
|
913
|
FaceTransform ft = mNewFaceTransf.get(f);
|
914
|
if( ft.face==0 )
|
915
|
{
|
916
|
index++;
|
917
|
inner=0;
|
918
|
ret[index] = new int[ft.numFaces];
|
919
|
}
|
920
|
|
921
|
ret[index][inner++] = ft.sticker;
|
922
|
}
|
923
|
|
924
|
int numStickers = mStickerCoords.size();
|
925
|
int numOuter=0;
|
926
|
|
927
|
for(int i=0; i<numStickers; i++)
|
928
|
{
|
929
|
StickerCoords sc = mStickerCoords.get(i);
|
930
|
if( sc.outer )
|
931
|
{
|
932
|
changeStickerPointers(ret,i,numOuter);
|
933
|
numOuter++;
|
934
|
}
|
935
|
else
|
936
|
{
|
937
|
changeStickerPointers(ret,i,-1);
|
938
|
}
|
939
|
}
|
940
|
|
941
|
return ret;
|
942
|
}
|
943
|
|
944
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
945
|
|
946
|
public void clear()
|
947
|
{
|
948
|
mStickerCoords.clear();
|
949
|
mNewFaceTransf.clear();
|
950
|
mOldFaceTransf.clear();
|
951
|
}
|
952
|
|
953
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
954
|
// This is for FactoryBandaged3x3Cubit. We need to know which direction each face faces.
|
955
|
// This assumes the factory has just been cleared and 'createNewFaceTransform' has just
|
956
|
// been called.
|
957
|
|
958
|
public Static4D getQuaternion(int face)
|
959
|
{
|
960
|
FaceTransform ft = mNewFaceTransf.get(face);
|
961
|
return ft!=null ? new Static4D(ft.qx,ft.qy,ft.qz,ft.qw) : null;
|
962
|
}
|
963
|
|
964
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
965
|
|
966
|
public void createNewFaceTransform(final ObjectShape shape, int[] outer)
|
967
|
{
|
968
|
float[][] vertices = shape.getVertices();
|
969
|
int[][] indices = shape.getVertIndices();
|
970
|
FaceTransform ft;
|
971
|
int numNew = mNewFaceTransf.size();
|
972
|
|
973
|
for(int i=0; i<numNew; i++)
|
974
|
{
|
975
|
ft = mNewFaceTransf.remove(0);
|
976
|
mOldFaceTransf.add(ft);
|
977
|
}
|
978
|
|
979
|
int numFaces = indices.length;
|
980
|
int numOld = mOldFaceTransf.size();
|
981
|
|
982
|
for (int face=0; face<numFaces; face++)
|
983
|
{
|
984
|
boolean collapsed = false;
|
985
|
boolean isOuter = (outer!=null && outer[face]>0);
|
986
|
float[][] vert = constructVert(vertices, indices[face]);
|
987
|
FaceTransform newT = constructNewTransform(vert,isOuter,face,numFaces);
|
988
|
|
989
|
for (int old=0; !collapsed && old<numOld; old++)
|
990
|
{
|
991
|
ft = mOldFaceTransf.get(old);
|
992
|
if (successfullyCollapsedStickers(newT, ft)) collapsed = true;
|
993
|
}
|
994
|
|
995
|
for (int pre=0; !collapsed && pre<face; pre++)
|
996
|
{
|
997
|
ft = mNewFaceTransf.get(pre);
|
998
|
if (successfullyCollapsedStickers(newT, ft)) collapsed = true;
|
999
|
}
|
1000
|
|
1001
|
mNewFaceTransf.add(newT);
|
1002
|
}
|
1003
|
}
|
1004
|
|
1005
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1006
|
|
1007
|
public MeshBase createRoundedSolid(final ObjectShape shape, final ObjectFaceShape faceShape,
|
1008
|
final ObjectVertexEffects effects, int meshState, int numComponents)
|
1009
|
{
|
1010
|
int[][] vertIndexes = shape.getVertIndices();
|
1011
|
float[][] bands = faceShape.getBands();
|
1012
|
int[] bandIndexes = faceShape.getBandIndices();
|
1013
|
float[] convexityCenter = faceShape.getConvexityCenter();
|
1014
|
|
1015
|
int numFaces = vertIndexes.length;
|
1016
|
float[] band, bandsComputed;
|
1017
|
MeshBase[] meshes = new MeshBase[numFaces];
|
1018
|
FaceTransform fInfo;
|
1019
|
StickerCoords sInfo;
|
1020
|
float[] convexXY = new float[4];
|
1021
|
int exIndex=0, exVertices=0, alpha=0, N=0;
|
1022
|
float height=0.0f, dist=0.0f, K=0.0f;
|
1023
|
|
1024
|
for(int face=0; face<numFaces; face++)
|
1025
|
{
|
1026
|
fInfo = mNewFaceTransf.get(face);
|
1027
|
sInfo = mStickerCoords.get(fInfo.sticker);
|
1028
|
|
1029
|
float[] verts = sInfo.vertices;
|
1030
|
int lenVerts = verts.length;
|
1031
|
float[] copiedVerts = new float[lenVerts];
|
1032
|
System.arraycopy(verts, 0, copiedVerts, 0, lenVerts);
|
1033
|
|
1034
|
computeConvexityCenter(convexXY,convexityCenter,fInfo);
|
1035
|
|
1036
|
int index = bandIndexes[face];
|
1037
|
band = bands[index];
|
1038
|
|
1039
|
switch(meshState)
|
1040
|
{
|
1041
|
case MESH_NICE: height = band[0];
|
1042
|
alpha = (int)band[1];
|
1043
|
dist = band[2];
|
1044
|
K = band[3];
|
1045
|
N = (int)band[4];
|
1046
|
exIndex = (int)band[5];
|
1047
|
exVertices = (int)band[6];
|
1048
|
break;
|
1049
|
case MESH_FAST: height = band[0]<0 ? band[0] : 0; // the negative heights are of the internal walls, leave that
|
1050
|
// (example: Ivy cube center and edge cubits!)
|
1051
|
alpha = 0;
|
1052
|
dist = 0;
|
1053
|
K = 0;
|
1054
|
N = 2;
|
1055
|
exIndex = 0;
|
1056
|
exVertices = 0;
|
1057
|
break;
|
1058
|
}
|
1059
|
|
1060
|
bandsComputed = computeBands(height,alpha,dist,K,N);
|
1061
|
meshes[face] = new MeshPolygon(copiedVerts,bandsComputed,exIndex,exVertices, convexXY[0], convexXY[1]);
|
1062
|
meshes[face].setEffectAssociation(0,0,face);
|
1063
|
}
|
1064
|
|
1065
|
MeshBase mesh = new MeshJoined(meshes);
|
1066
|
Static3D center = new Static3D(0,0,0);
|
1067
|
|
1068
|
for(int face=0; face<numFaces; face++)
|
1069
|
{
|
1070
|
fInfo = mNewFaceTransf.get(face);
|
1071
|
|
1072
|
float vx = fInfo.vx;
|
1073
|
float vy = fInfo.vy;
|
1074
|
float vz = fInfo.vz;
|
1075
|
float sc = fInfo.scale;
|
1076
|
float qx = fInfo.qx;
|
1077
|
float qy = fInfo.qy;
|
1078
|
float qz = fInfo.qz;
|
1079
|
float qw = fInfo.qw;
|
1080
|
|
1081
|
Static3D scale = new Static3D(sc,sc,sc);
|
1082
|
Static3D move3D= new Static3D(vx,vy,vz);
|
1083
|
Static4D quat = new Static4D(qx,qy,qz,qw);
|
1084
|
|
1085
|
mesh.apply(new MatrixEffectScale(scale) ,0,face);
|
1086
|
mesh.apply(new MatrixEffectQuaternion(quat,center),0,face);
|
1087
|
mesh.apply(new MatrixEffectMove(move3D) ,0,face);
|
1088
|
}
|
1089
|
|
1090
|
correctComponents(mesh,numComponents);
|
1091
|
if( effects!=null ) applyVertexEffects(mesh,effects,meshState);
|
1092
|
|
1093
|
return mesh;
|
1094
|
}
|
1095
|
}
|