plane.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. #include "sphere.h"
  8. #include "vector3.h"
  9. #include "math_types.h"
  10. namespace crown
  11. {
  12. /// Functions to manipulate Plane.
  13. ///
  14. /// @ingroup Math
  15. namespace plane
  16. {
  17. /// Normalizes the plane @a p and returns its result.
  18. Plane& normalize(Plane& p);
  19. /// Returns the signed distance between plane @a p and point @a point.
  20. float distance_to_point(const Plane& p, const Vector3& point);
  21. } // namespace plane
  22. namespace plane
  23. {
  24. const Plane ZERO = Plane(vector3::ZERO, 0.0);
  25. const Plane XAXIS = Plane(vector3::XAXIS, 0.0);
  26. const Plane YAXIS = Plane(vector3::YAXIS, 0.0);
  27. const Plane ZAXIS = Plane(vector3::ZAXIS, 0.0);
  28. inline Plane& normalize(Plane& p)
  29. {
  30. float len = vector3::length(p.n);
  31. if (equals(len, (float) 0.0))
  32. {
  33. return p;
  34. }
  35. const float inv_len = (float) 1.0 / len;
  36. p.n *= inv_len;
  37. p.d *= inv_len;
  38. return p;
  39. }
  40. inline float distance_to_point(const Plane& p, const Vector3& point)
  41. {
  42. return vector3::dot(p.n, point) + p.d;
  43. }
  44. } // namespace plane
  45. inline Plane::Plane()
  46. {
  47. // Do not initialize
  48. }
  49. inline Plane::Plane(const Vector3& normal, float dist)
  50. : n(normal), d(dist)
  51. {
  52. }
  53. } // namespace crown