]> git.tdb.fi Git - libs/math.git/blob - source/geometry/negation.h
0fc01964b869efd4d841f2bee439da3feffb24d8
[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
22         virtual Negation *clone() const;
23
24         const Shape<T, D> &get_shape() const { return *shape; }
25
26         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
27         virtual bool contains(const LinAl::Vector<T, D> &) const;
28         virtual bool check_intersection(const Ray<T, D> &) const;
29         virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
30         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
31 };
32
33 template<typename T, unsigned D>
34 inline Negation<T, D>::Negation(const Shape<T, D> &s):
35         shape(s.clone())
36 { }
37
38 template<typename T, unsigned D>
39 inline Negation<T, D> *Negation<T, D>::clone() const
40 {
41         return new Negation<T, D>(*shape);
42 }
43
44 template<typename T, unsigned D>
45 inline HyperBox<T, D> Negation<T, D>::get_axis_aligned_bounding_box() const
46 {
47         // XXX How do we handle this correctly?  In particular negation of a negation?
48         return HyperBox<T, D>();
49 }
50
51 template<typename T, unsigned D>
52 inline bool Negation<T, D>::contains(const LinAl::Vector<T, D> &point) const
53 {
54         return !shape->contains(point);
55 }
56
57 template<typename T, unsigned D>
58 inline bool Negation<T, D>::check_intersection(const Ray<T, D> &ray) const
59 {
60         return get_intersections(ray, 0, 1);
61 }
62
63 template<typename T, unsigned D>
64 inline unsigned Negation<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
65 {
66         unsigned count = shape->get_intersections(ray, points, size);
67         for(unsigned i=0; i<count; ++i)
68                 points[i].normal = -points[i].normal;
69         return count;
70 }
71
72 } // namespace Geometry
73 } // namespace Msp
74
75 #endif