/* * 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 9, 2005 */ package eu.moonlight3d.math; /** * A helper class representing a ray by two vectors, a starting position * and a direction. * * @author gregor */ public class Ray { public Vector3D start; public Vector3D direction; public Ray() { start=new Vector3D(); direction=new Vector3D(); } public Ray(Vector3D start, Vector3D direction) { this.start=start.clone(); this.direction=direction.clone(); } public boolean containsPoint(Vector3D point) { Vector3D pointDir=point.clone(); Vector3D.sub(pointDir, start); Vector3D.norm(pointDir); Vector3D normedDir=direction.norm(); if(pointDir.equals(normedDir)) { return true; } Vector3D.multiply(normedDir, -1); if(pointDir.equals(normedDir)) { return true; } return false; } public Vector3D getIntersectionPoint(Ray ray) { // TODO test this extensively // TODO check if there is a simpler way of computing this // compute possible intersection using projection into XY plane double denominator=(direction.X1*ray.direction.X2 - direction.X2*ray.direction.X1); if(denominator!=0) { double lambda=-(start.X1*ray.direction.X2 - start.X2*ray.direction.X1 - ray.start.X1*ray.direction.X2 + ray.start.X2*ray.direction.X1)/denominator; Vector3D intersectionPoint=direction.clone(); Vector3D.multiply(intersectionPoint, lambda); Vector3D.add(intersectionPoint, start); if(ray.containsPoint(intersectionPoint)) { return intersectionPoint; } } // compute possible intersection using projection into XZ plane denominator=(direction.X1*ray.direction.X3 - direction.X3*ray.direction.X1); if(denominator!=0) { double lambda=-(start.X1*ray.direction.X3 - start.X3*ray.direction.X1 - ray.start.X1*ray.direction.X3 + ray.start.X3*ray.direction.X1)/denominator; Vector3D intersectionPoint=direction.clone(); Vector3D.multiply(intersectionPoint, lambda); Vector3D.add(intersectionPoint, start); if(ray.containsPoint(intersectionPoint)) { return intersectionPoint; } } // compute possible intersection using projection into YZ plane denominator=(direction.X2*ray.direction.X3 - direction.X3*ray.direction.X2); if(denominator!=0) { double lambda=-(start.X2*ray.direction.X3 - start.X3*ray.direction.X2 - ray.start.X2*ray.direction.X3 + ray.start.X3*ray.direction.X2)/denominator; Vector3D intersectionPoint=direction.clone(); Vector3D.multiply(intersectionPoint, lambda); Vector3D.add(intersectionPoint, start); if(ray.containsPoint(intersectionPoint)) { return intersectionPoint; } } return null; } }