library of assembled shared sources

http://lass.cocamware.com

triangle_3d.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_SIMPLE_TRIANGLE_3D_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_SIMPLE_TRIANGLE_3D_INL
00047 
00048 #include "prim_common.h"
00049 #include "triangle_3d.h"
00050 
00051 namespace lass
00052 {
00053 namespace prim
00054 {
00055 
00056 /** constructs an empty triangle.
00057  *  all vertices are (0, 0) and thus equal.
00058  */
00059 template <typename T>
00060 Triangle3D<T>::Triangle3D()
00061 {
00062 }
00063 
00064 
00065 
00066 /** Constructs a triangle through three points in positive sequence.
00067  */
00068 template <typename T>
00069 Triangle3D<T>::Triangle3D(const TPoint& iA, const TPoint& iB, const TPoint& iC)
00070 {
00071     vertices_[0] = iA;
00072     vertices_[1] = iB;
00073     vertices_[2] = iC;
00074 }
00075 
00076 
00077 
00078 /** @copydoc SimplePolygon3D::operator[]
00079  */
00080 template <typename T>
00081 const typename Triangle3D<T>::TPoint&
00082 Triangle3D<T>::operator[](int iIndexOfVertex) const
00083 {
00084     LASS_ASSERT(isInRange(iIndexOfVertex));
00085     return vertices_[iIndexOfVertex];
00086 }
00087 
00088 
00089 
00090 /** @copydoc SimplePolygon3D::operator[]
00091  */
00092 template <typename T>
00093 typename Triangle3D<T>::TPoint&
00094 Triangle3D<T>::operator[](int iIndexOfVertex)
00095 {
00096     LASS_ASSERT(isInRange(iIndexOfVertex));
00097     return vertices_[iIndexOfVertex];
00098 }
00099 
00100 
00101 
00102 /** @copydoc SimplePolygon3D::at
00103  */
00104 template <typename T>
00105 const typename Triangle3D<T>::TPoint&
00106 Triangle3D<T>::at(int iIndexOfVertex) const
00107 {
00108     const int i = num::mod(iIndexOfVertex, static_cast<unsigned>(size_));
00109     LASS_ASSERT(isInRange(i));
00110     return vertices_[i];
00111 }
00112 
00113 
00114 
00115 /** @copydoc SimplePolygon3D::at
00116  */
00117 template <typename T>
00118 typename Triangle3D<T>::TPoint&
00119 Triangle3D<T>::at(int iIndexOfVertex)
00120 {
00121     const int i = num::mod(iIndexOfVertex, static_cast<unsigned>(size_));
00122     LASS_ASSERT(isInRange(i));
00123     return vertices_[i];
00124 }
00125 
00126 
00127 
00128 /** @copydoc SimplePolygon3D::edge
00129  */
00130 template <typename T>
00131 const typename Triangle3D<T>::TLineSegment
00132 Triangle3D<T>::edge(int iIndexOfTailVertex) const
00133 {
00134     return TLineSegment(at(iIndexOfTailVertex), at(iIndexOfTailVertex + 1));
00135 }
00136 
00137 
00138 
00139 /** @copydoc SimplePolygon3D::vector
00140  */
00141 template <typename T>
00142 const typename Triangle3D<T>::TVector
00143 Triangle3D<T>::vector(int iIndexOfTailVertex) const
00144 {
00145     return at(iIndexOfTailVertex + 1) - at(iIndexOfTailVertex);
00146 }
00147 
00148 
00149 
00150 /** @copydoc SimplePolygon3D::plane
00151  */
00152 template <typename T>
00153 const typename Triangle3D<T>::TPlane
00154 Triangle3D<T>::plane() const
00155 {
00156     return TPlane(vertices_[0], vertices_[1], vertices_[2]);
00157 }
00158 
00159 
00160 
00161 /** @copydoc SimplePolygon3D::isEmpty
00162  *  @par Triangle specific:
00163  *      if all vertices are equal, we assume the triangle is empty
00164  */
00165 template <typename T>
00166 const bool Triangle3D<T>::isEmpty() const
00167 {
00168     return vertices_[0] == vertices_[1] && vertices_[0] == vertices_[2];
00169 }
00170 
00171 
00172 
00173 /** @copydoc SimplePolygon3D::size
00174  */
00175 template <typename T>
00176 const int Triangle3D<T>::size() const
00177 {
00178     return size_;
00179 }
00180 
00181 
00182 
00183 /** returns squared area of triangle to avoid the square root.
00184  *  @return num::sqr(area()) but faster :)
00185  */
00186 template <typename T>
00187 const typename Triangle3D<T>::TValue
00188 Triangle3D<T>::squaredArea() const
00189 {
00190     LASS_ASSERT(size_ == 3);
00191     return cross(vertices_[1] - vertices_[0], vertices_[2] - vertices_[0]).squaredNorm() / 4;
00192 }
00193 
00194 
00195 
00196 /** @copydoc SimplePolygon3D::area
00197  */
00198 template <typename T>
00199 const typename Triangle3D<T>::TValue
00200 Triangle3D<T>::area() const
00201 {
00202     return num::sqrt(squaredArea());
00203 }
00204 
00205 
00206 
00207 /** @copydoc SimplePolygon3D::perimeter
00208  */
00209 template <typename T>
00210 const typename Triangle3D<T>::TValue
00211 Triangle3D<T>::perimeter() const
00212 {
00213     return distance(vertices_[0], vertices_[1]) +
00214            distance(vertices_[1], vertices_[2]) +
00215            distance(vertices_[2], vertices_[0]);
00216 }
00217 
00218 
00219 
00220 /** @copydoc SimplePolygon3D::vertexCentroid
00221  */
00222 template <typename T>
00223 const typename Triangle3D<T>::TPointH
00224 Triangle3D<T>::vertexCentroid() const
00225 {
00226     TPointH result = vertices_[0] + vertices_[1] + vertices_[2];
00227     return result;
00228 }
00229 
00230 
00231 
00232 /** @copydoc SimplePolygon3D::surfaceCentroid
00233  */
00234 template <typename T> inline
00235 const typename Triangle3D<T>::TPointH
00236 Triangle3D<T>::surfaceCentroid() const
00237 {
00238     return vertexCentroid();
00239 }
00240 
00241 
00242 
00243 /** @copydoc SimplePolygon3D::isSimple
00244  *  @par Triangle specific:
00245  *      A triangle is always simple
00246  */
00247 template <typename T>
00248 const bool Triangle3D<T>::isSimple() const
00249 {
00250     return true;
00251 }
00252 
00253 
00254 
00255 /** @copydoc SimplePolygon3D::isConvex
00256  *  @par Triangle specific:
00257  *      A triangle is always convex
00258  */
00259 template <typename T>
00260 const bool Triangle3D<T>::isConvex() const
00261 {
00262     return true;
00263 }
00264 
00265 
00266 
00267 /** @copydoc SimplePolygon3D::isReflex
00268  *  @par Triangle specific:
00269  *  A triangle never has reflex vertices
00270  */
00271 template <typename T>
00272 const bool Triangle3D<T>::isReflex(int iIndexOfVertex) const
00273 {
00274     return false;
00275 }
00276 
00277 
00278 
00279 // --- private -------------------------------------------------------------------------------------
00280 
00281 /** return if index of vertex is in range of the std::vector
00282  */
00283 template <typename T>
00284 const bool Triangle3D<T>::isInRange(int iIndexOfVertex) const
00285 {
00286     return iIndexOfVertex >= 0 && iIndexOfVertex < size_;
00287 }
00288 
00289 
00290 
00291 // --- free ----------------------------------------------------------------------------------------
00292 
00293 /** @relates lass::prim::Triangle3D
00294  */
00295 template <typename T>
00296 io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Triangle3D<T>& iTriangle)
00297 {
00298     LASS_ENFORCE_STREAM(ioOStream) << "<Triangle3D>\n";
00299     for (unsigned i = 0; i < 3; ++i)
00300     {
00301         ioOStream << "<vertex id='" << i << "'>" << iTriangle[i] << "</vertex>\n";
00302     }
00303     ioOStream << "</Triangle3D>\n";
00304     return ioOStream;
00305 }
00306 
00307 
00308 
00309 /** @relates lass::prim::Triangle3D
00310  */
00311 template <typename T>
00312 std::ostream& operator<<(std::ostream& ioOStream, const Triangle3D<T>& iTriangle)
00313 {
00314     LASS_ENFORCE_STREAM(ioOStream) 
00315         << "{" << iTriangle[0] << ", " << iTriangle[1] << ", " << iTriangle[2] << "}";
00316     return ioOStream;
00317 }
00318 
00319 
00320 
00321 }
00322 
00323 }
00324 
00325 #endif
00326 
00327 // EOF

Generated on Mon Nov 10 14:21:51 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo