]> git.tdb.fi Git - libs/math.git/blob - source/geometry/shape.h
Make the check_intersection function non-virtual
[libs/math.git] / source / geometry / shape.h
1 #ifndef MSP_GEOMETRY_SHAPE_H_
2 #define MSP_GEOMETRY_SHAPE_H_
3
4 #include <vector>
5 #include <msp/linal/vector.h>
6
7 namespace Msp {
8 namespace Geometry {
9
10 template<typename T, unsigned D>
11 class HyperBox;
12
13 template<typename T, unsigned D>
14 class Ray;
15
16 template<typename T, unsigned D>
17 class SurfacePoint;
18
19 /**
20 Base class and interface for geometric shapes.  Shapes may be bounded or
21 unbounded.  They are always considered to be solid, i.e. have a distinct inside
22 and an outside.
23 */
24 template<typename T, unsigned D>
25 class Shape
26 {
27 protected:
28         Shape() { }
29 public:
30         virtual ~Shape() { }
31
32         virtual Shape *clone() const = 0;
33
34         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const = 0;
35         virtual bool contains(const LinAl::Vector<T, D> &) const = 0;
36         bool check_intersection(const Ray<T, D> &) const;
37         virtual unsigned get_max_ray_intersections() const = 0;
38         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const = 0;
39         std::vector<SurfacePoint<T, D> > get_intersections(const Ray<T, D> &) const;
40 };
41
42 template<typename T, unsigned D>
43 inline bool Shape<T, D>::check_intersection(const Ray<T, D> &ray) const
44 {
45         return get_intersections(ray, 0, 1);
46 }
47
48 template<typename T, unsigned D>
49 inline std::vector<SurfacePoint<T, D> > Shape<T, D>::get_intersections(const Ray<T, D> &ray) const
50 {
51         unsigned max_isect = get_max_ray_intersections();
52         std::vector<SurfacePoint<T, D> > points(max_isect);
53         unsigned count = get_intersections(ray, &points[0], max_isect);
54         points.resize(count);
55         return points;
56 }
57
58 } // namespace Geometry
59 } // namespace Msp
60
61 #endif