]> git.tdb.fi Git - libs/math.git/blob - source/geometry/negation.h
Make the check_intersection function non-virtual
[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 unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
29         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
30 };
31
32 template<typename T, unsigned D>
33 inline Negation<T, D>::Negation(const Shape<T, D> &s):
34         shape(s.clone())
35 { }
36
37 template<typename T, unsigned D>
38 inline Negation<T, D> *Negation<T, D>::clone() const
39 {
40         return new Negation<T, D>(*shape);
41 }
42
43 template<typename T, unsigned D>
44 inline HyperBox<T, D> Negation<T, D>::get_axis_aligned_bounding_box() const
45 {
46         // XXX How do we handle this correctly?  In particular negation of a negation?
47         return HyperBox<T, D>();
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