1 // Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
26 SGTriangle(const SGVec3<T>& v0, const SGVec3<T>& v1, const SGVec3<T>& v2)
28 SGTriangle(const SGVec3<T> v[3])
31 void set(const SGVec3<T>& v0, const SGVec3<T>& v1, const SGVec3<T>& v2)
37 void set(const SGVec3<T> v[3])
44 SGVec3d getCenter() const
48 box.expandBy(_v0 + _d[0]);
49 box.expandBy(_v0 + _d[1]);
50 return box.getCenter();
53 // note that the index is unchecked
54 SGVec3<T> getVertex(unsigned i) const
60 /// return the normalized surface normal
61 SGVec3<T> getNormal() const
62 { return normalize(cross(_d[0], _d[1])); }
64 const SGVec3<T>& getBaseVertex() const
66 void setBaseVertex(const SGVec3<T>& v)
68 const SGVec3<T>& getEdge(unsigned i) const
70 void setEdge(unsigned i, const SGVec3<T>& d)
73 // flip the positive side
76 SGVec3<T> tmp = _d[0];
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].
89 /// Output to an ostream
90 template<typename char_type, typename traits_type, typename T>
92 std::basic_ostream<char_type, traits_type>&
93 operator<<(std::basic_ostream<char_type, traits_type>& s,
94 const SGTriangle<T>& triangle)
96 return s << "triangle: v0 = " << triangle.getVertex(0)
97 << ", v1 = " << triangle.getVertex(1)
98 << ", v2 = " << triangle.getVertex(2);