1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*
* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#ifndef NVBLASTEXTTRIANGLEPROCESSOR_H
#define NVBLASTEXTTRIANGLEPROCESSOR_H
#include <PxPhysicsAPI.h>
#include <vector>
#include <algorithm>
using namespace physx;
namespace Nv
{
namespace Blast
{
/**
Triangle processor internal triangle representation. Contains only vertex positions.
*/
struct TrPrcTriangle
{
PxVec3 points[3];
TrPrcTriangle(PxVec3 a = PxVec3(0.0f), PxVec3 b = PxVec3(0.0f), PxVec3 c = PxVec3(0.0f))
{
points[0] = a;
points[1] = b;
points[2] = c;
}
TrPrcTriangle& operator=(const TrPrcTriangle& b)
{
points[0] = b.points[0];
points[1] = b.points[1];
points[2] = b.points[2];
return *this;
}
TrPrcTriangle(const TrPrcTriangle& b)
{
points[0] = b.points[0];
points[1] = b.points[1];
points[2] = b.points[2];
}
PxVec3 getNormal() const
{
return (points[1] - points[0]).cross(points[2] - points[0]);
}
};
/**
Triangle processor internal 2D triangle representation. Contains only vertex positions.
*/
struct TrPrcTriangle2d
{
PxVec2 points[3];
TrPrcTriangle2d(PxVec2 a = PxVec2(0.0f), PxVec2 b = PxVec2(0.0f), PxVec2 c = PxVec2(0.0f))
{
points[0] = a;
points[1] = b;
points[2] = c;
}
TrPrcTriangle2d operator=(const TrPrcTriangle2d& b)
{
points[0] = b.points[0];
points[1] = b.points[1];
points[2] = b.points[2];
return *this;
}
TrPrcTriangle2d(const TrPrcTriangle2d& b)
{
points[0] = b.points[0];
points[1] = b.points[1];
points[2] = b.points[2];
}
};
class TriangleProcessor
{
public:
TriangleProcessor()
{};
~TriangleProcessor()
{
}
/**
Build intersection between two triangles
\param[in] a First triangle (A)
\param[in] aProjected Projected triangle A
\param[in] b Second triangle (B)
\param[in] centroid Centroid of first triangle (A)
\param[out] intersectionBuffer Result intersection polygon
\param[in] normal Normal vector to triangle (Common for both A and B).
\return 1 - if if intersection is found.
*/
uint32_t getTriangleIntersection(TrPrcTriangle& a, TrPrcTriangle2d& aProjected, TrPrcTriangle &b, PxVec3& centroid, std::vector<PxVec3>& intersectionBuffer, PxVec3 normal);
/**
Test whether BB of triangles intersect.
\param[in] a First triangle (A)
\param[in] b Second triangle (B)
\return true - if intersect
*/
bool triangleBoundingBoxIntersection(TrPrcTriangle2d& a, TrPrcTriangle2d& b);
/**
Test whether point is inside of triangle.
\param[in] point Point coordinates in 2d space.
\param[in] triangle Triangle in 2d space.
\return 1 - if inside, 2 if on edge, 0 if neither inside nor edge.
*/
uint32_t isPointInside(const PxVec2& point, const TrPrcTriangle2d& triangle);
/**
Segment intersection point
\param[in] s1 Segment-1 start point
\param[in] e1 Segment-1 end point
\param[in] s2 Segment-2 start point
\param[in] e2 Segment-2 end point
\param[out] t1 Intersection point parameter relatively to Segment-1, lies in [0.0, 1.0] range.
\return 0 if there is no intersections, 1 - if intersection is found.
*/
uint32_t getSegmentIntersection(const PxVec2& s1, const PxVec2& e1, const PxVec2& s2, const PxVec2& e2, PxF32& t1);
/**
Sort vertices of polygon in CCW-order
*/
void sortToCCW(std::vector<PxVec3>& points, PxVec3& normal);
/**
Builds convex polygon for given set of points. Points should be coplanar.
\param[in] points Input array of points
\param[out] convexHull Output polygon
\param[in] normal Normal vector to polygon.
*/
void buildConvexHull(std::vector<PxVec3>& points, std::vector<PxVec3>& convexHull, const PxVec3& normal);
};
} // namespace Blast
} // namespace Nv
#endif // NVBLASTEXTTRIANGLEPROCESSOR_H
|