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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
using Unity.Burst;
namespace Pathfinding.Graphs.Navmesh.Voxelization {
/// <summary>Utility for clipping polygons</summary>
internal struct Int3PolygonClipper {
/// <summary>Cache this buffer to avoid unnecessary allocations</summary>
float[] clipPolygonCache;
/// <summary>Cache this buffer to avoid unnecessary allocations</summary>
int[] clipPolygonIntCache;
/// <summary>Initialize buffers if they are null</summary>
public void Init () {
if (clipPolygonCache == null) {
clipPolygonCache = new float[7*3];
clipPolygonIntCache = new int[7*3];
}
}
/// <summary>
/// Clips a polygon against an axis aligned half plane.
///
/// Returns: Number of output vertices
///
/// The vertices will be scaled and then offset, after that they will be cut using either the
/// x axis, y axis or the z axis as the cutting line. The resulting vertices will be added to the
/// vOut array in their original space (i.e before scaling and offsetting).
/// </summary>
/// <param name="vIn">Input vertices</param>
/// <param name="n">Number of input vertices (may be less than the length of the vIn array)</param>
/// <param name="vOut">Output vertices, needs to be large enough</param>
/// <param name="multi">Scale factor for the input vertices</param>
/// <param name="offset">Offset to move the input vertices with before cutting</param>
/// <param name="axis">Axis to cut along, either x=0, y=1, z=2</param>
public int ClipPolygon (Int3[] vIn, int n, Int3[] vOut, int multi, int offset, int axis) {
Init();
int[] d = clipPolygonIntCache;
for (int i = 0; i < n; i++) {
d[i] = multi*vIn[i][axis]+offset;
}
// Number of resulting vertices
int m = 0;
for (int i = 0, j = n-1; i < n; j = i, i++) {
bool prev = d[j] >= 0;
bool curr = d[i] >= 0;
if (prev != curr) {
double s = (double)d[j] / (d[j] - d[i]);
vOut[m] = vIn[j] + (vIn[i]-vIn[j])*s;
m++;
}
if (curr) {
vOut[m] = vIn[i];
m++;
}
}
return m;
}
}
/// <summary>Utility for clipping polygons</summary>
internal struct VoxelPolygonClipper {
public unsafe fixed float x[8];
public unsafe fixed float y[8];
public unsafe fixed float z[8];
public int n;
public UnityEngine.Vector3 this[int i] {
set {
unsafe {
x[i] = value.x;
y[i] = value.y;
z[i] = value.z;
}
}
}
/// <summary>
/// Clips a polygon against an axis aligned half plane.
/// The polygons stored in this object are clipped against the half plane at x = -offset.
/// </summary>
/// <param name="result">Ouput vertices</param>
/// <param name="multi">Scale factor for the input vertices. Should be +1 or -1. If -1 the negative half plane is kept.</param>
/// <param name="offset">Offset to move the input vertices with before cutting</param>
public void ClipPolygonAlongX ([NoAlias] ref VoxelPolygonClipper result, float multi, float offset) {
unsafe {
// Number of resulting vertices
int m = 0;
float dj = multi*x[(n-1)]+offset;
for (int i = 0, j = n-1; i < n; j = i, i++) {
float di = multi*x[i]+offset;
bool prev = dj >= 0;
bool curr = di >= 0;
if (prev != curr) {
float s = dj / (dj - di);
result.x[m] = x[j] + (x[i]-x[j])*s;
result.y[m] = y[j] + (y[i]-y[j])*s;
result.z[m] = z[j] + (z[i]-z[j])*s;
m++;
}
if (curr) {
result.x[m] = x[i];
result.y[m] = y[i];
result.z[m] = z[i];
m++;
}
dj = di;
}
result.n = m;
}
}
/// <summary>
/// Clips a polygon against an axis aligned half plane.
/// The polygons stored in this object are clipped against the half plane at z = -offset.
/// </summary>
/// <param name="result">Ouput vertices. Only the Y and Z coordinates are calculated. The X coordinates are undefined.</param>
/// <param name="multi">Scale factor for the input vertices. Should be +1 or -1. If -1 the negative half plane is kept.</param>
/// <param name="offset">Offset to move the input vertices with before cutting</param>
public void ClipPolygonAlongZWithYZ ([NoAlias] ref VoxelPolygonClipper result, float multi, float offset) {
unsafe {
// Number of resulting vertices
int m = 0;
Unity.Burst.CompilerServices.Hint.Assume(n >= 0);
Unity.Burst.CompilerServices.Hint.Assume(n <= 8);
float dj = multi*z[(n-1)]+offset;
for (int i = 0, j = n-1; i < n; j = i, i++) {
float di = multi*z[i]+offset;
bool prev = dj >= 0;
bool curr = di >= 0;
if (prev != curr) {
float s = dj / (dj - di);
result.y[m] = y[j] + (y[i]-y[j])*s;
result.z[m] = z[j] + (z[i]-z[j])*s;
m++;
}
if (curr) {
result.y[m] = y[i];
result.z[m] = z[i];
m++;
}
dj = di;
}
result.n = m;
}
}
/// <summary>
/// Clips a polygon against an axis aligned half plane.
/// The polygons stored in this object are clipped against the half plane at z = -offset.
/// </summary>
/// <param name="result">Ouput vertices. Only the Y coordinates are calculated. The X and Z coordinates are undefined.</param>
/// <param name="multi">Scale factor for the input vertices. Should be +1 or -1. If -1 the negative half plane is kept.</param>
/// <param name="offset">Offset to move the input vertices with before cutting</param>
public void ClipPolygonAlongZWithY ([NoAlias] ref VoxelPolygonClipper result, float multi, float offset) {
unsafe {
// Number of resulting vertices
int m = 0;
Unity.Burst.CompilerServices.Hint.Assume(n >= 3);
Unity.Burst.CompilerServices.Hint.Assume(n <= 8);
float dj = multi*z[n-1]+offset;
for (int i = 0, j = n-1; i < n; j = i, i++) {
float di = multi*z[i]+offset;
bool prev = dj >= 0;
bool curr = di >= 0;
if (prev != curr) {
float s = dj / (dj - di);
result.y[m] = y[j] + (y[i]-y[j])*s;
m++;
}
if (curr) {
result.y[m] = y[i];
m++;
}
dj = di;
}
result.n = m;
}
}
}
}
|