AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
PolygonClipping.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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>
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
38namespace 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
Templated stack array class.
size_t size() const
Get the size of the array (number of filled slots).
void push_back(const T &value)
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition macros.h:96
#define AGX_FORCE_INLINE
Definition macros.h:58
This namespace consists of a set of classes for handling geometric intersection tests including boole...
double Real
Definition Real.h:42