]> git.tdb.fi Git - libs/math.git/blob - source/geometry/transformedshape.h
Add more collision check functions for shapes
[libs/math.git] / source / geometry / transformedshape.h
1 #ifndef MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
2 #define MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
3
4 #include "affinetransformation.h"
5 #include "ray.h"
6 #include "shape.h"
7
8 namespace Msp {
9 namespace Geometry {
10
11 template<typename T, unsigned D>
12 class TransformedShape: public Shape<T, D>
13 {
14 private:
15         Shape<T, D> *shape;
16         AffineTransformation<T, D> transformation;
17         AffineTransformation<T, D> inverse_trans;
18
19 public:
20         TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
21         TransformedShape(const TransformedShape &);
22         TransformedShape &operator=(const TransformedShape &);
23         ~TransformedShape();
24
25         virtual TransformedShape *clone() const;
26
27         const Shape<T, D> &get_shape() const { return *shape; }
28         const AffineTransformation<T, D> &get_transformation() const { return transformation; }
29
30         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
31         virtual bool contains(const LinAl::Vector<T, D> &) const;
32         virtual bool check_intersection(const Ray<T, D> &) const;
33         virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
34         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
35 };
36
37 template<typename T, unsigned D>
38 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
39         shape(s.clone()),
40         transformation(t),
41         inverse_trans(invert(t))
42 { }
43
44 template<typename T, unsigned D>
45 inline TransformedShape<T, D>::TransformedShape(const TransformedShape &other):
46         shape(other.shape->clone()),
47         transformation(other.transformation),
48         inverse_trans(other.inverse_trans)
49 { }
50
51 template<typename T, unsigned D>
52 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
53 {
54         delete shape;
55         shape = other.shape->clone();
56         transformation = other.transformation;
57         inverse_trans = other.inverse_trans;
58 }
59
60 template<typename T, unsigned D>
61 inline TransformedShape<T, D>::~TransformedShape()
62 {
63         delete shape;
64 }
65
66 template<typename T, unsigned D>
67 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
68 {
69         return new TransformedShape<T, D>(*this);
70 }
71
72 template<typename T, unsigned D>
73 inline HyperBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
74 {
75         // XXX This is not correct for most shapes
76         return shape->get_axis_aligned_bounding_box();
77 }
78
79 template<typename T, unsigned D>
80 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
81 {
82         return shape->contains(inverse_trans.transform(point));
83 }
84
85 template<typename T, unsigned D>
86 inline bool TransformedShape<T, D>::check_intersection(const Ray<T, D> &ray) const
87 {
88         Ray<T, D> local_ray(inverse_trans.transform(ray.get_start()),
89                 inverse_trans.transform_linear(ray.get_direction()));
90         return shape->check_intersection(local_ray);
91 }
92
93 template<typename T, unsigned D>
94 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
95 {
96         Ray<T, D> local_ray(inverse_trans.transform(ray.get_start()),
97                 inverse_trans.transform_linear(ray.get_direction()));
98         unsigned count = shape->get_intersections(local_ray, points, size);
99         if(points)
100         {
101                 for(unsigned i=0; i<count; ++i)
102                 {
103                         points[i].position = transformation.transform(points[i].position);
104                         /* XXX This is not correct for nonuniform scaling.  Inverse of the
105                         transpose of the upper DxD part of the matrix should be used. */
106                         points[i].normal = transformation.transform(points[i].normal);
107                 }
108         }
109         return count;
110 }
111
112 } // namespace Geometry
113 } // namespace Msp
114
115 #endif