/* * 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 ml.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 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)); } /** * 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) { if(normal.multiply(ray.direction)==0) { return null; } double lambda=(-ray.start.multiply(normal)-distance)/ray.direction.multiply(normal); if(lambda<0) { // the intersection is "behind" the start of the ray return null; } Vector3D intersectionPoint=ray.start.add(ray.direction.multiply(lambda)); return intersectionPoint; } }