UVMeshGrid.h

Go to the documentation of this file.
00001 #ifndef __UV_MESH_GRID_H 
00002 #define __UV_MESH_GRID_H 
00003 
00004 #if defined(JAMBUILD)
00005 #include <Mudbox/mudbox.h>
00006 #else
00007 #include "../../include/Mudbox/mudbox.h"
00008 #endif
00009 
00010 using namespace mudbox;
00011 
00012 struct Quad
00013 {
00014     Quad() :
00015         m_pMesh(0),
00016         m_iFaceIndex(0xffffffff),
00017         m_iSide(0xffffffff)
00018     {
00019     };
00020 
00021     Quad( const Mesh *pMesh, unsigned int iFaceIndex, int iSide = 0 )
00022     {
00023         m_pMesh = pMesh;
00024         m_iFaceIndex = iFaceIndex;
00025         m_iSide = iSide;
00026     };
00027 
00028     // 0 - current side
00029     // 1 - left
00030     // 2 - opposite
00031     // 3 - right
00032     // 
00033     bool Step( int iDirection = 0 )
00034     {
00035         unsigned int a = m_pMesh->QuadAdjacency( m_iFaceIndex, (m_iSide+iDirection)%4 );
00036         if ( a == 0xffffffff )
00037             return false;
00038         m_iFaceIndex = a/4;
00039         m_iSide = (a%4+2-iDirection)%4;
00040         return true;
00041     };
00042     unsigned int Corner( int iIndex ) const
00043     {
00044         return m_pMesh->QuadIndex( m_iFaceIndex, (m_iSide+iIndex)%4 );
00045     };
00046     unsigned int CornerTCI( int iIndex )
00047     {
00048         return m_pMesh->QuadTCI( m_iFaceIndex, (m_iSide+iIndex)%4 );
00049     };
00050     Vector CornerPosition( int iIndex ) const
00051     {
00052         return m_pMesh->QuadVertexPosition( m_iFaceIndex, (m_iSide+iIndex)%4 );
00053     };
00054     Vector Center( void ) const
00055     {
00056         return (CornerPosition(0)+CornerPosition(1)+CornerPosition(2)+CornerPosition(3))*0.25f;
00057     };
00058     void TurnRight( void )
00059     {
00060         m_iSide = (m_iSide+3)%4;
00061     };
00062     void TurnLeft( void )
00063     {
00064         m_iSide = (m_iSide+1)%4;
00065     };
00066     Quad& operator=( const Quad& pOther )
00067     {
00068         m_pMesh = pOther.m_pMesh;
00069         m_iFaceIndex = pOther.m_iFaceIndex;
00070         m_iSide = pOther.m_iSide;
00071         return *this;
00072     };
00073 
00074     const Mesh *m_pMesh;
00075     unsigned int m_iFaceIndex;
00076     int m_iSide;
00077 };
00078 
00079 // this is similar to the MudboxMeshGrid, 
00080 // but different in the following ways:
00081 // 1. dimensions of the grid do not need to be POT
00082 // 2. grids do not need to be square
00083 // 3. all grids do not need to be the same dimensions
00084 // 4. vertex/faceIDs are not reordered.
00085 // 5. works on non-quad meshes (but only quads are grouped together) 
00086 // 6. this one probably takes more memory, not to be used on dense meshes.
00087 class UVMeshGrid : public Node
00088 {
00089     DECLARE_CLASS;
00090 
00091     struct GridData
00092     {
00093         QVector< unsigned int > m_iFaces;
00094 
00095         // TODO: only need 2 bits per face, since there are only 4 possible values.
00096         // the other 6 bits are wasted :(
00097         QVector< char > m_iOrientations;
00098 
00099         unsigned int m_iWidth;
00100         unsigned int m_iHeight;
00101     };
00102 
00103     struct Solution
00104     {
00105         QVector< GridData* > m_aGrids;
00106         float m_fFitness;
00107         ~Solution()
00108         {
00109             for( int i = 0; i < m_aGrids.size(); ++i )
00110                 MB_SDN( m_aGrids[i] );
00111         };
00112     };
00113 
00114     UVMeshGrid();
00115     ~UVMeshGrid();
00116 
00117     void SetMaxGridDimension( unsigned int iMax ) { m_iMaxDimension = iMax; }
00118     void MeshGridDimensions( unsigned int iGrid, unsigned int& iWidth, unsigned int& iHeight ) const;
00119     unsigned int MeshGridCount( void ) const { return m_pSolution ? m_pSolution->m_aGrids.size() : 0; }
00120     unsigned int MeshGridFaceIndex( unsigned int iGrid, unsigned int iX, unsigned int iY ) const;
00121 
00122     // returns the bottom-left corner of the face, relative to the bottom-left corner of the grid.
00123     unsigned int MeshGridFaceOrientation( unsigned int iGrid, unsigned int iX, unsigned int iY ) const;
00124 
00125     unsigned int MeshGridBleed( unsigned int iGrid, unsigned int iDefaultBleed ) const;
00126 
00127     // returns the centerpoint of the grid, in local space
00128     Vector MeshGridCenter( unsigned int iGrid ) const;
00129 
00130     void SetMesh( Mesh* pMesh );
00131 
00132 private:
00133     aptr<Mesh> m_pMesh;
00134     aptr<Mesh> m_pQuadMesh;
00135     unsigned int m_iMaxDimension;
00136     Solution* m_pSolution;
00137 
00138     
00139     Solution* FindGrids( int iGridSize, Quad cCorner );
00140     float ComputeFitness( const Solution* pSolution );
00141     Quad FindEV( const Mesh* pMesh ) const;
00142     Mesh *QuadMesh( Mesh *pSource );
00143     unsigned int QuadMeshIndex( unsigned int iQuadFaceIndex );
00144     const Vector& QuadVertexPosition( unsigned int iTriangleIndex, unsigned int iMeshCorner, unsigned int iFaceOrientation ) const;
00145     void AddNonQuadFaces();
00146 
00147 
00148     QVector<unsigned int > m_aQuadMap;
00149 };
00150 
00151 #endif