Revision 7e9a35eb
Added by Leszek Koltunski 11 months ago
src/main/java/org/distorted/objectlib/helpers/FactoryCubit.java | ||
---|---|---|
44 | 44 |
|
45 | 45 |
private static class StickerCoords |
46 | 46 |
{ |
47 |
float[] vertices; |
|
47 |
float[][] vertices;
|
|
48 | 48 |
float[][] fullVertices; |
49 | 49 |
float scale; |
50 | 50 |
boolean outer; |
... | ... | |
131 | 131 |
|
132 | 132 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
133 | 133 |
|
134 |
private boolean areColinear(float[][] vertices, int index1, int index2, int index3) |
|
134 |
private boolean areNotColinear(float[][] vertices, int index1, int index2, int index3)
|
|
135 | 135 |
{ |
136 | 136 |
float x1 = vertices[index1][0]; |
137 | 137 |
float y1 = vertices[index1][1]; |
... | ... | |
152 | 152 |
|
153 | 153 |
double A = Math.sqrt( (v1x*v1x+v1y*v1y+v1z*v1z) / (v2x*v2x+v2y*v2y+v2z*v2z) ); |
154 | 154 |
|
155 |
return (v1x==A*v2x && v1y==A*v2y && v1z==A*v2z); |
|
155 |
return !(v1x==A*v2x && v1y==A*v2y && v1z==A*v2z);
|
|
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
191 |
|
|
191 |
/* |
|
192 | 192 |
private float[] detectFirstOuterVertex(float[][] vertices) |
193 | 193 |
{ |
194 | 194 |
float X = -Float.MAX_VALUE; |
... | ... | |
265 | 265 |
|
266 | 266 |
return new float[] {x,y}; |
267 | 267 |
} |
268 |
|
|
268 |
*/ |
|
269 | 269 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
270 | 270 |
// same as in MeshMultigon |
271 | 271 |
|
272 |
private float[] computeOuterEdge(float[][] vertices) |
|
272 |
private float[][] computeOuterEdge(float[][] vertices)
|
|
273 | 273 |
{ |
274 |
int[][] edgesUp = MeshMultigon.computeEdgesUp(vertices); |
|
275 |
float[][][] tmp = MeshMultigon.computeOuterAndHoleVertices(vertices,edgesUp); |
|
276 |
return tmp[0]; |
|
277 |
/* |
|
274 | 278 |
ArrayList<float[]> tmp = new ArrayList<>(); |
275 | 279 |
|
276 | 280 |
float[] vect = new float[] {1,0}; |
... | ... | |
298 | 302 |
} |
299 | 303 |
|
300 | 304 |
return ret; |
305 |
*/ |
|
301 | 306 |
} |
302 | 307 |
|
303 | 308 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
336 | 341 |
StickerCoords sInfo = new StickerCoords(); |
337 | 342 |
sInfo.outer = isOuter; |
338 | 343 |
sInfo.scale = info.scale; |
339 |
sInfo.vertices = new float[2*len];
|
|
344 |
sInfo.vertices = new float[len][2];
|
|
340 | 345 |
sInfo.fullVertices = null; |
341 | 346 |
|
342 | 347 |
for( int vertex=0; vertex<len; vertex++ ) |
343 | 348 |
{ |
344 |
sInfo.vertices[2*vertex ] = vert3D[vertex][0] / info.scale;
|
|
345 |
sInfo.vertices[2*vertex+1] = vert3D[vertex][1] / info.scale;
|
|
349 |
sInfo.vertices[vertex][0] = vert3D[vertex][0] / info.scale;
|
|
350 |
sInfo.vertices[vertex][1] = vert3D[vertex][1] / info.scale;
|
|
346 | 351 |
} |
347 | 352 |
|
348 | 353 |
mStickerCoords.add(sInfo); |
... | ... | |
450 | 455 |
|
451 | 456 |
for(int vertex=2; vertex<len; vertex++) |
452 | 457 |
{ |
453 |
if( !areColinear(vert3D,0,1,vertex) )
|
|
458 |
if( areNotColinear(vert3D,0,1,vertex) )
|
|
454 | 459 |
{ |
455 | 460 |
foundIndex = vertex; |
456 | 461 |
break; |
... | ... | |
577 | 582 |
|
578 | 583 |
for(int vertex=2; vertex<len; vertex++) |
579 | 584 |
{ |
580 |
if( !areColinear(vert3D[0],0,1,vertex) )
|
|
585 |
if( areNotColinear(vert3D[0],0,1,vertex) )
|
|
581 | 586 |
{ |
582 | 587 |
foundIndex = vertex; |
583 | 588 |
break; |
... | ... | |
664 | 669 |
|
665 | 670 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
666 | 671 |
|
667 |
private void rotateAllVertices(float[] result, int len, float[] vertices, float sin, float cos)
|
|
672 |
private void rotateAllVertices(float[][] result, int len, float[][] vertices, float sin, float cos)
|
|
668 | 673 |
{ |
669 | 674 |
for(int i=0; i<len; i++) |
670 | 675 |
{ |
671 |
float x = vertices[2*i];
|
|
672 |
float y = vertices[2*i+1];
|
|
673 |
result[2*i ] = x*cos - y*sin;
|
|
674 |
result[2*i+1] = x*sin + y*cos;
|
|
676 |
float x = vertices[i][0];
|
|
677 |
float y = vertices[i][1];
|
|
678 |
result[i][0] = x*cos - y*sin;
|
|
679 |
result[i][1] = x*sin + y*cos;
|
|
675 | 680 |
} |
676 | 681 |
} |
677 | 682 |
|
678 | 683 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
679 | 684 |
|
680 |
private float computeScale(float[] v1, float[] v2, int v1i, int v2i)
|
|
685 |
private float computeScale(float[][] v1, float[][] v2, int v1i, int v2i)
|
|
681 | 686 |
{ |
682 |
float v1x = v1[2*v1i];
|
|
683 |
float v1y = v1[2*v1i+1];
|
|
684 |
float v2x = v2[2*v2i];
|
|
685 |
float v2y = v2[2*v2i+1];
|
|
687 |
float v1x = v1[v1i][0];
|
|
688 |
float v1y = v1[v1i][1];
|
|
689 |
float v2x = v2[v2i][0];
|
|
690 |
float v2y = v2[v2i][1];
|
|
686 | 691 |
|
687 | 692 |
float lenSq1 = v1x*v1x + v1y*v1y; |
688 | 693 |
float lenSq2 = v2x*v2x + v2y*v2y; |
... | ... | |
720 | 725 |
|
721 | 726 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
722 | 727 |
|
723 |
private boolean isScaledVersionOf(float[] newVert, float[] oldVert, int len, int vertex)
|
|
728 |
private boolean isScaledVersionOf(float[][] newVert, float[][] oldVert, int len, int vertex)
|
|
724 | 729 |
{ |
725 | 730 |
int newZeroIndex = computeRotatedIndex(0,len,vertex); |
726 | 731 |
float EPSILON = 0.001f; |
... | ... | |
730 | 735 |
{ |
731 | 736 |
int index = computeRotatedIndex(i,len,vertex); |
732 | 737 |
|
733 |
float horz = oldVert[2*i ] - scale*newVert[2*index ];
|
|
734 |
float vert = oldVert[2*i+1] - scale*newVert[2*index+1];
|
|
738 |
float horz = oldVert[i][0] - scale*newVert[index][0];
|
|
739 |
float vert = oldVert[i][1] - scale*newVert[index][1];
|
|
735 | 740 |
|
736 | 741 |
if( horz>EPSILON || horz<-EPSILON || vert>EPSILON || vert<-EPSILON ) return false; |
737 | 742 |
} |
... | ... | |
771 | 776 |
|
772 | 777 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
773 | 778 |
|
774 |
private void printVert(double[] buffer) |
|
775 |
{ |
|
776 |
int len = buffer.length/2; |
|
777 |
String str = ""; |
|
778 |
|
|
779 |
for(int i=0; i<len; i++) |
|
780 |
{ |
|
781 |
str += (" ("+buffer[2*i]+" , "+buffer[2*i+1]+" ) "); |
|
782 |
} |
|
783 |
|
|
784 |
android.util.Log.d("D", str); |
|
785 |
} |
|
786 |
|
|
787 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
788 |
|
|
789 |
private boolean foundVertex(FaceTransform info, float[] buffer, int len, float[] newVert, float[] oldVert, int oldSticker) |
|
779 |
private boolean foundVertex(FaceTransform info, float[][] buffer, float[][] newVert, float[][] oldVert, int oldSticker) |
|
790 | 780 |
{ |
791 |
int lenVertOld = oldVert.length/2; |
|
792 | 781 |
float lenOld=0.0f, oldX=0.0f, oldY=0.0f; |
782 |
int len = newVert.length; |
|
793 | 783 |
|
794 |
for(int oldV=0; oldV<lenVertOld; oldV++)
|
|
784 |
for(float[] ov : oldVert)
|
|
795 | 785 |
{ |
796 |
oldX = oldVert[2*oldV];
|
|
797 |
oldY = oldVert[2*oldV+1];
|
|
798 |
lenOld = (float)Math.sqrt(oldX*oldX + oldY*oldY);
|
|
786 |
oldX = ov[0];
|
|
787 |
oldY = ov[1];
|
|
788 |
lenOld = (float) Math.sqrt(oldX*oldX+oldY*oldY);
|
|
799 | 789 |
|
800 |
if( lenOld!=0 ) break;
|
|
790 |
if(lenOld!=0) break;
|
|
801 | 791 |
} |
802 | 792 |
|
803 | 793 |
for(int vertex=0; vertex<len; vertex++) |
804 | 794 |
{ |
805 |
float newX = newVert[2*vertex ];
|
|
806 |
float newY = newVert[2*vertex+1];
|
|
795 |
float newX = newVert[vertex][0];
|
|
796 |
float newY = newVert[vertex][1];
|
|
807 | 797 |
float lenNew = (float)Math.sqrt(newX*newX + newY*newY); |
808 | 798 |
|
809 | 799 |
if( lenNew!=0 ) |
... | ... | |
828 | 818 |
|
829 | 819 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
830 | 820 |
|
831 |
private float computeCoreDistance(float[] verts) |
|
821 |
private float computeCoreDistance(float[][] verts)
|
|
832 | 822 |
{ |
833 | 823 |
float ret = 0.0f; |
834 | 824 |
float centerX=0.0f,centerY=0.0f; |
835 |
int len = verts.length/2;
|
|
825 |
int len = verts.length; |
|
836 | 826 |
|
837 |
for(int i=0; i<len; i++)
|
|
827 |
for(float[] vert : verts)
|
|
838 | 828 |
{ |
839 |
centerX += verts[2*i ];
|
|
840 |
centerY += verts[2*i+1];
|
|
829 |
centerX += vert[0];
|
|
830 |
centerY += vert[1];
|
|
841 | 831 |
} |
842 | 832 |
|
843 |
centerX /= (2*len);
|
|
844 |
centerY /= (2*len);
|
|
833 |
centerX /= len;
|
|
834 |
centerY /= len;
|
|
845 | 835 |
|
846 |
for(int i=0; i<len; i++)
|
|
836 |
for(float[] vert : verts)
|
|
847 | 837 |
{ |
848 |
float distX = centerX-verts[2*i ];
|
|
849 |
float distY = centerY-verts[2*i+1];
|
|
850 |
ret += (float)Math.sqrt(distX*distX + distY*distY);
|
|
838 |
float distX = centerX-vert[0];
|
|
839 |
float distY = centerY-vert[1];
|
|
840 |
ret += (float) Math.sqrt(distX*distX+distY*distY);
|
|
851 | 841 |
} |
852 | 842 |
|
853 | 843 |
return ret; |
... | ... | |
862 | 852 |
StickerCoords sNewInfo = mStickerCoords.get(newInfo.sticker); |
863 | 853 |
StickerCoords sOldInfo = mStickerCoords.get(oldInfo.sticker); |
864 | 854 |
|
865 |
float[] newVert = sNewInfo.vertices; |
|
866 |
float[] oldVert = sOldInfo.vertices; |
|
855 |
float[][] newVert = sNewInfo.vertices;
|
|
856 |
float[][] oldVert = sOldInfo.vertices;
|
|
867 | 857 |
int oldLen = oldVert.length; |
868 | 858 |
int newLen = newVert.length; |
869 | 859 |
|
... | ... | |
875 | 865 |
if( diff<1.0-MAX_CORE_DIFF || diff>1.0+MAX_CORE_DIFF ) return false; // and two triangular stickers of different size. |
876 | 866 |
|
877 | 867 |
int oldSticker = oldInfo.sticker; |
878 |
float[] buffer1 = new float[oldLen];
|
|
868 |
float[][] buffer = new float[oldLen][2];
|
|
879 | 869 |
|
880 |
if( foundVertex(newInfo, buffer1, oldLen/2, newVert, oldVert, oldSticker) )
|
|
870 |
if( foundVertex(newInfo, buffer, newVert, oldVert, oldSticker) ) |
|
881 | 871 |
{ |
882 | 872 |
if( sNewInfo.outer ) sOldInfo.outer = true; |
883 | 873 |
return true; |
... | ... | |
1130 | 1120 |
} |
1131 | 1121 |
} |
1132 | 1122 |
|
1123 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
1124 |
|
|
1125 |
private void printVert(double[][] buffer) |
|
1126 |
{ |
|
1127 |
StringBuilder str = new StringBuilder(); |
|
1128 |
|
|
1129 |
for(double[] buf : buffer) |
|
1130 |
{ |
|
1131 |
str.append(" ("); |
|
1132 |
str.append(buf[0]); |
|
1133 |
str.append(" , "); |
|
1134 |
str.append(buf[1]); |
|
1135 |
str.append(" ) "); |
|
1136 |
} |
|
1137 |
|
|
1138 |
android.util.Log.d("D", str.toString()); |
|
1139 |
} |
|
1140 |
|
|
1133 | 1141 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
1134 | 1142 |
// PUBLIC API |
1135 | 1143 |
|
... | ... | |
1208 | 1216 |
|
1209 | 1217 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
1210 | 1218 |
|
1211 |
public float[][] getStickerCoords() |
|
1219 |
public float[][][] getStickerCoords()
|
|
1212 | 1220 |
{ |
1213 | 1221 |
int index=0,num=0,len = mStickerCoords.size(); |
1214 | 1222 |
|
... | ... | |
1216 | 1224 |
|
1217 | 1225 |
if( num>0 ) |
1218 | 1226 |
{ |
1219 |
float[][] coords = new float[num][];
|
|
1227 |
float[][][] coords = new float[num][][];
|
|
1220 | 1228 |
|
1221 | 1229 |
for(int i=0; i<len; i++) |
1222 | 1230 |
{ |
... | ... | |
1439 | 1447 |
} |
1440 | 1448 |
else |
1441 | 1449 |
{ |
1442 |
float[] verts = sInfo.vertices; |
|
1450 |
float[][] verts = sInfo.vertices;
|
|
1443 | 1451 |
int lenVerts = verts.length; |
1444 |
float[] copiedVerts = new float[lenVerts]; |
|
1445 |
System.arraycopy(verts, 0, copiedVerts, 0, lenVerts); |
|
1452 |
float[][] copiedVerts = new float[lenVerts][2]; |
|
1453 |
|
|
1454 |
for(int v=0; v<lenVerts; v++) |
|
1455 |
{ |
|
1456 |
float[] ve = verts[v]; |
|
1457 |
copiedVerts[v][0] = ve[0]; |
|
1458 |
copiedVerts[v][1] = ve[1]; |
|
1459 |
} |
|
1460 |
|
|
1446 | 1461 |
meshes[face] = new MeshPolygon(copiedVerts,bandsComputed,exIndex,exVertices, convexXY[0], convexXY[1]); |
1447 | 1462 |
} |
1448 | 1463 |
|
Also available in: Unified diff
change the way the coordinates of stickers are kept from float[2*N] to float[N][2]