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