-
Notifications
You must be signed in to change notification settings - Fork 0
/
unreal_kdtree_face.h
392 lines (330 loc) · 10.6 KB
/
unreal_kdtree_face.h
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
// Ported to the Unreal Engine by Timothy Davison, 2019.
// Based on: https://github.com/cnr-isti-vclab/vcglib/blob/master/vcg/space/index/kdtree/kdtree_face.h
#pragma once
#include <vector>
#include <array>
#include <unordered_set>
#include <Vector.h>
#include <Box.h>
USTRUCT(BlueprintType)
struct FSurfaceIndex
{
GENERATED_BODY()
public:
FSurfaceIndex() {}
FSurfaceIndex(int32 sectionIndex, int32 faceIndex) : sectionIndex(sectionIndex), faceIndex(faceIndex) {}
UPROPERTY(EditAnywhere) int32 faceIndex = -1; // if surface walking, the index of the face that the element is on
UPROPERTY(EditAnywhere) int32 sectionIndex = -1; // if surface walking, the mesh section index of the face that the element is on
bool isOnSurface() const { return faceIndex > 0 && sectionIndex > 0; }
void setOffSurface() { faceIndex = -1; sectionIndex = -1; }
static const FSurfaceIndex OffSurface;
};
namespace vcg {
struct TriangleSample
{
std::array<FVector, 3> triangle;
FSurfaceIndex surfaceIndex;
// \return true if closer than the passed distance, false otherwise.
bool closestPoint(const FVector& query, float& distance, FVector& nearestPoint)
{
const float in_distance = distance;
nearestPoint = FMath::ClosestPointOnTriangleToPoint(query, triangle[0], triangle[1], triangle[2]);
distance = FVector::Dist(nearestPoint, query);
if (distance > in_distance)
return false;
else
return true;
}
bool hackIntersectsBox(const FBox& box)
{
return box.IsInsideOrOn(triangle[0]) || box.IsInsideOrOn(triangle[1]) || box.IsInsideOrOn(triangle[2]);
}
};
/**
* This class allows to create a Kd-Tree thought to perform the neighbour query using the mesh faces (closest search).
* The class implemetantion is thread-safe.
*/
class KdTreeFace
{
public:
typedef typename float Scalar;
typedef typename TriangleSample TriangleSample;
class Node
{
public:
Scalar splitValue;
unsigned int firstChildId : 24;
unsigned int dim : 2;
unsigned int leaf : 1;
FBox aabb = FBox(EForceInit::ForceInitToZero);
std::vector<TriangleSample*> list;
};
typedef std::vector<Node> NodeListPointer;
public:
std::vector<TriangleSample> triangles;
KdTreeFace(std::vector<TriangleSample>& trianglesIin, FBox aabb, unsigned int maxObjPerCell = 64, unsigned int maxDepth = 64) : epsilon(std::numeric_limits<Scalar>::epsilon())
{
triangles = trianglesIin;
targetCellSize = maxObjPerCell;
targetMaxDepth = maxDepth;
mNodes.resize(1);
Node& node = mNodes.back();
node.leaf = 0;
node.aabb = aabb;
node.aabb = node.aabb.ExpandBy(FVector(epsilon));
std::transform(triangles.begin(), triangles.end(), std::back_inserter(node.list),
[](TriangleSample &s) { return &s; }
);
numLevel = createTree(0, 1);
};
~KdTreeFace()
{
};
struct ObjectMarker
{
std::unordered_set<TriangleSample*> marks;
bool IsMarked(TriangleSample * sample)
{
return marks.find(sample) != marks.end();
}
void Mark(TriangleSample * sample)
{
marks.insert(sample);
}
};
TriangleSample* doQueryClosest(const FVector& queryPoint, FVector& narestPoint, Scalar& dist, ObjectMarker& marker, Scalar maxDist = std::numeric_limits<Scalar>::max())
{
float maxDistSqrd = maxDist * maxDist;
{
float distSqrd = mNodes[0].aabb.ComputeSquaredDistanceToPoint(queryPoint);
if (maxDist < std::numeric_limits<Scalar>::max() &&
!mNodes[0].aabb.IsInside(queryPoint) &&
distSqrd >= maxDistSqrd
)
{
dist = std::sqrt(distSqrd);
return nullptr;
}
}
std::vector<QueryNode> mNodeStack(numLevel + 1);
mNodeStack[0].nodeId = 0;
mNodeStack[0].sq = 0.f;
unsigned int count = 1;
float minDist = maxDist;
FVector p;
TriangleSample* face = nullptr;
while (count)
{
QueryNode& qnode = mNodeStack[count - 1];
Node& node = mNodes[qnode.nodeId];
if (qnode.sq < minDist)
{
if (node.leaf)
{
--count; // pop
for (int i = 0; i < node.list.size(); i++)
{
if (!marker.IsMarked(node.list[i]))
{
marker.Mark(node.list[i]);
Scalar tempDist = minDist;
FVector tempP;
if (node.list[i]->closestPoint(queryPoint, tempDist, tempP))
{
if (tempDist < minDist)
{
minDist = tempDist;
p = tempP;
face = node.list[i];
}
}
}
}
}
else
{
// replace the stack top by the farthest and push the closest
float new_off = queryPoint[node.dim] - node.splitValue;
float abs_off = abs(new_off);
if (abs_off < minDist)
{
if (new_off < 0.)
{
mNodeStack[count].nodeId = node.firstChildId;
qnode.nodeId = node.firstChildId + 1;
const float distSqrd = mNodes[node.firstChildId + 1].aabb.ComputeSquaredDistanceToPoint(queryPoint);
new_off = std::sqrt(distSqrd);
}
else
{
mNodeStack[count].nodeId = node.firstChildId + 1;
qnode.nodeId = node.firstChildId;
const float distSqrd = mNodes[node.firstChildId].aabb.ComputeSquaredDistanceToPoint(queryPoint);
new_off = std::sqrt(distSqrd);
}
mNodeStack[count].sq = qnode.sq;
qnode.sq = new_off;
++count;
}
else
{
if (new_off < 0.)
qnode.nodeId = node.firstChildId;
else
qnode.nodeId = node.firstChildId + 1;
}
}
}
else
{
// pop
--count;
}
}
dist = minDist;
narestPoint = p;
return face;
}
protected:
// element of the stack
struct QueryNode
{
QueryNode() {}
QueryNode(unsigned int id) : nodeId(id) {}
unsigned int nodeId; // id of the next node
Scalar sq; // distance to the next node
};
int createTree(unsigned int nodeId, unsigned int level)
{
Node& node = mNodes[nodeId];
FVector diag = node.aabb.Max - node.aabb.Min;
unsigned int dim;
if (diag.X > diag.Y)
dim = diag.X > diag.Z ? 0 : 2;
else
dim = diag.Y > diag.Z ? 1 : 2;
node.splitValue = Scalar(0.5*(node.aabb.Max[dim] + node.aabb.Min[dim]));
node.dim = dim;
FBox leftBox(EForceInit::ForceInitToZero);
FBox rightBox(EForceInit::ForceInitToZero);
leftBox += node.aabb.Min;
rightBox += node.aabb.Max;
if (node.dim == 0)
{
leftBox += FVector(node.splitValue, node.aabb.Max[1], node.aabb.Max[2]);
rightBox += FVector(node.splitValue, node.aabb.Min[1], node.aabb.Min[2]);
}
else if (node.dim == 1)
{
leftBox += FVector(node.aabb.Max[0], node.splitValue, node.aabb.Max[2]);
rightBox += FVector(node.aabb.Min[0], node.splitValue, node.aabb.Min[2]);
}
else if (node.dim == 2)
{
leftBox += FVector(node.aabb.Max[0], node.aabb.Max[1], node.splitValue);
rightBox += FVector(node.aabb.Min[0], node.aabb.Min[1], node.splitValue);
}
leftBox = leftBox.ExpandBy(FVector(epsilon, epsilon, epsilon));
rightBox = rightBox.ExpandBy(FVector(epsilon, epsilon, epsilon));
node.firstChildId = mNodes.size();
int firstChildId = node.firstChildId;
mNodes.resize(mNodes.size() + 2);
Node& parent = mNodes[nodeId];
Node& leftChild = mNodes[firstChildId];
Node& rightChild = mNodes[firstChildId + 1];
leftChild.aabb.Init();
rightChild.aabb.Init();
for (int i = 0; i < parent.list.size(); i++)
{
unsigned int state = 0;
TriangleSample * fp = parent.list[i];
for (int j = 0; j < 3; j++)
{
if (fp->triangle[j][dim] < parent.splitValue)
state |= (1 << 0);
else if (fp->triangle[j][dim] > parent.splitValue)
state |= (1 << 1);
else
{
state |= (1 << 0);
state |= (1 << 1);
}
}
if (state & (1 << 0))
{
leftChild.list.push_back(fp);
leftChild.aabb += fp->triangle[0];
leftChild.aabb += fp->triangle[1];
leftChild.aabb += fp->triangle[2];
}
if (state & (1 << 1))
{
rightChild.list.push_back(fp);
rightChild.aabb += fp->triangle[0];
rightChild.aabb += fp->triangle[1];
rightChild.aabb += fp->triangle[2];
}
}
parent.list.clear();
leftChild.aabb = leftChild.aabb.Overlap(leftBox);
rightChild.aabb = rightChild.aabb.Overlap(rightBox);
int leftLevel, rightLevel;
{
if (leftChild.list.size() <= targetCellSize || level >= targetMaxDepth)
{
leftChild.leaf = 1;
leftLevel = level;
}
else
{
leftChild.leaf = 0;
leftLevel = createTree(firstChildId, level + 1);
}
}
{
Node& rightChild = mNodes[firstChildId + 1];
if (rightChild.list.size() <= targetCellSize || level >= targetMaxDepth)
{
rightChild.leaf = 1;
rightLevel = level;
}
else
{
rightChild.leaf = 0;
rightLevel = createTree(firstChildId + 1, level + 1);
}
}
if (leftLevel > rightLevel)
return leftLevel;
return rightLevel;
};
protected:
NodeListPointer mNodes; //kd-tree nodes
unsigned int numLevel;
const Scalar epsilon;
unsigned int targetCellSize;
unsigned int targetMaxDepth;
};
}