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

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