]> git.tdb.fi Git - libs/math.git/blob - source/geometry/union.h
Optimize bounding box bisection with more early culling
[libs/math.git] / source / geometry / union.h
1 #ifndef MSP_GEOMETRY_UNION_H_
2 #define MSP_GEOMETRY_UNION_H_
3
4 #include "compositeshape.h"
5
6 namespace Msp {
7 namespace Geometry {
8
9 template<typename T, unsigned D>
10 struct UnionOps
11 {
12         static BoundingBox<T, D> combine_aabb(const BoundingBox<T, D> &a, const BoundingBox<T, D> &b) { return a|b; }
13         static Coverage combine_coverage(Coverage a, Coverage b) { return std::max(a, b); }
14         static bool shortcircuit(bool c) { return c; }
15 };
16
17 /**
18 Joins component shapes together into one.
19 */
20 template<typename T, unsigned D>
21 class Union: public CompositeShape<T, D, UnionOps<T, D> >
22 {
23 private:
24         Union() { }
25 public:
26         Union(const Shape<T, D> &, const Shape<T, D> &);
27
28         template<typename Iter>
29         static Union from_iterator_range(const Iter &, const Iter &);
30
31         virtual Union *clone() const;
32 };
33
34 template<typename T, unsigned D>
35 inline Union<T, D>::Union(const Shape<T, D> &s1, const Shape<T, D> &s2):
36         CompositeShape<T, D, UnionOps<T, D> >(s1, s2)
37 { }
38
39 template<typename T, unsigned D>
40 template<typename Iter>
41 inline Union<T, D> Union<T, D>::from_iterator_range(const Iter &begin, const Iter &end)
42 {
43         Union<T, D> shape;
44         shape.init_from_iter_range(begin, end);
45         return shape;
46 }
47
48 template<typename T, unsigned D>
49 inline Union<T, D> *Union<T, D>::clone() const
50 {
51         return new Union<T, D>(*this);
52 }
53
54 } // namespace Geometry
55 } // namespace Msp
56
57 #endif