]> git.tdb.fi Git - libs/math.git/blob - source/geometry/union.h
Add shapes representing set operations
[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 HyperBox<T, D> combine_aabb(const HyperBox<T, D> &, const HyperBox<T, D> &);
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 public:
27         Union(const Shape<T, D> &, const Shape<T, D> &);
28         Union(const std::vector<Shape<T, D> *> &);
29
30         virtual Union *clone() const;
31 };
32
33 template<typename T, unsigned D>
34 inline Union<T, D>::Union(const Shape<T, D> &s1, const Shape<T, D> &s2):
35         CompositeShape<T, D, UnionOps<T, D> >(s1, s2)
36 { }
37
38 template<typename T, unsigned D>
39 inline Union<T, D>::Union(const std::vector<Shape<T, D> *> &s):
40         CompositeShape<T, D, UnionOps<T, D> >(s)
41 { }
42
43 template<typename T, unsigned D>
44 inline Union<T, D> *Union<T, D>::clone() const
45 {
46         return new Union<T, D>(this->shapes);
47 }
48
49
50 template<typename T, unsigned D>
51 inline HyperBox<T, D> UnionOps<T, D>::combine_aabb(const HyperBox<T, D> &box1, const HyperBox<T, D> &box2)
52 {
53         LinAl::Vector<T, D> dimensions;
54         for(unsigned i=0; i<D; ++i)
55                 dimensions[i] = std::max(box1.get_dimension(i), box2.get_dimension(i));
56         return HyperBox<T, D>(dimensions);
57 }
58
59 } // namespace Geometry
60 } // namespace Msp
61
62 #endif