PolyTools.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file PolyTools.h, various utilities for our dealings with arbitrary polygons */
  34. #pragma once
  35. #ifndef AI_POLYTOOLS_H_INCLUDED
  36. #define AI_POLYTOOLS_H_INCLUDED
  37. #include <assimp/material.h>
  38. #include <assimp/ai_assert.h>
  39. namespace Assimp {
  40. // -------------------------------------------------------------------------------
  41. /** Compute the signed area of a triangle.
  42. * The function accepts an unconstrained template parameter for use with
  43. * both aiVector3D and aiVector2D, but generally ignores the third coordinate.*/
  44. template <typename T>
  45. inline double GetArea2D(const T& v1, const T& v2, const T& v3) {
  46. return 0.5 * (v1.x * ((double)v3.y - v2.y) + v2.x * ((double)v1.y - v3.y) + v3.x * ((double)v2.y - v1.y));
  47. }
  48. // -------------------------------------------------------------------------------
  49. /** Test if a given point p2 is on the left side of the line formed by p0-p1.
  50. * The function accepts an unconstrained template parameter for use with
  51. * both aiVector3D and aiVector2D, but generally ignores the third coordinate.*/
  52. template <typename T>
  53. inline int OnLeftSideOfLine2D(const T& p0, const T& p1,const T& p2) {
  54. double area = GetArea2D(p0,p2,p1);
  55. if(std::abs(area) < ai_epsilon)
  56. return 0;
  57. else if(area > 0)
  58. return 1;
  59. else
  60. return -1;
  61. }
  62. // -------------------------------------------------------------------------------
  63. /** Test if a given point is inside a given triangle in R2.
  64. * The function accepts an unconstrained template parameter for use with
  65. * both aiVector3D and aiVector2D, but generally ignores the third coordinate.*/
  66. template <typename T>
  67. inline bool PointInTriangle2D(const T& p0, const T& p1,const T& p2, const T& pp) {
  68. // pp should be left side of the three triangle side, by ccw arrow
  69. int c1 = OnLeftSideOfLine2D(p0, p1, pp);
  70. int c2 = OnLeftSideOfLine2D(p1, p2, pp);
  71. int c3 = OnLeftSideOfLine2D(p2, p0, pp);
  72. return (c1 >= 0) && (c2 >= 0) && (c3 >= 0);
  73. }
  74. // -------------------------------------------------------------------------------
  75. /** Check whether the winding order of a given polygon is counter-clockwise.
  76. * The function accepts an unconstrained template parameter, but is intended
  77. * to be used only with aiVector2D and aiVector3D (z axis is ignored, only
  78. * x and y are taken into account).
  79. * @note Code taken from http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian/applet1.html and translated to C++
  80. */
  81. template <typename T>
  82. inline bool IsCCW(T* in, size_t npoints) {
  83. double aa, bb, cc, b, c, theta;
  84. double convex_turn;
  85. double convex_sum = 0;
  86. ai_assert(npoints >= 3);
  87. for (size_t i = 0; i < npoints - 2; i++) {
  88. aa = ((in[i+2].x - in[i].x) * (in[i+2].x - in[i].x)) +
  89. ((-in[i+2].y + in[i].y) * (-in[i+2].y + in[i].y));
  90. bb = ((in[i+1].x - in[i].x) * (in[i+1].x - in[i].x)) +
  91. ((-in[i+1].y + in[i].y) * (-in[i+1].y + in[i].y));
  92. cc = ((in[i+2].x - in[i+1].x) *
  93. (in[i+2].x - in[i+1].x)) +
  94. ((-in[i+2].y + in[i+1].y) *
  95. (-in[i+2].y + in[i+1].y));
  96. b = std::sqrt(bb);
  97. c = std::sqrt(cc);
  98. theta = std::acos((bb + cc - aa) / (2 * b * c));
  99. if (OnLeftSideOfLine2D(in[i],in[i+2],in[i+1]) == 1) {
  100. // if (convex(in[i].x, in[i].y,
  101. // in[i+1].x, in[i+1].y,
  102. // in[i+2].x, in[i+2].y)) {
  103. convex_turn = AI_MATH_PI_F - theta;
  104. convex_sum += convex_turn;
  105. } else {
  106. convex_sum -= AI_MATH_PI_F - theta;
  107. }
  108. }
  109. aa = ((in[1].x - in[npoints-2].x) *
  110. (in[1].x - in[npoints-2].x)) +
  111. ((-in[1].y + in[npoints-2].y) *
  112. (-in[1].y + in[npoints-2].y));
  113. bb = ((in[0].x - in[npoints-2].x) *
  114. (in[0].x - in[npoints-2].x)) +
  115. ((-in[0].y + in[npoints-2].y) *
  116. (-in[0].y + in[npoints-2].y));
  117. cc = ((in[1].x - in[0].x) * (in[1].x - in[0].x)) +
  118. ((-in[1].y + in[0].y) * (-in[1].y + in[0].y));
  119. b = std::sqrt(bb);
  120. c = std::sqrt(cc);
  121. theta = std::acos((bb + cc - aa) / (2 * b * c));
  122. //if (convex(in[npoints-2].x, in[npoints-2].y,
  123. // in[0].x, in[0].y,
  124. // in[1].x, in[1].y)) {
  125. if (OnLeftSideOfLine2D(in[npoints-2],in[1],in[0]) == 1) {
  126. convex_turn = AI_MATH_PI_F - theta;
  127. convex_sum += convex_turn;
  128. } else {
  129. convex_sum -= AI_MATH_PI_F - theta;
  130. }
  131. return convex_sum >= (2 * AI_MATH_PI_F);
  132. }
  133. // -------------------------------------------------------------------------------
  134. /** Compute the normal of an arbitrary polygon in R3.
  135. *
  136. * The code is based on Newell's formula, that is a polygons normal is the ratio
  137. * of its area when projected onto the three coordinate axes.
  138. *
  139. * @param out Receives the output normal
  140. * @param num Number of input vertices
  141. * @param x X data source. x[ofs_x*n] is the n'th element.
  142. * @param y Y data source. y[ofs_y*n] is the y'th element
  143. * @param z Z data source. z[ofs_z*n] is the z'th element
  144. *
  145. * @note The data arrays must have storage for at least num+2 elements. Using
  146. * this method is much faster than the 'other' NewellNormal()
  147. */
  148. template <int ofs_x, int ofs_y, int ofs_z, typename TReal>
  149. inline void NewellNormal (aiVector3t<TReal>& out, int num, TReal* x, TReal* y, TReal* z) {
  150. // Duplicate the first two vertices at the end
  151. x[(num+0)*ofs_x] = x[0];
  152. x[(num+1)*ofs_x] = x[ofs_x];
  153. y[(num+0)*ofs_y] = y[0];
  154. y[(num+1)*ofs_y] = y[ofs_y];
  155. z[(num+0)*ofs_z] = z[0];
  156. z[(num+1)*ofs_z] = z[ofs_z];
  157. TReal sum_xy = 0.0, sum_yz = 0.0, sum_zx = 0.0;
  158. TReal *xptr = x +ofs_x, *xlow = x, *xhigh = x + ofs_x*2;
  159. TReal *yptr = y +ofs_y, *ylow = y, *yhigh = y + ofs_y*2;
  160. TReal *zptr = z +ofs_z, *zlow = z, *zhigh = z + ofs_z*2;
  161. for (int tmp=0; tmp < num; tmp++) {
  162. sum_xy += (*xptr) * ( (*yhigh) - (*ylow) );
  163. sum_yz += (*yptr) * ( (*zhigh) - (*zlow) );
  164. sum_zx += (*zptr) * ( (*xhigh) - (*xlow) );
  165. xptr += ofs_x;
  166. xlow += ofs_x;
  167. xhigh += ofs_x;
  168. yptr += ofs_y;
  169. ylow += ofs_y;
  170. yhigh += ofs_y;
  171. zptr += ofs_z;
  172. zlow += ofs_z;
  173. zhigh += ofs_z;
  174. }
  175. out = aiVector3t<TReal>(sum_yz,sum_zx,sum_xy);
  176. }
  177. } // ! namespace Assimp
  178. #endif // AI_POLYTOOLS_H_INCLUDED