]> git.tdb.fi Git - libs/math.git/blob - source/geometry/negation.h
Implement bounding boxes with a separate class
[libs/math.git] / source / geometry / negation.h
1 #ifndef MSP_GEOMETRY_NEGATION_H_
2 #define MSP_GEOMETRY_NEGATION_H_
3
4 #include "boundingbox.h"
5 #include "shape.h"
6
7 namespace Msp {
8 namespace Geometry {
9
10 /**
11 Negates a shape.  Not particulary useful on its own, but can be used together
12 with Intersection to cut holes.
13 */
14 template<typename T, unsigned D>
15 class Negation: public Shape<T, D>
16 {
17 private:
18         Shape<T, D> *shape;
19
20 public:
21         Negation(const Shape<T, D> &);
22
23         virtual Negation *clone() const;
24
25         const Shape<T, D> &get_shape() const { return *shape; }
26
27         virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
28         virtual bool contains(const LinAl::Vector<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 BoundingBox<T, D> Negation<T, D>::get_axis_aligned_bounding_box() const
46 {
47         return ~shape->get_axis_aligned_bounding_box();
48 }
49
50 template<typename T, unsigned D>
51 inline bool Negation<T, D>::contains(const LinAl::Vector<T, D> &point) const
52 {
53         return !shape->contains(point);
54 }
55
56 template<typename T, unsigned D>
57 inline unsigned Negation<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
58 {
59         unsigned count = shape->get_intersections(ray, points, size);
60         for(unsigned i=0; i<count; ++i)
61                 points[i].normal = -points[i].normal;
62         return count;
63 }
64
65 } // namespace Geometry
66 } // namespace Msp
67
68 #endif