]> git.tdb.fi Git - libs/math.git/blob - source/geometry/negation.h
Negation contains a pointer and needs copy c'tor and operator=
[libs/math.git] / source / geometry / negation.h
1 #ifndef MSP_GEOMETRY_NEGATION_H_
2 #define MSP_GEOMETRY_NEGATION_H_
3
4 #include "shape.h"
5
6 namespace Msp {
7 namespace Geometry {
8
9 /**
10 Negates a shape.  Not particulary useful on its own, but can be used together
11 with Intersection to cut holes.
12 */
13 template<typename T, unsigned D>
14 class Negation: public Shape<T, D>
15 {
16 private:
17         Shape<T, D> *shape;
18
19 public:
20         Negation(const Shape<T, D> &);
21         Negation(const Negation &);
22         Negation &operator=(const Negation &);
23         ~Negation();
24
25         virtual Negation *clone() const;
26
27         const Shape<T, D> &get_shape() const { return *shape; }
28
29         virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
30         virtual bool contains(const LinAl::Vector<T, D> &) const;
31         virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
32         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
33 };
34
35 template<typename T, unsigned D>
36 inline Negation<T, D>::Negation(const Shape<T, D> &s):
37         shape(s.clone())
38 { }
39
40 template<typename T, unsigned D>
41 inline Negation<T, D>::Negation(const Negation<T, D> &other):
42         shape(other.shape->clone())
43 { }
44
45 template<typename T, unsigned D>
46 inline Negation<T, D> &Negation<T, D>::operator=(const Negation<T, D> &other)
47 {
48         delete shape;
49         shape = other.shape->clone();
50         return *this;
51 }
52
53 template<typename T, unsigned D>
54 inline Negation<T, D>::~Negation()
55 {
56         delete shape;
57 }
58
59 template<typename T, unsigned D>
60 inline Negation<T, D> *Negation<T, D>::clone() const
61 {
62         return new Negation<T, D>(*shape);
63 }
64
65 template<typename T, unsigned D>
66 inline BoundingBox<T, D> Negation<T, D>::get_axis_aligned_bounding_box() const
67 {
68         return ~shape->get_axis_aligned_bounding_box();
69 }
70
71 template<typename T, unsigned D>
72 inline bool Negation<T, D>::contains(const LinAl::Vector<T, D> &point) const
73 {
74         return !shape->contains(point);
75 }
76
77 template<typename T, unsigned D>
78 inline unsigned Negation<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
79 {
80         unsigned count = shape->get_intersections(ray, points, size);
81         for(unsigned i=0; i<count; ++i)
82                 points[i].normal = -points[i].normal;
83         return count;
84 }
85
86 } // namespace Geometry
87 } // namespace Msp
88
89 #endif