]> git.tdb.fi Git - libs/math.git/commitdiff
Add a HalfSpace shape
authorMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2013 12:31:57 +0000 (15:31 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2013 12:35:04 +0000 (15:35 +0300)
source/geometry/dummy.cpp
source/geometry/halfspace.h [new file with mode: 0644]

index ed9c219180a6c702a226129282337c33243bcc8f..a262e268caf98bd4022297bba8a310762aa0280e 100644 (file)
@@ -2,6 +2,7 @@
 #include "angle.h"
 #include "box.h"
 #include "circle.h"
+#include "halfspace.h"
 #include "hyperbox.h"
 #include "hypersphere.h"
 #include "intersection.h"
diff --git a/source/geometry/halfspace.h b/source/geometry/halfspace.h
new file mode 100644 (file)
index 0000000..9ed80ff
--- /dev/null
@@ -0,0 +1,85 @@
+#ifndef MSP_GEOMETRY_HALFSPACE_H_
+#define MSP_GEOMETRY_HALFSPACE_H_
+
+#include "shape.h"
+
+namespace Msp {
+namespace Geometry {
+
+/**
+An infinite shape consisting of the space on one side of a plane.  Mostly
+useful when composited with other shapes.
+*/
+template<typename T, unsigned D>
+class HalfSpace: public Shape<T, D>
+{
+private:
+       LinAl::Vector<T, D> normal;
+
+public:
+       HalfSpace();
+       HalfSpace(const LinAl::Vector<T, D> &);
+
+       virtual HalfSpace *clone() const;
+
+       const LinAl::Vector<T, D> &get_normal() const { return normal; }
+
+       virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
+       virtual bool contains(const LinAl::Vector<T, D> &) const;
+       virtual unsigned get_max_ray_intersections() const { return 1; }
+       virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
+};
+
+template<typename T, unsigned D>
+inline HalfSpace<T, D>::HalfSpace()
+{
+       normal[0] = 1;
+}
+
+template<typename T, unsigned D>
+inline HalfSpace<T, D>::HalfSpace(const LinAl::Vector<T, D> &n):
+       normal(normalize(n))
+{ }
+
+template<typename T, unsigned D>
+inline HalfSpace<T, D> *HalfSpace<T, D>::clone() const
+{
+       return new HalfSpace<T, D>(normal);
+}
+
+template<typename T, unsigned D>
+inline HyperBox<T, D> HalfSpace<T, D>::get_axis_aligned_bounding_box() const
+{
+       // XXX Implement this properly
+       return HyperBox<T, D>();
+}
+
+template<typename T, unsigned D>
+inline bool HalfSpace<T, D>::contains(const LinAl::Vector<T, D> &point) const
+{
+       return inner_product(point, normal)<=0;
+}
+
+template<typename T, unsigned D>
+inline unsigned HalfSpace<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
+{
+       T x = -inner_product(ray.get_start(), normal)/inner_product(ray.get_direction(), normal);
+       if(ray.check_limits(x))
+       {
+               if(points && size>0)
+               {
+                       points[0].position = ray.get_start()+ray.get_direction()*x;
+                       points[0].normal = normal;
+                       points[0].distance = x;
+               }
+
+               return 1;
+       }
+
+       return 0;
+}
+
+} // namespace Geometry
+} // namespace Msp
+
+#endif