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 SGVec3<T> getCenter() const
45 { return _v0 + T(1)/T(3)*(_d[0] + _d[1]); }
47 // note that the index is unchecked
48 SGVec3<T> getVertex(unsigned i) const
54 /// return the normalized surface normal
55 SGVec3<T> getNormal() const
56 { return normalize(cross(_d[0], _d[1])); }
58 const SGVec3<T>& getBaseVertex() const
60 void setBaseVertex(const SGVec3<T>& v)
62 const SGVec3<T>& getEdge(unsigned i) const
64 void setEdge(unsigned i, const SGVec3<T>& d)
67 // flip the positive side
70 SGVec3<T> tmp = _d[0];
75 SGTriangle<T> transform(const SGMatrix<T>& matrix) const
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]);
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].
93 /// Output to an ostream
94 template<typename char_type, typename traits_type, typename T>
96 std::basic_ostream<char_type, traits_type>&
97 operator<<(std::basic_ostream<char_type, traits_type>& s,
98 const SGTriangle<T>& triangle)
100 return s << "triangle: v0 = " << triangle.getVertex(0)
101 << ", v1 = " << triangle.getVertex(1)
102 << ", v2 = " << triangle.getVertex(2);