![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 PolygonalModel.cpp 00003 00004 A (Generic) Polygonal Model 00005 00006 SUKITTI PUNAK (10/31/2004) 00007 UPDATE (02/12/2006) 00008 ******************************************************************************/ 00009 #include "TAPsPolygonalModel.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__OpenGL 00015 //============================================================================= 00016 //----------------------------------------------------------------------------- 00017 // default constructor 00018 template <typename T> 00019 PolygonalModel<T>::PolygonalModel () 00020 : OpenGLMeshModel<T>(), m_prVertex( NULL ), m_prFace( NULL ) 00021 { 00022 #ifdef TAPs_DEBUG_MODE 00023 std::cout << "PolygonalModel<" << typeid(T).name() << "> Constructor\n"; 00024 #endif//TAPs_DEBUG_MODE 00025 } 00026 //----------------------------------------------------------------------------- 00027 // destructor 00028 template <typename T> 00029 PolygonalModel<T>::~PolygonalModel () 00030 { 00031 if ( m_prVertex != NULL ) { 00032 delete [] m_prVertex; 00033 m_prVertex = NULL; 00034 } 00035 if ( m_prFace != NULL ) { 00036 delete [] m_prFace; 00037 m_prFace = NULL; 00038 } 00039 00040 #ifdef TAPs_DEBUG_MODE 00041 std::cout << "PolygonalModel<" << typeid(T).name() << "> Destructor\n"; 00042 #endif//TAPs_DEBUG_MODE 00043 } 00044 //----------------------------------------------------------------------------- 00045 // Initialize 00046 template <typename T> 00047 void PolygonalModel<T>::Initialize () 00048 { 00049 // Model Initialization 00050 CalAndSetNormals(); 00051 ApplyMaterial(); 00052 CalBoundingAABB(); 00053 CalBoundingEllipsoid(); 00054 CalBoundingSphere(); 00055 } 00056 //----------------------------------------------------------------------------- 00057 // New/Delete Fn 00058 template <typename T> 00059 void PolygonalModel<T>::NewVertexListByNoVertices () 00060 { 00061 if ( m_prVertex != NULL ) { 00062 delete [] m_prVertex; 00063 } 00064 m_prVertex = new Vertex<T>[m_iNoVertices]; 00065 } 00066 //----------------------------------------------------------------------------- 00067 // New/Delete Fn 00068 template <typename T> 00069 void PolygonalModel<T>::NewFaceListByNoFaces () 00070 { 00071 if ( m_prFace != NULL ) { 00072 delete [] m_prFace; 00073 } 00074 m_prFace = new Face<T>[m_iNoFaces]; 00075 } 00076 //----------------------------------------------------------------------------- 00077 // Get Half Length 00078 template <typename T> 00079 T PolygonalModel<T>::GetMaxHalfLength () const 00080 { 00081 T maxHalfSize = 0.0; 00082 for ( int i = 0; i < m_iNoVertices; ++i ) 00083 { 00084 if ( fabs( m_prVertex[i][0] ) > maxHalfSize ) 00085 maxHalfSize = fabs( m_prVertex[i][0] ); 00086 if ( fabs( m_prVertex[i][1] ) > maxHalfSize ) 00087 maxHalfSize = fabs( m_prVertex[i][1] ); 00088 if ( fabs( m_prVertex[i][2] ) > maxHalfSize ) 00089 maxHalfSize = fabs( m_prVertex[i][2] ); 00090 } 00091 return maxHalfSize; 00092 } 00093 //----------------------------------------------------------------------------- 00094 // Calculate and set the normals 00095 template <typename T> 00096 void PolygonalModel<T>::CalAndSetNormals () 00097 { 00098 CalAndSetFaceNormalsNotNormalized(); 00099 NormalizeFaceNormals(); 00100 } 00101 //----------------------------------------------------------------------------- 00102 // Calculate and set the face normals (without normalized) 00103 template <typename T> 00104 inline void PolygonalModel<T>::CalAndSetFaceNormalsNotNormalized () 00105 { 00106 // Calculate and set the normal of each face 00107 for ( int i = 0; i < m_iNoFaces; ++i ) { 00108 m_prFace[i].SetNormal( 00109 ( m_prVertex[ m_prFace[i].GetVertexNo( 1 ) ].GetPosition() 00110 - m_prVertex[ m_prFace[i].GetVertexNo( 0 ) ].GetPosition() ) 00111 ^ ( m_prVertex[ m_prFace[i].GetVertexNo( 2 ) ].GetPosition() 00112 - m_prVertex[ m_prFace[i].GetVertexNo( 1 ) ].GetPosition() ) 00113 ); 00114 } 00115 } 00116 //----------------------------------------------------------------------------- 00117 // Normalize the face normals 00118 template <typename T> 00119 inline void PolygonalModel<T>::NormalizeFaceNormals () 00120 { 00121 for ( int i = 0; i < m_iNoFaces; ++i ) { 00122 const_cast< Vector3<T> * >( &m_prFace[i].GetNormal() )->Normalized(); 00123 //m_prFace[i].GetNormal().Normalized(); 00124 } 00125 } 00126 //----------------------------------------------------------------------------- 00127 00128 //****************************************************************************** 00129 // ApplyAndResetTransform 00130 //****************************************************************************** 00131 //----------------------------------------------------------------------------- 00132 template <typename T> 00133 void PolygonalModel<T>::ApplyAndResetTransform () 00134 { 00135 for ( int i = 0; i < m_iNoVertices; ++i ) { 00136 m_prVertex[i].SetPosition( 00137 ( GetTransform().RefToMatrixTransform() 00138 * Vector4<T>( m_prVertex[i].GetPosition() ) 00139 ).GetVector3() 00140 ); 00141 } 00142 00143 00144 /* 00145 int i; 00146 //-------------------------------------------------------------------- 00147 Vector3<T> vTransform = GetTransform().GetTranslation(); 00148 if ( vTransform[0] != Math<T>::ZERO ) { 00149 for ( i = 0; i < m_iNoVertices; ++i ) 00150 m_prVertex[i][0] += vTransform[0]; 00151 GetTransform().SetTranslation( 0, Math<T>::ZERO ); 00152 } 00153 if ( vTransform[1] != Math<T>::ZERO ) { 00154 for ( i = 0; i < m_iNoVertices; ++i ) 00155 m_prVertex[i][1] += vTransform[1]; 00156 GetTransform().SetTranslation( 1, Math<T>::ZERO ); 00157 } 00158 if ( vTransform[2] != Math<T>::ZERO ) { 00159 for ( i = 0; i < m_iNoVertices; ++i ) 00160 m_prVertex[i][2] += vTransform[2]; 00161 GetTransform().SetTranslation( 2, Math<T>::ZERO ); 00162 } 00163 //-------------------------------------------------------------------- 00164 vTransform = GetTransform().GetRotationAngles(); 00165 T tmpX; 00166 if ( vTransform[0] != Math<T>::ZERO ) { 00167 T deg = static_cast<T>( vTransform[0] * Math<T>::PI / 180.0 ); 00168 T c = Math<T>::Cos( deg ); 00169 T s = Math<T>::Sin( deg ); 00170 for ( i = 0; i < m_iNoVertices; ++i ) { 00171 tmpX = m_prVertex[i][1]*c - m_prVertex[i][2]*s; 00172 m_prVertex[i][2] = m_prVertex[i][1]*s + m_prVertex[i][2]*c; 00173 m_prVertex[i][1] = tmpX; 00174 } 00175 GetTransform().SetRotationAxisAngle( 0, Math<T>::ZERO ); 00176 } 00177 if ( vTransform[1] != Math<T>::ZERO ) { 00178 T deg = static_cast<T>( vTransform[1] * Math<T>::PI / 180.0 ); 00179 T c = Math<T>::Cos( deg ); 00180 T s = Math<T>::Sin( deg ); 00181 for ( i = 0; i < m_iNoVertices; ++i ) { 00182 tmpX = m_prVertex[i][0]*c + m_prVertex[i][2]*s; 00183 m_prVertex[i][2] = -(m_prVertex[i][0]*s) + m_prVertex[i][2]*c; 00184 m_prVertex[i][0] = tmpX; 00185 } 00186 GetTransform().SetRotationAxisAngle( 1, Math<T>::ZERO ); 00187 } 00188 if ( vTransform[2] != Math<T>::ZERO ) { 00189 T deg = static_cast<T>( vTransform[2] * Math<T>::PI / 180.0 ); 00190 T c = Math<T>::Cos( deg ); 00191 T s = Math<T>::Sin( deg ); 00192 for ( i = 0; i < m_iNoVertices; ++i ) { 00193 tmpX = m_prVertex[i][0]*c - m_prVertex[i][1]*s; 00194 m_prVertex[i][1] = m_prVertex[i][0]*s + m_prVertex[i][1]*c; 00195 m_prVertex[i][0] = tmpX; 00196 } 00197 GetTransform().SetRotationAxisAngle( 2, Math<T>::ZERO ); 00198 } 00199 //-------------------------------------------------------------------- 00200 vTransform = GetTransform().GetScale(); 00201 for ( int d = 0; d < 3; ++d ) { 00202 if ( vTransform[d] > Math<T>::ZERO ) { 00203 for ( i = 0; i < m_iNoVertices; ++i ) 00204 m_prVertex[i][d] *= vTransform[d]; 00205 GetTransform().SetScale( d, Math<T>::ONE ); 00206 } 00207 } 00208 //-------------------------------------------------------------------- 00209 //*/ 00210 00211 00212 00213 //-------------------------------------------------------------------- 00214 // Set Transformation to Identity Matrix 00215 GetTransform().MakeIdentity(); 00216 //-------------------------------------------------------------------- 00217 // Recalculate Bounding Volume(s) 00218 CalBoundingAABB(); 00219 CalBoundingEllipsoid(); 00220 CalBoundingSphere(); 00221 } 00222 //----------------------------------------------------------------------------- 00223 //****************************************************************************** 00224 00225 //****************************************************************************** 00226 // Virtual Fns from Collision Detection from ColDetSupport class 00227 //****************************************************************************** 00228 //----------------------------------------------------------------------------- 00229 template <typename T> 00230 void PolygonalModel<T>::CalBoundingAABB () 00231 { 00232 m_paBoundingAABB[0][0] = m_paBoundingAABB[1][0] = m_prVertex[0][0]; 00233 m_paBoundingAABB[0][1] = m_paBoundingAABB[1][1] = m_prVertex[0][1]; 00234 m_paBoundingAABB[0][2] = m_paBoundingAABB[1][2] = m_prVertex[0][2]; 00235 for ( int i = 1; i < m_iNoVertices; ++i ) 00236 { 00237 // Find lowest x, y, and z 00238 if ( m_paBoundingAABB[0][0] > m_prVertex[i][0] ) 00239 m_paBoundingAABB[0][0] = m_prVertex[i][0]; 00240 if ( m_paBoundingAABB[0][1] > m_prVertex[i][1] ) 00241 m_paBoundingAABB[0][1] = m_prVertex[i][1]; 00242 if ( m_paBoundingAABB[0][2] > m_prVertex[i][2] ) 00243 m_paBoundingAABB[0][2] = m_prVertex[i][2]; 00244 // Find highest x, y, and z 00245 if ( m_paBoundingAABB[1][0] < m_prVertex[i][0] ) 00246 m_paBoundingAABB[1][0] = m_prVertex[i][0]; 00247 if ( m_paBoundingAABB[1][1] < m_prVertex[i][1] ) 00248 m_paBoundingAABB[1][1] = m_prVertex[i][1]; 00249 if ( m_paBoundingAABB[1][2] < m_prVertex[i][2] ) 00250 m_paBoundingAABB[1][2] = m_prVertex[i][2]; 00251 } 00252 // Find the bounding volume center 00253 m_pBoundingCenter[0] = ( m_paBoundingAABB[0][0] + m_paBoundingAABB[1][0] ) / 2.0; 00254 m_pBoundingCenter[1] = ( m_paBoundingAABB[0][1] + m_paBoundingAABB[1][1] ) / 2.0; 00255 m_pBoundingCenter[2] = ( m_paBoundingAABB[0][2] + m_paBoundingAABB[1][2] ) / 2.0; 00256 } 00257 //----------------------------------------------------------------------------- 00258 template <typename T> 00259 void PolygonalModel<T>::CalBoundingEllipsoid () 00260 { 00261 T x = m_paBoundingAABB[1][0] - m_pBoundingCenter[0]; 00262 x *= x; 00263 T y = m_paBoundingAABB[1][1] - m_pBoundingCenter[1]; 00264 y *= y; 00265 T z = m_paBoundingAABB[1][2] - m_pBoundingCenter[2]; 00266 z *= z; 00267 m_pBoundingEllipsoid[1] = sqrt( y + z ); 00268 m_pBoundingEllipsoid[2] = sqrt( z + x ); 00269 m_pBoundingEllipsoid[0] = sqrt( x + y ); 00270 } 00271 //----------------------------------------------------------------------------- 00272 template <typename T> 00273 void PolygonalModel<T>::CalBoundingSphere () 00274 { 00275 T squaredLength; 00276 m_pBoundingSphere = 0; 00277 for ( int i = 0; i < m_iNoVertices; ++i ) { 00278 squaredLength = (m_prVertex[i].GetPosition() - m_pBoundingCenter).SquaredLength(); 00279 if ( squaredLength > m_pBoundingSphere ) 00280 m_pBoundingSphere = squaredLength; 00281 } 00282 m_pBoundingSphere = sqrt( m_pBoundingSphere ); 00283 } 00284 //----------------------------------------------------------------------------- 00285 //****************************************************************************** 00286 00287 //----------------------------------------------------------------------------- 00288 //============================================================================= 00289 END_NAMESPACE_TAPs__OpenGL 00290 //----------------------------------------------------------------------------- 00291 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00292 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8