/** * @file Plane3d.java * @author Robert S Laramee * Start Date Saturday, 07 November 1998 * "Finish" Date Friday, 13 November 1998 * * Description This file contains methods associated with planes in 3D. */ //import matrix.*; public class Plane3d { /** * Epsilon value -to make this plane "thick" * The statics to differentiate between front/back/spanning * "evaluations" of a point... */ public final static double EPSILON = 0.001; public final static int SPANNING = 0; public final static int IN_FRONT = 1; public final static int IN_BACK = 2; public final static int COINCIDENT = 3; /** * The 4 element array, the plane coefficients * Ax + By + Cz + D = 0 * And just for the fun of it, we'll have data structures for the * individual coefficients too. */ private double[] coefficients; private double A = 0.0; private double B = 0.0; private double C = 0.0; private double D = 0.0; /** * Constructs a new Plane3d object */ public Plane3d(Face f) { coefficients = new double[4]; computePlaneEquation(f); } /** * Sets A, B, C, D in the plane equation: * Ax + By + Cz + D = 0 */ public void setA(double a) { coefficients [0] = a; A = a; } public void setB(double b) { coefficients [1] = b; B = b; } public void setC(double c) { coefficients [2] = c; C = c; } public void setD(double d) { coefficients [3] = d; D = d; } /** * Gets A, B, C, D in the plane equation: * Ax + By + Cz + D = 0 */ public double getA() { return coefficients [0]; } public double getB() { return coefficients [1]; } public double getC() { return coefficients [2]; } public double getD() { return coefficients [3]; } public double[] getCoefficients() { return coefficients; } /** * This function computes the plane equation for a given face. * Given plane equation: Ax + By + Cz + D = 0 * 1. Find A,B,C from computing the normal to the face and * 2. Substitute one any 1 of the 3 points (P0, P1, P2) to find D * e.g. D = -(A*P0x + B*P0y + C*P0z) * @param face -the face of a cube or tetrahedron */ public void computePlaneEquation(Face face) { A = face._norm.x; coefficients[0] = A; B = face._norm.y; coefficients[1] = B; C = face._norm.z; coefficients[2] = C; D = -(A*face._verts[0].x + B*face._verts[0].y + C*face._verts[0].z); coefficients[3] = D; } /** * a function to evaluate whether a point (Point4) is IN_FRONT or * IN_BACK of the partition plane. It also gives the plane EPSILON * in thickness. * Substitute point (x,y,z) into the plane equation * Ax + By + Cz + D = 0 * The result is the distance from the plane to the point along the * plane's normal * IF abs(distance) < EPSILON (and do this test first) * point is on the plane * IF distance > 0 THEN * point is on front (positive) side of plane * IF distance < 0 THEN * point is on back (negative) side of plane * * @param p the point to evaluate * @return the distance from the point to the plane */ public double pointPlaneDistance(Point4 point) { return A*point.x + B*point.y + C*point.z + D; } /** * Converts instance to a string * @return A string that represents the equation coefficients */ public String toString() { return new String("A:" + A + ", B:" + B + "\n" + "C:" + C + ", D:" + D); } /** * Function to split a line with this plane. *

All this function will do is return the intersection point. * * From: grelb.src.gla.ac.uk:8000/~acrobat/CompGraf/FAQs/C.G.Al * * Subject: 9) How do I find the intersection of a line and a plane? * * The plane is defined as: A*x + B*y + C*z + D = 0 * The line is defined as: * * x = x1 + (x2 - x1)*t = x1 + i*t * y = y1 + (y2 - y1)*t = y1 + j*t * z = z1 + (z2 - z1)*t = z1 + k*t * * THEN just substitute these into the plane equation. You end up with: * * t = - (A*x1 + B*y1 + C*z1 + D)/(A*i + B*j + C*k) * * IF the denominator is zero, then the vector (a,b,c) and the vector * (i,j,k) are perpendicular. Note that (a,b,c) is the normal to the * plane and (i,j,k) is the direction of the line. It follows that * the line is either parallel to the plane or contained in the * plane. In either case there is no unique intersection point. * * @param point1 -the first point * @param point2 -the second point * @return -the intersection point */ public Point4 linePlaneIntersect(Point4 point1, Point4 point2) { double x,y,z,t; x = y = z = t = 0.0; double denominator; denominator = ( (A * (point2.x - point1.x)) + (B * (point2.y - point1.y)) + (C * (point2.z - point1.z)) ); if (denominator == 0) { System.out.println("** Error: The line and plane are parallel."); } else { t = - (A * point1.x + B * point1.y + C * point1.z + D)/denominator; x = point1.x + ((point2.x - point1.x) * t); y = point1.y + ((point2.y - point1.y) * t); z = point1.z + ((point2.z - point1.z) * t); } return new Point4(x, y, z); } }