2
0

Plane.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2009-2010 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.math;
  33. import com.jme3.export.InputCapsule;
  34. import com.jme3.export.JmeExporter;
  35. import com.jme3.export.JmeImporter;
  36. import com.jme3.export.OutputCapsule;
  37. import com.jme3.export.Savable;
  38. import java.io.IOException;
  39. import java.util.logging.Logger;
  40. /**
  41. * <code>Plane</code> defines a plane where Normal dot (x,y,z) = Constant.
  42. * This provides methods for calculating a "distance" of a point from this
  43. * plane. The distance is pseudo due to the fact that it can be negative if the
  44. * point is on the non-normal side of the plane.
  45. *
  46. * @author Mark Powell
  47. * @author Joshua Slack
  48. */
  49. public class Plane implements Savable, Cloneable {
  50. private static final Logger logger = Logger
  51. .getLogger(Plane.class.getName());
  52. public static enum Side {
  53. None,
  54. Positive,
  55. Negative
  56. }
  57. /**
  58. * Vector normal to the plane.
  59. */
  60. protected Vector3f normal = new Vector3f();
  61. /**
  62. * Constant of the plane. See formula in class definition.
  63. */
  64. protected float constant;
  65. /**
  66. * Constructor instantiates a new <code>Plane</code> object. This is the
  67. * default object and contains a normal of (0,0,0) and a constant of 0.
  68. */
  69. public Plane() {
  70. }
  71. /**
  72. * Constructor instantiates a new <code>Plane</code> object. The normal
  73. * and constant values are set at creation.
  74. *
  75. * @param normal
  76. * the normal of the plane.
  77. * @param constant
  78. * the constant of the plane.
  79. */
  80. public Plane(Vector3f normal, float constant) {
  81. if (normal == null) {
  82. throw new IllegalArgumentException("normal cannot be null");
  83. }
  84. this.normal.set(normal);
  85. this.constant = constant;
  86. }
  87. /**
  88. * <code>setNormal</code> sets the normal of the plane.
  89. *
  90. * @param normal
  91. * the new normal of the plane.
  92. */
  93. public void setNormal(Vector3f normal) {
  94. if (normal == null) {
  95. throw new IllegalArgumentException("normal cannot be null");
  96. }
  97. this.normal.set(normal);
  98. }
  99. /**
  100. * <code>setNormal</code> sets the normal of the plane.
  101. *
  102. */
  103. public void setNormal(float x, float y, float z) {
  104. if (normal == null) {
  105. throw new IllegalArgumentException("normal cannot be null");
  106. }
  107. this.normal.set(x,y,z);
  108. }
  109. /**
  110. * <code>getNormal</code> retrieves the normal of the plane.
  111. *
  112. * @return the normal of the plane.
  113. */
  114. public Vector3f getNormal() {
  115. return normal;
  116. }
  117. /**
  118. * <code>setConstant</code> sets the constant value that helps define the
  119. * plane.
  120. *
  121. * @param constant
  122. * the new constant value.
  123. */
  124. public void setConstant(float constant) {
  125. this.constant = constant;
  126. }
  127. /**
  128. * <code>getConstant</code> returns the constant of the plane.
  129. *
  130. * @return the constant of the plane.
  131. */
  132. public float getConstant() {
  133. return constant;
  134. }
  135. public Vector3f getClosestPoint(Vector3f point, Vector3f store){
  136. // float t = constant - normal.dot(point);
  137. // return store.set(normal).multLocal(t).addLocal(point);
  138. float t = (constant - normal.dot(point)) / normal.dot(normal);
  139. return store.set(normal).multLocal(t).addLocal(point);
  140. }
  141. public Vector3f getClosestPoint(Vector3f point){
  142. return getClosestPoint(point, new Vector3f());
  143. }
  144. public Vector3f reflect(Vector3f point, Vector3f store){
  145. if (store == null)
  146. store = new Vector3f();
  147. float d = pseudoDistance(point);
  148. store.set(normal).negateLocal().multLocal(d * 2f);
  149. store.addLocal(point);
  150. return store;
  151. }
  152. /**
  153. * <code>pseudoDistance</code> calculates the distance from this plane to
  154. * a provided point. If the point is on the negative side of the plane the
  155. * distance returned is negative, otherwise it is positive. If the point is
  156. * on the plane, it is zero.
  157. *
  158. * @param point
  159. * the point to check.
  160. * @return the signed distance from the plane to a point.
  161. */
  162. public float pseudoDistance(Vector3f point) {
  163. return normal.dot(point) - constant;
  164. }
  165. /**
  166. * <code>whichSide</code> returns the side at which a point lies on the
  167. * plane. The positive values returned are: NEGATIVE_SIDE, POSITIVE_SIDE and
  168. * NO_SIDE.
  169. *
  170. * @param point
  171. * the point to check.
  172. * @return the side at which the point lies.
  173. */
  174. public Side whichSide(Vector3f point) {
  175. float dis = pseudoDistance(point);
  176. if (dis < 0) {
  177. return Side.Negative;
  178. } else if (dis > 0) {
  179. return Side.Positive;
  180. } else {
  181. return Side.None;
  182. }
  183. }
  184. public boolean isOnPlane(Vector3f point){
  185. float dist = pseudoDistance(point);
  186. if (dist < FastMath.FLT_EPSILON && dist > -FastMath.FLT_EPSILON)
  187. return true;
  188. else
  189. return false;
  190. }
  191. /**
  192. * Initialize this plane using the three points of the given triangle.
  193. *
  194. * @param t
  195. * the triangle
  196. */
  197. public void setPlanePoints(AbstractTriangle t) {
  198. setPlanePoints(t.get1(), t.get2(), t.get3());
  199. }
  200. /**
  201. * Initialize this plane using a point of origin and a normal.
  202. *
  203. * @param origin
  204. * @param normal
  205. */
  206. public void setOriginNormal(Vector3f origin, Vector3f normal){
  207. this.normal.set(normal);
  208. this.constant = normal.x * origin.x + normal.y * origin.y + normal.z * origin.z;
  209. }
  210. /**
  211. * Initialize the Plane using the given 3 points as coplanar.
  212. *
  213. * @param v1
  214. * the first point
  215. * @param v2
  216. * the second point
  217. * @param v3
  218. * the third point
  219. */
  220. public void setPlanePoints(Vector3f v1, Vector3f v2, Vector3f v3) {
  221. normal.set(v2).subtractLocal(v1);
  222. normal.crossLocal(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z)
  223. .normalizeLocal();
  224. constant = normal.dot(v1);
  225. }
  226. /**
  227. * <code>toString</code> returns a string thta represents the string
  228. * representation of this plane. It represents the normal as a
  229. * <code>Vector3f</code> object, so the format is the following:
  230. * com.jme.math.Plane [Normal: org.jme.math.Vector3f [X=XX.XXXX, Y=YY.YYYY,
  231. * Z=ZZ.ZZZZ] - Constant: CC.CCCCC]
  232. *
  233. * @return the string representation of this plane.
  234. */
  235. @Override
  236. public String toString() {
  237. return getClass().getSimpleName() + " [Normal: " + normal + " - Constant: "
  238. + constant + "]";
  239. }
  240. public void write(JmeExporter e) throws IOException {
  241. OutputCapsule capsule = e.getCapsule(this);
  242. capsule.write(normal, "normal", Vector3f.ZERO);
  243. capsule.write(constant, "constant", 0);
  244. }
  245. public void read(JmeImporter e) throws IOException {
  246. InputCapsule capsule = e.getCapsule(this);
  247. normal = (Vector3f) capsule.readSavable("normal", Vector3f.ZERO.clone());
  248. constant = capsule.readFloat("constant", 0);
  249. }
  250. @Override
  251. public Plane clone() {
  252. try {
  253. Plane p = (Plane) super.clone();
  254. p.normal = normal.clone();
  255. return p;
  256. } catch (CloneNotSupportedException e) {
  257. throw new AssertionError();
  258. }
  259. }
  260. }