💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.3 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_GEOM_PLANE_HPP
  20. #define ANTKEEPER_GEOM_PLANE_HPP
  21. #include "math/math.hpp"
  22. namespace geom {
  23. template <class T>
  24. struct plane
  25. {
  26. typedef math::vector<T, 3> vector_type;
  27. /// Plane normal vector.
  28. vector_type normal;
  29. /// Plane distance.
  30. T distance;
  31. /**
  32. * Creates a plane given a normal vector and distance.
  33. */
  34. plane(const vector_type& normal, T distance);
  35. /**
  36. * Creates a plane given a normal vector and offset vector.
  37. */
  38. plane(const vector_type& normal, const vector_type& offset);
  39. /**
  40. * Creates a plane given three points.
  41. */
  42. plane(const vector_type& a, const vector_type& b, const vector_type& c);
  43. /**
  44. * Creates a plane given its coefficients.
  45. *
  46. * @param coefficients Vector containing the plane coefficients, A, B, C and D, as x, y, z, and w, respectively.
  47. */
  48. plane(const math::vector<T, 4>& coefficients);
  49. /// Creates an uninitialized plane.
  50. plane() = default;
  51. /**
  52. * Calculates the signed distance between a plane and a vector.
  53. *
  54. * @param v Vector.
  55. * @return Signed distance between the plane and vector.
  56. */
  57. T signed_distance(const vector_type& v) const;
  58. /**
  59. * Calculates the point of intersection between three planes.
  60. */
  61. static vector_type intersection(const plane& p0, const plane& p1, const plane& p2);
  62. };
  63. template <class T>
  64. inline plane<T>::plane(const vector_type& normal, T distance):
  65. normal(normal),
  66. distance(distance)
  67. {}
  68. template <class T>
  69. plane<T>::plane(const vector_type& normal, const vector_type& offset):
  70. normal(normal),
  71. distance(-math::dot(normal, offset))
  72. {}
  73. template <class T>
  74. plane<T>::plane(const vector_type& a, const vector_type& b, const vector_type& c)
  75. {
  76. normal = math::normalize(math::cross(c - b, a - b));
  77. distance = -(math::dot(normal, b));
  78. }
  79. template <class T>
  80. plane<T>::plane(const math::vector<T, 4>& coefficients)
  81. {
  82. const vector_type abc = math::resize<3>(coefficients);
  83. const float inverse_length = T(1) / math::length(abc);
  84. normal = abc * inverse_length;
  85. distance = coefficients[3] * inverse_length;
  86. }
  87. template <class T>
  88. inline T plane<T>::signed_distance(const vector_type& v) const
  89. {
  90. return distance + math::dot(normal, v);
  91. }
  92. template <class T>
  93. typename plane<T>::vector_type plane<T>::intersection(const plane& p0, const plane& p1, const plane& p2)
  94. {
  95. return -(p0.distance * math::cross(p1.normal, p2.normal) + p1.distance * math::cross(p2.normal, p0.normal) + p2.distance * math::cross(p0.normal, p1.normal)) / math::dot(p0.normal, math::cross(p1.normal, p2.normal));
  96. }
  97. } // namespace geom
  98. #endif // ANTKEEPER_GEOM_PLANE_HPP