]> git.tdb.fi Git - libs/math.git/blob - source/geometry/transformedshape.h
Don't qualify potentially overloaded functions with the std namespace
[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
18 public:
19         TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
20         TransformedShape(const TransformedShape &);
21         TransformedShape &operator=(const TransformedShape &);
22         ~TransformedShape();
23
24         virtual TransformedShape *clone() const;
25
26         const Shape<T, D> &get_shape() const { return *shape; }
27         const AffineTransformation<T, D> &get_transformation() const { return transformation; }
28
29         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
30         virtual bool check_intersection(const Ray<T, D> &) const;
31 };
32
33 template<typename T, unsigned D>
34 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
35         shape(s.clone()),
36         transformation(t)
37 { }
38
39 template<typename T, unsigned D>
40 inline TransformedShape<T, D>::TransformedShape(const TransformedShape &other):
41         shape(other.shape->clone()),
42         transformation(other.transformation)
43 { }
44
45 template<typename T, unsigned D>
46 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
47 {
48         delete shape;
49         shape = other.shape->clone();
50         transformation = other.transformation();
51 }
52
53 template<typename T, unsigned D>
54 inline TransformedShape<T, D>::~TransformedShape()
55 {
56         delete shape;
57 }
58
59 template<typename T, unsigned D>
60 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
61 {
62         return new TransformedShape<T, D>(*this);
63 }
64
65 template<typename T, unsigned D>
66 inline HyperBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
67 {
68         // XXX This is not correct for most shapes
69         return shape->get_axis_aligned_bounding_box();
70 }
71
72 template<typename T, unsigned D>
73 inline bool TransformedShape<T, D>::check_intersection(const Ray<T, D> &ray) const
74 {
75         // TODO cache the inverse transformation for performance
76         LinAl::SquareMatrix<T, D+1> inverse_trans = LinAl::invert(transformation.get_matrix());
77         Ray<T, D> trans_ray(reduce_vector(inverse_trans*augment_vector(ray.get_start(), T(1))),
78                 reduce_vector(inverse_trans*augment_vector(ray.get_direction(), T(0))));
79         return shape->check_intersection(trans_ray);
80 }
81
82 } // namespace Geometry
83 } // namespace Msp
84
85 #endif