]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/union.h
Optimize bounding box bisection with more early culling
[libs/math.git] / source / geometry / union.h
index fadeff2ac9270dd71718aade2e733504e6a44e5b..8c02f2b808dc8a18301653c77e8175e83b2ea555 100644 (file)
@@ -6,26 +6,27 @@
 namespace Msp {
 namespace Geometry {
 
-/**
-Joins component shapes together into one.
-*/
 template<typename T, unsigned D>
 struct UnionOps
 {
-       static HyperBox<T, D> combine_aabb(const HyperBox<T, D> &, const HyperBox<T, D> &);
-       static bool init_inside() { return false; }
-       static bool combine_inside(bool a, bool b) { return a || b; }
-       static bool is_inside_decided(bool a) { return a; }
-       static bool init_surface() { return true; }
-       static bool combine_surface(bool a, bool b) { return a && !b; }
+       static BoundingBox<T, D> combine_aabb(const BoundingBox<T, D> &a, const BoundingBox<T, D> &b) { return a|b; }
+       static Coverage combine_coverage(Coverage a, Coverage b) { return std::max(a, b); }
+       static bool shortcircuit(bool c) { return c; }
 };
 
+/**
+Joins component shapes together into one.
+*/
 template<typename T, unsigned D>
 class Union: public CompositeShape<T, D, UnionOps<T, D> >
 {
+private:
+       Union() { }
 public:
        Union(const Shape<T, D> &, const Shape<T, D> &);
-       Union(const std::vector<Shape<T, D> *> &);
+
+       template<typename Iter>
+       static Union from_iterator_range(const Iter &, const Iter &);
 
        virtual Union *clone() const;
 };
@@ -36,24 +37,18 @@ inline Union<T, D>::Union(const Shape<T, D> &s1, const Shape<T, D> &s2):
 { }
 
 template<typename T, unsigned D>
-inline Union<T, D>::Union(const std::vector<Shape<T, D> *> &s):
-       CompositeShape<T, D, UnionOps<T, D> >(s)
-{ }
-
-template<typename T, unsigned D>
-inline Union<T, D> *Union<T, D>::clone() const
+template<typename Iter>
+inline Union<T, D> Union<T, D>::from_iterator_range(const Iter &begin, const Iter &end)
 {
-       return new Union<T, D>(this->shapes);
+       Union<T, D> shape;
+       shape.init_from_iter_range(begin, end);
+       return shape;
 }
 
-
 template<typename T, unsigned D>
-inline HyperBox<T, D> UnionOps<T, D>::combine_aabb(const HyperBox<T, D> &box1, const HyperBox<T, D> &box2)
+inline Union<T, D> *Union<T, D>::clone() const
 {
-       LinAl::Vector<T, D> dimensions;
-       for(unsigned i=0; i<D; ++i)
-               dimensions[i] = std::max(box1.get_dimension(i), box2.get_dimension(i));
-       return HyperBox<T, D>(dimensions);
+       return new Union<T, D>(*this);
 }
 
 } // namespace Geometry