-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathadaptor.eigen.h
419 lines (318 loc) · 17.6 KB
/
adaptor.eigen.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#pragma once
/*
MIT License
Copyright (c) 2021 Attila Csikós
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "octree.h"
// #include <Eigen/Geometry>
namespace OrthoTree
{
namespace EigenAdaptor
{
using namespace Eigen;
template<typename Scalar_, int AmbientDim_>
struct EigenAdaptorBasics
{
using VectorType_ = Matrix<Scalar_, AmbientDim_, 1>;
using AlignedBox_ = AlignedBox<Scalar_, AmbientDim_>;
using Ray_ = ParametrizedLine<Scalar_, AmbientDim_>;
using Plane_ = Hyperplane<Scalar_, AmbientDim_>;
static constexpr Scalar_ GetPointC(VectorType_ const& point, dim_t dimensionID) noexcept { return point(dimensionID); }
static constexpr void SetPointC(VectorType_& point, dim_t dimensionID, Scalar_ value) noexcept { point(dimensionID) = value; }
static constexpr Scalar_ GetBoxMinC(AlignedBox_ const& box, dim_t dimensionID) { return box.min()(dimensionID); }
static constexpr Scalar_ GetBoxMaxC(AlignedBox_ const& box, dim_t dimensionID) { return box.max()(dimensionID); }
static constexpr void SetBoxMinC(AlignedBox_& box, dim_t dimensionID, Scalar_ value) { box.min()(dimensionID) = value; }
static constexpr void SetBoxMaxC(AlignedBox_& box, dim_t dimensionID, Scalar_ value) { box.max()(dimensionID) = value; }
static constexpr VectorType_ const& GetRayDirection(Ray_ const& ray) noexcept { return ray.direction(); }
static constexpr VectorType_ const& GetRayOrigin(Ray_ const& ray) noexcept { return ray.origin(); }
static constexpr VectorType_ GetPlaneNormal(Plane_ const& plane) noexcept { return plane.normal(); }
static constexpr Scalar_ GetPlaneOrigoDistance(Plane_ const& plane) noexcept { return -plane.offset(); }
};
template<typename Scalar_, int AmbientDim_>
struct EigenAdaptorGeneralBase : EigenAdaptorBasics<Scalar_, AmbientDim_>
{
using Base = EigenAdaptorBasics<Scalar_, AmbientDim_>;
using VectorType_ = Base::VectorType_;
using AlignedBox_ = Base::AlignedBox_;
using Ray_ = ParametrizedLine<Scalar_, AmbientDim_>;
using Plane_ = Hyperplane<Scalar_, AmbientDim_>;
static_assert(AdaptorBasicsConcept<Base, VectorType_, AlignedBox_, Ray_, Plane_, Scalar_>);
static constexpr Scalar_ Size2(VectorType_ const& v) noexcept { return v.squaredNorm(); }
static constexpr Scalar_ Size(VectorType_ const& v) noexcept { return v.norm(); }
static constexpr VectorType_ Add(VectorType_ const& v1, VectorType_ const& v2) noexcept { return v1 + v2; }
static constexpr VectorType_ Subtract(VectorType_ const& v1, VectorType_ const& v2) noexcept { return v1 - v2; }
static constexpr VectorType_ Multiply(VectorType_ const& v, double scalarFactor) noexcept { return v * scalarFactor; }
static constexpr Scalar_ Dot(VectorType_ const& v1, VectorType_ const& v2) noexcept { return v1.dot(v2); }
static constexpr Scalar_ Distance(VectorType_ const& v1, VectorType_ const& v2) noexcept { return Size(v1 - v2); }
static constexpr Scalar_ Distance2(VectorType_ const& v1, VectorType_ const& v2) noexcept { return Size2(v1 - v2); }
static constexpr bool ArePointsEqual(VectorType_ const& v1, VectorType_ const& v2, Scalar_ tolerance) noexcept
{
return Distance2(v1, v2) <= tolerance * tolerance;
}
static constexpr bool IsNormalizedVector(VectorType_ const& normal) noexcept { return std::abs(Size2(normal) - 1.0) < 0.000001; }
static constexpr bool DoesBoxContainPoint(AlignedBox_ const& box, VectorType_ const& point) noexcept { return box.contains(point); }
static constexpr bool DoesBoxContainPointStrict(AlignedBox_ const& box, VectorType_ const& point) noexcept
{
for (dim_t dimensionID = 0; dimensionID < AmbientDim_; ++dimensionID)
if (!(Base::GetBoxMinC(box, dimensionID) < Base::GetPointC(point, dimensionID) &&
Base::GetPointC(point, dimensionID) < Base::GetBoxMaxC(box, dimensionID)))
return false;
return true;
}
static constexpr bool AreBoxesOverlappedStrict(AlignedBox_ const& e1, AlignedBox_ const& e2) noexcept
{
auto const e3 = e1.intersection(e2);
auto const sizes = e3.sizes();
for (dim_t dimensionID = 0; dimensionID < AmbientDim_; ++dimensionID)
if (sizes[dimensionID] <= 0.0)
return false;
return true;
}
static constexpr bool AreBoxesOverlapped(
AlignedBox_ const& e1, AlignedBox_ const& e2, bool e1_must_contain_e2 = true, bool fOverlapPtTouchAllowed = false) noexcept
{
if (e1_must_contain_e2)
return e1.contains(e2);
else if (!fOverlapPtTouchAllowed)
return AreBoxesOverlappedStrict(e1, e2);
else
return e1.intersects(e2);
}
static AlignedBox_ GetBoxOfPoints(std::span<VectorType_ const> const& points) noexcept
{
auto ext = points.size() == 0 ? AlignedBox_{} : AlignedBox_(points[0]);
for (auto const& point : points)
ext.extend(point);
return ext;
}
static AlignedBox_ GetBoxOfBoxes(std::span<AlignedBox_ const> const& extents) noexcept
{
auto ext = extents.size() == 0 ? AlignedBox_{} : extents[0];
for (auto const& extent : extents)
ext.extend(extent);
return ext;
}
static void MoveBox(AlignedBox_& box, VectorType_ const& moveVector) noexcept { box.translate(moveVector); }
static constexpr std::optional<double> GetRayBoxDistance(
AlignedBox_ const& box, VectorType_ const& rayBasePoint, VectorType_ const& rayHeading, Scalar_ tolerance) noexcept
{
auto const toleranceVector = VectorType_::Ones() * tolerance;
auto const rayBasePointBox = AlignedBox_(rayBasePoint - toleranceVector, rayBasePoint + toleranceVector);
if (box.intersects(rayBasePointBox))
return 0.0;
auto constexpr inf = std::numeric_limits<double>::infinity();
auto minBoxDistances = std::array<double, AmbientDim_>{};
auto maxBoxDistances = std::array<double, AmbientDim_>{};
for (dim_t dimensionID = 0; dimensionID < AmbientDim_; ++dimensionID)
{
auto const dirComp = Base::GetPointC(rayHeading, dimensionID);
if (dirComp == 0)
{
if (tolerance != 0.0)
{
// Box should be within tolerance (<, not <=)
assert(tolerance > 0);
if (Base::GetBoxMaxC(box, dimensionID) + tolerance <= Base::GetPointC(rayBasePoint, dimensionID))
return std::nullopt;
if (Base::GetBoxMinC(box, dimensionID) - tolerance >= Base::GetPointC(rayBasePoint, dimensionID))
return std::nullopt;
}
else
{
if (Base::GetBoxMaxC(box, dimensionID) < Base::GetPointC(rayBasePoint, dimensionID))
return std::nullopt;
if (Base::GetBoxMinC(box, dimensionID) > Base::GetPointC(rayBasePoint, dimensionID))
return std::nullopt;
}
minBoxDistances[dimensionID] = -inf;
maxBoxDistances[dimensionID] = +inf;
}
else
{
auto const minBox = Base::GetBoxMinC(box, dimensionID) - tolerance;
auto const maxBox = Base::GetBoxMaxC(box, dimensionID) + tolerance;
auto const pointComp = Base::GetPointC(rayBasePoint, dimensionID);
auto const dirCompRecip = 1.0 / dirComp;
if (dirComp < 0.0)
{
minBoxDistances[dimensionID] = (maxBox - pointComp) * dirCompRecip;
maxBoxDistances[dimensionID] = (minBox - pointComp) * dirCompRecip;
}
else
{
minBoxDistances[dimensionID] = (minBox - pointComp) * dirCompRecip;
maxBoxDistances[dimensionID] = (maxBox - pointComp) * dirCompRecip;
}
}
}
auto const minBoxDistance = *std::ranges::max_element(minBoxDistances);
auto const maxBoxDistance = *std::ranges::min_element(maxBoxDistances);
if (minBoxDistance > maxBoxDistance || maxBoxDistance < 0.0)
return std::nullopt;
else
return minBoxDistance < 0 ? maxBoxDistance : minBoxDistance;
}
static constexpr std::optional<double> GetRayBoxDistance(AlignedBox_ const& box, Ray_ const& ray, Scalar_ tolerance) noexcept
{
return GetRayBoxDistance(box, Base::GetRayOrigin(ray), Base::GetRayDirection(ray), tolerance);
}
// Get point-Hyperplane relation (Plane equation: dotProduct(planeNormal, point) = distanceOfOrigo)
static constexpr PlaneRelation GetPointPlaneRelation(
VectorType_ const& point, Scalar_ distanceOfOrigo, VectorType_ const& planeNormal, Scalar_ tolerance) noexcept
{
assert(IsNormalizedVector(planeNormal));
auto const pointProjected = Dot(planeNormal, point);
if (pointProjected < distanceOfOrigo - tolerance)
return PlaneRelation::Negative;
if (pointProjected > distanceOfOrigo + tolerance)
return PlaneRelation::Positive;
return PlaneRelation::Hit;
}
// Get box-Hyperplane relation (Plane equation: dotProduct(planeNormal, point) = distanceOfOrigo)
static constexpr PlaneRelation GetBoxPlaneRelation(AlignedBox_ const& box, Scalar_ distanceOfOrigo, VectorType_ const& planeNormal, Scalar_ tolerance) noexcept
{
assert(IsNormalizedVector(planeNormal));
VectorType_ center, radius;
for (dim_t dimensionID = 0; dimensionID < AmbientDim_; ++dimensionID)
{
auto const minComponent = Base::GetBoxMinC(box, dimensionID);
auto const maxComponent = Base::GetBoxMaxC(box, dimensionID);
auto const centerComponent = static_cast<Scalar_>((minComponent + maxComponent) * 0.5);
Base::SetPointC(center, dimensionID, centerComponent);
Base::SetPointC(radius, dimensionID, centerComponent - minComponent);
}
auto radiusProjected = double(tolerance);
for (dim_t dimensionID = 0; dimensionID < AmbientDim_; ++dimensionID)
radiusProjected += Base::GetPointC(radius, dimensionID) * std::abs(Base::GetPointC(planeNormal, dimensionID));
auto const centerProjected = Dot(planeNormal, center);
if (centerProjected + radiusProjected < distanceOfOrigo)
return PlaneRelation::Negative;
if (centerProjected - radiusProjected > distanceOfOrigo)
return PlaneRelation::Positive;
return PlaneRelation::Hit;
}
};
} // namespace EigenAdaptor
} // namespace OrthoTree
namespace Eigen
{
using namespace OrthoTree::EigenAdaptor;
template<typename Scalar_, int AmbientDim_>
using PointSpan = std::span<Matrix<Scalar_, AmbientDim_, 1> const>;
template<typename Scalar_, int AmbientDim_>
using BoxSpan = std::span<AlignedBox<Scalar_, AmbientDim_> const>;
template<typename Scalar_, int AmbientDim_>
using PointMap = std::unordered_map<int, Matrix<Scalar_, AmbientDim_, 1>>;
template<typename Scalar_, int AmbientDim_>
using BoxMap = std::unordered_map<int, AlignedBox<Scalar_, AmbientDim_>>;
// Basic OrthoTree types
template<typename Scalar_, int AmbientDim_, typename Container_ = PointSpan<Scalar_, AmbientDim_>>
using EigenOrthoTreePoint = OrthoTree::OrthoTreePoint<
AmbientDim_,
Matrix<Scalar_, AmbientDim_, 1>,
AlignedBox<Scalar_, AmbientDim_>,
ParametrizedLine<Scalar_, AmbientDim_>,
Hyperplane<Scalar_, AmbientDim_>,
Scalar_,
EigenAdaptorGeneralBase<Scalar_, AmbientDim_>,
Container_>;
template<typename Scalar_, int AmbientDim_, uint32_t SPLIT_DEPTH_INCREASEMENT = 2, typename Container_ = BoxSpan<Scalar_, AmbientDim_>>
using EigenOrthoTreeBox = OrthoTree::OrthoTreeBoundingBox<
AmbientDim_,
Matrix<Scalar_, AmbientDim_, 1>,
AlignedBox<Scalar_, AmbientDim_>,
ParametrizedLine<Scalar_, AmbientDim_>,
Hyperplane<Scalar_, AmbientDim_>,
Scalar_,
SPLIT_DEPTH_INCREASEMENT,
EigenAdaptorGeneralBase<Scalar_, AmbientDim_>,
Container_>;
template<typename Scalar_, int AmbientDim_, typename Container_ = PointSpan<Scalar_, AmbientDim_>>
using OrthoTreeContainerPointC = OrthoTree::OrthoTreeContainerPoint<EigenOrthoTreePoint<Scalar_, AmbientDim_, Container_>>;
template<typename Scalar_, int AmbientDim_, uint32_t SPLIT_DEPTH_INCREASEMENT = 2, typename Container_ = BoxSpan<Scalar_, AmbientDim_>>
using OrthoTreeContainerBoxC = OrthoTree::OrthoTreeContainerBox<EigenOrthoTreeBox<Scalar_, AmbientDim_, SPLIT_DEPTH_INCREASEMENT, Container_>>;
// Non-owning types
using QuadtreePoint2f = EigenOrthoTreePoint<float, 2>;
using QuadtreePoint2d = EigenOrthoTreePoint<double, 2>;
using OctreePoint3f = EigenOrthoTreePoint<float, 3>;
using OctreePoint3d = EigenOrthoTreePoint<double, 3>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using QuadtreeBox2fs = EigenOrthoTreeBox<float, 2, SPLIT_DEPTH_INCREASEMENT>;
using QuadtreeBox2f = QuadtreeBox2fs<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using QuadtreeBox2ds = EigenOrthoTreeBox<double, 2, SPLIT_DEPTH_INCREASEMENT>;
using QuadtreeBox2d = QuadtreeBox2ds<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using OctreeBox3fs = EigenOrthoTreeBox<float, 3, SPLIT_DEPTH_INCREASEMENT>;
using OctreeBox3f = OctreeBox3fs<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using OctreeBox3ds = EigenOrthoTreeBox<double, 3, SPLIT_DEPTH_INCREASEMENT>;
using OctreeBox3d = OctreeBox3ds<2>;
// Container types
using QuadtreePointC2f = OrthoTreeContainerPointC<float, 2>;
using QuadtreePointC2d = OrthoTreeContainerPointC<double, 2>;
using OctreePointC3f = OrthoTreeContainerPointC<float, 3>;
using OctreePointC3d = OrthoTreeContainerPointC<double, 3>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using QuadtreeBoxC2fs = OrthoTreeContainerBoxC<float, 2, SPLIT_DEPTH_INCREASEMENT>;
using QuadtreeBoxC2f = QuadtreeBoxC2fs<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using QuadtreeBoxC2ds = OrthoTreeContainerBoxC<double, 2, SPLIT_DEPTH_INCREASEMENT>;
using QuadtreeBoxC2d = QuadtreeBoxC2ds<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using OctreeBoxC3fs = OrthoTreeContainerBoxC<float, 3, SPLIT_DEPTH_INCREASEMENT>;
using OctreeBoxC3f = OctreeBoxC3fs<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using OctreeBoxC3ds = OrthoTreeContainerBoxC<double, 3, SPLIT_DEPTH_INCREASEMENT>;
using OctreeBoxC3d = OctreeBoxC3ds<2>;
// Map types
// Non-owning types
using QuadtreePointMap2f = EigenOrthoTreePoint<float, 2, PointMap<float, 2>>;
using QuadtreePointMap2d = EigenOrthoTreePoint<double, 2, PointMap<double, 2>>;
using OctreePointMap3f = EigenOrthoTreePoint<float, 3, PointMap<float, 3>>;
using OctreePointMap3d = EigenOrthoTreePoint<double, 3, PointMap<double, 3>>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using QuadtreeBox2Mapfs = EigenOrthoTreeBox<float, 2, SPLIT_DEPTH_INCREASEMENT, BoxMap<float, 2>>;
using QuadtreeBox2Mapf = QuadtreeBox2Mapfs<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using QuadtreeBox2Mapds = EigenOrthoTreeBox<double, 2, SPLIT_DEPTH_INCREASEMENT, BoxMap<double, 2>>;
using QuadtreeBox2Mapd = QuadtreeBox2Mapds<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using OctreeBox3Mapfs = EigenOrthoTreeBox<float, 3, SPLIT_DEPTH_INCREASEMENT, BoxMap<float, 3>>;
using OctreeBox3Mapf = OctreeBox3Mapfs<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using OctreeBox3Mapds = EigenOrthoTreeBox<double, 3, SPLIT_DEPTH_INCREASEMENT, BoxMap<double, 3>>;
using OctreeBox3Mapd = OctreeBox3Mapds<2>;
// Container types
using QuadtreePointCMap2f = OrthoTreeContainerPointC<float, 2, PointMap<float, 2>>;
using QuadtreePointCMap2d = OrthoTreeContainerPointC<double, 2, PointMap<double, 2>>;
using OctreePointCMap3f = OrthoTreeContainerPointC<float, 3, PointMap<float, 3>>;
using OctreePointCMap3d = OrthoTreeContainerPointC<double, 3, PointMap<double, 3>>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using QuadtreeBoxCMap2fs = OrthoTreeContainerBoxC<float, 2, SPLIT_DEPTH_INCREASEMENT, BoxMap<float, 2>>;
using QuadtreeBoxCMap2f = QuadtreeBoxCMap2fs<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using QuadtreeBoxCMap2ds = OrthoTreeContainerBoxC<double, 2, SPLIT_DEPTH_INCREASEMENT, BoxMap<double, 2>>;
using QuadtreeBoxCMap2d = QuadtreeBoxCMap2ds<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using OctreeBoxCMap3fs = OrthoTreeContainerBoxC<float, 3, SPLIT_DEPTH_INCREASEMENT, BoxMap<float, 3>>;
using OctreeBoxCMap3f = OctreeBoxCMap3fs<2>;
template<uint32_t SPLIT_DEPTH_INCREASEMENT = 2>
using OctreeBoxC3Mapds = OrthoTreeContainerBoxC<double, 3, SPLIT_DEPTH_INCREASEMENT, BoxMap<double, 3>>;
using OctreeBoxC3Mapd = OctreeBoxC3Mapds<2>;
} // namespace Eigen