]> git.tdb.fi Git - libs/math.git/blob - source/geometry/halfspace.h
09ec62ce600b9258b1044a0113a61251f5c424aa
[libs/math.git] / source / geometry / halfspace.h
1 #ifndef MSP_GEOMETRY_HALFSPACE_H_
2 #define MSP_GEOMETRY_HALFSPACE_H_
3
4 #include "shape.h"
5
6 namespace Msp {
7 namespace Geometry {
8
9 /**
10 An unbounded shape consisting of the space on one side of a plane.  Mostly
11 useful when intersected with other shapes.
12 */
13 template<typename T, unsigned D>
14 class HalfSpace: public Shape<T, D>
15 {
16 private:
17         LinAl::Vector<T, D> normal;
18
19 public:
20         HalfSpace();
21         HalfSpace(const LinAl::Vector<T, D> &);
22
23         virtual HalfSpace *clone() const;
24
25         const LinAl::Vector<T, D> &get_normal() const { return normal; }
26
27         virtual BoundingBox<T, D> get_axis_aligned_bounding_box() 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;
32 };
33
34 template<typename T, unsigned D>
35 inline HalfSpace<T, D>::HalfSpace()
36 {
37         normal[0] = T(1);
38 }
39
40 template<typename T, unsigned D>
41 inline HalfSpace<T, D>::HalfSpace(const LinAl::Vector<T, D> &n):
42         normal(normalize(n))
43 { }
44
45 template<typename T, unsigned D>
46 inline HalfSpace<T, D> *HalfSpace<T, D>::clone() const
47 {
48         return new HalfSpace<T, D>(normal);
49 }
50
51 template<typename T, unsigned D>
52 inline BoundingBox<T, D> HalfSpace<T, D>::get_axis_aligned_bounding_box() const
53 {
54         // XXX If the normal is aligned to an axis, should the bounding box reflect that?
55         return ~BoundingBox<T, D>();
56 }
57
58 template<typename T, unsigned D>
59 inline bool HalfSpace<T, D>::contains(const LinAl::Vector<T, D> &point) const
60 {
61         return inner_product(point, normal)<=T(0);
62 }
63
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
66 {
67         T d = inner_product(ray.get_start(), normal);
68         T c = inner_product(ray.get_direction(), normal);
69         if(c==T(0))
70                 return 0;
71
72         T x = -d/c;
73         if(ray.check_limits(x))
74         {
75                 if(points && size>0)
76                 {
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));
81                 }
82
83                 return 1;
84         }
85
86         return 0;
87 }
88
89 template<typename T, unsigned D>
90 inline Coverage HalfSpace<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
91 {
92         LinAl::Vector<T, D> low_point;
93         LinAl::Vector<T, D> high_point;
94         for(unsigned i=0; i<D; ++i)
95         {
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));
98         }
99
100         if(contains(high_point))
101                 return FULL_COVERAGE;
102         else if(contains(low_point))
103                 return PARTIAL_COVERAGE;
104         else
105                 return NO_COVERAGE;
106 }
107
108 } // namespace Geometry
109 } // namespace Msp
110
111 #endif