Library of Assembled Shared Sources
triangle_3d.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_SIMPLE_TRIANGLE_3D_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_SIMPLE_TRIANGLE_3D_INL
47
48#include "prim_common.h"
49#include "triangle_3d.h"
50
51namespace lass
52{
53namespace prim
54{
55
56/** constructs an empty triangle.
57 * all vertices are (0, 0) and thus equal.
58 */
59template <typename T>
63
64
65
66/** Constructs a triangle through three points in positive sequence.
67 */
68template <typename T>
69Triangle3D<T>::Triangle3D(const TPoint& iA, const TPoint& iB, const TPoint& iC)
70{
71 vertices_[0] = iA;
72 vertices_[1] = iB;
73 vertices_[2] = iC;
74}
75
76
77
78/** @copydoc SimplePolygon3D::operator[]
79 */
80template <typename T>
81const typename Triangle3D<T>::TPoint&
82Triangle3D<T>::operator[](size_t iIndexOfVertex) const
83{
84 LASS_ASSERT(isInRange(iIndexOfVertex));
85 return vertices_[iIndexOfVertex];
86}
87
90/** @copydoc SimplePolygon3D::operator[]
91 */
92template <typename T>
93typename Triangle3D<T>::TPoint&
94Triangle3D<T>::operator[](size_t iIndexOfVertex)
96 LASS_ASSERT(isInRange(iIndexOfVertex));
97 return vertices_[iIndexOfVertex];
98}
101
102/** @copydoc SimplePolygon3D::at
103 */
104template <typename T>
105const typename Triangle3D<T>::TPoint&
106Triangle3D<T>::at(int iIndexOfVertex) const
107{
108 const int i = num::mod(iIndexOfVertex, static_cast<unsigned>(size_));
109 LASS_ASSERT(isInRange(i));
110 return vertices_[i];
112
113
114
115/** @copydoc SimplePolygon3D::at
116 */
117template <typename T>
118typename Triangle3D<T>::TPoint&
119Triangle3D<T>::at(int iIndexOfVertex)
120{
121 const int i = num::mod(iIndexOfVertex, static_cast<unsigned>(size_));
122 LASS_ASSERT(isInRange(i));
123 return vertices_[i];
124}
125
126
127
128/** @copydoc SimplePolygon3D::edge
129 */
130template <typename T>
131const typename Triangle3D<T>::TLineSegment
132Triangle3D<T>::edge(int iIndexOfTailVertex) const
133{
134 return TLineSegment(at(iIndexOfTailVertex), at(iIndexOfTailVertex + 1));
135}
136
137
138
139/** @copydoc SimplePolygon3D::vector
140 */
141template <typename T>
142const typename Triangle3D<T>::TVector
143Triangle3D<T>::vector(int iIndexOfTailVertex) const
144{
145 return at(iIndexOfTailVertex + 1) - at(iIndexOfTailVertex);
146}
147
148
149
150/** @copydoc SimplePolygon3D::plane
151 */
152template <typename T>
153const typename Triangle3D<T>::TPlane
155{
156 return TPlane(vertices_[0], vertices_[1], vertices_[2]);
157}
158
159
160
161/** @copydoc SimplePolygon3D::isEmpty
162 * @par Triangle specific:
163 * if all vertices are equal, we assume the triangle is empty
164 */
165template <typename T>
167{
168 return vertices_[0] == vertices_[1] && vertices_[0] == vertices_[2];
169}
170
171
172
173/** @copydoc SimplePolygon3D::size
174 */
175template <typename T>
177{
178 return size_;
179}
180
181
182
183/** returns squared area of triangle to avoid the square root.
184 * @return num::sqr(area()) but faster :)
185 */
186template <typename T>
187const typename Triangle3D<T>::TValue
189{
190 LASS_ASSERT(size_ == 3);
191 return cross(vertices_[1] - vertices_[0], vertices_[2] - vertices_[0]).squaredNorm() / 4;
192}
193
194
195
196/** @copydoc SimplePolygon3D::area
197 */
198template <typename T>
199const typename Triangle3D<T>::TValue
201{
202 return num::sqrt(squaredArea());
203}
204
205
206
207/** @copydoc SimplePolygon3D::perimeter
208 */
209template <typename T>
210const typename Triangle3D<T>::TValue
212{
213 return distance(vertices_[0], vertices_[1]) +
214 distance(vertices_[1], vertices_[2]) +
215 distance(vertices_[2], vertices_[0]);
216}
217
218
219
220/** @copydoc SimplePolygon3D::vertexCentroid
221 */
222template <typename T>
223const typename Triangle3D<T>::TPointH
225{
226 TPointH result = vertices_[0] + vertices_[1] + vertices_[2];
227 return result;
228}
229
230
231
232/** @copydoc SimplePolygon3D::surfaceCentroid
233 */
234template <typename T> inline
235const typename Triangle3D<T>::TPointH
237{
238 return vertexCentroid();
239}
240
241
242
243/** @copydoc SimplePolygon3D::isSimple
244 * @par Triangle specific:
245 * A triangle is always simple
246 */
247template <typename T>
249{
250 return true;
251}
252
253
254
255/** @copydoc SimplePolygon3D::isConvex
256 * @par Triangle specific:
257 * A triangle is always convex
258 */
259template <typename T>
261{
262 return true;
263}
264
265
266
267/** @copydoc SimplePolygon3D::isReflex
268 * @par Triangle specific:
269 * A triangle never has reflex vertices
270 */
271template <typename T>
272bool Triangle3D<T>::isReflex(int /*iIndexOfVertex*/) const
273{
274 return false;
275}
276
277
278
279// --- private -------------------------------------------------------------------------------------
280
281/** return if index of vertex is in range of the std::vector
282 */
283template <typename T>
284bool Triangle3D<T>::isInRange(size_t iIndexOfVertex) const
285{
286 return iIndexOfVertex < size_;
287}
288
289
290
291// --- free ----------------------------------------------------------------------------------------
292
293/** @relates lass::prim::Triangle3D
294 */
295template <typename T>
296io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Triangle3D<T>& iTriangle)
297{
298 LASS_ENFORCE_STREAM(ioOStream) << "<Triangle3D>\n";
299 for (unsigned i = 0; i < 3; ++i)
300 {
301 ioOStream << "<vertex id='" << i << "'>" << iTriangle[i] << "</vertex>\n";
302 }
303 ioOStream << "</Triangle3D>\n";
304 return ioOStream;
305}
306
307
308
309/** @relates lass::prim::Triangle3D
310 */
311template <typename T>
312std::ostream& operator<<(std::ostream& ioOStream, const Triangle3D<T>& iTriangle)
313{
314 LASS_ENFORCE_STREAM(ioOStream)
315 << "{" << iTriangle[0] << ", " << iTriangle[1] << ", " << iTriangle[2] << "}";
316 return ioOStream;
317}
318
319
320
321}
322
323}
324
325#endif
326
327// EOF
Output stream for writing a selection of geometric primitives to XML files.
A very simple 3D polygon :)
Definition triangle_3d.h:64
const TValue squaredArea() const
returns squared area of triangle to avoid the square root.
const TPoint & operator[](size_t iIndexOfVertex) const
return vertex of polygon by its index, not wrapped, no bounds check.
bool isEmpty() const
return true if polygon has no vertices
size_t size() const
return number of vertices
const TValue area() const
return area of the polygons surface.
bool isSimple() const
return true if polygon is simple, false if not.
const TPoint & at(int iIndexOfVertex) const
return vertex of polygon by its index, but wrap around the bounds.
const TLineSegment edge(int iIndexOfTailVertex) const
return the edge of the polygon between vertices at(iIndex) and at(iIndex + 1).
bool isReflex(int iIndexOfVertex) const
return true if inner angle of vertex is reflex (is > 180 degrees).
const TPointH vertexCentroid() const
return the barycenter of all vertices.
const TPlane plane() const
return support plane of polygon.
bool isConvex() const
return true if polygon is convex, false if not.
const TVector vector(int iIndexOfTailVertex) const
return the vector between vertices at(iIndex) and at(iIndex + 1)\
const TPointH surfaceCentroid() const
return the centroid of the filled polygon.
const TValue perimeter() const
return sum of the lengths of all edges
Triangle3D()
constructs an empty triangle.
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53