1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
4 // FEATURE: represent a plane by the matrix that projects points onto that plane?
5 public class Plane implements AffineConstraint {
8 public final float a, b, c, d;
11 public Vec norm() { return new Vec(a, b, c).norm(); }
13 public Plane(Point p, Vec norm) {
18 this.d = p.x*norm.x+p.y*norm.y+p.z*norm.z;
22 public Plane(float a, float b, float c, float d) {
23 // FIXME, what if c==0?
24 this(a, b, c, d, new Point(0, 0, d/c));
27 /** provided at least one of a,b,c is nonzero, return the Plane representing ax+by+cz=d */
28 public Plane(float a, float b, float c, float d, Point p) {
36 public Point intersect(Plane p1, Plane p2) {
39 float z = p1.norm().dot(p2.norm().cross(p3.norm()));
40 if (Math.abs(z) < 0.0001) return null; // planes do not intersect at a point
41 Vec v1 = p2.norm().cross(p3.norm()).times(-1 * p1.d);
42 Vec v2 = p3.norm().cross(p1.norm()).times(-1 * p2.d);
43 Vec v3 = p1.norm().cross(p2.norm()).times(-1 * p3.d);
44 return Point.ZERO.plus(v1.plus(v2).plus(v3).times(1/z));
47 public AffineConstraint intersect(AffineConstraint c, float epsilon) {
48 if (c instanceof Plane) {
52 if (Math.abs(p.a*this.d-this.a*p.d) <= epsilon &&
53 Math.abs(p.b*this.d-this.b*p.d) <= epsilon &&
54 Math.abs(p.c*this.d-this.c*p.d) <= epsilon &&
55 Math.abs(p.d*this.d-this.d*p.d) <= epsilon)
59 if (Math.abs(p.norm().cross(norm()).mag()) <= epsilon)
60 return AffineConstraint.NONE;
62 Vec u = norm().cross(p.norm());
64 if (Math.abs(u.z) >= Math.abs(u.y) && Math.abs(u.z) >= Math.abs(u.y)) {
65 point = new Point( (this.b*p.d - p.b*this.d)/(this.a*p.b - p.a*this.b),
66 (this.d*p.a - p.d*this.a)/(this.a*p.b - p.a*this.b),
68 } else if (Math.abs(u.y) >= Math.abs(u.z) && Math.abs(u.y) >= Math.abs(u.x)) {
69 point = new Point( (this.c*p.d - p.c*this.d)/(this.a*p.c - p.a*this.c),
71 (this.d*p.a - p.d*this.a)/(this.a*p.c - p.a*this.c));
74 (this.c*p.d - p.c*this.d)/(this.b*p.c - p.b*this.c),
75 (this.d*p.b - p.d*this.b)/(this.b*p.c - p.b*this.c));
78 return new Line(point, u);
80 } else if (c instanceof Line) {
82 if (Math.abs(a + b*l.m + this.c*l.n) <= epsilon &&
83 Math.abs(b * l.c + this.c*l.d + d) <= epsilon)
86 // FIXME: parallel case
88 float x = (d - b*l.c - this.c*l.d) / (a + b*l.m + this.c*l.n);
91 return new Point(x,y,z);
92 //throw new RuntimeException("not yet implemented");
95 return c.intersect(this, epsilon);
98 public AffineConstraint multiply(Matrix m) {
99 return new Plane( m.times(p), m.times(norm()).norm() );
102 public Point getProjection(Point p) {
103 Point ret = norm().times(p.minus(this.p).dot(norm())).times(-1).plus(p);
106 public String toString() {
107 return "[plane "+a+"x+"+b+"y+"+c+"z="+d+"]";