]> git.tdb.fi Git - libs/math.git/blob - source/geometry/transformedshape.h
Put ray and bounding box transformations in AffineTransformation
[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 "shape.h"
6
7 namespace Msp {
8 namespace Geometry {
9
10 /**
11 A shape modified by an affine transformation.
12 */
13 template<typename T, unsigned D>
14 class TransformedShape: public Shape<T, D>
15 {
16 private:
17         Shape<T, D> *shape;
18         AffineTransformation<T, D> transformation;
19         AffineTransformation<T, D> inverse_trans;
20
21 public:
22         TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
23         TransformedShape(const TransformedShape &);
24         TransformedShape &operator=(const TransformedShape &);
25         ~TransformedShape();
26
27         virtual TransformedShape *clone() const;
28
29         const Shape<T, D> &get_shape() const { return *shape; }
30         const AffineTransformation<T, D> &get_transformation() const { return transformation; }
31
32         virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
33         virtual bool contains(const LinAl::Vector<T, D> &) const;
34         virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
35         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
36 };
37
38 template<typename T, unsigned D>
39 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
40         shape(s.clone()),
41         transformation(t),
42         inverse_trans(invert(t))
43 { }
44
45 template<typename T, unsigned D>
46 inline TransformedShape<T, D>::TransformedShape(const TransformedShape<T, D> &other):
47         shape(other.shape->clone()),
48         transformation(other.transformation),
49         inverse_trans(other.inverse_trans)
50 { }
51
52 template<typename T, unsigned D>
53 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
54 {
55         delete shape;
56         shape = other.shape->clone();
57         transformation = other.transformation;
58         inverse_trans = other.inverse_trans;
59 }
60
61 template<typename T, unsigned D>
62 inline TransformedShape<T, D>::~TransformedShape()
63 {
64         delete shape;
65 }
66
67 template<typename T, unsigned D>
68 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
69 {
70         return new TransformedShape<T, D>(*this);
71 }
72
73 template<typename T, unsigned D>
74 inline BoundingBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
75 {
76         return transformation.transform(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 unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
87 {
88         Ray<T, D> local_ray = inverse_trans.transform(ray);
89
90         unsigned count = shape->get_intersections(local_ray, points, size);
91         if(points)
92         {
93                 for(unsigned i=0; i<count; ++i)
94                 {
95                         points[i].position = transformation.transform(points[i].position);
96                         /* XXX This is not correct for nonuniform scaling.  Inverse of the
97                         transpose of the upper DxD part of the matrix should be used. */
98                         points[i].normal = transformation.transform_linear(points[i].normal);
99                         points[i].distance = inner_product(points[i].position-ray.get_start(), ray.get_direction());
100                 }
101         }
102         return count;
103 }
104
105 } // namespace Geometry
106 } // namespace Msp
107
108 #endif