package graphics; public class Plane extends Shape { //coefficients //ax + by + cz + d double a, b, c, d; Vector3 n, abc; public Plane(double a, double b, double c, double d) { this.a = a; this.b = b; this.c = c; this.d = d; //normal to plane doesn't change this.abc = new Vector3(a, b, c); this.n = new Vector3(abc); this.n.normalize(); //this.abc.normalize(); //this.a = abc.get(0); this.b = abc.get(1); this.c = abc.get(2); this.matrix = new Matrix3D(); } //plane equation public double equation(double x, double y, double z) { return a*x + b*y + c*z + d; } //plane equation public double equation(Vector3 pt) { return abc.dot(pt) + d; } //returns array of t values (roots) or void if no intersection //sets point of intersection and normal there public double[] testRay(Vector3 v, Vector3 w, Vector3 pos, Vector3 normal) { double numer = n.dot(v) + d; //equation(v) double denom = n.dot(w); //check for degenerate case (normal and w are orthogonal) if (Math.abs(denom) < 0.001) //basically denom == 0 { //where is V relative to plane? if (numer >= 0) return null; //outside half-space else return new double[]{0}; //inside half-space } double t = 0 - (numer)/denom; //System.err.println("t = " + t); //we don't care about intersections behind the camera //if (t < 0.001) return null; //calculate intersection point and normal setIntersectionPoint(v, w, t, pos); //System.err.println("pos = " + pos); normal.copy(n); double[] roots = new double[1]; roots[0] = t; return roots; } //transformation of polyhedron = transformation of planes public void transform() { if (this.matrix == null) { System.err.println("Warning: transformation matrix is null. Cannot transform Plane."); return; } Matrix3D m = new Matrix3D(this.matrix); m.invert(); //m.transpose(); Vector4 abcd = new Vector4(a, b, c, d); abcd.multiply(m); //post-multiply abcd by m a = abcd.get(0); b = abcd.get(1); c = abcd.get(2); d = abcd.get(3); //recalculate normal this.n = new Vector3(a, b, c); //scale coefficients down double s = this.n.magnitude(); a /= s; b /= s; c /= s; d /= s; this.n.scale(s); //same as normalize, but why take extra sqrt? this.abc = new Vector3(a, b, c); } public Plane deepCopy() { return new Plane(a, b, c, d); } public String toString() { return "("+a+", "+b+", "+c+", "+d+")"; } }