From 484eec5f57755ae6430471b1e13b67f86f15c8fa Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 22 May 2013 15:31:57 +0300 Subject: [PATCH] Add a HalfSpace shape --- source/geometry/dummy.cpp | 1 + source/geometry/halfspace.h | 85 +++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 source/geometry/halfspace.h diff --git a/source/geometry/dummy.cpp b/source/geometry/dummy.cpp index ed9c219..a262e26 100644 --- a/source/geometry/dummy.cpp +++ b/source/geometry/dummy.cpp @@ -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 index 0000000..9ed80ff --- /dev/null +++ b/source/geometry/halfspace.h @@ -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 +class HalfSpace: public Shape +{ +private: + LinAl::Vector normal; + +public: + HalfSpace(); + HalfSpace(const LinAl::Vector &); + + virtual HalfSpace *clone() const; + + const LinAl::Vector &get_normal() const { return normal; } + + virtual HyperBox get_axis_aligned_bounding_box() const; + virtual bool contains(const LinAl::Vector &) const; + virtual unsigned get_max_ray_intersections() const { return 1; } + virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; +}; + +template +inline HalfSpace::HalfSpace() +{ + normal[0] = 1; +} + +template +inline HalfSpace::HalfSpace(const LinAl::Vector &n): + normal(normalize(n)) +{ } + +template +inline HalfSpace *HalfSpace::clone() const +{ + return new HalfSpace(normal); +} + +template +inline HyperBox HalfSpace::get_axis_aligned_bounding_box() const +{ + // XXX Implement this properly + return HyperBox(); +} + +template +inline bool HalfSpace::contains(const LinAl::Vector &point) const +{ + return inner_product(point, normal)<=0; +} + +template +inline unsigned HalfSpace::get_intersections(const Ray &ray, SurfacePoint *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 -- 2.43.0