]> git.tdb.fi Git - libs/math.git/blob - source/geometry/hypersphere.h
Fix AffineTransformation::translate
[libs/math.git] / source / geometry / hypersphere.h
1 #ifndef MSP_GEOMETRY_HYPERSPHERE_H_
2 #define MSP_GEOMETRY_HYPERSPHERE_H_
3
4 #include <msp/linal/vector.h>
5 #include "hyperbox.h"
6 #include "ray.h"
7 #include "shape.h"
8
9 namespace Msp {
10 namespace Geometry {
11
12 template<typename T, unsigned D>
13 class HyperSphere: public Shape<T, D>
14 {
15 private:
16         T radius;
17
18 public:
19         HyperSphere();
20         explicit HyperSphere(T);
21
22         virtual HyperSphere *clone() const;
23
24         T get_radius() const { return radius; }
25
26         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
27         virtual bool check_intersection(const Ray<T, D> &) const;
28 };
29
30 template<typename T, unsigned D>
31 inline HyperSphere<T, D>::HyperSphere():
32         radius(1)
33 { }
34
35 template<typename T, unsigned D>
36 inline HyperSphere<T, D>::HyperSphere(T r):
37         radius(r)
38 { }
39
40 template<typename T, unsigned D>
41 inline HyperSphere<T, D> *HyperSphere<T, D>::clone() const
42 {
43         return new HyperSphere<T, D>(radius);
44 }
45
46 template<typename T, unsigned D>
47 inline HyperBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
48 {
49         LinAl::Vector<T, D> dimensions;
50         for(unsigned i=0; i<D; ++i)
51                 dimensions[i] = radius;
52         return HyperBox<T, D>(dimensions);
53 }
54
55 template<typename T, unsigned D>
56 inline bool HyperSphere<T, D>::check_intersection(const Ray<T, D> &ray) const
57 {
58         T x = inner_product(ray.get_direction(), ray.get_start());
59         if(x>0)
60                 return inner_product(ray.get_start(), ray.get_start())<=radius*radius;
61         else
62         {
63                 LinAl::Vector<T, D> nearest = ray.get_start()-ray.get_direction()*x;
64                 return inner_product(nearest, nearest)<=radius*radius;
65         }
66 }
67
68 } // namespace Geometry
69 } // namespace Msp
70
71 #endif