forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SurfaceSimplifier.cc
344 lines (314 loc) · 10 KB
/
SurfaceSimplifier.cc
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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file orange/surf/SurfaceSimplifier.cc
//---------------------------------------------------------------------------//
#include "SurfaceSimplifier.hh"
#include "corecel/cont/Range.hh"
#include "corecel/math/Algorithms.hh"
#include "corecel/math/ArrayOperators.hh"
#include "corecel/math/ArrayUtils.hh"
#include "corecel/math/SoftEqual.hh"
#include "ConeAligned.hh"
#include "CylAligned.hh"
#include "CylCentered.hh"
#include "GeneralQuadric.hh"
#include "Plane.hh"
#include "PlaneAligned.hh"
#include "SimpleQuadric.hh"
#include "Sphere.hh"
#include "SphereCentered.hh"
#include "detail/PlaneAlignedConverter.hh"
#include "detail/QuadricConeConverter.hh"
#include "detail/QuadricCylConverter.hh"
#include "detail/QuadricPlaneConverter.hh"
#include "detail/QuadricSphereConverter.hh"
namespace celeritas
{
namespace
{
//---------------------------------------------------------------------------//
#define ORANGE_INSTANTIATE_OP(OUT, IN) \
template SurfaceSimplifier::Optional<OUT<Axis::x>> \
SurfaceSimplifier::operator()(IN<Axis::x> const&) const; \
template SurfaceSimplifier::Optional<OUT<Axis::y>> \
SurfaceSimplifier::operator()(IN<Axis::y> const&) const; \
template SurfaceSimplifier::Optional<OUT<Axis::z>> \
SurfaceSimplifier::operator()(IN<Axis::z> const&) const
class ZeroSnapper
{
public:
explicit ZeroSnapper(real_type tol) : soft_zero_{tol} {}
//! Transform the value so that near-zeros (and signed zeros) become zero
real_type operator()(real_type v) const
{
if (soft_zero_(v))
return 0;
return v;
}
private:
SoftZero<> soft_zero_;
};
//---------------------------------------------------------------------------//
} // namespace
//---------------------------------------------------------------------------//
/*!
* Plane may be snapped to origin.
*/
template<Axis T>
auto SurfaceSimplifier::operator()(PlaneAligned<T> const& p) const
-> Optional<PlaneAligned<T>>
{
if (p.position() != real_type{0} && SoftZero{tol_}(p.position()))
{
// Snap to zero since it's not already zero
return PlaneAligned<T>{real_type{0}};
}
// No simplification performed
return {};
}
//! \cond
ORANGE_INSTANTIATE_OP(PlaneAligned, PlaneAligned);
//! \endcond
//---------------------------------------------------------------------------//
/*!
* Cylinder at origin will be simplified.
*
* \verbatim
distance({0,0}, {u,v}) < tol
sqrt(u^2 + v^2) < tol
u^2 + v^2 < tol^2
\endverbatim
*/
template<Axis T>
auto SurfaceSimplifier::operator()(CylAligned<T> const& c) const
-> Optional<CylCentered<T>>
{
real_type origin_dist = ipow<2>(c.origin_u()) + ipow<2>(c.origin_v());
if (origin_dist < ipow<2>(tol_))
{
// Snap to zero since it's not already zero
return CylCentered<T>::from_radius_sq(c.radius_sq());
}
// No simplification performed
return {};
}
//! \cond
ORANGE_INSTANTIATE_OP(CylCentered, CylAligned);
//! \endcond
//---------------------------------------------------------------------------//
/*!
* A cone whose origin is close to any axis will be snapped to it.
*
* This uses a 1-norm for simplicity.
*/
template<Axis T>
auto SurfaceSimplifier::operator()(ConeAligned<T> const& c) const
-> Optional<ConeAligned<T>>
{
bool simplified = false;
Real3 origin = c.origin();
SoftZero const soft_zero{tol_};
for (auto ax : range(to_int(Axis::size_)))
{
if (origin[ax] != 0 && soft_zero(origin[ax]))
{
origin[ax] = 0;
simplified = true;
}
}
if (simplified)
{
return ConeAligned<T>::from_tangent_sq(origin, c.tangent_sq());
}
// No simplification performed
return {};
}
//! \cond
ORANGE_INSTANTIATE_OP(ConeAligned, ConeAligned);
//! \endcond
//---------------------------------------------------------------------------//
/*!
* Plane may be flipped, adjusted, or become axis-aligned.
*
* If a plane has a normal of {-1, 0 + eps, 0}, it will first be truncated to
* {-1, 0, 0}, then flipped to {1, 0, 0}, and a new Plane will be returned.
* That plane can *then* be simplified to an axis-aligned one.
*/
auto SurfaceSimplifier::operator()(Plane const& p)
-> Optional<PlaneX, PlaneY, PlaneZ, Plane>
{
{
// First, try to snap to aligned plane
detail::PlaneAlignedConverter to_aligned{tol_};
if (auto pa = to_aligned(AxisTag<Axis::x>{}, p))
return *pa;
if (auto pa = to_aligned(AxisTag<Axis::y>{}, p))
return *pa;
if (auto pa = to_aligned(AxisTag<Axis::z>{}, p))
return *pa;
}
Real3 n{p.normal()};
real_type d{p.displacement()};
// Snap nearly-zero normals to zero
std::transform(n.begin(), n.end(), n.begin(), ZeroSnapper{tol_});
// To prevent opposite-value planes from being defined but not
// deduplicated, ensure the first non-zero normal component is in the
// positive half-space. This also takes care of flipping orthogonal planes
// defined like {-x = 3}, translating them to { x = -3 }.
for (auto ax : range(to_int(Axis::size_)))
{
if (n[ax] > 0)
{
break;
}
else if (n[ax] < 0)
{
// Flip the sign of this and any remaining nonzero axes
// (previous axes are zero so just skip them)
for (auto ax2 : range(ax, to_int(Axis::size_)))
{
n[ax2] = negate(n[ax2]);
}
// Flip sign of d (without introducing -0)
d = negate(d);
// Flip sense
*sense_ = flip_sense(*sense_);
break;
}
}
if (n != p.normal())
{
// The direction was changed: renormalize and return the updated plane
real_type norm_factor = 1 / celeritas::norm(n);
n *= norm_factor;
d *= norm_factor;
return Plane{n, d};
}
if (d != 0 && SoftZero<>{tol_}(d))
{
// Snap zero-distances to zero
return Plane{n, 0};
}
// No simplification performed
return {};
}
//---------------------------------------------------------------------------//
/*!
* Sphere near center can be snapped.
*/
auto SurfaceSimplifier::operator()(Sphere const& s) const
-> Optional<SphereCentered>
{
if (dot_product(s.origin(), s.origin()) < ipow<2>(tol_))
{
// Sphere is less than tolerance from the origin
return SphereCentered::from_radius_sq(s.radius_sq());
}
// No simplification performed
return {};
}
//---------------------------------------------------------------------------//
/*!
* Simple quadric with near-zero terms can be another second-order surface.
*
* TODO: renormalize so that second-order terms are O(1) (and simplifying
* quadrics that are scaled by a constant)?
*/
auto SurfaceSimplifier::operator()(SimpleQuadric const& sq)
-> Optional<Plane,
Sphere,
CylAligned<Axis::x>,
CylAligned<Axis::y>,
CylAligned<Axis::z>,
ConeAligned<Axis::x>,
ConeAligned<Axis::y>,
ConeAligned<Axis::z>,
SimpleQuadric>
{
// Determine possible simplifications by calculating number of zeros
int num_pos{0};
int num_neg{0};
for (auto v : sq.second())
{
if (v < -tol_)
++num_neg;
else if (v > tol_)
++num_pos;
}
if (num_pos == 0 && num_neg == 0)
{
// It's a plane
return detail::QuadricPlaneConverter{tol_}(sq);
}
else if (num_neg > num_pos)
{
// Normalize sign so that it has more positive signs than negative
auto arr = make_array(sq.data());
for (auto& v : arr)
{
v = negate(v);
}
// Flip sense
*sense_ = flip_sense(*sense_);
// Construct reversed-sign quadric
// Todo: make_span doesn't use the correct overload and creates a
// dynamic extent span
return SimpleQuadric{
Span<decltype(arr)::value_type, SimpleQuadric::StorageSpan::extent>{
make_span(arr)}};
}
else if (num_pos == 3)
{
// Could be a sphere
detail::QuadricSphereConverter to_sphere{tol_};
if (auto s = to_sphere(sq))
return *s;
}
else if (num_pos == 2 && num_neg == 1)
{
// Cone: one second-order term less than zero, others equal
detail::QuadricConeConverter to_cone{tol_};
if (auto c = to_cone(AxisTag<Axis::x>{}, sq))
return *c;
if (auto c = to_cone(AxisTag<Axis::y>{}, sq))
return *c;
if (auto c = to_cone(AxisTag<Axis::z>{}, sq))
return *c;
}
else if (num_pos == 2 && num_neg == 0)
{
// Cyl: one second-order term is zero, others are equal
detail::QuadricCylConverter to_cyl{tol_};
if (auto c = to_cyl(AxisTag<Axis::x>{}, sq))
return *c;
if (auto c = to_cyl(AxisTag<Axis::y>{}, sq))
return *c;
if (auto c = to_cyl(AxisTag<Axis::z>{}, sq))
return *c;
}
// No simplification performed
return {};
}
//---------------------------------------------------------------------------//
/*!
* Quadric with no cross terms is "simple".
*
* TODO: guard against different-signed GQs?
*/
auto SurfaceSimplifier::operator()(GeneralQuadric const& gq)
-> Optional<SimpleQuadric>
{
auto cross = gq.cross();
if (std::all_of(cross.begin(), cross.end(), SoftZero{tol_}))
{
// No cross terms
return SimpleQuadric{
make_array(gq.second()), make_array(gq.first()), gq.zeroth()};
}
return {};
}
//---------------------------------------------------------------------------//
} // namespace celeritas