/* * pVector.java * * (C) 2005, Alex S. */ import java.util.*; import java.awt.*; /** * basic vector routines. */ public class pVector { /** * given 2 points, find their sum */ public static void vectorAdd (double[] va, double[] vb, double[] out){ out[0] = va[0]+vb[0]; out[1] = va[1]+vb[1]; out[2] = va[2]+vb[2]; } /** * given 2 points, find their difference */ public static void vectorSubtract (double[] va, double[] vb, double[] out){ out[0] = va[0]-vb[0]; out[1] = va[1]-vb[1]; out[2] = va[2]-vb[2]; } /** * cross product of two vectors */ public static void crossProduct(double[] v1, double[] v2, double[] cross ) { cross[0] = v1[1]*v2[2] - v1[2]*v2[1]; cross[1] = v1[2]*v2[0] - v1[0]*v2[2]; cross[2] = v1[0]*v2[1] - v1[1]*v2[0]; } /** * given 2 points, find their sum */ public static void vectorScale (double[] v, double s, double[] out){ out[0] = v[0]*s; out[1] = v[1]*s; out[2] = v[2]*s; } /** * normalize */ public static double vectorNormalize(double[] in, double[] out ) { double length, ilength; length = (double)Math.sqrt(in[0]*in[0] + in[1]*in[1] + in[2]*in[2]); if (length == 0) { return 0; } ilength = (double)1.0/length; out[0] = in[0]*ilength; out[1] = in[1]*ilength; out[2] = in[2]*ilength; return length; } /** * dot product */ public static double dotProduct (double[] v1, double[] v2){ return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]; } /** * gets plane equation from three points on plane * * Plane is: Ax + By + Cx + D = 0 */ public static boolean planeFromPoints(double[] plane, double[] a, double[] b,double[] c,double[] d ){ double[] d1 = new double[4]; double[] d2 = new double[4]; vectorSubtract( b, a, d1 ); if (dotProduct(d1,d1) < (double)0.02) { vectorSubtract( c, a, d1 ); vectorSubtract( d, a, d2 ); } else { vectorSubtract( c, a, d2 ); if (dotProduct(d2,d2) < (double)0.02) { vectorSubtract( d, a, d2 ); } } crossProduct( d1, d2, plane ); if ( vectorNormalize( plane, plane ) == 0 ) { return false; } plane[3] = -dotProduct( a, plane ); return true; } }