1 #ifndef MSP_GEOMETRY_HALFSPACE_H_
2 #define MSP_GEOMETRY_HALFSPACE_H_
10 An unbounded shape consisting of the space on one side of a plane. Mostly
11 useful when intersected with other shapes.
13 template<typename T, unsigned D>
14 class HalfSpace: public Shape<T, D>
17 LinAl::Vector<T, D> normal;
21 HalfSpace(const LinAl::Vector<T, D> &);
23 virtual HalfSpace *clone() const;
25 const LinAl::Vector<T, D> &get_normal() const { return normal; }
27 virtual BoundingBox<T, D> get_axis_aligned_bounding_box(unsigned = 0) const;
28 virtual bool contains(const LinAl::Vector<T, D> &) const;
29 virtual unsigned get_max_ray_intersections() const { return 1; }
30 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
31 virtual Coverage get_coverage(const BoundingBox<T, D> &) const;
34 template<typename T, unsigned D>
35 inline HalfSpace<T, D>::HalfSpace()
40 template<typename T, unsigned D>
41 inline HalfSpace<T, D>::HalfSpace(const LinAl::Vector<T, D> &n):
45 template<typename T, unsigned D>
46 inline HalfSpace<T, D> *HalfSpace<T, D>::clone() const
48 return new HalfSpace<T, D>(normal);
51 template<typename T, unsigned D>
52 inline BoundingBox<T, D> HalfSpace<T, D>::get_axis_aligned_bounding_box(unsigned) const
54 // XXX If the normal is aligned to an axis, should the bounding box reflect that?
55 return ~BoundingBox<T, D>();
58 template<typename T, unsigned D>
59 inline bool HalfSpace<T, D>::contains(const LinAl::Vector<T, D> &point) const
61 return inner_product(point, normal)<=T(0);
64 template<typename T, unsigned D>
65 inline unsigned HalfSpace<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
67 T d = inner_product(ray.get_start(), normal);
68 T c = inner_product(ray.get_direction(), normal);
73 if(ray.check_limits(x))
77 points[0].position = ray.get_start()+ray.get_direction()*x;
78 points[0].normal = normal;
79 points[0].distance = x;
80 points[0].entry = (c<T(0));
89 template<typename T, unsigned D>
90 inline Coverage HalfSpace<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
92 LinAl::Vector<T, D> low_point;
93 LinAl::Vector<T, D> high_point;
94 for(unsigned i=0; i<D; ++i)
96 low_point[i] = (normal[i]>=T(0) ? bbox.get_minimum_coordinate(i) : bbox.get_maximum_coordinate(i));
97 high_point[i] = (normal[i]>=T(0) ? bbox.get_maximum_coordinate(i) : bbox.get_minimum_coordinate(i));
100 if(contains(high_point))
101 return FULL_COVERAGE;
102 else if(contains(low_point))
103 return PARTIAL_COVERAGE;
108 } // namespace Geometry