]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGTriangle.hxx
Merge branch 'timoore/effects-anim-rebase' into next
[simgear.git] / simgear / math / SGTriangle.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 SGTriangle_H
19 #define SGTrianlge_H
20
21 template<typename T>
22 class SGTriangle {
23 public:
24   SGTriangle()
25   { }
26   SGTriangle(const SGVec3<T>& v0, const SGVec3<T>& v1, const SGVec3<T>& v2)
27   { set(v0, v1, v2); }
28   SGTriangle(const SGVec3<T> v[3])
29   { set(v); }
30
31   void set(const SGVec3<T>& v0, const SGVec3<T>& v1, const SGVec3<T>& v2)
32   {
33     _v0 = v0;
34     _d[0] = v1 - v0;
35     _d[1] = v2 - v0;
36   }
37   void set(const SGVec3<T> v[3])
38   {
39     _v0 = v[0];
40     _d[0] = v[1] - v[0];
41     _d[1] = v[2] - v[0];
42   }
43
44   SGVec3<T> getCenter() const
45   { return _v0 + T(1)/T(3)*(_d[0] + _d[1]); }
46
47   // note that the index is unchecked
48   SGVec3<T> getVertex(unsigned i) const
49   {
50     if (0 < i)
51       return _v0 + _d[i-1];
52     return _v0;
53   }
54   /// return the normalized surface normal
55   SGVec3<T> getNormal() const
56   { return normalize(cross(_d[0], _d[1])); }
57
58   const SGVec3<T>& getBaseVertex() const
59   { return _v0; }
60   void setBaseVertex(const SGVec3<T>& v)
61   { _v0 = v; }
62   const SGVec3<T>& getEdge(unsigned i) const
63   { return _d[i]; }
64   void setEdge(unsigned i, const SGVec3<T>& d)
65   { _d[i] = d; }
66
67   // flip the positive side
68   void flip()
69   {
70     SGVec3<T> tmp = _d[0];
71     _d[0] = _d[1];
72     _d[1] = tmp;
73   }
74
75   SGTriangle<T> transform(const SGMatrix<T>& matrix) const
76   {
77     SGTriangle<T> triangle;
78     triangle._v0 = matrix.xformPt(_v0);
79     triangle._d[0] = matrix.xformVec(_d[0]);
80     triangle._d[1] = matrix.xformVec(_d[1]);
81     return triangle;
82   }
83
84 private:
85   /// Store one vertex directly, _d is the offset of the other two
86   /// vertices wrt the base vertex
87   /// For fast intersection tests this format prooves usefull. For that same
88   /// purpose also cache the cross product of the _d[i].
89   SGVec3<T> _v0;
90   SGVec3<T> _d[2];
91 };
92
93 /// Output to an ostream
94 template<typename char_type, typename traits_type, typename T>
95 inline
96 std::basic_ostream<char_type, traits_type>&
97 operator<<(std::basic_ostream<char_type, traits_type>& s,
98            const SGTriangle<T>& triangle)
99 {
100   return s << "triangle: v0 = " << triangle.getVertex(0)
101            << ", v1 = " << triangle.getVertex(1)
102            << ", v2 = " << triangle.getVertex(2);
103 }
104
105 #endif