]> git.tdb.fi Git - libs/math.git/blob - source/geometry/intersection.h
Optimize bounding box bisection with more early culling
[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 template<typename T, unsigned D>
10 struct IntersectionOps
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 ((a==PARTIAL_COVERAGE && b==a) ? UNCERTAIN_COVERAGE : std::min(a, b)); }
14         static bool shortcircuit(bool c) { return !c; }
15 };
16
17 /**
18 Forms a shape from the common parts of component shapes.
19 */
20 template<typename T, unsigned D>
21 class Intersection: public CompositeShape<T, D, IntersectionOps<T, D> >
22 {
23 private:
24         Intersection() { }
25 public:
26         Intersection(const Shape<T, D> &, const Shape<T, D> &);
27
28         template<typename Iter>
29         static Intersection from_iterator_range(const Iter &, const Iter &);
30
31         virtual Intersection *clone() const;
32 };
33
34 template<typename T, unsigned D>
35 inline Intersection<T, D>::Intersection(const Shape<T, D> &s1, const Shape<T, D> &s2):
36         CompositeShape<T, D, IntersectionOps<T, D> >(s1, s2)
37 { }
38
39 template<typename T, unsigned D>
40 template<typename Iter>
41 inline Intersection<T, D> Intersection<T, D>::from_iterator_range(const Iter &begin, const Iter &end)
42 {
43         Intersection<T, D> shape;
44         shape.init_from_iter_range(begin, end);
45         return shape;
46 }
47
48 template<typename T, unsigned D>
49 inline Intersection<T, D> *Intersection<T, D>::clone() const
50 {
51         return new Intersection<T, D>(*this);
52 }
53
54 } // namespace Geometry
55 } // namespace Msp
56
57 #endif