]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGRay.hxx
eef28cc46beadbae5ae2582fbddf0e7045fd3d1a
[simgear.git] / simgear / math / SGRay.hxx
1 // Copyright (C) 2006  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifndef SGRay_H
19 #define SGRay_H
20
21 template<typename T>
22 class SGRay {
23 public:
24   SGRay()
25   { }
26   SGRay(const SGVec3<T>& origin, const SGVec3<T>& dir) :
27     _origin(origin), _direction(dir)
28   { }
29   template<typename S>
30   explicit SGRay(const SGRay<S>& ray) :
31     _origin(ray.getOrigin()), _direction(ray.getDirection())
32   { }
33
34   void set(const SGVec3<T>& origin, const SGVec3<T>& dir)
35   { _origin = origin; _direction = dir; }
36
37   void setOrigin(const SGVec3<T>& origin)
38   { _origin = origin; }
39   const SGVec3<T>& getOrigin() const
40   { return _origin; }
41
42   void setDirection(const SGVec3<T>& direction)
43   { _direction = direction; }
44   const SGVec3<T>& getDirection() const
45   { return _direction; }
46
47   SGVec3<T> getNormalizedDirection() const
48   { return normalize(getDirection()); }
49
50   SGVec3<T> getClosestPointTo(const SGVec3<T>& point)
51   {
52       SGVec3<T> u(getNormalizedDirection()),
53           v(point - _origin);
54       return (dot(u, v) * u) + _origin; 
55   }
56 private:
57   SGVec3<T> _origin;
58   SGVec3<T> _direction;
59 };
60
61 /// Output to an ostream
62 template<typename char_type, typename traits_type, typename T>
63 inline
64 std::basic_ostream<char_type, traits_type>&
65 operator<<(std::basic_ostream<char_type, traits_type>& s,
66            const SGRay<T>& ray)
67 {
68   return s << "ray: origin = " << ray.getOrigin()
69            << ", direction = " << ray.getDirection();
70 }
71
72 #endif