PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://mt4j.googlecode.com/
Java | 166 lines | 50 code | 21 blank | 95 comment | 0 complexity | 2c8c897561856310d48bbef481f4f7a9 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 Ray.
 22 * 
 23 * @author C.Ruff
 24 */
 25public class Ray {
 26	
 27	/** The ray start point. */
 28	private Vector3D rayStartPoint;
 29	
 30	/** The point in ray direction. */
 31	private Vector3D pointInRayDirection;
 32	
 33	/** The ray direction. */
 34	private Vector3D rayDirection;
 35	
 36	
 37	/**
 38	 * Instantiates a new ray.
 39	 * 
 40	 * @param ray the ray
 41	 */
 42	public Ray(Ray ray){
 43		super();
 44		this.rayStartPoint 			= ray.getRayStartPoint().getCopy();
 45		this.pointInRayDirection 	= ray.getPointInRayDirection().getCopy();
 46	}
 47	
 48	/**
 49	 * Instantiates a new ray.
 50	 * 
 51	 * @param rayStartPoint the ray start point
 52	 * @param pointInRayDirection the point in ray direction
 53	 */
 54	public Ray(Vector3D rayStartPoint, Vector3D pointInRayDirection) {
 55		super();
 56		this.rayStartPoint 			= rayStartPoint;
 57		this.pointInRayDirection 	= pointInRayDirection;
 58	}
 59	
 60	
 61	/**
 62	 * Gets the ray direction.
 63	 * 
 64	 * @return the ray direction
 65	 */
 66	public Vector3D getRayDirection(){
 67		return pointInRayDirection.getSubtracted(rayStartPoint);
 68	}
 69	
 70	/**
 71	 * Gets the ray direction normalized.
 72	 * 
 73	 * @return the ray direction normalized
 74	 */
 75	public Vector3D getRayDirectionNormalized(){
 76		return getRayDirection().normalizeLocal();
 77	}
 78
 79	/**
 80	 * Gets the point in ray direction.
 81	 * 
 82	 * @return the point in ray direction
 83	 */
 84	public Vector3D getPointInRayDirection() {
 85		return pointInRayDirection;
 86	}
 87
 88	/**
 89	 * Sets the point in ray direction.
 90	 * 
 91	 * @param pointInRayDirection the new point in ray direction
 92	 */
 93	public void setPointInRayDirection(Vector3D pointInRayDirection) {
 94		this.pointInRayDirection = pointInRayDirection;
 95	}
 96
 97	/**
 98	 * Gets the ray start point.
 99	 * 
100	 * @return the ray start point
101	 */
102	public Vector3D getRayStartPoint() {
103		return rayStartPoint;
104	}
105
106	/**
107	 * Sets the ray start point.
108	 * 
109	 * @param rayStartPoint the new ray start point
110	 */
111	public void setRayStartPoint(Vector3D rayStartPoint) {
112		this.rayStartPoint = rayStartPoint;
113	}
114	
115	/**
116	 * Transforms the ray.
117	 * The direction vector is multiplied with the transpose of the matrix.
118	 * 
119	 * @param m the m
120	 */
121	public void transform(Matrix m){
122		rayStartPoint.transform(m);
123		
124//		pointInRayDirection.transformNormal(m);
125//		pointInRayDirection.normalize(); //FIXME TRIAL REMOVE? NO oder vielleicht gleich bei transformNOrmal mit rein?
126//		
127//		pointInRayDirection = rayStartPoint.plus(pointInRayDirection);
128		
129		pointInRayDirection.transform(m);
130	}
131	
132	/**
133	 * Calculates and returns the direction vector.
134	 * This is calced by subtracting the rayStartpoint from the point in the rays direction.
135	 * 
136	 * @return the direction
137	 */
138	public Vector3D getDirection(){
139		return pointInRayDirection.getSubtracted(rayStartPoint);
140	}
141	
142	/**
143	 * Returns a new ray, transformed by the matrix.
144	 * 
145	 * @param ray the ray
146	 * @param m the m
147	 * 
148	 * @return the transformed ray
149	 */
150	public static Ray getTransformedRay(Ray ray, Matrix m){
151//		if (!Matrix.equalIdentity(m)){
152			//Get a copy of the origonal ray
153			Ray transformedRay = new Ray(ray);
154			transformedRay.transform(m);
155			return transformedRay;
156//		}else{
157//			return ray;
158//		}
159	}
160	
161	@Override
162	public String toString(){
163		return "Ray start: " + this.rayStartPoint + " PointInRayDirection: " + this.pointInRayDirection + " " + super.toString();
164	}
165
166}