]> git.tdb.fi Git - libs/math.git/blob - source/geometry/intersection.h
Add shapes representing set operations
[libs/math.git] / source / geometry / intersection.h
1 #ifndef MSP_GEOMETRY_INTERSECTION_H_
2 #define MSP_GEOMETRY_INTERSECTION_H_
3
4 #include "compositeshape.h"
5
6 namespace Msp {
7 namespace Geometry {
8
9 /**
10 Forms a shape from the common parts of component shapes.
11 */
12 template<typename T, unsigned D>
13 struct IntersectionOps
14 {
15         static HyperBox<T, D> combine_aabb(const HyperBox<T, D> &, const HyperBox<T, D> &);
16         static bool init_inside() { return true; }
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 Intersection: public CompositeShape<T, D, IntersectionOps<T, D> >
25 {
26 public:
27         Intersection(const Shape<T, D> &, const Shape<T, D> &);
28         Intersection(const std::vector<Shape<T, D> *> &);
29
30         virtual Intersection *clone() const;
31 };
32
33 template<typename T, unsigned D>
34 Intersection<T, D>::Intersection(const Shape<T, D> &s1, const Shape<T, D> &s2):
35         CompositeShape<T, D, IntersectionOps<T, D> >(s1, s2)
36 { }
37
38 template<typename T, unsigned D>
39 Intersection<T, D>::Intersection(const std::vector<Shape<T, D> *> &s):
40         CompositeShape<T, D, IntersectionOps<T, D> >(s)
41 { }
42
43 template<typename T, unsigned D>
44 Intersection<T, D> *Intersection<T, D>::clone() const
45 {
46         return new Intersection<T, D>(this->shapes);
47 }
48
49
50 template<typename T, unsigned D>
51 inline HyperBox<T, D> IntersectionOps<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::min(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