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

http://mt4j.googlecode.com/ · Java · 151 lines · 52 code · 22 blank · 77 comment · 4 complexity · 389f8fcfd49d2e2080125b139edafb5c 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 Plane.
  21. */
  22. public class Plane {
  23. /** The Constant XY. */
  24. public static final Plane XY = new Plane(new Vector3D(), Vector3D.Z_AXIS);
  25. /** The Constant XZ. */
  26. public static final Plane XZ = new Plane(new Vector3D(), Vector3D.Y_AXIS);
  27. /** The Constant YZ. */
  28. public static final Plane YZ = new Plane(new Vector3D(), Vector3D.X_AXIS);
  29. public static final int PLANE_FRONT = -1;
  30. public static final int PLANE_BACK = 1;
  31. public static final int ON_PLANE = 0;
  32. /** The Normal of the plane. */
  33. public Vector3D normal;
  34. /** The origin of the plane (a point in the plane). */
  35. public Vector3D origin;
  36. /**
  37. * The Constructor.
  38. *
  39. * @param origin the origin
  40. * @param norm the norm
  41. */
  42. public Plane(Vector3D origin, Vector3D norm) {
  43. this.origin = origin;
  44. this.normal = norm.getNormalized();
  45. }
  46. /**
  47. * The Constructor.
  48. *
  49. * @param v0 the v0
  50. * @param v1 the v1
  51. * @param v2 the v2
  52. */
  53. public Plane(Vector3D v0, Vector3D v1, Vector3D v2) {
  54. this.normal = ToolsGeometry.getNormal(v0, v1, v2, true);
  55. this.origin = v0;
  56. }
  57. public void reconstruct(Vector3D v0, Vector3D v1, Vector3D v2) {
  58. this.normal = ToolsGeometry.getNormal(v0, v1, v2, true);
  59. this.origin = v0;
  60. }
  61. public void reconstruct(Vector3D origin, Vector3D norm) {
  62. this.origin = origin;
  63. this.normal = norm.getNormalized();
  64. }
  65. //TODO transform methods?
  66. /**
  67. * Calculates distance from the plane to point P.
  68. *
  69. * @param p the p
  70. *
  71. * @return distance
  72. */
  73. public float getDistanceToPoint(Vector3D p) {
  74. float sn = - normal.dot(p.getSubtracted(this.origin));
  75. float sd = normal.lengthSquared();
  76. Vector3D isec = p.getAdded(normal.getScaled(sn / sd));
  77. return Vector3D.distance(isec, p);
  78. }
  79. /**
  80. * Calculates the intersection point between plane and ray (line).
  81. *
  82. * @param r the r
  83. *
  84. * @return intersection point or null if ray doesn't intersect plane
  85. */
  86. public Vector3D getIntersectionLocal(Ray r) {
  87. // float denom = normal.dot(r.getRayDirectionNormalized());
  88. // if (denom > FastMath.FLT_EPSILON) {
  89. // float u = normal.dot(this.origin.getSubtracted(r.getRayStartPoint())) / denom;
  90. // Vector3D p = r.getRayStartPoint().getAdded(r.getRayDirectionNormalized().getScaled(u));
  91. // return p;
  92. // } else
  93. // return null;
  94. return ToolsGeometry.getRayPlaneIntersection(r, this.normal, this.origin);
  95. }
  96. /**
  97. * Classifies the relative position of the given point to the plane.
  98. *
  99. * @param p the p
  100. *
  101. * @return One of the 3 integer classification codes: PLANE_FRONT, PLANE_BACK, ON_PLANE
  102. */
  103. public int classifyPoint(Vector3D p) {
  104. float d = this.origin.getSubtracted(p).dot(normal);
  105. if (d < - ToolsMath.FLT_EPSILON)
  106. return PLANE_FRONT;
  107. else if (d > ToolsMath.FLT_EPSILON)
  108. return PLANE_BACK;
  109. return ON_PLANE;
  110. }
  111. /**
  112. * Component contains point local.
  113. *
  114. * @param testPoint the test point
  115. *
  116. * @return true, if successful
  117. */
  118. public boolean componentContainsPointLocal(Vector3D testPoint) {
  119. return this.classifyPoint(testPoint) == ON_PLANE;
  120. }
  121. /* (non-Javadoc)
  122. * @see java.lang.Object#toString()
  123. */
  124. public String toString() {
  125. StringBuffer sb = new StringBuffer();
  126. sb.append("origin: ").append(super.toString()).append(" norm: ").append(normal.toString());
  127. return sb.toString();
  128. }
  129. }