]> git.tdb.fi Git - libs/math.git/blob - source/geometry/ray.h
Always explicitly construct values of type T
[libs/math.git] / source / geometry / ray.h
1 #ifndef MSP_GEOMETRY_RAY_H_
2 #define MSP_GEOMETRY_RAY_H_
3
4 #include <stdexcept>
5 #include <msp/linal/vector.h>
6
7 namespace Msp {
8 namespace Geometry {
9
10 template<typename T, unsigned D>
11 class Ray
12 {
13 private:
14         LinAl::Vector<T, D> start;
15         LinAl::Vector<T, D> direction;
16         T limit;
17
18 public:
19         Ray();
20         Ray(const LinAl::Vector<T, D> &, const LinAl::Vector<T, D> &);
21         Ray(const LinAl::Vector<T, D> &, const LinAl::Vector<T, D> &, T);
22
23         const LinAl::Vector<T, D> &get_start() const { return start; }
24         const LinAl::Vector<T, D> &get_direction() const { return direction; }
25         T get_limit() const { return limit; }
26         bool check_limits(T) const;
27 };
28
29 template<typename T, unsigned D>
30 inline Ray<T, D>::Ray():
31         limit(0)
32 {
33         direction[0] = T(1);
34 }
35
36 template<typename T, unsigned D>
37 inline Ray<T, D>::Ray(const LinAl::Vector<T, D> &s, const LinAl::Vector<T, D> &d):
38         start(s),
39         direction(normalize(d)),
40         limit(0)
41 { }
42
43 template<typename T, unsigned D>
44 inline Ray<T, D>::Ray(const LinAl::Vector<T, D> &s, const LinAl::Vector<T, D> &d, T l):
45         start(s),
46         direction(normalize(d)),
47         limit(l)
48 {
49         if(l<T(0))
50                 throw std::invalid_argument("Ray::Ray");
51 }
52
53 template<typename T, unsigned D>
54 inline bool Ray<T, D>::check_limits(T x) const
55 {
56         return x>=T(0) && (!limit || x<=limit);
57 }
58
59 } // namespace Geometry
60 } // namespace Msp
61
62 #endif