💿🐜 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.

123 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. /**
  24. * A flat 2-dimensional surface.
  25. */
  26. template <class T>
  27. struct plane
  28. {
  29. typedef math::vector<T, 3> vector_type;
  30. /// Plane normal vector.
  31. vector_type normal;
  32. /// Plane distance.
  33. T distance;
  34. /**
  35. * Creates a plane given a normal vector and distance.
  36. */
  37. plane(const vector_type& normal, T distance);
  38. /**
  39. * Creates a plane given a normal vector and offset vector.
  40. */
  41. plane(const vector_type& normal, const vector_type& offset);
  42. /**
  43. * Creates a plane given three points.
  44. */
  45. plane(const vector_type& a, const vector_type& b, const vector_type& c);
  46. /**
  47. * Creates a plane given its coefficients.
  48. *
  49. * @param coefficients Vector containing the plane coefficients, A, B, C and D, as x, y, z, and w, respectively.
  50. */
  51. plane(const math::vector<T, 4>& coefficients);
  52. /// Creates an uninitialized plane.
  53. plane() = default;
  54. /**
  55. * Calculates the signed distance between a plane and a vector.
  56. *
  57. * @param v Vector.
  58. * @return Signed distance between the plane and vector.
  59. */
  60. T signed_distance(const vector_type& v) const;
  61. /**
  62. * Calculates the point of intersection between three planes.
  63. */
  64. static vector_type intersection(const plane& p0, const plane& p1, const plane& p2);
  65. };
  66. template <class T>
  67. inline plane<T>::plane(const vector_type& normal, T distance):
  68. normal(normal),
  69. distance(distance)
  70. {}
  71. template <class T>
  72. plane<T>::plane(const vector_type& normal, const vector_type& offset):
  73. normal(normal),
  74. distance(-math::dot(normal, offset))
  75. {}
  76. template <class T>
  77. plane<T>::plane(const vector_type& a, const vector_type& b, const vector_type& c)
  78. {
  79. normal = math::normalize(math::cross(c - b, a - b));
  80. distance = -(math::dot(normal, b));
  81. }
  82. template <class T>
  83. plane<T>::plane(const math::vector<T, 4>& coefficients)
  84. {
  85. const vector_type abc = math::resize<3>(coefficients);
  86. const float inverse_length = T(1) / math::length(abc);
  87. normal = abc * inverse_length;
  88. distance = coefficients[3] * inverse_length;
  89. }
  90. template <class T>
  91. inline T plane<T>::signed_distance(const vector_type& v) const
  92. {
  93. return distance + math::dot(normal, v);
  94. }
  95. template <class T>
  96. typename plane<T>::vector_type plane<T>::intersection(const plane& p0, const plane& p1, const plane& p2)
  97. {
  98. 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));
  99. }
  100. } // namespace geom
  101. #endif // ANTKEEPER_GEOM_PLANE_HPP