TAPs 0.7.7.3
TAPsTetrahedron.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsTetrahedron.cpp
00003 
00004 Tetrahedron class is a class for a tetrahedron.
00005 
00006 SUKITTI PUNAK   (09/13/2005)
00007 UPDATE          (09/25/2005)
00008 ******************************************************************************/
00009 #include "TAPsTetrahedron.hpp"
00010 // Using Inclusion Model (i.e. definitions are included in declarations)
00011 //                       (this name.cpp is included in name.hpp)
00012 // Each friend is defined directly inside its declaration.
00013 
00014 BEGIN_NAMESPACE_TAPs
00015 //=============================================================================
00016 // Static Variables
00017 //-----------------------------------------------------------------------------
00018 //*
00019 template <typename T>
00020 bool    Tetrahedron<T>::m_gbHasBeenSet = false;
00021 template <typename T>
00022 std::vector< std::vector<int> > Tetrahedron<T>::m_vVertexValence;
00023 template <typename T>
00024 std::vector< std::vector<int> > Tetrahedron<T>::m_vVertexFace;
00025 template <typename T>
00026 std::vector< std::vector<int> > Tetrahedron<T>::m_vFace(4);
00027 template <typename T>
00028 std::vector< std::vector<int> >::const_iterator Tetrahedron<T>::m_itor;
00029 //*/
00030 //=============================================================================
00031 // Constructors and Destructor
00032 //-----------------------------------------------------------------------------
00033 template <typename T>
00034 Tetrahedron<T>::Tetrahedron ()
00035     : AbstractPolyhedron<T>( 4, 4 )
00036 {
00037     //----------------------------------------------------------------
00038     m_vVertex[0] = Vector3<T>( 0, 0, 0 );   // origin
00039     m_vVertex[1] = Vector3<T>( 1, 0, 0 );   // x
00040     m_vVertex[2] = Vector3<T>( 0, 1, 0 );   // y
00041     m_vVertex[3] = Vector3<T>( 0, 0, 1 );   // z
00042     Setup();
00043 }
00044 //-----------------------------------------------------------------------------
00045 template <typename T>
00046 Tetrahedron<T>::Tetrahedron ( Vector3<T> const p[4] )
00047     : AbstractPolyhedron<T>( 4, 4 )
00048 {
00049     m_vVertex[0] = p[0];    // origin
00050     m_vVertex[1] = p[1];    // x
00051     m_vVertex[2] = p[2];    // y
00052     m_vVertex[3] = p[3];    // z
00053     Setup();
00054 }
00055 //-----------------------------------------------------------------------------
00056 template <typename T>
00057 Tetrahedron<T>::~Tetrahedron ()
00058 {}
00059 //-----------------------------------------------------------------------------
00060 //=============================================================================
00061 // Helper Function(s)
00062 //-----------------------------------------------------------------------------
00063 // SetupStaticMembers Fn
00064 template <typename T>
00065 void Tetrahedron<T>::SetupStaticMembers ()
00066 {
00067     //----------------------------------------------------------------
00068     m_vVertexValence.resize(4);
00069     m_vVertexFace.resize(4);
00070     m_vFace.resize(4);
00071     //----------------------------------------------------------------
00072     // Initialize Vertex Valence
00073     //----------------------------------------------------------------
00074     // Vertex#0: 1 2 3 ----------------
00075     m_vVertexValence[0].push_back( 1 );
00076     m_vVertexValence[0].push_back( 2 );
00077     m_vVertexValence[0].push_back( 3 );
00078     // Vertex#1: 0 2 3 ----------------
00079     m_vVertexValence[1].push_back( 0 );
00080     m_vVertexValence[1].push_back( 2 );
00081     m_vVertexValence[1].push_back( 3 );
00082     // Vertex#2: 0 1 3 ----------------
00083     m_vVertexValence[2].push_back( 0 );
00084     m_vVertexValence[2].push_back( 1 );
00085     m_vVertexValence[2].push_back( 3 );
00086     // Vertex#3: 0 1 2 ----------------
00087     m_vVertexValence[3].push_back( 0 );
00088     m_vVertexValence[3].push_back( 1 );
00089     m_vVertexValence[3].push_back( 2 );
00090     //----------------------------------------------------------------
00091     // Initialize Face List
00092     //----------------------------------------------------------------
00093     // Face#0: 0 2 1 ---------
00094     m_vFace[0].push_back( 0 );
00095     m_vFace[0].push_back( 2 );
00096     m_vFace[0].push_back( 1 );
00097     // Face#1: 0 3 2 ---------
00098     m_vFace[1].push_back( 0 );
00099     m_vFace[1].push_back( 3 );
00100     m_vFace[1].push_back( 2 );
00101     // Face#2: 0 1 3 ---------
00102     m_vFace[2].push_back( 0 );
00103     m_vFace[2].push_back( 1 );
00104     m_vFace[2].push_back( 3 );
00105     // Face#3: 1 2 3 ---------
00106     m_vFace[3].push_back( 1 );
00107     m_vFace[3].push_back( 2 );
00108     m_vFace[3].push_back( 3 );
00109     //----------------------------------------------------------------
00110     // Initialize Vertex Face List (face that has this vertex)
00111     //----------------------------------------------------------------
00112     // Vertex#0: 0 1 2 ---------
00113     m_vVertexFace[0].push_back( 0 );
00114     m_vVertexFace[0].push_back( 1 );
00115     m_vVertexFace[0].push_back( 2 );
00116     // Vertex#1: 0 2 3 ---------
00117     m_vVertexFace[1].push_back( 0 );
00118     m_vVertexFace[1].push_back( 2 );
00119     m_vVertexFace[1].push_back( 3 );
00120     // Vertex#2: 0 1 3 ---------
00121     m_vVertexFace[2].push_back( 0 );
00122     m_vVertexFace[2].push_back( 1 );
00123     m_vVertexFace[2].push_back( 3 );
00124     // Vertex#3: 1 2 3 ---------
00125     m_vVertexFace[3].push_back( 1 );
00126     m_vVertexFace[3].push_back( 2 );
00127     m_vVertexFace[3].push_back( 3 );
00128 }
00129 //-----------------------------------------------------------------------------
00130 // Setup Fn
00131 template <typename T>
00132 void Tetrahedron<T>::Setup ()
00133 {
00134     // REMARK:
00135     // =======
00136     // In theory, we should be able to set m_gbHasBeenSet to true.
00137     // However, calling DrawByOpenGL fn will have static member size equals zero.
00138     // Therefore, we will set it to true in DrawByOpenGL fn.
00139     // See DrawByOpenGL source code below.
00140     // I believe this must be an effect from DrawByOpenGL is passed as an 
00141     // argument to a function.
00142     if ( !m_gbHasBeenSet ) {
00143         std::cout << "Call Tetrahedron<T>::SetupStaticMembers()\n";
00144         //m_gbHasBeenSet = true;
00145         SetupStaticMembers();
00146     }
00147     //----------------------------------------------------------------
00148     CalNormals();
00149 }
00150 //-----------------------------------------------------------------------------
00151 // CalNormals Fn
00152 template <typename T>
00153 void Tetrahedron<T>::CalNormals ()
00154 {
00155     // Assume all faces are planer which works perfectly for triangular faces
00156     //----------------------------------------------------------------
00157     // Calculate Face Normals (Unnormalized)
00158     int p = 0;
00159     for ( m_itor = m_vFace.begin(); m_itor != m_vFace.end(); ++m_itor, ++p ) {
00160         m_vFaceNormal[p] = ( m_vVertex[(*m_itor)[1]] - m_vVertex[(*m_itor)[0]] )
00161                          ^ ( m_vVertex[(*m_itor)[2]] - m_vVertex[(*m_itor)[0]] );
00162     }
00163     //----------------------------------------------------------------
00164     // Calculate Vertex Normals
00165     for ( p = 0; p < 4; ++p ) {
00166         m_vVertexNormal[p].SetXYZ( 0, 0, 0);
00167         for ( int i = 0; i < static_cast<int>( m_vVertexFace[p].size() ); ++i ) {
00168             m_vVertexNormal[p] += m_vFaceNormal[ m_vVertexFace[p][i] ];
00169         }
00170         m_vVertexNormal[p].Normalized();
00171         std::cout << m_vVertexNormal[p] << std::endl;
00172     }
00173     //----------------------------------------------------------------
00174     // Normalize Face Normals
00175     for ( p = 0; p < static_cast<int>(m_vFaceNormal.size()); ++p ) {
00176         m_vFaceNormal[p].Normalized();
00177         //std::cout << m_vFaceNormal[p] << std::endl;
00178         //m_vVertexNormal[p] = m_vFaceNormal[p];
00179     }
00180 }
00181 //-----------------------------------------------------------------------------
00182 #if defined(__gl_h_) || defined(__GL_H__)
00183 //=============================================================================
00184 // DrawByOpenGL
00185 //-----------------------------------------------------------------------------
00186 template <typename T>
00187 void Tetrahedron<T>::DrawByOpenGL ()
00188 {
00189     if ( !m_gbHasBeenSet ) {
00190         std::cout << "Tetrahedron<T>::DrawByOpenGL() ==> ";
00191         std::cout << "Call Tetrahedron<T>::SetupStaticMembers()\n";
00192         m_gbHasBeenSet = true;
00193         SetupStaticMembers();
00194     }
00195     //----------------------------------------------------------------
00196     int p;
00197     //int count = 0;
00198     //----------------------------------------------------------------
00199     glPushMatrix();
00200     for ( m_itor = m_vFace.begin(); m_itor != m_vFace.end(); ++m_itor ) {
00201         //std::cout << "\n(*m_itor).size() = " << (*m_itor).size();
00202         //std::cout << "\nFace# " << ++count;
00203         glBegin( GL_POLYGON );
00204         for ( int i = 0; i < static_cast<int>( (*m_itor).size() ); ++i ) {
00205             p = (*m_itor)[i];
00206             glNormal3f( m_vVertexNormal[p][0], m_vVertexNormal[p][1], m_vVertexNormal[p][2] );
00207             glVertex3f( m_vVertex[p][0], m_vVertex[p][1], m_vVertex[p][2] );
00208         }
00209         glEnd();
00210 
00211         // Draw boundary of face
00212         glColor3f( 0, 1, 1 );
00213         glDisable( GL_LIGHTING );
00214         glBegin( GL_LINE_LOOP );
00215         for ( int i = 0; i < static_cast<int>( (*m_itor).size() ); ++i ) {
00216             p = (*m_itor)[i];
00217             glNormal3f( m_vVertexNormal[p][0], m_vVertexNormal[p][1], m_vVertexNormal[p][2] );
00218             glVertex3f( m_vVertex[p][0], m_vVertex[p][1], m_vVertex[p][2] );
00219         }
00220         glEnd();
00221         glEnable( GL_LIGHTING );
00222 
00223     }
00224     glPopMatrix();
00225     //*/
00226 
00227     /*
00228     glPushMatrix();
00229         glBegin( GL_TRIANGLE_FAN ); // Face#: 0, 1, and 2
00230             glNormal3f( m_vVertexNormal[0][0], m_vVertexNormal[0][1], m_vVertexNormal[0][2] );
00231             glVertex3f( m_vVertex[0][0], m_vVertex[0][1], m_vVertex[0][2] );
00232             glNormal3f( m_vVertexNormal[2][0], m_vVertexNormal[2][1], m_vVertexNormal[2][2] );
00233             glVertex3f( m_vVertex[2][0], m_vVertex[2][1], m_vVertex[2][2] );
00234             glNormal3f( m_vVertexNormal[1][0], m_vVertexNormal[1][1], m_vVertexNormal[1][2] );
00235             glVertex3f( m_vVertex[1][0], m_vVertex[1][1], m_vVertex[1][2] );
00236             glNormal3f( m_vVertexNormal[3][0], m_vVertexNormal[3][1], m_vVertexNormal[3][2] );
00237             glVertex3f( m_vVertex[3][0], m_vVertex[3][1], m_vVertex[3][2] );
00238             glNormal3f( m_vVertexNormal[2][0], m_vVertexNormal[2][1], m_vVertexNormal[2][2] );
00239             glVertex3f( m_vVertex[2][0], m_vVertex[2][1], m_vVertex[2][2] );
00240         glEnd();
00241         glBegin( GL_TRIANGLES );    // Face#: 3
00242             glNormal3f( m_vVertexNormal[1][0], m_vVertexNormal[1][1], m_vVertexNormal[1][2] );
00243             glVertex3f( m_vVertex[1][0], m_vVertex[1][1], m_vVertex[1][2] );
00244             glNormal3f( m_vVertexNormal[2][0], m_vVertexNormal[2][1], m_vVertexNormal[2][2] );
00245             glVertex3f( m_vVertex[2][0], m_vVertex[2][1], m_vVertex[2][2] );
00246             glNormal3f( m_vVertexNormal[3][0], m_vVertexNormal[3][1], m_vVertexNormal[3][2] );
00247             glVertex3f( m_vVertex[3][0], m_vVertex[3][1], m_vVertex[3][2] );
00248         glEnd();
00249     glPopMatrix();
00250     //*/
00251     //----------------------------------------------------------------
00252     DrawVertexNormals();
00253     DrawFaceNormals();
00254 }
00255 //-----------------------------------------------------------------------------
00256 #endif
00257 //=============================================================================
00258 END_NAMESPACE_TAPs
00259 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00260 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines