/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Feb 15, 2005 */ package eu.moonlight3d.math; /** * This class represents a plane in Hesse form * * @author gregor */ public class Plane { public Vector3D normal; public double distance; /** * Simple constructor. */ public Plane() { normal=new Vector3D(); } /** * Construct a plane from a point lying on the plane and the * plane normal. * * @param point point lying on the plane * @param normal plane normal */ public void fromPointAndNormal(Vector3D point, Vector3D normal) { this.normal=normal.norm(); distance=-this.normal.multiply(point); } /** * Construct plane from a point and two vectors which lie in the plane * @param point a point which lies on the plane * @param a Vector defining plane direction * @param b Vector defining plane direction */ public void fromPointAndVectors(Vector3D point, Vector3D a, Vector3D b) { normal=a.crossMultiply(b).norm(); distance=-normal.multiply(point); } /** * Construct a plane through three given points * @param a Point defining the plane * @param b Point defining the plane * @param c Point defining the plane */ public void fromPoints(Vector3D a, Vector3D b, Vector3D c) { fromPointAndVectors(a,b.sub(a),c.sub(a)); } /** * Return the signed distance between the given point and the plane. Distances * are positive in the direction of the normal. * * @param point the point to compute the distance to * @return signed distance between point and plane */ public double getSignedDistance(Vector3D point) { double pointDistance=point.multiply(normal)+distance; return pointDistance; } /** * Return the distance between the given point and the plane. * * @param point the point to compute the distance to * @return the distance between point and plane */ public double getDistance(Vector3D point) { return Math.abs(getSignedDistance(point)); } /** * Calculate the intersection point between the given ray and this plane. * If the intersection point does not exist, the function returns null. * Since the ray starts in a point by definition, valid intersections * exist only from the starting point onward. * * @param ray the ray to test for intersection with * @param allowNegativeDistances allow intersection points with negative * distances, that is, points that lie behind the starting point on the ray * @return the intersection point or null if there is no intersection. */ public Vector3D getIntersectionPoint(Ray ray, boolean allowNegativeDistances) { if(normal.multiply(ray.direction)==0) { return null; } double lambda=(-ray.start.multiply(normal)-distance)/ray.direction.multiply(normal); if(lambda<0 && !allowNegativeDistances) { // the intersection is "behind" the start of the ray return null; } Vector3D intersectionPoint=ray.start.add(ray.direction.multiply(lambda)); return intersectionPoint; } /** * Calculate the intersection point between the given ray and this plane. * If the intersection point does not exist, the function returns null. * Since the ray starts in a point by definition, valid intersections * exist only from the starting point onward. * * @param ray the ray to test for intersection with * @return the intersection point or null if there is no intersection. */ public Vector3D getIntersectionPoint(Ray ray) { return getIntersectionPoint(ray, false); } }