add items to TODO list
[anneal.git] / src / edu / berkeley / qfat / geom / Plane.java
index b3aacdd..966eebf 100644 (file)
@@ -1,26 +1,36 @@
 package edu.berkeley.qfat.geom;
 import javax.media.opengl.*;
 
+// FEATURE: represent a plane by the matrix that projects points onto that plane?
 public class Plane implements AffineConstraint {
 
     // ax+by+cz=d
     public final float a, b, c, d;
+    public final Point p;
 
-    public Vec norm() { return new Vec(a, b, c); }
+    public Vec norm() { return new Vec(a, b, c).norm(); }
 
     public Plane(Point p, Vec norm) {
+        norm = norm.norm();
         this.a = norm.x;
         this.b = norm.y;
         this.c = norm.z;
         this.d = p.x*norm.x+p.y*norm.y+p.z*norm.z;
+        this.p = p;
     }
 
-    /** provided at least one of a,b,c is nonzero, return the Plane representing ax+by+cz=d */
     public Plane(float a, float b, float c, float d) {
+        // FIXME, what if c==0?
+        this(a, b, c, d, new Point(0, 0, d/c));
+    }
+
+    /** provided at least one of a,b,c is nonzero, return the Plane representing ax+by+cz=d */
+    public Plane(float a, float b, float c, float d, Point p) {
         this.a = a;
         this.b = b;
         this.c = c;
         this.d = d;
+        this.p = p;
     }
 
     public Point intersect(Plane p1, Plane p2) {
@@ -34,15 +44,66 @@ public class Plane implements AffineConstraint {
         return Point.ZERO.plus(v1.plus(v2).plus(v3).times(1/z));
     }
 
-    public Point getProjection(Point p) {
-        throw new RuntimeException("not implemented yet");
-    }
-
     public AffineConstraint intersect(AffineConstraint c, float epsilon) {
-        throw new RuntimeException("not implemented yet");
+        if (c instanceof Plane) {
+            Plane p = (Plane)c;
+
+            // same plane
+            if (Math.abs(p.a*this.d-this.a*p.d) <= epsilon &&
+                Math.abs(p.b*this.d-this.b*p.d) <= epsilon &&
+                Math.abs(p.c*this.d-this.c*p.d) <= epsilon &&
+                Math.abs(p.d*this.d-this.d*p.d) <= epsilon)
+                return this;
+
+            // parallel planes
+            if (Math.abs(p.norm().cross(norm()).mag()) <= epsilon)
+                return AffineConstraint.NONE;
+
+            Vec u = norm().cross(p.norm());
+            Point point = null;
+            if (Math.abs(u.z) >= Math.abs(u.y) && Math.abs(u.z) >= Math.abs(u.y)) {
+                point = new Point( (this.b*p.d - p.b*this.d)/(this.a*p.b - p.a*this.b),
+                                   (this.d*p.a - p.d*this.a)/(this.a*p.b - p.a*this.b),
+                                   0);
+            } else if (Math.abs(u.y) >= Math.abs(u.z) && Math.abs(u.y) >= Math.abs(u.x)) {
+                point = new Point( (this.c*p.d - p.c*this.d)/(this.a*p.c - p.a*this.c),
+                                   0,
+                                   (this.d*p.a - p.d*this.a)/(this.a*p.c - p.a*this.c));
+            } else {
+                point = new Point( 0,
+                                   (this.c*p.d - p.c*this.d)/(this.b*p.c - p.b*this.c),
+                                   (this.d*p.b - p.d*this.b)/(this.b*p.c - p.b*this.c));
+            }
+
+            return new Line(point, u);
+
+        } else if (c instanceof Line) {
+            Line l = (Line)c;
+            if (Math.abs(a + b*l.m + this.c*l.n)     <= epsilon &&
+                Math.abs(b * l.c   + this.c*l.d + d) <= epsilon)
+                return l;
+
+            // FIXME: parallel case
+
+            float x = (d - b*l.c - this.c*l.d) / (a + b*l.m + this.c*l.n);
+            float y = l.m*x+l.c;
+            float z = l.n*x+l.d;
+            return new Point(x,y,z);
+            //throw new RuntimeException("not yet implemented");
+
+        } else
+            return c.intersect(this, epsilon);
     }
 
     public AffineConstraint multiply(Matrix m) {
-        throw new RuntimeException("not yet implemented");
+        return new Plane( m.times(p), m.times(norm()).norm() );
+    }
+
+    public Point getProjection(Point p) {
+        Point ret = norm().times(p.minus(this.p).dot(norm())).times(-1).plus(p);
+        return ret;
+    }
+    public String toString() {
+        return "[plane "+a+"x+"+b+"y+"+c+"z="+d+"]";
     }
 }