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