EBGeometry 1.0
Loading...
Searching...
No Matches
EBGeometry_CSG.hpp
Go to the documentation of this file.
1/* EBGeometry
2 * Copyright © 2022 Robert Marskar
3 * Please refer to Copyright.txt and LICENSE in the EBGeometry root directory.
4 */
5
12#ifndef EBGeometry_CSG
13#define EBGeometry_CSG
14
15// Std includes
16#include <vector>
17#include <type_traits>
18
19// Our includes
22
28template <class T, class P = ImplicitFunction<T>>
29std::shared_ptr<ImplicitFunction<T>>
30Union(const std::vector<std::shared_ptr<P>>& a_implicitFunctions) noexcept;
31
38template <class T, class P1, class P2>
39std::shared_ptr<ImplicitFunction<T>>
40Union(const std::shared_ptr<P1>& a_implicitFunctionA, const std::shared_ptr<P2>& a_implicitFunctionB) noexcept;
41
48template <class T, class P = ImplicitFunction<T>>
49std::shared_ptr<ImplicitFunction<T>>
50SmoothUnion(const std::vector<std::shared_ptr<P>>& a_implicitFunctions, const T a_smooth) noexcept;
51
59template <class T, class P1, class P2>
60std::shared_ptr<ImplicitFunction<T>>
61SmoothUnion(const std::shared_ptr<P1>& a_implicitFunctionA,
62 const std::shared_ptr<P2>& a_implicitFunctionB,
63 const T a_smooth) noexcept;
64
71template <class T, class P, class BV, size_t K>
72std::shared_ptr<ImplicitFunction<T>>
73FastUnion(const std::vector<std::shared_ptr<P>>& a_implicitFunctions,
74 const std::vector<BV>& a_boundingVolumes) noexcept;
75
83template <class T, class P, class BV, size_t K>
84std::shared_ptr<ImplicitFunction<T>>
85FastSmoothUnion(const std::vector<std::shared_ptr<P>>& a_implicitFunctions,
86 const std::vector<BV>& a_boundingVolumes,
87 const T a_smoothLen) noexcept;
88
94template <class T, class P>
95std::shared_ptr<ImplicitFunction<T>>
96Intersection(const std::vector<std::shared_ptr<P>>& a_implicitFunctions) noexcept;
97
104template <class T, class P1, class P2>
105std::shared_ptr<ImplicitFunction<T>>
106Intersection(const std::shared_ptr<std::shared_ptr<P1>>& a_implicitFunctionA,
107 const std::shared_ptr<std::shared_ptr<P2>>& a_implicitFunctionB) noexcept;
108
115template <class T, class P>
116std::shared_ptr<ImplicitFunction<T>>
117SmoothIntersection(const std::vector<std::shared_ptr<P>>& a_implicitFunctions, const T a_smooth) noexcept;
118
126template <class T, class P1, class P2>
127std::shared_ptr<ImplicitFunction<T>>
128SmoothIntersection(const std::shared_ptr<P1>& a_implicitFunctionA,
129 const std::shared_ptr<P2>& a_implicitFunctionB,
130 const T a_smooth) noexcept;
131
138template <class T, class P1 = ImplicitFunction<T>, class P2 = ImplicitFunction<T>>
139std::shared_ptr<ImplicitFunction<T>>
140Difference(const std::shared_ptr<P1>& a_implicitFunctionA, const std::shared_ptr<P2>& a_implicitFunctionB) noexcept;
141
149template <class T, class P1 = ImplicitFunction<T>, class P2 = ImplicitFunction<T>>
150std::shared_ptr<ImplicitFunction<T>>
151SmoothDifference(const std::shared_ptr<P1>& a_implicitFunctionA,
152 const std::shared_ptr<P2>& a_implicitFunctionB,
153 const T a_smoothLen) noexcept;
154
163template <class T, class P = ImplicitFunction<T>>
164std::shared_ptr<ImplicitFunction<T>>
165FiniteRepetition(const std::shared_ptr<P>& a_implicitFunction,
166 const Vec3T<T>& a_period,
167 const Vec3T<T>& a_repeatLo,
168 const Vec3T<T>& a_repeatHi) noexcept;
169
176template <class T>
177std::function<T(const T& a, const T& b, const T& s)> expMin = [](const T& a, const T& b, const T& s) -> T {
178 T ret = exp(-a / s) + exp(-b / s);
179
180 return -log(ret) * s;
181};
182
189template <class T>
190std::function<T(const T& a, const T& b, const T& s)> smoothMin = [](const T& a, const T& b, const T& s) -> T {
191 const T h = std::max(s - std::abs(a - b), 0.0) / s;
192
193 return std::min(a, b) - 0.25 * h * h * s;
194};
195
202template <class T>
203std::function<T(const T& a, const T& b, const T& s)> smoothMax = [](const T& a, const T& b, const T& s) -> T {
204 const T h = std::max(s - std::abs(a - b), 0.0) / s;
205
206 return std::max(a, b) + 0.25 * h * h * s;
207};
208
212template <class T>
213class UnionIF : public ImplicitFunction<T>
214{
215public:
219 UnionIF() = delete;
220
225 UnionIF(const std::vector<std::shared_ptr<ImplicitFunction<T>>>& a_implicitFunctions) noexcept;
226
230 virtual ~UnionIF() = default;
231
236 T
237 value(const Vec3T<T>& a_point) const noexcept override;
238
239protected:
243 std::vector<std::shared_ptr<const ImplicitFunction<T>>> m_implicitFunctions;
244};
245
249template <class T>
251{
252public:
256 SmoothUnionIF() = delete;
257
264 SmoothUnionIF(const std::vector<std::shared_ptr<ImplicitFunction<T>>>& a_implicitFunctions,
265 const T a_smoothLen,
266 const std::function<T(const T& a, const T& b, const T& s)> a_smoothMin = smoothMin<T>) noexcept;
267
271 virtual ~SmoothUnionIF() = default;
272
277 T
278 value(const Vec3T<T>& a_point) const noexcept override;
279
280protected:
284 std::vector<std::shared_ptr<const ImplicitFunction<T>>> m_implicitFunctions;
285
290
294 std::function<T(const T&, const T&, const T&)> m_smoothMin;
295};
296
300template <class T, class P, class BV, size_t K>
302{
303public:
304 static_assert(std::is_base_of<EBGeometry::ImplicitFunction<T>, P>::value,
305 "FastUnionIF requires an implicit function");
306
310 using Root = typename EBGeometry::BVH::LinearBVH<T, P, BV, K>;
311
315 using Node = typename Root::LinearNode;
316
320 FastUnionIF() = delete;
321
326 FastUnionIF(const std::vector<std::pair<std::shared_ptr<const P>, BV>>& a_primsAndBVs) noexcept;
327
333 FastUnionIF(const std::vector<std::shared_ptr<P>>& a_primitives, const std::vector<BV>& a_boundingVolumes) noexcept;
334
338 virtual ~FastUnionIF() = default;
339
344 virtual T
345 value(const Vec3T<T>& a_point) const noexcept override;
346
350 const BV&
352
357 std::shared_ptr<EBGeometry::BVH::LinearBVH<T, P, BV, K>> m_bvh;
358
364 inline void
366 const BVH::Build a_build = BVH::Build::TopDown) noexcept;
367};
368
374template <class T, class P, class BV, size_t K>
376{
377public:
378 static_assert(std::is_base_of<EBGeometry::ImplicitFunction<T>, P>::value,
379 "FastSmoothUnionIF requires an implicit function");
380
384 using Root = typename EBGeometry::BVH::LinearBVH<T, P, BV, K>;
385
389 using Node = typename Root::LinearNode;
390
395
403 FastSmoothUnionIF(const std::vector<std::shared_ptr<P>>& a_distanceFunctions,
404 const std::vector<BV>& a_boundingVolumes,
405 const T a_smoothLen,
406 const std::function<T(const T&, const T&, const T&)> a_smoothMin = smoothMin<T>) noexcept;
407
411 virtual ~FastSmoothUnionIF() = default;
412
417 virtual T
418 value(const Vec3T<T>& a_point) const noexcept override;
419
420protected:
425
429 std::function<T(const T&, const T&, const T&)> m_smoothMin;
430};
431
435template <class T>
437{
438public:
442 IntersectionIF() = delete;
443
448 IntersectionIF(const std::vector<std::shared_ptr<ImplicitFunction<T>>>& a_implicitFunctions) noexcept;
449
453 virtual ~IntersectionIF() = default;
454
459 T
460 value(const Vec3T<T>& a_point) const noexcept override;
461
462protected:
466 std::vector<std::shared_ptr<const ImplicitFunction<T>>> m_implicitFunctions;
467};
468
472template <class T>
474{
475public:
480
489 const std::shared_ptr<ImplicitFunction<T>>& a_implicitFunctionB,
490 const T a_smoothLen,
491 const std::function<T(const T& a, const T& b, const T& s)>& a_smoothMax = smoothMax<T>) noexcept;
492
500 const T a_smoothLen,
501 const std::function<T(const T& a, const T& b, const T& s)>& a_smoothMax = smoothMax<T>) noexcept;
502
506 virtual ~SmoothIntersectionIF() = default;
507
512 T
513 value(const Vec3T<T>& a_point) const noexcept override;
514
515protected:
519 std::vector<std::shared_ptr<const ImplicitFunction<T>>> m_implicitFunctions;
520
525
529 std::function<T(const T& a, const T& b, const T& s)> m_smoothMax;
530};
531
535template <class T>
537{
538public:
542 DifferenceIF() = delete;
543
550 const std::shared_ptr<ImplicitFunction<T>>& a_implicitFunctionB) noexcept;
551
558 const std::vector<std::shared_ptr<ImplicitFunction<T>>>& a_implicitFunctionsB) noexcept;
559
563 virtual ~DifferenceIF() = default;
564
569 T
570 value(const Vec3T<T>& a_point) const noexcept override;
571
572protected:
576 std::shared_ptr<ImplicitFunction<T>> m_implicitFunctionA;
577
581 std::shared_ptr<ImplicitFunction<T>> m_implicitFunctionB;
582};
583
587template <class T>
589{
590public:
595
604 const std::shared_ptr<ImplicitFunction<T>>& a_implicitFunctionB,
605 const T a_smoothLen,
606 const std::function<T(const T& a, const T& b, const T& s)>& a_smoothMax = smoothMax<T>) noexcept;
607
616 const std::vector<std::shared_ptr<ImplicitFunction<T>>>& a_implicitFunctionsB,
617 const T a_smoothLen,
618 const std::function<T(const T& a, const T& b, const T& s)>& a_smoothMax = smoothMax<T>) noexcept;
619
623 virtual ~SmoothDifferenceIF() = default;
624
629 T
630 value(const Vec3T<T>& a_point) const noexcept override;
631
632protected:
636 std::shared_ptr<SmoothIntersectionIF<T>> m_smoothIntersectionIF;
637};
638
644template <class T>
646{
647public:
652
661 const Vec3T<T>& a_period,
662 const Vec3T<T>& a_repeatLo,
663 const Vec3T<T>& a_repeatHi) noexcept;
664
668 virtual ~FiniteRepetitionIF() = default;
669
674 T
675 value(const Vec3T<T>& a_point) const noexcept override;
676
677protected:
682
687
692
696 std::shared_ptr<ImplicitFunction<T>> m_implicitFunction;
697};
698
700
701#include "EBGeometry_CSGImplem.hpp"
702
703#endif
std::function< T(const T &a, const T &b, const T &s)> expMin
Exponential minimum function for CSG.
Definition EBGeometry_CSG.hpp:177
std::shared_ptr< ImplicitFunction< T > > Union(const std::vector< std::shared_ptr< P > > &a_implicitFunctions) noexcept
Convenience function for taking the union of a bunch of a implicit functions.
std::shared_ptr< ImplicitFunction< T > > FiniteRepetition(const std::shared_ptr< P > &a_implicitFunction, const Vec3T< T > &a_period, const Vec3T< T > &a_repeatLo, const Vec3T< T > &a_repeatHi) noexcept
Convenience function for creating a periodically repeated implicit function (FiniteRepetitionIF).
std::shared_ptr< ImplicitFunction< T > > SmoothDifference(const std::shared_ptr< P1 > &a_implicitFunctionA, const std::shared_ptr< P2 > &a_implicitFunctionB, const T a_smoothLen) noexcept
Convenience function for taking the smooth CSG difference.
std::shared_ptr< ImplicitFunction< T > > SmoothIntersection(const std::vector< std::shared_ptr< P > > &a_implicitFunctions, const T a_smooth) noexcept
Convenience function for taking the smooth intersection of a bunch of a implicit functions.
std::shared_ptr< ImplicitFunction< T > > FastSmoothUnion(const std::vector< std::shared_ptr< P > > &a_implicitFunctions, const std::vector< BV > &a_boundingVolumes, const T a_smoothLen) noexcept
Convenience function for taking the BVH-accelerated union of a bunch of a implicit functions.
std::function< T(const T &a, const T &b, const T &s)> smoothMin
Smooth minimum function for CSG.
Definition EBGeometry_CSG.hpp:190
std::shared_ptr< ImplicitFunction< T > > Intersection(const std::vector< std::shared_ptr< P > > &a_implicitFunctions) noexcept
Convenience function for taking the intersection of a bunch of a implicit functions.
std::shared_ptr< ImplicitFunction< T > > FastUnion(const std::vector< std::shared_ptr< P > > &a_implicitFunctions, const std::vector< BV > &a_boundingVolumes) noexcept
Convenience function for taking the BVH-accelerated union of a bunch of a implicit functions.
std::shared_ptr< ImplicitFunction< T > > SmoothUnion(const std::vector< std::shared_ptr< P > > &a_implicitFunctions, const T a_smooth) noexcept
Convenience function for taking the union of a bunch of a implicit functions.
std::function< T(const T &a, const T &b, const T &s)> smoothMax
Smooth maximum function for CSG.
Definition EBGeometry_CSG.hpp:203
std::shared_ptr< ImplicitFunction< T > > Difference(const std::shared_ptr< P1 > &a_implicitFunctionA, const std::shared_ptr< P2 > &a_implicitFunctionB) noexcept
Convenience function for taking the CSG difference.
Abstract base class for representing an implicit function.
CSG difference between two implicit functions.
Definition EBGeometry_CSG.hpp:537
DifferenceIF(const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunctionA, const std::vector< std::shared_ptr< ImplicitFunction< T > > > &a_implicitFunctionsB) noexcept
Full constructor. Computes the CSG difference between A and the union of the B's.
std::shared_ptr< ImplicitFunction< T > > m_implicitFunctionA
First implicit function.
Definition EBGeometry_CSG.hpp:576
DifferenceIF(const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunctionA, const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunctionB) noexcept
Full constructor. Computes the CSG difference.
virtual ~DifferenceIF()=default
Destructor (does nothing)
DifferenceIF()=delete
Disallowed, use the full constructor.
std::shared_ptr< ImplicitFunction< T > > m_implicitFunctionB
Subtracted implicit function.
Definition EBGeometry_CSG.hpp:581
T value(const Vec3T< T > &a_point) const noexcept override
Value function.
Implicit function smoothed union using BVHs.
Definition EBGeometry_CSG.hpp:376
FastSmoothUnionIF(const std::vector< std::shared_ptr< P > > &a_distanceFunctions, const std::vector< BV > &a_boundingVolumes, const T a_smoothLen, const std::function< T(const T &, const T &, const T &)> a_smoothMin=smoothMin< T >) noexcept
Full constructor - constructs bounding volumes in place.
typename Root::LinearNode Node
Alias for linear BVH node.
Definition EBGeometry_CSG.hpp:389
FastSmoothUnionIF()=delete
Disallowed, use the full constructor.
T m_smoothLen
Smoothing length.
Definition EBGeometry_CSG.hpp:424
typename EBGeometry::BVH::LinearBVH< T, P, BV, K > Root
Alias for linear BVH type.
Definition EBGeometry_CSG.hpp:384
virtual ~FastSmoothUnionIF()=default
Destructor (does nothing)
std::function< T(const T &, const T &, const T &)> m_smoothMin
Smooth min operator.
Definition EBGeometry_CSG.hpp:429
virtual T value(const Vec3T< T > &a_point) const noexcept override
Value function.
Implicit function union using BVHs.
Definition EBGeometry_CSG.hpp:302
FastUnionIF(const std::vector< std::pair< std::shared_ptr< const P >, BV > > &a_primsAndBVs) noexcept
Full constructor - constructs bounding volumes in place.
FastUnionIF(const std::vector< std::shared_ptr< P > > &a_primitives, const std::vector< BV > &a_boundingVolumes) noexcept
Full constructor - constructs bounding volumes in place.
void buildTree(const std::vector< std::pair< std::shared_ptr< const P >, BV > > &a_primsAndBVs, const BVH::Build a_build=BVH::Build::TopDown) noexcept
Build BVH tree for the input objects.
FastUnionIF()=delete
Disallowed, use the full constructor.
std::shared_ptr< EBGeometry::BVH::LinearBVH< T, P, BV, K > > m_bvh
Root node for linearized BVH tree.
Definition EBGeometry_CSG.hpp:357
typename EBGeometry::BVH::LinearBVH< T, P, BV, K > Root
Alias for linear BVH type.
Definition EBGeometry_CSG.hpp:310
const BV & getBoundingVolume() const noexcept
Get the bounding volume.
typename Root::LinearNode Node
Alias for linear BVH node.
Definition EBGeometry_CSG.hpp:315
virtual T value(const Vec3T< T > &a_point) const noexcept override
Value function.
virtual ~FastUnionIF()=default
Destructor (does nothing)
Class which creates a periodic repetition of an implicit function.
Definition EBGeometry_CSG.hpp:646
virtual ~FiniteRepetitionIF()=default
Destructor (does nothing)
std::shared_ptr< ImplicitFunction< T > > m_implicitFunction
Underlying implicit function.
Definition EBGeometry_CSG.hpp:696
Vec3T< T > m_repeatLo
Number of repetition over increasing coordinate direction.
Definition EBGeometry_CSG.hpp:691
Vec3T< T > m_repeatHi
Number of repetition over increasing coordinate direction.
Definition EBGeometry_CSG.hpp:686
T value(const Vec3T< T > &a_point) const noexcept override
Value function.
FiniteRepetitionIF(const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunction, const Vec3T< T > &a_period, const Vec3T< T > &a_repeatLo, const Vec3T< T > &a_repeatHi) noexcept
Full constructor.
FiniteRepetitionIF()=delete
Disallowed - use the full constructor.
Vec3T< T > m_period
Repetition period.
Definition EBGeometry_CSG.hpp:681
Abstract representation of an implicit function function (not necessarily signed distance).
Definition EBGeometry_ImplicitFunction.hpp:27
CSG intersection. Computes the maximum value of all input primitives.
Definition EBGeometry_CSG.hpp:437
IntersectionIF(const std::vector< std::shared_ptr< ImplicitFunction< T > > > &a_implicitFunctions) noexcept
Full constructor. Computes the CSG intersection.
T value(const Vec3T< T > &a_point) const noexcept override
Value function.
IntersectionIF()=delete
Disallowed, use the full constructor.
std::vector< std::shared_ptr< const ImplicitFunction< T > > > m_implicitFunctions
List of primitives.
Definition EBGeometry_CSG.hpp:466
virtual ~IntersectionIF()=default
Destructor (does nothing)
CSG difference between two implicit functions.
Definition EBGeometry_CSG.hpp:589
SmoothDifferenceIF(const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunctionA, const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunctionB, const T a_smoothLen, const std::function< T(const T &a, const T &b, const T &s)> &a_smoothMax=smoothMax< T >) noexcept
Full constructor. Computes the smooth CSG difference.
virtual ~SmoothDifferenceIF()=default
Destructor (does nothing)
SmoothDifferenceIF()=delete
Disallowed, use the full constructor.
T value(const Vec3T< T > &a_point) const noexcept override
Value function.
SmoothDifferenceIF(const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunctionA, const std::vector< std::shared_ptr< ImplicitFunction< T > > > &a_implicitFunctionsB, const T a_smoothLen, const std::function< T(const T &a, const T &b, const T &s)> &a_smoothMax=smoothMax< T >) noexcept
Full constructor. Computes the CSG difference between A and the union of the B's.
std::shared_ptr< SmoothIntersectionIF< T > > m_smoothIntersectionIF
Resulting smooth intersection.
Definition EBGeometry_CSG.hpp:636
Smooth intersection.
Definition EBGeometry_CSG.hpp:474
std::vector< std::shared_ptr< const ImplicitFunction< T > > > m_implicitFunctions
List of primitives.
Definition EBGeometry_CSG.hpp:519
SmoothIntersectionIF(const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunctionA, const std::shared_ptr< ImplicitFunction< T > > &a_implicitFunctionB, const T a_smoothLen, const std::function< T(const T &a, const T &b, const T &s)> &a_smoothMax=smoothMax< T >) noexcept
Full constructor. Computes the CSG intersection.
std::function< T(const T &a, const T &b, const T &s)> m_smoothMax
Smooth max operator.
Definition EBGeometry_CSG.hpp:529
SmoothIntersectionIF()=delete
Disallowed, use the full constructor.
T m_smoothLen
Smoothing parameter.
Definition EBGeometry_CSG.hpp:524
T value(const Vec3T< T > &a_point) const noexcept override
Value function.
virtual ~SmoothIntersectionIF()=default
Destructor (does nothing)
SmoothIntersectionIF(const std::vector< std::shared_ptr< ImplicitFunction< T > > > &a_implicitFunctions, const T a_smoothLen, const std::function< T(const T &a, const T &b, const T &s)> &a_smoothMax=smoothMax< T >) noexcept
Full constructor. Computes the CSG intersection.
Smooth CSG union. Computes the minimum value of all input primitives.
Definition EBGeometry_CSG.hpp:251
T value(const Vec3T< T > &a_point) const noexcept override
Value function.
SmoothUnionIF()=delete
Disallowed, use the full constructor.
std::vector< std::shared_ptr< const ImplicitFunction< T > > > m_implicitFunctions
List of primitives.
Definition EBGeometry_CSG.hpp:284
std::function< T(const T &, const T &, const T &)> m_smoothMin
Function for taking the smooth minimum.
Definition EBGeometry_CSG.hpp:294
T m_smoothLen
Smoothing parameter.
Definition EBGeometry_CSG.hpp:289
SmoothUnionIF(const std::vector< std::shared_ptr< ImplicitFunction< T > > > &a_implicitFunctions, const T a_smoothLen, const std::function< T(const T &a, const T &b, const T &s)> a_smoothMin=smoothMin< T >) noexcept
Full constructor. Computes the CSG union.
virtual ~SmoothUnionIF()=default
Destructor (does nothing)
CSG union. Computes the minimum value of all input primitives.
Definition EBGeometry_CSG.hpp:214
UnionIF(const std::vector< std::shared_ptr< ImplicitFunction< T > > > &a_implicitFunctions) noexcept
Full constructor. Computes the CSG union.
virtual ~UnionIF()=default
Destructor (does nothing)
std::vector< std::shared_ptr< const ImplicitFunction< T > > > m_implicitFunctions
List of primitives.
Definition EBGeometry_CSG.hpp:243
UnionIF()=delete
Disallowed, use the full constructor.
T value(const Vec3T< T > &a_point) const noexcept override
Value function.
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:218
Namespace for various bounding volume hierarchy (BVH) functionality.
Definition EBGeometry_BVH.hpp:30
Name space for all of EBGeometry.
Definition EBGeometry.hpp:23