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