X-Git-Url: http://git.megacz.com/?p=anneal.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2Fgeom%2FPlane.java;h=36f18a302712a89b41f09b299b81cc4050ef53c5;hp=419bb9392a86789a406546575609527c64a70f06;hb=eabe4f7acd947415f183290dc3269b2502a25a1c;hpb=5f15a6155bf7fbf2d62b3ab9fd992a54af10a95a diff --git a/src/edu/berkeley/qfat/geom/Plane.java b/src/edu/berkeley/qfat/geom/Plane.java index 419bb93..36f18a3 100644 --- a/src/edu/berkeley/qfat/geom/Plane.java +++ b/src/edu/berkeley/qfat/geom/Plane.java @@ -1,24 +1,89 @@ package edu.berkeley.qfat.geom; import javax.media.opengl.*; -public class Plane { +public class Plane implements AffineConstraint { - public final Vec norm; - public final float dvalue; + // ax+by+cz=d + public final float a, b, c, d; + + public Vec norm() { return new Vec(a, b, c); } public Plane(Point p, Vec norm) { - this.norm = norm.norm(); - this.dvalue = p.x*this.norm.x+p.y*this.norm.y+p.z*this.norm.z; + 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; + } + + /** 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) { + this.a = a; + this.b = b; + this.c = c; + this.d = d; } public Point intersect(Plane p1, Plane p2) { + Vec norm = norm(); Plane p3 = this; - float z = p1.norm.dot(p2.norm.cross(p3.norm)); + float z = p1.norm().dot(p2.norm().cross(p3.norm())); if (Math.abs(z) < 0.0001) return null; // planes do not intersect at a point - Vec v1 = p2.norm.cross(p3.norm).times(-1 * p1.dvalue); - Vec v2 = p3.norm.cross(p1.norm).times(-1 * p2.dvalue); - Vec v3 = p1.norm.cross(p2.norm).times(-1 * p3.dvalue); - return Point.ORIGIN.plus(v1.plus(v2).plus(v3).times(1/z)); + Vec v1 = p2.norm().cross(p3.norm()).times(-1 * p1.d); + Vec v2 = p3.norm().cross(p1.norm()).times(-1 * p2.d); + Vec v3 = p1.norm().cross(p2.norm()).times(-1 * p3.d); + 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) { + if (c instanceof Plane) { + Plane p = (Plane)c; + + // same plane + if (Math.abs(p.a-this.a) <= epsilon && + Math.abs(p.b-this.b) <= epsilon && + Math.abs(p.c-this.c) <= epsilon && + Math.abs(p.d-this.d) <= epsilon) + return this; + + // parallel planes + if (Math.abs(p.norm().cross(norm()).mag()) <= epsilon) + return new AffineConstraint.Nothing(); + + 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; + throw new RuntimeException("not yet implemented"); + + } else + return c.intersect(this, epsilon); } + public AffineConstraint multiply(Matrix m) { + throw new RuntimeException("not yet implemented"); + } }