Revision a60f7acf
Added by Leszek Koltunski 11 months ago
src/main/java/org/distorted/library/mesh/MeshMultigon.java | ||
---|---|---|
20 | 20 |
|
21 | 21 |
package org.distorted.library.mesh; |
22 | 22 |
|
23 |
import java.util.ArrayList; |
|
24 |
|
|
25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
26 |
|
|
23 | 27 |
/** |
24 | 28 |
* Create a 'multigon' mesh - a union of several polygons. |
25 | 29 |
* |
... | ... | |
31 | 35 |
public class MeshMultigon extends MeshBase |
32 | 36 |
{ |
33 | 37 |
private static final float MAX_ERROR = 0.0001f; |
38 |
private float[][] mOuterVertices; |
|
34 | 39 |
|
35 |
private boolean isSame(float[] v1, float[] v2) |
|
40 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
41 |
|
|
42 |
private boolean isSame(float dx, float dy) |
|
36 | 43 |
{ |
37 |
float dx = v1[0]-v2[0]; |
|
38 |
float dy = v1[1]-v2[1]; |
|
39 | 44 |
return (dx*dx + dy*dy < MAX_ERROR); |
40 | 45 |
} |
41 | 46 |
|
42 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
43 | 48 |
|
44 |
private boolean isUp(float[][][] vertices, int polygon, int edge)
|
|
49 |
private boolean isUp(float[][] vertices, int polygon, int edge) |
|
45 | 50 |
{ |
46 |
float[][] p = vertices[polygon]; |
|
47 |
int lenP = p.length; |
|
51 |
//android.util.Log.e("D", "checking polygon "+polygon+" edge "+edge); |
|
52 |
|
|
53 |
float[] p= vertices[polygon]; |
|
54 |
int lenP = p.length/2; |
|
48 | 55 |
int len = vertices.length; |
49 | 56 |
int next = (edge==lenP-1 ? 0 : edge+1); |
50 | 57 |
|
51 |
float[] v1 = p[edge]; |
|
52 |
float[] v2 = p[next]; |
|
58 |
float v1x = p[2*edge ]; |
|
59 |
float v1y = p[2*edge+1]; |
|
60 |
float v2x = p[2*next ]; |
|
61 |
float v2y = p[2*next+1]; |
|
62 |
|
|
63 |
//android.util.Log.e("D", " v1="+v1x+" "+v1y+" v2="+v2x+" "+v2y); |
|
53 | 64 |
|
54 | 65 |
for(int i=0; i<len; i++) |
55 | 66 |
if( i!=polygon ) |
56 | 67 |
{ |
57 |
int num = vertices[i].length; |
|
68 |
int num = vertices[i].length/2;
|
|
58 | 69 |
|
59 | 70 |
for(int j=0; j<num; j++) |
60 | 71 |
{ |
61 | 72 |
int n = (j==num-1 ? 0 : j+1); |
62 |
if( isSame(v2,vertices[i][j]) && isSame(v1,vertices[i][n]) ) return true; |
|
73 |
float[] v = vertices[i]; |
|
74 |
float x1 = v[2*j ]; |
|
75 |
float y1 = v[2*j+1]; |
|
76 |
float x2 = v[2*n ]; |
|
77 |
float y2 = v[2*n+1]; |
|
78 |
|
|
79 |
//android.util.Log.e("D", "comparing v2 to "+x1+" "+y1); |
|
80 |
//android.util.Log.e("D", "comparing v1 to "+x2+" "+y2); |
|
81 |
|
|
82 |
if( isSame(v2x-x1,v2y-y1) && isSame(v1x-x2,v1y-y2) ) return true; |
|
63 | 83 |
} |
64 | 84 |
} |
65 | 85 |
|
86 |
//android.util.Log.e("D", "FALSE"); |
|
87 |
|
|
66 | 88 |
return false; |
67 | 89 |
} |
68 | 90 |
|
69 | 91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
70 | 92 |
|
71 |
private boolean[][] computeUp(float[][] vertices, float[][] centers)
|
|
93 |
private boolean[][] computeEdgesUp(float[][] vertices)
|
|
72 | 94 |
{ |
73 | 95 |
int num = vertices.length; |
74 | 96 |
boolean[][] up = new boolean[num][]; |
75 |
float[][][] v = new float[num][][]; |
|
76 | 97 |
|
77 | 98 |
for(int i=0; i<num; i++) |
78 | 99 |
{ |
79 | 100 |
int len = vertices[i].length/2; |
80 |
v[i] = new float[len][2]; |
|
81 |
|
|
101 |
up[i] = new boolean[len]; |
|
82 | 102 |
for(int j=0; j<len; j++) |
83 | 103 |
{ |
84 |
v[i][j][0] = vertices[i][2*j ] + centers[i][0];
|
|
85 |
v[i][j][1] = vertices[i][2*j+1] + centers[i][1];
|
|
104 |
up[i][j] = isUp(vertices,i,j);
|
|
105 |
//android.util.Log.e("D", "polygon "+i+" edge "+j+" up: "+up[i][j]);
|
|
86 | 106 |
} |
87 | 107 |
} |
88 | 108 |
|
89 |
for(int i=0; i<num; i++) |
|
109 |
return up; |
|
110 |
} |
|
111 |
|
|
112 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
113 |
|
|
114 |
private float[] detectFirstOuterVertex(float[][] vertices) |
|
115 |
{ |
|
116 |
float X = -Float.MAX_VALUE; |
|
117 |
int I=0,J=0, len = vertices.length; |
|
118 |
|
|
119 |
for(int i=0; i<len; i++ ) |
|
90 | 120 |
{ |
91 |
int len = vertices[i].length/2; |
|
92 |
up[i] = new boolean[len]; |
|
93 |
for(int j=0; j<len; j++) |
|
121 |
float[] v = vertices[i]; |
|
122 |
int num = v.length/2; |
|
123 |
|
|
124 |
for(int j=0; j<num; j++) |
|
125 |
if(v[2*j]>X) |
|
126 |
{ |
|
127 |
X = v[2*j]; |
|
128 |
I = i; |
|
129 |
J = j; |
|
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
float[] v = vertices[I]; |
|
134 |
return new float[] {v[2*J],v[2*J+1]}; |
|
135 |
} |
|
136 |
|
|
137 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
138 |
|
|
139 |
private double computeAngle(float x1,float y1, float x2, float y2) |
|
140 |
{ |
|
141 |
double diff = Math.atan2(y2,x2)-Math.atan2(y1,x1); |
|
142 |
return diff<0 ? diff+(2*Math.PI) : diff; |
|
143 |
} |
|
144 |
|
|
145 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
146 |
|
|
147 |
private float[] detectNextOuterVertex(float[][] vertices, float[] curr, float[] vect) |
|
148 |
{ |
|
149 |
double minAngle = 2*Math.PI; |
|
150 |
float x=0, y=0; |
|
151 |
|
|
152 |
for( float[] v : vertices ) |
|
153 |
{ |
|
154 |
int num = v.length/2; |
|
155 |
|
|
156 |
for( int j=0; j<num; j++) |
|
94 | 157 |
{ |
95 |
up[i][j] = isUp(v,i,j); |
|
158 |
float xc = v[2*j]; |
|
159 |
float yc = v[2*j+1]; |
|
160 |
|
|
161 |
if( xc==curr[0] && yc==curr[1]) |
|
162 |
{ |
|
163 |
int n = (j==num-1 ? 0 : j+1); |
|
164 |
float xn = v[2*n]; |
|
165 |
float yn = v[2*n+1]; |
|
166 |
|
|
167 |
double angle = computeAngle(vect[0], vect[1], xn-xc, yn-yc); |
|
168 |
|
|
169 |
if (angle < minAngle) |
|
170 |
{ |
|
171 |
minAngle = angle; |
|
172 |
x = xn; |
|
173 |
y = yn; |
|
174 |
} |
|
96 | 175 |
|
97 |
android.util.Log.e("D", "polygon "+i+" edge "+j+" up: "+up[i][j]); |
|
176 |
break; |
|
177 |
} |
|
98 | 178 |
} |
99 | 179 |
} |
100 | 180 |
|
181 |
return new float[] {x,y}; |
|
182 |
} |
|
183 |
|
|
184 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
185 |
|
|
186 |
private void computeOuter(float[][] vertices) |
|
187 |
{ |
|
188 |
ArrayList<float[]> tmp = new ArrayList<>(); |
|
189 |
|
|
190 |
float[] vect = new float[] {1,0}; |
|
191 |
float[] first= detectFirstOuterVertex(vertices); |
|
192 |
float[] next = first; |
|
193 |
|
|
194 |
do |
|
195 |
{ |
|
196 |
float[] prev = next; |
|
197 |
next = detectNextOuterVertex(vertices,next,vect); |
|
198 |
vect[0] = prev[0]-next[0]; |
|
199 |
vect[1] = prev[1]-next[1]; |
|
200 |
tmp.add(next); |
|
201 |
} |
|
202 |
while( next[0]!=first[0] || next[1]!=first[1] ); |
|
203 |
|
|
204 |
int num = tmp.size(); |
|
205 |
mOuterVertices = new float[num][]; |
|
206 |
for(int i=0; i<num; i++) mOuterVertices[i] = tmp.remove(0); |
|
207 |
} |
|
208 |
|
|
209 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
210 |
|
|
211 |
private boolean doesntBelongToOuter(float x, float y) |
|
212 |
{ |
|
213 |
for( float[] v : mOuterVertices ) |
|
214 |
if( x==v[0] && y==v[1] ) return false; |
|
215 |
|
|
216 |
return true; |
|
217 |
} |
|
218 |
|
|
219 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
220 |
|
|
221 |
private boolean[][] computeVertsUp(float[][] vertices) |
|
222 |
{ |
|
223 |
computeOuter(vertices); |
|
224 |
|
|
225 |
int num = vertices.length; |
|
226 |
boolean[][] up = new boolean[num][]; |
|
227 |
|
|
228 |
for(int i=0; i<num; i++) |
|
229 |
{ |
|
230 |
float[] v = vertices[i]; |
|
231 |
int len = v.length/2; |
|
232 |
up[i] = new boolean[len]; |
|
233 |
for(int j=0; j<len; j++) up[i][j] = doesntBelongToOuter(v[2*j],v[2*j+1]); |
|
234 |
} |
|
235 |
|
|
101 | 236 |
return up; |
102 | 237 |
} |
103 | 238 |
|
... | ... | |
121 | 256 |
|
122 | 257 |
int numPolygons = vertices.length; |
123 | 258 |
MeshPolygon[] meshes = new MeshPolygon[numPolygons]; |
124 |
boolean[][] up = computeUp(vertices,centers); |
|
259 |
boolean[][] edgesUp = computeEdgesUp(vertices); |
|
260 |
boolean[][] vertsUp = computeVertsUp(vertices); |
|
261 |
|
|
262 |
for(int i=0; i<numPolygons; i++) |
|
263 |
meshes[i] = new MeshPolygon(vertices[i],band,edgesUp[i],vertsUp[i],exIndex,exVertices,centers[i][0],centers[i][1]); |
|
264 |
|
|
265 |
join(meshes); |
|
266 |
} |
|
267 |
|
|
268 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
269 |
/** |
|
270 |
* Auto-create the centers to be the centers of gravity of each polygon. |
|
271 |
*/ |
|
272 |
|
|
273 |
public MeshMultigon(float[][] vertices, float[] band, int exIndex, int exVertices) |
|
274 |
{ |
|
275 |
super(); |
|
276 |
|
|
277 |
int numPolygons = vertices.length; |
|
278 |
MeshPolygon[] meshes = new MeshPolygon[numPolygons]; |
|
279 |
boolean[][] edgesUp = computeEdgesUp(vertices); |
|
280 |
boolean[][] vertsUp = computeVertsUp(vertices); |
|
125 | 281 |
|
126 | 282 |
for(int i=0; i<numPolygons; i++) |
127 |
meshes[i] = new MeshPolygon(vertices[i],band,up[i],exIndex,exVertices,centers[i][0],centers[i][1]); |
|
283 |
{ |
|
284 |
float[] v = vertices[i]; |
|
285 |
int num = v.length/2; |
|
286 |
float xsum=0, ysum = 0; |
|
287 |
|
|
288 |
for(int j=0; j<num; j++) |
|
289 |
{ |
|
290 |
xsum += v[2*j]; |
|
291 |
ysum += v[2*j+1]; |
|
292 |
} |
|
293 |
|
|
294 |
meshes[i] = new MeshPolygon(v,band,edgesUp[i],vertsUp[i],exIndex,exVertices,xsum/num,ysum/num); |
|
295 |
} |
|
128 | 296 |
|
129 | 297 |
join(meshes); |
130 | 298 |
} |
Also available in: Unified diff
MeshMultigon finished.