]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/intersection.h
Add shapes representing set operations
[libs/math.git] / source / geometry / intersection.h
diff --git a/source/geometry/intersection.h b/source/geometry/intersection.h
new file mode 100644 (file)
index 0000000..53c25c2
--- /dev/null
@@ -0,0 +1,62 @@
+#ifndef MSP_GEOMETRY_INTERSECTION_H_
+#define MSP_GEOMETRY_INTERSECTION_H_
+
+#include "compositeshape.h"
+
+namespace Msp {
+namespace Geometry {
+
+/**
+Forms a shape from the common parts of component shapes.
+*/
+template<typename T, unsigned D>
+struct IntersectionOps
+{
+       static HyperBox<T, D> combine_aabb(const HyperBox<T, D> &, const HyperBox<T, D> &);
+       static bool init_inside() { return true; }
+       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; }
+};
+
+template<typename T, unsigned D>
+class Intersection: public CompositeShape<T, D, IntersectionOps<T, D> >
+{
+public:
+       Intersection(const Shape<T, D> &, const Shape<T, D> &);
+       Intersection(const std::vector<Shape<T, D> *> &);
+
+       virtual Intersection *clone() const;
+};
+
+template<typename T, unsigned D>
+Intersection<T, D>::Intersection(const Shape<T, D> &s1, const Shape<T, D> &s2):
+       CompositeShape<T, D, IntersectionOps<T, D> >(s1, s2)
+{ }
+
+template<typename T, unsigned D>
+Intersection<T, D>::Intersection(const std::vector<Shape<T, D> *> &s):
+       CompositeShape<T, D, IntersectionOps<T, D> >(s)
+{ }
+
+template<typename T, unsigned D>
+Intersection<T, D> *Intersection<T, D>::clone() const
+{
+       return new Intersection<T, D>(this->shapes);
+}
+
+
+template<typename T, unsigned D>
+inline HyperBox<T, D> IntersectionOps<T, D>::combine_aabb(const HyperBox<T, D> &box1, const HyperBox<T, D> &box2)
+{
+       LinAl::Vector<T, D> dimensions;
+       for(unsigned i=0; i<D; ++i)
+               dimensions[i] = std::min(box1.get_dimension(i), box2.get_dimension(i));
+       return HyperBox<T, D>(dimensions);
+}
+
+} // namespace Geometry
+} // namespace Msp
+
+#endif