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