checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Vec.java
index c331d39..979cf2d 100644 (file)
@@ -1,18 +1,39 @@
 package edu.berkeley.qfat.geom;
+import javax.media.opengl.*;
+import javax.media.opengl.glu.*;
 
 /** vector in 3-space; immutable */
 public final class Vec {
+
+    public static final Vec ZERO = new Vec(0,0,0);
+
     public final float x, y, z;
     public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
     public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
     public Vec(Point p1, Point p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
     public Vec cross(Vec v) { return new Vec(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
     public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
+    public Point plus(Point p) { return p.plus(this); }
+    public Vec minus(Vec v) { return new Vec(x-v.x, y-v.y, z-v.z); }
     public Vec norm() { return mag()==0 ? this : div(mag()); }
-    public Vec times(Matrix m) { return m.apply(this); }
     public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
     public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
     public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
     public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
     public String toString() { return "<"+x+","+y+","+z+">"; }
+    public void glNormal(GL gl) { gl.glNormal3f(x, y, z); }
+
+    /** fundamental error quadric for the plane with this normal passing through p */
+    public Matrix fundamentalQuadric(Point p) {
+        Vec n = this;
+        if (mag() != 1) n = norm();
+        float a = n.x;
+        float b = n.y;
+        float c = n.z;
+        float d = (-a * p.x) + (-b * p.y) + (-c * p.z);
+        return new Matrix(a*a, a*b, a*c, a*d,
+                          a*b, b*b, b*c, b*d,
+                          a*c, b*c, c*c, c*d,
+                          a*d, b*d, c*d, d*d);
+    }
 }