Main Page   Class Hierarchy   Compound List   File List   Compound Members  

geMesh.cpp

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 2002-2003 David Pritchard <drpritch@alumni.uwaterloo.ca>
00003 // 
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either
00007 // version 2 of the License, or (at your option) any later
00008 // version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 #include <freecloth/geom/geMesh.h>
00020 #include <freecloth/geom/geVector.h>
00021 
00022 ////////////////////////////////////////////////////////////////////////////////
00023 // LOCAL DECLARATIONS
00024 
00025 FREECLOTH_NAMESPACE_START
00026 
00027 ////////////////////////////////////////////////////////////////////////////////
00028 // CLASS GeMesh
00029 
00030 //------------------------------------------------------------------------------
00031 
00032 const GeMesh::VertexType* GeMesh::getVertexArray() const
00033 {
00034     return &_vertices.front();
00035 }
00036 
00037 //------------------------------------------------------------------------------
00038 
00039 const GeMesh::TextureVertexType* GeMesh::getTextureVertexArray() const
00040 {
00041     return &_textureVertices.front();
00042 }
00043 
00044 
00045 ////////////////////////////////////////////////////////////////////////////////
00046 // CLASS GeMesh::FaceWrapper
00047 //
00048 
00049 //------------------------------------------------------------------------------
00050 
00051 Float GeMesh::FaceWrapper::calcArea() const
00052 {
00053     GeVector v1( getVertex( 0 ), getVertex( 1 ) );
00054     GeVector v2( getVertex( 1 ), getVertex( 2 ) );
00055     return v1.cross( v2 ).length() / 2;
00056 }
00057 
00058 //------------------------------------------------------------------------------
00059 
00060 GeVector GeMesh::FaceWrapper::calcNormal() const
00061 {
00062     GeVector v1( getVertex( 0 ), getVertex( 1 ) );
00063     GeVector v2( getVertex( 1 ), getVertex( 2 ) );
00064     return v1.cross( v2 ).getUnit();
00065 }
00066 
00067 //------------------------------------------------------------------------------
00068 
00069 GePoint GeMesh::FaceWrapper::calcBarycentric( const GePoint& p ) const
00070 {
00071     return calcBarycentric( p, getVertex( 0 ), getVertex( 1 ), getVertex( 2 ) );
00072 }
00073 
00074 //------------------------------------------------------------------------------
00075 
00076 GePoint GeMesh::FaceWrapper::calcBarycentric(
00077     const GePoint& p,
00078     const GePoint& v1,
00079     const GePoint& v2,
00080     const GePoint& v3
00081 ) {
00082     Float denom =
00083         ( v2._x - v1._x ) * ( v3._y - v1._y ) -
00084         ( v3._x - v1._x ) * ( v2._y - v1._y );
00085     // If this fails, the triangle is zero-area.
00086     //DGFX_ASSERT( ! BaMath::isEqual( denom, 0 ) );
00087 
00088     return GePoint(
00089         ( ( v2._x - p._x ) * ( v3._y - p._y ) -
00090           ( v3._x - p._x ) * ( v2._y - p._y ) ) / denom,
00091         ( ( v3._x - p._x ) * ( v1._y - p._y ) -
00092           ( v1._x - p._x ) * ( v3._y - p._y ) ) / denom,
00093         ( ( v1._x - p._x ) * ( v2._y - p._y ) -
00094           ( v2._x - p._x ) * ( v1._y - p._y ) ) / denom
00095     );
00096 }
00097 
00098 
00099 ////////////////////////////////////////////////////////////////////////////////
00100 // GLOBAL FUNCTIONS
00101 //
00102 
00103 //------------------------------------------------------------------------------
00104 
00105 std::ostream& operator<<( std::ostream& os, const GeMesh& mesh )
00106 {
00107     os << "GeMesh {" << std::endl;
00108 
00109     GeMesh::VertexConstIterator vi;
00110     UInt32 i;
00111 
00112     os << "vertices = " << std::endl;
00113     for ( vi = mesh.beginVertex(), i = 0; vi != mesh.endVertex(); ++vi, ++i ) {
00114         os << "[" << i << "] " << *vi << std::endl;
00115     }
00116 
00117     GeMesh::TextureVertexConstIterator tvi;
00118     os << "texture vertices = " << std::endl;
00119     for (
00120         tvi = mesh.beginTextureVertex(), i = 0;
00121         tvi != mesh.endTextureVertex();
00122         ++tvi, ++i
00123     ) {
00124         os << "[" << i << "] " << *tvi << std::endl;
00125     }
00126 
00127     GeMesh::FaceConstIterator fi;
00128     os << "faces = " << std::endl;
00129     for ( fi = mesh.beginFace(), i = 0; fi != mesh.endFace(); ++fi, ++i ) {
00130         os << "[" << i << "] " << *fi << std::endl;
00131     }
00132     os << "}";
00133     return os;
00134 }
00135 
00136 //------------------------------------------------------------------------------
00137 
00138 std::ostream& operator<<( std::ostream& os, const GeMesh::FaceWrapper& face )
00139 {
00140     os << "{ vids={ "
00141         << face.getVertexId( 0 ) << ","
00142         << face.getVertexId( 1 ) << ","
00143         << face.getVertexId( 2 )
00144         << " }, tvids={ "
00145         << face.getTextureVertexId( 0 ) << ","
00146         << face.getTextureVertexId( 1 ) << ","
00147         << face.getTextureVertexId( 2 )
00148         << " } }";
00149     return os;
00150 }
00151 
00152 FREECLOTH_NAMESPACE_END

Generated on Wed Apr 23 15:58:46 2003 for Freecloth by doxygen1.3-rc3