]> git.tdb.fi Git - libs/math.git/blob - source/geometry/ray.h
Rename AffineTransformation to AffineTransform
[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 /**
11 A directed line segment.  Can be used to point at things.
12 */
13 template<typename T, unsigned D>
14 class Ray
15 {
16 private:
17         LinAl::Vector<T, D> start;
18         LinAl::Vector<T, D> direction;
19         T limit;
20
21 public:
22         Ray();
23         Ray(const LinAl::Vector<T, D> &, const LinAl::Vector<T, D> &);
24         Ray(const LinAl::Vector<T, D> &, const LinAl::Vector<T, D> &, T);
25
26         const LinAl::Vector<T, D> &get_start() const { return start; }
27         const LinAl::Vector<T, D> &get_direction() const { return direction; }
28         T get_limit() const { return limit; }
29         bool check_limits(T) const;
30 };
31
32 template<typename T, unsigned D>
33 inline Ray<T, D>::Ray():
34         limit(0)
35 {
36         direction[0] = T(1);
37 }
38
39 template<typename T, unsigned D>
40 inline Ray<T, D>::Ray(const LinAl::Vector<T, D> &s, const LinAl::Vector<T, D> &d):
41         start(s),
42         direction(normalize(d)),
43         limit(0)
44 { }
45
46 template<typename T, unsigned D>
47 inline Ray<T, D>::Ray(const LinAl::Vector<T, D> &s, const LinAl::Vector<T, D> &d, T l):
48         start(s),
49         direction(normalize(d)),
50         limit(l)
51 {
52         if(l<T(0))
53                 throw std::invalid_argument("Ray::Ray");
54 }
55
56 template<typename T, unsigned D>
57 inline bool Ray<T, D>::check_limits(T x) const
58 {
59         return x>=T(0) && (!limit || x<=limit);
60 }
61
62 } // namespace Geometry
63 } // namespace Msp
64
65 #endif