AGX Dynamics  2.37.2.0
PolygonClipping.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXCOLLIDE_POLYGONCLIPPING_H
18 #define AGXCOLLIDE_POLYGONCLIPPING_H
19 
20 #include <agx/macros.h>
21 
23 
24 #include <agx/agx_vector_types.h>
25 #include <agx/agxPhysics_export.h>
26 #include <agx/StackArray.h>
28 
29 #include <agx/Integer.h>
30 
31 #ifndef _WIN32
32 #include <float.h>
33 #endif
34 
35 #include <agx/agx.h>
36 #include <agx/Vec3.h>
37 
38 namespace agxCollide
39 {
40 
41 
42  template <size_t N>
43  bool clipConvexPolygonAgainstTrianglePrism(
44  const agx::StackArray<agx::Vec3, N>& cPolygon,
45  const agx::StackArray<agx::Vec3,3>& triangle,
46  const agx::Vec3& normalTriangle,
48  const agx::Real epsilon );
49 
50 
52 
53 
54  template <size_t N>
55  AGX_FORCE_INLINE bool clipConvexPolygonAgainstTrianglePrism(
56  const agx::StackArray<agx::Vec3, N>& cPolygon,
57  const agx::StackArray<agx::Vec3, 3>& triangle,
58  const agx::Vec3& normalTriangle,
60  const agx::Real epsilon )
61  {
62  //
63  // Modified Sutherland-Hodgman polygon clipping in 3D.
64  //
65  if (cPolygon.size() == 0)
66  return false;
67 
68  result.clear();
69 
70  for (size_t i = 0; i < cPolygon.size(); ++i)
71  result.push_back(cPolygon[i]);
72 
73  agx::StackArray<agx::Vec3, 3> planeNormals; // these planeNormals are not normalized. Pointing outside from triangle.
75 
76  planeNormals.push_back((triangle[1] - triangle[0]) ^ normalTriangle);
77  planeNormals.push_back((triangle[2] - triangle[1]) ^ normalTriangle);
78  planeNormals.push_back((triangle[0] - triangle[2]) ^ normalTriangle);
79  planeDist.push_back(triangle[0] * planeNormals[0]);
80  planeDist.push_back(triangle[1] * planeNormals[1]);
81  planeDist.push_back(triangle[2] * planeNormals[2]);
82 
83 
85 
86  // clip convex polygon against each plane defined by triangle edges and normal
87  for (unsigned int i = 0; i < 3; ++i) {
88  tmp.clear();
89  bool lastPointWasOutside = result[0] * planeNormals[i] - planeDist[i] > epsilon;
90  // test each edge of the polygon
91  for (size_t j = 0; j < result.size(); ++j) {
92  size_t j_next = (j + 1) % result.size();
93  bool nextPointIsOutside = result[j_next] * planeNormals[i] - planeDist[i] > epsilon;
94  if (lastPointWasOutside != nextPointIsOutside) {
95  // crossing plane of triangle, clip
96  agx::Vec3 newPoint;
97  agx::Real tmpT;
98  if (intersectLineSegmentHyperPlane( result[j], result[j_next], planeNormals[i], planeDist[i], tmpT, newPoint, epsilon ))
99  tmp.push_back( newPoint );
100  }
101  if (!nextPointIsOutside)
102  tmp.push_back( result[j_next] );
103  lastPointWasOutside = nextPointIsOutside;
104  }
105 
106  result.clear();
107  if (tmp.size() == 0)
108  return false;
109 
110  for (size_t j = 0; j < tmp.size(); ++j) {
111  if (result.size() == 0 ||
112  ((result.back() - tmp[j]).length2() > epsilon && (result.front() - tmp[j]).length2() > epsilon))
113  result.push_back( tmp[j] );
114  }
115  }
116  return true;
117  }
118 
119 }
120 
122 #endif
This namespace consists of a set of classes for handling geometric intersection tests including boole...
Templated stack array class.
Definition: StackArray.h:33
#define AGX_FORCE_INLINE
Definition: Object.h:59
void push_back(const T &value)
Definition: StackArray.h:202
size_t size() const
Get the size of the array (number of filled slots).
Definition: StackArray.h:157
double Real
Definition: Real.h:42
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97