PageRenderTime 20ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/util/math/Plane.java

http://mt4j.googlecode.com/
Java | 151 lines | 52 code | 22 blank | 77 comment | 4 complexity | 389f8fcfd49d2e2080125b139edafb5c MD5 | raw file
  1/***********************************************************************
  2 * mt4j Copyright (c) 2008 - 2009 C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3 *  
  4 *   This program is free software: you can redistribute it and/or modify
  5 *   it under the terms of the GNU General Public License as published by
  6 *   the Free Software Foundation, either version 3 of the License, or
  7 *   (at your option) any later version.
  8 *
  9 *   This program is distributed in the hope that it will be useful,
 10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *   GNU General Public License for more details.
 13 *
 14 *   You should have received a copy of the GNU General Public License
 15 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16 *
 17 ***********************************************************************/
 18package org.mt4j.util.math;
 19
 20/**
 21 * The Class Plane.
 22 */
 23public class Plane {
 24
 25	 /** The Constant XY. */
 26 	public static final Plane XY = new Plane(new Vector3D(), Vector3D.Z_AXIS);
 27     
 28     /** The Constant XZ. */
 29     public static final Plane XZ = new Plane(new Vector3D(), Vector3D.Y_AXIS);
 30     
 31     /** The Constant YZ. */
 32     public static final Plane YZ = new Plane(new Vector3D(), Vector3D.X_AXIS);
 33
 34     public static final int PLANE_FRONT = -1;
 35
 36     public static final int PLANE_BACK = 1;
 37
 38     public static final int ON_PLANE = 0;
 39
 40     /** The Normal of the plane. */
 41     public Vector3D normal;
 42     
 43     /** The origin of the plane (a point in the plane). */
 44     public Vector3D origin;
 45
 46     /**
 47      * The Constructor.
 48      * 
 49      * @param origin the origin
 50      * @param norm the norm
 51      */
 52     public Plane(Vector3D origin, Vector3D norm) {
 53    	 this.origin = origin;
 54         this.normal = norm.getNormalized();
 55     }
 56     
 57     /**
 58      * The Constructor.
 59      * 
 60      * @param v0 the v0
 61      * @param v1 the v1
 62      * @param v2 the v2
 63      */
 64     public Plane(Vector3D v0, Vector3D v1, Vector3D v2) {
 65    	 this.normal = ToolsGeometry.getNormal(v0, v1, v2, true);
 66    	 this.origin = v0;
 67     }
 68
 69     public void reconstruct(Vector3D v0, Vector3D v1, Vector3D v2) {
 70    	 this.normal = ToolsGeometry.getNormal(v0, v1, v2, true);
 71    	 this.origin = v0;
 72     }
 73     
 74     public void reconstruct(Vector3D origin, Vector3D norm) {
 75    	 this.origin = origin;
 76         this.normal = norm.getNormalized();
 77     }
 78     
 79//TODO transform methods?
 80     
 81     /**
 82      * Calculates distance from the plane to point P.
 83      * 
 84      * @param p the p
 85      * 
 86      * @return distance
 87      */
 88     public float getDistanceToPoint(Vector3D p) {
 89    	 float sn = - normal.dot(p.getSubtracted(this.origin));
 90    	 float sd = normal.lengthSquared();
 91    	 Vector3D isec = p.getAdded(normal.getScaled(sn / sd));
 92    	 return Vector3D.distance(isec, p);
 93     }
 94
 95     /**
 96      * Calculates the intersection point between plane and ray (line).
 97      * 
 98      * @param r the r
 99      * 
100      * @return intersection point or null if ray doesn't intersect plane
101      */
102     public Vector3D getIntersectionLocal(Ray r) {
103//             float denom = normal.dot(r.getRayDirectionNormalized());
104//             if (denom > FastMath.FLT_EPSILON) {
105//                     float u = normal.dot(this.origin.getSubtracted(r.getRayStartPoint())) / denom;
106//                     Vector3D p =  r.getRayStartPoint().getAdded(r.getRayDirectionNormalized().getScaled(u));  
107//                     return p;
108//             } else
109//                     return null;
110             return ToolsGeometry.getRayPlaneIntersection(r, this.normal, this.origin);
111     }
112
113     /**
114      * Classifies the relative position of the given point to the plane.
115      * 
116      * @param p the p
117      * 
118      * @return One of the 3 integer classification codes: PLANE_FRONT, PLANE_BACK, ON_PLANE
119      */
120     public int classifyPoint(Vector3D p) {
121             float d = this.origin.getSubtracted(p).dot(normal);
122             if (d < - ToolsMath.FLT_EPSILON)
123                     return PLANE_FRONT;
124             else if (d > ToolsMath.FLT_EPSILON)
125                     return PLANE_BACK;
126             return ON_PLANE;
127     }
128     
129     /**
130      * Component contains point local.
131      * 
132      * @param testPoint the test point
133      * 
134      * @return true, if successful
135      */
136     public boolean componentContainsPointLocal(Vector3D testPoint) {
137    	 return this.classifyPoint(testPoint) == ON_PLANE;
138     }
139
140     
141     /* (non-Javadoc)
142     * @see java.lang.Object#toString()
143     */
144    public String toString() {
145             StringBuffer sb = new StringBuffer();
146             sb.append("origin: ").append(super.toString()).append(" norm: ").append(normal.toString());
147             return sb.toString();
148     }
149
150	
151}