Changeset 2855
 Timestamp:
 06/12/07 10:00:10 (13 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/loci/visbio/util/MathUtil.java
r2725 r2855 83 83 } 84 84 return Math.sqrt(sum); 85 } 86 87 /** 88 * Computes the minimum distance between the point p and the point v in 89 * double precision. 90 * 91 * @param p Coordinates of the first point 92 * @param v Coordinates of the second point 93 */ 94 public static double getDistance (float[] p, float[] v) { 95 double[] pp = new double[p.length]; 96 double[] vv = new double[v.length]; 97 return getDistance(pp, vv); 85 98 } 86 99 … … 284 297 return result; 285 298 } 299 300 //  Vector Math Methods  301 302 /** Computes the cross product of two 2D vectors. */ 303 public static float cross2D(float[] v1, float[] v2) { 304 return v1[0] * v2[1]  v1[1] * v2[0]; 305 } 306 307 /** Computes the ND unit vector in the direction as the vector supplied. */ 308 public static float[] unit(float[] v) { 309 float mag = mag(v); 310 float[] vHat = new float[v.length]; 311 for (int i=0; i<v.length; i++) { 312 vHat[i] = v[i] / mag; 313 } 314 return vHat; 315 } 316 317 /** Computes the dot product of two ND vectors */ 318 public static float dot(float[] a, float[] b) { 319 float sum = 0f; 320 for (int i=0; i<a.length; i++) { 321 sum += a[i] * b[i]; 322 } 323 return sum; 324 } 325 326 /** Computes the magnitude of an ND vector. */ 327 public static float mag(float[] a) { 328 return (float) Math.sqrt(dot(a, a)); 329 } 330 331 /** Creates the vector p2(minus) p1. */ 332 public static float[] vector(float[] p1, float[] p2) { 333 // assumes p1, p2 have same lengths 334 if (p1.length != p2.length) return null; 335 int len = p1.length; 336 float[] v = new float[len]; 337 for (int i=0; i<len; i++) { 338 v[i] = p1[i]  p2[i]; 339 } 340 return v; 341 } 342 343 /** Whether the point a is inside the ND box implied by points 344 * b2 and b2 (i.e., in 2D, whether a is inside the box with diagonal 345 * b1b2; in 3D, whether a is inside the cube with diagonal b1b2). 346 */ 347 public static boolean between(float[] a, float[] b1, float[] b2) { 348 // assumes a, b1, b2 have same lengths 349 boolean between = true; 350 for (int i=0; i<a.length; i++) { 351 boolean flip = b1[i] < b2[i] ? false : true; 352 float lo = flip ? b2[i] : b1[i]; 353 float hi = flip ? b1[i] : b2[i]; 354 if (a[i] < lo  a[i] > hi) { 355 between = false; 356 break; 357 } 358 } 359 return between; 360 } 361 362 /** Gets a unit vector which bisects (p1  p2) and (p3  p2). */ 363 public static float[] getBisectorVector2D(float[] p1, float[] p2, float[] p3) 364 { 365 System.out.println("entering getBisectorVector2D ..."); //TEMP 366 float[] v1 = vector(p1, p2); 367 float[] v2 = vector(p3, p2); 368 float[] v1Hat = unit(v1); 369 float[] v2Hat = unit(v2); 370 float[] vAvg = {(v1Hat[0] + v2Hat[0]) / 2f, (v1Hat[1] + v2Hat[1]) / 2f}; 371 372 float[] bisector; 373 if (vAvg[0] == 0 && vAvg[1] == 0) { 374 float[] aBisector = new float[]{v1[1], v1[0]}; 375 bisector = unit(aBisector); 376 } 377 else { 378 bisector = unit(vAvg); 379 } 380 381 // System.out.println("v1 = " + v1[0] + " " + v1[1]); 382 // System.out.println("v2 = " + v2[0] + " " + v2[1]); 383 // System.out.println("v1Hat = " + v1Hat[0] + " " + v1Hat[1]); 384 // System.out.println("v2Hat = " + v2Hat[0] + " " + v2Hat[1]); 385 // System.out.println("vAvg = " + vAvg[0] + " " + vAvg[1]); 386 // System.out.println("bisector = " + bisector[0] + " " + bisector[1]); 387 // System.out.println("p1a = " + p1a[0] + " " + p1a[1]); 388 // System.out.println("p2a = " + p2a[0] + " " + p2a[1]); 389 // System.out.println("p4a = " + p4a[0] + " " + p4a[1]); 390 // System.out.println("v42 = " + v42[0] + " " + v42[1]); 391 // System.out.println("v42Hat = " +v42Hat[0] + " " + v42Hat[1]); 392 return bisector; 393 } 394 395 /** Adds two ND vectors. */ 396 public static float[] add(float[] v1, float[] v2) { 397 // v1 and v2 should have same lengths 398 if (v1.length != v2.length) return null; 399 int len = v1.length; 400 float[] r = new float[v1.length]; 401 for (int i=0; i<v1.length; i++) { 402 r[i] = v1[i] + v2[i]; 403 } 404 return r; 405 } 406 407 /** Multiplies an ND vector by a scalar. */ 408 public static float[] scalarMultiply(float[] v, float s) { 409 int len = v.length; 410 float[] r = new float[len]; 411 for (int i=0; i<len; i++) { 412 r[i] = v[i] * s; 413 } 414 return r; 415 } 286 416 287 417 }
Note: See TracChangeset
for help on using the changeset viewer.