Revision f4ed769a
Added by Leszek Koltunski about 2 months ago
src/main/java/org/distorted/objectlib/helpers/FactoryCubit.java | ||
---|---|---|
182 | 182 |
mBuffer[0] /= len; |
183 | 183 |
mBuffer[1] /= len; |
184 | 184 |
mBuffer[2] /= len; |
185 |
|
|
186 |
// this theoretically should be uncommented to combat the same effect like in |
|
187 |
// TouchControlShapeChanging.FaceInfo, but when I try, the center sticker of |
|
188 |
// CoinTetrahedron gets inverted. Weird! |
|
189 |
// Uncommenting this does fix the fact that the convexity of the center cubits |
|
190 |
// of CoinT and CoinH goes concave |
|
191 |
|
|
192 |
/* |
|
193 |
if( totalAngle(vertices,mBuffer)<0 ) |
|
194 |
{ |
|
195 |
mBuffer[0] *= -1; |
|
196 |
mBuffer[1] *= -1; |
|
197 |
mBuffer[2] *= -1; |
|
198 |
} |
|
199 |
*/ |
|
200 |
} |
|
201 |
|
|
202 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
203 |
// this returns the total angle we get rotated about when we travel from the first vert to the last. |
|
204 |
// It's always should be +2PI or -2PI, depending on if the verts are CW or CCW (when looking at the |
|
205 |
// plane formed by the vertices from the direction the 'normal' vector points towards) |
|
206 |
// |
|
207 |
// The point: this way we can detect if the 'normal' vector is correct, i.e. if it points in the |
|
208 |
// right direction. |
|
209 |
// Sometimes it doesn't, when the first three vertices (from which the vector is computed) are 'concave'. |
|
210 |
|
|
211 |
public static float totalAngle(float[][] vert, float[] normal) |
|
212 |
{ |
|
213 |
float ret = 0; |
|
214 |
int num = vert.length; |
|
215 |
|
|
216 |
for(int c=0; c<num; c++) |
|
217 |
{ |
|
218 |
int n = (c==num-1 ? 0 : c+1); |
|
219 |
int m = (n==num-1 ? 0 : n+1); |
|
220 |
ret += angle( vert[c],vert[n],vert[m], normal); |
|
221 |
} |
|
222 |
|
|
223 |
return ret; |
|
224 |
} |
|
225 |
|
|
226 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
227 |
|
|
228 |
private static float angle(float[] v0, float[] v1, float[] v2, float[] normal) |
|
229 |
{ |
|
230 |
float px = v1[0] - v0[0]; |
|
231 |
float py = v1[1] - v0[1]; |
|
232 |
float pz = v1[2] - v0[2]; |
|
233 |
|
|
234 |
float rx = v2[0] - v1[0]; |
|
235 |
float ry = v2[1] - v1[1]; |
|
236 |
float rz = v2[2] - v1[2]; |
|
237 |
|
|
238 |
float l1 = (float)Math.sqrt(px*px + py*py + pz*pz); |
|
239 |
float l2 = (float)Math.sqrt(rx*rx + ry*ry + rz*rz); |
|
240 |
|
|
241 |
px /= l1; |
|
242 |
py /= l1; |
|
243 |
pz /= l1; |
|
244 |
|
|
245 |
rx /= l2; |
|
246 |
ry /= l2; |
|
247 |
rz /= l2; |
|
248 |
|
|
249 |
float s = px*rx + py*ry + pz*rz; |
|
250 |
|
|
251 |
if( s> 1 ) s= 1; |
|
252 |
if( s<-1 ) s=-1; |
|
253 |
|
|
254 |
float a = (float) Math.acos(s); |
|
255 |
float[] cross = crossProduct(px,py,pz,rx,ry,rz); |
|
256 |
|
|
257 |
float ax = cross[0] + normal[0]; |
|
258 |
float ay = cross[1] + normal[1]; |
|
259 |
float az = cross[2] + normal[2]; |
|
260 |
|
|
261 |
float bx = cross[0] - normal[0]; |
|
262 |
float by = cross[1] - normal[1]; |
|
263 |
float bz = cross[2] - normal[2]; |
|
264 |
|
|
265 |
float f1 = ax*ax + ay*ay + az*az; |
|
266 |
float f2 = bx*bx + by*by + bz*bz; |
|
267 |
|
|
268 |
return f1>f2 ? a : -a; |
|
269 |
} |
|
270 |
|
|
271 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
272 |
|
|
273 |
private static float[] crossProduct(float a1, float a2, float a3, float b1, float b2, float b3) |
|
274 |
{ |
|
275 |
float[] ret = new float[3]; |
|
276 |
|
|
277 |
ret[0] = a2*b3 - a3*b2; |
|
278 |
ret[1] = a3*b1 - a1*b3; |
|
279 |
ret[2] = a1*b2 - a2*b1; |
|
280 |
|
|
281 |
return ret; |
|
185 | 282 |
} |
186 | 283 |
|
187 | 284 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
minor.