]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGTriangle.hxx
easyxml.cxx: add missing endXML visitor call
[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   SGVec3d getCenter() const
45   {
46     SGBoxd box;
47     box.expandBy(_v0);
48     box.expandBy(_v0 + _d[0]);
49     box.expandBy(_v0 + _d[1]);
50     return box.getCenter();
51   }
52
53   // note that the index is unchecked
54   SGVec3<T> getVertex(unsigned i) const
55   {
56     if (0 < i)
57       return _v0 + _d[i-1];
58     return _v0;
59   }
60   /// return the normalized surface normal
61   SGVec3<T> getNormal() const
62   { return normalize(cross(_d[0], _d[1])); }
63
64   const SGVec3<T>& getBaseVertex() const
65   { return _v0; }
66   void setBaseVertex(const SGVec3<T>& v)
67   { _v0 = v; }
68   const SGVec3<T>& getEdge(unsigned i) const
69   { return _d[i]; }
70   void setEdge(unsigned i, const SGVec3<T>& d)
71   { _d[i] = d; }
72
73   // flip the positive side
74   void flip()
75   {
76     SGVec3<T> tmp = _d[0];
77     _d[0] = _d[1];
78     _d[1] = tmp;
79   }
80 private:
81   /// Store one vertex directly, _d is the offset of the other two
82   /// vertices wrt the base vertex
83   /// For fast intersection tests this format prooves usefull. For that same
84   /// purpose also cache the cross product of the _d[i].
85   SGVec3<T> _v0;
86   SGVec3<T> _d[2];
87 };
88
89 /// Output to an ostream
90 template<typename char_type, typename traits_type, typename T>
91 inline
92 std::basic_ostream<char_type, traits_type>&
93 operator<<(std::basic_ostream<char_type, traits_type>& s,
94            const SGTriangle<T>& triangle)
95 {
96   return s << "triangle: v0 = " << triangle.getVertex(0)
97            << ", v1 = " << triangle.getVertex(1)
98            << ", v2 = " << triangle.getVertex(2);
99 }
100
101 #endif