tri_tri_intersect.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2021 Vladimir S. FONOV <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/
  8. /*
  9. *
  10. * C++ version based on the routines published in
  11. * "Fast and Robust Triangle-Triangle Overlap Test
  12. * Using Orientation Predicates" P. Guigue - O. Devillers
  13. *
  14. * Works with Eigen data structures instead of plain C arrays
  15. * returns bool values
  16. *
  17. * Code is rewritten to get rid of the macros and use C++ lambda and
  18. * inline functions instead
  19. *
  20. * Original notice:
  21. *
  22. * Triangle-Triangle Overlap Test Routines
  23. * July, 2002
  24. * Updated December 2003
  25. *
  26. * Updated by Vladimir S. FONOV
  27. * March, 2023
  28. *
  29. * This file contains C implementation of algorithms for
  30. * performing two and three-dimensional triangle-triangle intersection test
  31. * The algorithms and underlying theory are described in
  32. *
  33. * "Fast and Robust Triangle-Triangle Overlap Test
  34. * Using Orientation Predicates" P. Guigue - O. Devillers
  35. *
  36. * Journal of Graphics Tools, 8(1), 2003
  37. *
  38. * Several geometric predicates are defined. Their parameters are all
  39. * points. Each point is an array of two or three double precision
  40. * floating point numbers. The geometric predicates implemented in
  41. * this file are:
  42. *
  43. * int tri_tri_overlap_test_3d(p1,q1,r1,p2,q2,r2)
  44. * int tri_tri_overlap_test_2d(p1,q1,r1,p2,q2,r2)
  45. *
  46. * int tri_tri_intersection_test_3d(p1,q1,r1,p2,q2,r2,
  47. * coplanar,source,target)
  48. *
  49. * is a version that computes the segment of intersection when
  50. * the triangles overlap (and are not coplanar)
  51. *
  52. * each function returns 1 if the triangles (including their
  53. * boundary) intersect, otherwise 0
  54. *
  55. *
  56. * Other information are available from the Web page
  57. * http://www.acm.org/jgt/papers/GuigueDevillers03/
  58. *
  59. */
  60. /*
  61. Original MIT License (from https://github.com/erich666/jgt-code)
  62. Copyright (c) <year> <copyright holders>
  63. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  64. and associated documentation files (the "Software"), to deal in the Software without
  65. restriction, including without limitation the rights to use, copy, modify, merge, publish,
  66. distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  67. Software is furnished to do so, subject to the following conditions:
  68. The above copyright notice and this permission notice shall be included in all copies or
  69. substantial portions of the Software.
  70. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  71. BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  72. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  73. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  74. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  75. */
  76. #pragma once
  77. #ifndef IGL_TRI_TRI_INTERSECT_H
  78. #define IGL_TRI_TRI_INTERSECT_H
  79. #include "igl_inline.h"
  80. #include <Eigen/Core>
  81. namespace igl {
  82. // Three-dimensional Triangle-Triangle overlap test
  83. // if triangles are co-planar
  84. //
  85. // Input:
  86. // p1,q1,r1 - vertices of the 1st triangle (3D)
  87. // p2,q2,r2 - vertices of the 2nd triangle (3D)
  88. //
  89. // Output:
  90. //
  91. // Return true if two triangles overlap
  92. template <typename DerivedP1,typename DerivedQ1,typename DerivedR1,
  93. typename DerivedP2,typename DerivedQ2,typename DerivedR2>
  94. IGL_INLINE bool tri_tri_overlap_test_3d(
  95. const Eigen::MatrixBase<DerivedP1> & p1,
  96. const Eigen::MatrixBase<DerivedQ1> & q1,
  97. const Eigen::MatrixBase<DerivedR1> & r1,
  98. const Eigen::MatrixBase<DerivedP2> & p2,
  99. const Eigen::MatrixBase<DerivedQ2> & q2,
  100. const Eigen::MatrixBase<DerivedR2> & r2);
  101. // Three-dimensional Triangle-Triangle Intersection Test
  102. // additionaly computes the segment of intersection of the two triangles if it exists.
  103. // coplanar returns whether the triangles are coplanar,
  104. // source and target are the endpoints of the line segment of intersection
  105. //
  106. // Input:
  107. // p1,q1,r1 - vertices of the 1st triangle (3D)
  108. // p2,q2,r2 - vertices of the 2nd triangle (3D)
  109. //
  110. // Output:
  111. // coplanar - flag if two triangles are coplanar
  112. // source - 1st point of intersection (if exists)
  113. // target - 2nd point in intersection (if exists)
  114. //
  115. // Return true if two triangles intersect
  116. template <typename DerivedP1,typename DerivedQ1,typename DerivedR1,
  117. typename DerivedP2,typename DerivedQ2,typename DerivedR2,
  118. typename DerivedS,typename DerivedT>
  119. IGL_INLINE bool tri_tri_intersection_test_3d(
  120. const Eigen::MatrixBase<DerivedP1> & p1, const Eigen::MatrixBase<DerivedQ1> & q1, const Eigen::MatrixBase<DerivedR1> & r1,
  121. const Eigen::MatrixBase<DerivedP2> & p2, const Eigen::MatrixBase<DerivedQ2> & q2, const Eigen::MatrixBase<DerivedR2> & r2,
  122. bool & coplanar,
  123. Eigen::MatrixBase<DerivedS> & source,
  124. Eigen::MatrixBase<DerivedT> & target );
  125. // Two dimensional Triangle-Triangle Overlap Test
  126. // Input:
  127. // p1,q1,r1 - vertices of the 1st triangle (2D)
  128. // p2,q2,r2 - vertices of the 2nd triangle (2D)
  129. //
  130. // Output:
  131. // Return true if two triangles overlap
  132. template <typename DerivedP1,typename DerivedQ1,typename DerivedR1,
  133. typename DerivedP2,typename DerivedQ2,typename DerivedR2>
  134. IGL_INLINE bool tri_tri_overlap_test_2d(
  135. const Eigen::MatrixBase<DerivedP1> &p1, const Eigen::MatrixBase<DerivedQ1> &q1, const Eigen::MatrixBase<DerivedR1> &r1,
  136. const Eigen::MatrixBase<DerivedP2> &p2, const Eigen::MatrixBase<DerivedQ2> &q2, const Eigen::MatrixBase<DerivedR2> &r2);
  137. };
  138. #ifndef IGL_STATIC_LIBRARY
  139. # include "tri_tri_intersect.cpp"
  140. #endif
  141. #endif // IGL_TRI_TRI_INTERSECT_H