Triangle.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "Vec3.h"
  25. #include "Plane.h"
  26. #include "MathUtils.h"
  27. namespace crown
  28. {
  29. /// 3D Triangle.
  30. ///
  31. /// Used mainly for collision detection.
  32. class Triangle
  33. {
  34. public:
  35. /// Does nothing for efficiency.
  36. Triangle();
  37. Triangle(const Vec3& v1, const Vec3& v2, const Vec3& v3);
  38. ~Triangle();
  39. float area() const;
  40. /// Returns the center of gravity (a.k.a. "centroid").
  41. Vec3 centroid() const;
  42. /// Returns the barycentric coordinates of point @a p in respect to the triangle.
  43. Vec3 barycentric_coords(const Vec3& p) const;
  44. /// Returns whether the triangle contains the @a p point.
  45. bool contains_point(const Vec3& p) const;
  46. /// Returns the plane containing the triangle.
  47. Plane to_plane() const;
  48. private:
  49. // Vertices in CCW order
  50. Vec3 m_vertex[3];
  51. friend class Intersection;
  52. };
  53. //-----------------------------------------------------------------------------
  54. inline Triangle::Triangle()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. inline Triangle::Triangle(const Vec3& v1, const Vec3& v2, const Vec3& v3)
  59. {
  60. m_vertex[0] = v1;
  61. m_vertex[1] = v2;
  62. m_vertex[2] = v3;
  63. }
  64. //-----------------------------------------------------------------------------
  65. inline Triangle::~Triangle()
  66. {
  67. }
  68. //-----------------------------------------------------------------------------
  69. inline float Triangle::area() const
  70. {
  71. return ((m_vertex[1] - m_vertex[0]).cross(m_vertex[2] - m_vertex[0])).length() * (float)0.5;
  72. }
  73. //-----------------------------------------------------------------------------
  74. inline Vec3 Triangle::centroid() const
  75. {
  76. return (m_vertex[0] + m_vertex[1] + m_vertex[2]) * math::ONE_OVER_THREE;
  77. }
  78. //-----------------------------------------------------------------------------
  79. inline Vec3 Triangle::barycentric_coords(const Vec3& p) const
  80. {
  81. Vec3 e1 = m_vertex[2] - m_vertex[1];
  82. Vec3 e2 = m_vertex[0] - m_vertex[2];
  83. Vec3 e3 = m_vertex[1] - m_vertex[0];
  84. Vec3 d1 = p - m_vertex[0];
  85. Vec3 d2 = p - m_vertex[1];
  86. Vec3 d3 = p - m_vertex[2];
  87. Vec3 n = e1.cross(e2) / e1.cross(e2).length();
  88. // Signed areas
  89. float at = (float)(e1.cross(e2).dot(n) * 0.5);
  90. float at1 = (float)(e1.cross(d3).dot(n) * 0.5);
  91. float at2 = (float)(e2.cross(d1).dot(n) * 0.5);
  92. float at3 = (float)(e3.cross(d2).dot(n) * 0.5);
  93. float oneOverAt = (float)(1.0 / at);
  94. return Vec3(at1 * oneOverAt, at2 * oneOverAt, at3 * oneOverAt);
  95. }
  96. //-----------------------------------------------------------------------------
  97. inline bool Triangle::contains_point(const Vec3& p) const
  98. {
  99. Vec3 bc = barycentric_coords(p);
  100. if (bc.x < 0.0 || bc.y < 0.0 || bc.z < 0.0)
  101. {
  102. return false;
  103. }
  104. return true;
  105. }
  106. //-----------------------------------------------------------------------------
  107. inline Plane Triangle::to_plane() const
  108. {
  109. Vec3 e1 = m_vertex[2] - m_vertex[1];
  110. Vec3 e2 = m_vertex[1] - m_vertex[0];
  111. Vec3 n = e2.cross(e1).normalize();
  112. float d = -n.dot(m_vertex[0]);
  113. return Plane(n, d);
  114. }
  115. } // namespace crown