plane.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. #include "sphere.h"
  26. #include "vector3.h"
  27. #include "math_types.h"
  28. namespace crown
  29. {
  30. /// Functions to manipulate Plane.
  31. ///
  32. /// @ingroup Math
  33. namespace plane
  34. {
  35. /// Normalizes the plane @æ p and returns its result.
  36. Plane& normalize(Plane& p);
  37. /// Returns the signed distance between plane @a p and point @a point.
  38. float distance_to_point(const Plane& p, const Vector3& point);
  39. } // namespace plane
  40. namespace plane
  41. {
  42. const Plane ZERO = Plane(vector3::ZERO, 0.0);
  43. const Plane XAXIS = Plane(vector3::XAXIS, 0.0);
  44. const Plane YAXIS = Plane(vector3::YAXIS, 0.0);
  45. const Plane ZAXIS = Plane(vector3::ZAXIS, 0.0);
  46. //-----------------------------------------------------------------------------
  47. inline Plane& normalize(Plane& p)
  48. {
  49. float len = vector3::length(p.n);
  50. if (math::equals(len, (float) 0.0))
  51. {
  52. return p;
  53. }
  54. const float inv_len = (float) 1.0 / len;
  55. p.n *= inv_len;
  56. p.d *= inv_len;
  57. return p;
  58. }
  59. //-----------------------------------------------------------------------------
  60. inline float distance_to_point(const Plane& p, const Vector3& point)
  61. {
  62. return vector3::dot(p.n, point) + p.d;
  63. }
  64. } // namespace plane
  65. //-----------------------------------------------------------------------------
  66. inline Plane::Plane()
  67. {
  68. // Do not initialize
  69. }
  70. //-----------------------------------------------------------------------------
  71. inline Plane::Plane(const Vector3& normal, float dist)
  72. : n(normal), d(dist)
  73. {
  74. }
  75. } // namespace crown