]> git.tdb.fi Git - libs/math.git/blob - source/geometry/union.h
Implement bounding boxes with a separate class
[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 /**
10 Joins component shapes together into one.
11 */
12 template<typename T, unsigned D>
13 struct UnionOps
14 {
15         static BoundingBox<T, D> combine_aabb(const BoundingBox<T, D> &a, const BoundingBox<T, D> &b) { return a|b; }
16         static bool init_inside() { return false; }
17         static bool combine_inside(bool a, bool b) { return a || b; }
18         static bool is_inside_decided(bool a) { return a; }
19         static bool init_surface() { return true; }
20         static bool combine_surface(bool a, bool b) { return a && !b; }
21 };
22
23 template<typename T, unsigned D>
24 class Union: public CompositeShape<T, D, UnionOps<T, D> >
25 {
26 private:
27         Union() { }
28 public:
29         Union(const Shape<T, D> &, const Shape<T, D> &);
30
31         template<typename Iter>
32         static Union from_iterator_range(const Iter &, const Iter &);
33
34         virtual Union *clone() const;
35 };
36
37 template<typename T, unsigned D>
38 inline Union<T, D>::Union(const Shape<T, D> &s1, const Shape<T, D> &s2):
39         CompositeShape<T, D, UnionOps<T, D> >(s1, s2)
40 { }
41
42 template<typename T, unsigned D>
43 template<typename Iter>
44 inline Union<T, D> Union<T, D>::from_iterator_range(const Iter &begin, const Iter &end)
45 {
46         Union<T, D> shape;
47         shape.init_from_iter_range(begin, end);
48         return shape;
49 }
50
51 template<typename T, unsigned D>
52 inline Union<T, D> *Union<T, D>::clone() const
53 {
54         return new Union<T, D>(*this);
55 }
56
57 } // namespace Geometry
58 } // namespace Msp
59
60 #endif