TAPs 0.7.7.3
TAPsGridGenerator_ForTheDefModBasedOnMSS.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsGridGenerator_ForTheDefModBasedOnMSS.cpp
00003 ******************************************************************************/
00007 /******************************************************************************
00008 SUKITTI PUNAK   (07/22/2007)
00009 UPDATE          (01/07/2010)
00010 ******************************************************************************/
00011 #include "TAPsGridGenerator_ForTheDefModBasedOnMSS.hpp"
00012 // Using Inclusion Model (i.e. definitions are included in declarations)
00013 //                       (this name.cpp is included in name.hpp)
00014 // Each friend is defined directly inside its declaration.
00015 
00016 BEGIN_NAMESPACE_TAPs
00017 //=============================================================================
00018 //-----------------------------------------------------------------------------
00019 // Constructor
00020 template <typename T>
00021 GridGenerator<T>::GridGenerator ( OpenGL::Model<T> * pModel, 
00022     bool useSoftware, bool genInversely, 
00023     bool useDefaultIntpValue = false, 
00024     T    defaultIntpValue = 0.5 )
00025     : m_pModel( pModel ), 
00026       m_atVertexPosition( NULL ), 
00027       m_atVertexNormal( NULL ), 
00028       m_abVertexFlag1( NULL ), 
00029       m_atVertexInterpolation( NULL ), 
00030       m_bUseSoftwareToGenGrid( useSoftware ), 
00031       m_bGenGridInversely( genInversely ), 
00032       m_bUseDefaultIntpVal( useDefaultIntpValue ), 
00033       m_tDefaultIntpVal( defaultIntpValue ) 
00034 {
00035     m_atGridDimension[0] = m_atGridDimension[1] = m_atGridDimension[2] = 0;
00036     m_auiGridSize[0] = m_auiGridSize[1] = m_auiGridSize[2] = 0;
00037     //---------------------------------------------------------------
00038 
00039 //=============================================================================
00040 // TAPs in TIPS
00041 #ifdef TAPs_IN_TIPS
00042     //---------------------------------
00043     // Set default 8 corner points
00044     // based on a cube centering at (0,0,0).
00045     T halfSize = 0.5;
00046     m_vBBPts[0].SetXYZ(  halfSize, -halfSize, -halfSize );
00047     m_vBBPts[1].SetXYZ(  halfSize,  halfSize, -halfSize );
00048     m_vBBPts[2].SetXYZ( -halfSize,  halfSize, -halfSize );
00049     m_vBBPts[3].SetXYZ( -halfSize, -halfSize, -halfSize );
00050     m_vBBPts[4].SetXYZ(  halfSize, -halfSize,  halfSize );
00051     m_vBBPts[5].SetXYZ(  halfSize,  halfSize,  halfSize );
00052     m_vBBPts[6].SetXYZ( -halfSize,  halfSize,  halfSize );
00053     m_vBBPts[7].SetXYZ( -halfSize, -halfSize,  halfSize );
00054     //---------------------------------
00055     // Set default grid dimension and size
00056     T size = 8;
00057     m_atGridDimension[0] = m_atGridDimension[1] = m_atGridDimension[2] = (halfSize * 2.0) / (size-1.0);
00058     m_auiGridSize[0] = m_auiGridSize[1] = m_auiGridSize[2] = static_cast<int>( size );
00059 #endif // TAPs_IN_TIPS
00060 //=============================================================================
00061 }
00062 //-----------------------------------------------------------------------------
00063 // Destructor
00064 template <typename T>
00065 GridGenerator<T>::~GridGenerator ()
00066 {
00067     DeleteGrids();
00068 }
00069 //-----------------------------------------------------------------------------
00070 // Regenerate Grids with set Dimension
00071 template <typename T>
00072 bool GridGenerator<T>::ReGenerateGrids () 
00073 {
00074     if ( !m_pModel )                    return false;
00075     if ( m_atGridDimension[0] == 0 )    return false;
00076     if ( !IsGridGenerated() )           return false;
00077     //---------------------------------------------------------------
00078     // Generate Grids
00079 #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00080     if ( m_bUseSoftwareToGenGrid ) {
00081         SetGridPointsToInsideOutsideModelBoundary_SW();
00082         SetAllGridPointsToInsideOutsideModel_SW();
00083     }
00084 #endif
00085 #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00086     if ( !m_bUseSoftwareToGenGrid ) {
00087         SetAllGridPointsToInsideOutsideModel_HW();
00088     }
00089 #endif
00090     //---------------------------------------------------------------
00091     ClearExtraRightOutsidePoints();
00092     if ( m_bGenGridInversely ) {
00093         InverseInsideOutside();
00094         ClearExtraRightOutsidePoints();
00095     }
00096     SetVertexInterpolationData( 0.5 );
00097     //---------------------------------------------------------------
00098 #ifdef TAPs_DEBUG_MODE
00099 #ifdef TAPs_USE_WXWIDGETS
00100     wxLogWarning( wxT( "Regenerated Grids:" ) );
00101     wxLogWarning( wxT( "  w/ size: %i, %i, %i" ), m_auiGridSize[0], m_auiGridSize[1], m_auiGridSize[2] );
00102     wxLogWarning( wxT( "  w/ dim: %g, %g, %g" ), m_atGridDimension[0], m_atGridDimension[1], m_atGridDimension[2] );
00103 #else
00104     //std::cerr << "Error: " << glewGetErrorString( err ) << std::endl;
00105 #endif
00106 #endif
00107     //---------------------------------------------------------------
00108     return true;
00109 }
00110 //-----------------------------------------------------------------------------
00111 // Generate Grids with Input Dimension
00112 template <typename T>
00113 bool GridGenerator<T>::GenerateGridsWithDimension ( 
00114     T iGridWidth, T iGridHeight, T iGridDepth, 
00115     Vector3<T> * lowPoint, Vector3<T> * highPoint 
00116 )
00117 {
00118     if ( !m_pModel )    return false;
00119     //---------------------------------------------------------------
00120     //if (  iGridWidth  == m_atGridDimension[0]
00121     //  &&  iGridHeight == m_atGridDimension[1]
00122     //  &&  iGridDepth  == m_atGridDimension[2] )
00123     //{
00124     //  return false;
00125     //}
00126     //---------------------------------------------------------------
00127     DeleteGrids();
00128     //---------------------------------------------------------------
00129     // Set Dimension
00130     m_atGridDimension[0] = iGridWidth;
00131     m_atGridDimension[1] = iGridHeight;
00132     m_atGridDimension[2] = iGridDepth;
00133     //---------------------------------------------------------------
00134     // Set Size
00135     FindBoundingBoxDimension( lowPoint, highPoint );
00136     T xSize = ( m_tAABBMax[0] - m_tAABBMin[0] ) / m_atGridDimension[0];
00137     T ySize = ( m_tAABBMax[1] - m_tAABBMin[1] ) / m_atGridDimension[1];
00138     T zSize = ( m_tAABBMax[2] - m_tAABBMin[2] ) / m_atGridDimension[2];
00139     m_auiGridSize[0] = ceil( xSize );
00140     m_auiGridSize[1] = ceil( ySize );
00141     m_auiGridSize[2] = ceil( zSize );
00142     ++m_auiGridSize[0];
00143     ++m_auiGridSize[1];
00144     ++m_auiGridSize[2];
00145 
00146     // DEBUG
00147     //wxLogWarning( wxT( "  w/ left over: %g, %g, %g" ), 
00148     //  xSize - m_atGridDimension[0], 
00149     //  ySize - m_atGridDimension[1], 
00150     //  zSize - m_atGridDimension[2] );
00151     //---------------------------------------------------------------
00152     GenerateGrids();
00153     //---------------------------------------------------------------
00154 #ifdef TAPs_DEBUG_MODE
00155 #ifdef TAPs_USE_WXWIDGETS
00156     wxLogWarning( wxT( "Grid Generator:" ) );
00157     wxLogWarning( wxT( "  w/ size: %i, %i, %i" ), m_auiGridSize[0], m_auiGridSize[1], m_auiGridSize[2] );
00158     wxLogWarning( wxT( "  w/ dim: %g, %g, %g" ), m_atGridDimension[0], m_atGridDimension[1], m_atGridDimension[2] );
00159 #else
00160     //std::cerr << "Error: " << glewGetErrorString( err ) << std::endl;
00161 #endif
00162 #endif
00163     //---------------------------------------------------------------
00164     return true;
00165 }
00166 //-----------------------------------------------------------------------------
00167 // Generate Grids with Input Size
00168 template <typename T>
00169 bool GridGenerator<T>::GenerateGridsWithSize ( 
00170     int iNumOfXSlices, int iNumOfYSlices, int iNumOfZSlices, 
00171     Vector3<T> * lowPoint, Vector3<T> * highPoint 
00172 )
00173 {
00174     if ( !m_pModel )    return false;
00175     //---------------------------------------------------------------
00176     //if (  iNumOfXSlices == m_auiGridSize[0]
00177     //  &&  iNumOfYSlices == m_auiGridSize[1]
00178     //  &&  iNumOfZSlices == m_auiGridSize[2] )
00179     //{
00180     //  return false;
00181     //}
00182     //---------------------------------------------------------------
00183     DeleteGrids();
00184     //---------------------------------------------------------------
00185     // Set Size
00186     m_auiGridSize[0] = iNumOfXSlices;
00187     m_auiGridSize[1] = iNumOfYSlices;
00188     m_auiGridSize[2] = iNumOfZSlices;
00189     //---------------------------------------------------------------
00190     //*
00191     // Set Dimension
00192     // without adjustment
00193     FindBoundingBoxDimension( lowPoint, highPoint );
00194     m_atGridDimension[0] = ( m_tAABBMax[0] - m_tAABBMin[0] ) / ( m_auiGridSize[0] - 1 );
00195     m_atGridDimension[1] = ( m_tAABBMax[1] - m_tAABBMin[1] ) / ( m_auiGridSize[1] - 1 );
00196     m_atGridDimension[2] = ( m_tAABBMax[2] - m_tAABBMin[2] ) / ( m_auiGridSize[2] - 1 );
00197     //*/
00198     //---------------------------------------------------------------
00199     /*
00200     //===============================================================
00201     // Set Dimension
00202     // with adjustment for expaning the AABB box 
00203     // so that all of the boundary elements are outside of the mesh
00204     //---------------------------------------------------------------
00205     FindBoundingBoxDimension( lowPoint, highPoint );
00206     m_atGridDimension[0] = ( m_tAABBMax[0] - m_tAABBMin[0] ) / ( m_auiGridSize[0] - 3 );
00207     m_atGridDimension[1] = ( m_tAABBMax[1] - m_tAABBMin[1] ) / ( m_auiGridSize[1] - 3 );
00208     m_atGridDimension[2] = ( m_tAABBMax[2] - m_tAABBMin[2] ) / ( m_auiGridSize[2] - 3 );
00209     //-------------------------------------------
00210     // Adjust Mins and Maxs of AABB
00211     m_tAABBMin[0] -= m_atGridDimension[0];
00212     m_tAABBMin[1] -= m_atGridDimension[1];
00213     m_tAABBMin[2] -= m_atGridDimension[2];
00214     m_tAABBMax[0] += m_atGridDimension[0];
00215     m_tAABBMax[1] += m_atGridDimension[1];
00216     m_tAABBMax[2] += m_atGridDimension[2];
00217     //---------------------------------------------------------------
00218     //===============================================================
00219     //*/
00220     //---------------------------------------------------------------
00221     GenerateGrids();
00222     //---------------------------------------------------------------
00223 #ifdef TAPs_DEBUG_MODE
00224 #ifdef TAPs_USE_WXWIDGETS
00225     wxLogWarning( wxT( "Grid Generator:" ) );
00226     wxLogWarning( wxT( "  w/ size: %i, %i, %i" ), m_auiGridSize[0], m_auiGridSize[1], m_auiGridSize[2] );
00227     wxLogWarning( wxT( "  w/ dim: %g, %g, %g" ), m_atGridDimension[0], m_atGridDimension[1], m_atGridDimension[2] );
00228 #else
00229     //std::cerr << "Error: " << glewGetErrorString( err ) << std::endl;
00230 #endif
00231 #endif
00232     //---------------------------------------------------------------
00233     return true;
00234 }
00235 //-----------------------------------------------------------------------------
00236 
00237 //*
00238 //*****************************************************************************
00239 // START: DEBUG for Checking Marching Cubes Tables
00240 //-----------------------------------------------------------------------------
00241 // Generate Grids
00242 template <typename T>
00243 bool GridGenerator<T>::GenerateDefinedGrids2x2x2 ( 
00244     unsigned char config, 
00245     int iNumOfXSlices, int iNumOfYSlices, int iNumOfZSlices, 
00246     Vector3<T> * lowPoint, Vector3<T> * highPoint 
00247 )
00248 {
00249     // Flag arrangement (right-handed xyz-coordinates)
00250     //     3---2
00251     //    /|  /|
00252     //   / 0-/-1
00253     //  7---6 /
00254     //  |/  |/
00255     //  4---5
00256     int idx[8][3] = {
00257         { 0,0,0 }, 
00258         { 1,0,0 }, 
00259         { 1,1,0 }, 
00260         { 0,1,0 }, 
00261         { 0,0,1 }, 
00262         { 1,0,1 }, 
00263         { 1,1,1 }, 
00264         { 0,1,1 } 
00265     };
00266 
00267     if ( !m_pModel )    return false;
00268     //---------------------------------------------------------------
00269     DeleteGrids();
00270     //---------------------------------------------------------------
00271     // Set Size
00272     m_auiGridSize[0] = iNumOfXSlices = 2;
00273     m_auiGridSize[1] = iNumOfYSlices = 2;
00274     m_auiGridSize[2] = iNumOfZSlices = 2;
00275     //---------------------------------------------------------------
00276     //*
00277     // Set Dimension
00278     // without adjustment
00279     FindBoundingBoxDimension( lowPoint, highPoint );
00280     m_atGridDimension[0] = ( m_tAABBMax[0] - m_tAABBMin[0] ) / ( m_auiGridSize[0] - 1 );
00281     m_atGridDimension[1] = ( m_tAABBMax[1] - m_tAABBMin[1] ) / ( m_auiGridSize[1] - 1 );
00282     m_atGridDimension[2] = ( m_tAABBMax[2] - m_tAABBMin[2] ) / ( m_auiGridSize[2] - 1 );
00283 
00284     T tMinMaxIdx[8][3] = {
00285         { m_tAABBMin[0], m_tAABBMin[1], m_tAABBMin[2] }, 
00286         { m_tAABBMax[0], m_tAABBMin[1], m_tAABBMin[2] }, 
00287         { m_tAABBMax[0], m_tAABBMax[1], m_tAABBMin[2] }, 
00288         { m_tAABBMin[0], m_tAABBMax[1], m_tAABBMin[2] }, 
00289         { m_tAABBMin[0], m_tAABBMin[1], m_tAABBMax[2] }, 
00290         { m_tAABBMax[0], m_tAABBMin[1], m_tAABBMax[2] }, 
00291         { m_tAABBMax[0], m_tAABBMax[1], m_tAABBMax[2] }, 
00292         { m_tAABBMin[0], m_tAABBMax[1], m_tAABBMax[2] }, 
00293     };
00294     //*/
00295     //---------------------------------------------------------------
00296     //GenerateGrids();
00297     GenerateGridsForPosition();
00298     GenerateGridsForNormal();
00299     GenerateGridsForFlag1();
00300     GenerateGridsForVertexInterpolation();
00301     //---------------------------------------------------------------
00302     char mask = 1;
00303     for ( int i = 0; i < 8; ++i ) {
00304         m_atVertexPosition[idx[i][0]][idx[i][1]][idx[i][2]][0] = tMinMaxIdx[i][0];
00305         m_atVertexPosition[idx[i][0]][idx[i][1]][idx[i][2]][1] = tMinMaxIdx[i][1];
00306         m_atVertexPosition[idx[i][0]][idx[i][1]][idx[i][2]][2] = tMinMaxIdx[i][2];
00307         if ( config & mask ) {
00308             m_abVertexFlag1[idx[i][0]][idx[i][1]][idx[i][2]] = RIGHT_INSIDE_BOUNDARY;
00309         }
00310         else {
00311             m_abVertexFlag1[idx[i][0]][idx[i][1]][idx[i][2]] = RIGHT_OUTSIDE_BOUNDARY;
00312         }
00313         mask = mask<<1;
00314     }
00315     //---------------------------------------------------------------
00316     SetVertexInterpolationData( 0.5 );
00317     //---------------------------------------------------------------
00318 #ifdef TAPs_DEBUG_MODE
00319 #ifdef TAPs_USE_WXWIDGETS
00320     wxLogWarning( wxT( "Grid Generator (GenerateDefinedGrids2x2x2):" ) );
00321     wxLogWarning( wxT( "  w/ size: %i, %i, %i" ), m_auiGridSize[0], m_auiGridSize[1], m_auiGridSize[2] );
00322     wxLogWarning( wxT( "  w/ dim: %g, %g, %g" ), m_atGridDimension[0], m_atGridDimension[1], m_atGridDimension[2] );
00323 #else
00324     //std::cerr << "Error: " << glewGetErrorString( err ) << std::endl;
00325 #endif
00326 #endif
00327     //---------------------------------------------------------------
00328     return true;
00329 }
00330 //-----------------------------------------------------------------------------
00331 // END: DEBUG for Checking Marching Cubes Tables
00332 //*****************************************************************************
00333 //*/
00334 
00335 //-----------------------------------------------------------------------------
00336 // Find Bounding Box Dimension
00337 template <typename T>
00338 void GridGenerator<T>::FindBoundingBoxDimension ( 
00339         Vector3<T> * lowPoint, Vector3<T> * highPoint 
00340 )
00341 {
00342     if ( lowPoint == NULL || highPoint == NULL ) {
00343         m_pModel->CalBoundingAABB();        // Could be commented out
00344         Vector3<T> lowPoint  = m_pModel->GetBoundingAABBLowPoint();
00345         Vector3<T> highPoint = m_pModel->GetBoundingAABBHighPoint();
00346 
00347         // AABB SCALING ADJUSTER
00348         //-----------------------------------------------------
00349         // This scaling makes the grid box bigger than the AABB of the mesh.
00350         // So that all of the border grid points are outside the mesh.
00351         //*
00352         Vector3<T> scale = (highPoint - lowPoint) * 0.00001;
00353         //Vector3<T> scale = (highPoint - lowPoint) * 0.25;
00354         //Vector3<T> scale = (highPoint - lowPoint) * 1;
00355         lowPoint  -= scale;
00356         highPoint += scale;
00357         //*/
00358         //-----------------------------------------------------
00359         // Set the size of the grid box
00360         m_tAABBMin[0] = lowPoint[0];
00361         m_tAABBMin[1] = lowPoint[1];
00362         m_tAABBMin[2] = lowPoint[2];
00363         m_tAABBMax[0] = highPoint[0];
00364         m_tAABBMax[1] = highPoint[1];
00365         m_tAABBMax[2] = highPoint[2];
00366     }
00367     else {
00368         //-----------------------------------------------------
00369         // Set the size of the grid box
00370         m_tAABBMin[0] = lowPoint->GetX();
00371         m_tAABBMin[1] = lowPoint->GetY();
00372         m_tAABBMin[2] = lowPoint->GetZ();
00373         m_tAABBMax[0] = highPoint->GetX();
00374         m_tAABBMax[1] = highPoint->GetY();
00375         m_tAABBMax[2] = highPoint->GetZ();
00376     }
00377 }
00378 //-----------------------------------------------------------------------------
00379 
00380 
00381 //=============================================================================
00382 // TAPs in TIPS
00383 #ifdef TAPs_IN_TIPS
00384 //-----------------------------------------------------------------------------
00385 // Generate Grids
00386 template <typename T>
00387 void GridGenerator<T>::GenerateGridsInTIPS ()
00388 {
00389     //---------------------------------------------------------------
00390     // Delete (Old) Grids
00391     DeleteGrids();
00392     //---------------------------------------------------------------
00393     // Find the current positions of the eight grid points using OpenGL matrix
00394     GLfloat matModView[16];
00395     glGetFloatv( GL_MODELVIEW_MATRIX, matModView );
00396     Vector3<T> pts[8];
00397     for ( int i = 0; i < 8; ++i ) {
00398         pts[i][0] = matModView[0]*m_vBBPts[i][0] + matModView[4]*m_vBBPts[i][1] + matModView[ 8]*m_vBBPts[i][2] + matModView[12];
00399         pts[i][1] = matModView[1]*m_vBBPts[i][0] + matModView[5]*m_vBBPts[i][1] + matModView[ 9]*m_vBBPts[i][2] + matModView[13];
00400         pts[i][2] = matModView[2]*m_vBBPts[i][0] + matModView[6]*m_vBBPts[i][1] + matModView[10]*m_vBBPts[i][2] + matModView[14];
00401     }
00402     //---------------------------------------------------------------
00403     // Generate Grids
00404     GenerateGridsForPositionInTIPS( pts );
00405     GenerateGridsForNormal();
00406     GenerateGridsForFlag1();
00407     GenerateGridsForVertexInterpolation();
00408     //---------------------------------------------------------------
00409 #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00410     if ( m_bUseSoftwareToGenGrid ) {
00411         SetGridPtsToInOutBoundary_SW_InTIPS();
00412         SetAllGridPtsToInOut_SW_InTIPs();
00413     }
00414 #endif
00415 #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00416     if ( !m_bUseSoftwareToGenGrid ) {
00417         SetAllGridPtsToInOut_HW_InTIPs();
00418     }
00419 #endif
00420     //---------------------------------------------------------------
00421     ClearExtraRightOutsidePoints();
00422     if ( m_bGenGridInversely ) {
00423         InverseInsideOutside();
00424         ClearExtraRightOutsidePoints();
00425     }
00426     SetVertexInterpolationDataInTIPS();
00427     //---------------------------------------------------------------
00428     //---------------------------------------------------------------
00429     // TESTING
00430     {
00431         for ( int w = 0; w < m_auiGridSize[0]; ++w ) {
00432             for ( int h = 0; h < m_auiGridSize[1]; ++h ) {
00433                 for ( int d = 0; d < m_auiGridSize[2]; ++d ) {
00434                     m_abVertexFlag1[w][h][d] = RIGHT_OUTSIDE_BOUNDARY;
00435                 }
00436             }
00437         }
00438         for ( int w = 1; w < m_auiGridSize[0]-1; ++w ) {
00439             for ( int h = 1; h < m_auiGridSize[1]-1; ++h ) {
00440                 for ( int d = 1; d < m_auiGridSize[2]-1; ++d ) {
00441                     m_abVertexFlag1[w][h][d] = RIGHT_INSIDE_BOUNDARY;
00442                 }
00443             }
00444         }
00445     }
00446     //---------------------------------------------------------------
00447 }
00448 //-----------------------------------------------------------------------------
00449 // Generate Position Grids (in TIPS)
00450 template <typename T>
00451 void GridGenerator<T>::GenerateGridsForPositionInTIPS ( Vector3<T> pts[8] )
00452 {
00453     AllocateGridsForPosition();
00454     //---------------------------------------------------------------
00455     //-----------------------
00456     //     2---1    2=y
00457     //    /|  /|    |
00458     //   6-3-5-0    3---0=x
00459     //   |/  |/    /
00460     //   7---4    7=z
00461     //-----------------------
00462     // incX, incY, incZ
00463     Vector3<T> incX = ( pts[0] - pts[3] ) / ( m_auiGridSize[0]-1.0 );
00464     Vector3<T> incY = ( pts[2] - pts[3] ) / ( m_auiGridSize[1]-1.0 );
00465     Vector3<T> incZ = ( pts[7] - pts[3] ) / ( m_auiGridSize[2]-1.0 );
00466     //-----------------------
00467     //---------------------------------------------------------------
00468     Vector3<T> posX = pts[3];
00469     for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
00470         Vector3<T> posXY = posX;
00471         for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
00472             Vector3<T> posXYZ = posXY;
00473             for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
00474                 m_atVertexPosition[i][j][k][0] = posXYZ[0];
00475                 m_atVertexPosition[i][j][k][1] = posXYZ[1];
00476                 m_atVertexPosition[i][j][k][2] = posXYZ[2];// - 20;
00477                 posXYZ += incZ;
00478 
00479                 //std::cout <<"["<<i<<","<<j<<","<<k<<"]: (" 
00480                 //  << m_atVertexPosition[i][j][k][0] << ","
00481                 //  << m_atVertexPosition[i][j][k][1] << ","
00482                 //  << m_atVertexPosition[i][j][k][2] << ")"
00483                 //  << std::endl;
00484 
00485             }
00486             posXY += incY;
00487         }
00488         posX += incX;
00489     }
00490     //---------------------------------------------------------------
00491 }
00492 
00493 #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00494 //-------------------------------------------------------------------
00495 template <typename T>
00496 void GridGenerator<T>::SetGridPtsToInOutBoundary_SW_InTIPS ()
00497 {
00498 }
00499 //-------------------------------------------------------------------
00500 template <typename T>
00501 void GridGenerator<T>::SetAllGridPtsToInOut_SW_InTIPs ()
00502 {
00503 }
00504 //-------------------------------------------------------------------
00505 #endif // #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00506 
00507 #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00508 //-------------------------------------------------------------------
00509 template <typename T>
00510 void GridGenerator<T>::SetAllGridPtsToInOut_HW_InTIPs ()
00511 {
00512 }
00513 //-------------------------------------------------------------------
00514 #endif // #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00515 
00516 //-------------------------------------------------------------------
00517 template <typename T>
00518 void GridGenerator<T>::SetVertexInterpolationDataInTIPS ()
00519 {
00520     //-----------------------------------------------------
00521     // TESTING
00522     for ( int x = 0; x < m_auiGridSize[0]; ++x ) {
00523         for ( int y = 0; y < m_auiGridSize[1]; ++y ) {
00524             for ( int z = 0; z < m_auiGridSize[2]; ++z ) {
00525                 m_atVertexInterpolation[x][y][z][0] = 
00526                 m_atVertexInterpolation[x][y][z][1] = 
00527                 m_atVertexInterpolation[x][y][z][2] = 0.49;
00528             }
00529         }
00530     }
00531 }
00532 //-------------------------------------------------------------------
00533 //-----------------------------------------------------------------------------
00534 #endif // #ifdef TAPs_IN_TIPS
00535 //=============================================================================
00536 
00537 
00538 //-----------------------------------------------------------------------------
00539 // Generate Grids
00540 template <typename T>
00541 void GridGenerator<T>::GenerateGrids ()
00542 {
00543     GenerateGridsForPosition();
00544     GenerateGridsForNormal();
00545     GenerateGridsForFlag1();
00546     GenerateGridsForVertexInterpolation();
00547     //---------------------------------------------------------------
00548 #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00549     if ( m_bUseSoftwareToGenGrid ) {
00550         SetGridPointsToInsideOutsideModelBoundary_SW();
00551         SetAllGridPointsToInsideOutsideModel_SW();
00552     }
00553 #endif
00554 #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00555     if ( !m_bUseSoftwareToGenGrid ) {
00556         SetAllGridPointsToInsideOutsideModel_HW();
00557     }
00558 #endif
00559     //---------------------------------------------------------------
00560     ClearExtraRightOutsidePoints();
00561     if ( m_bGenGridInversely ) {
00562         InverseInsideOutside();
00563         ClearExtraRightOutsidePoints();
00564     }
00565     SetVertexInterpolationData();
00566 }
00567 //-----------------------------------------------------------------------------
00568 // Generate Position Grids
00569 template <typename T>
00570 void GridGenerator<T>::GenerateGridsForPosition ()
00571 {
00572     AllocateGridsForPosition();
00573     //---------------------------------------------------------------
00574     T x = m_tAABBMin[0];// - m_atGridDimension[0];
00575     for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
00576         T y = m_tAABBMin[1];// - m_atGridDimension[1];
00577         for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
00578             T z = m_tAABBMin[2];// - m_atGridDimension[2];
00579             for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
00580                 m_atVertexPosition[i][j][k][0] = x;
00581                 m_atVertexPosition[i][j][k][1] = y;
00582                 m_atVertexPosition[i][j][k][2] = z;
00583                 z += m_atGridDimension[2];
00584             }
00585             y += m_atGridDimension[1];
00586         }
00587         x += m_atGridDimension[0];
00588     }
00589     //---------------------------------------------------------------
00590 }
00591 //-----------------------------------------------------------------------------
00592 // Generate Normal Grids
00593 template <typename T>
00594 void GridGenerator<T>::GenerateGridsForNormal ()
00595 {
00596     //---------------------------------------------------------------
00597     AllocateGridsForNormal();
00598     //---------------------------------------------------------------
00599 }
00600 //-----------------------------------------------------------------------------
00601 // Generate Flag1 Grids
00602 template <typename T>
00603 void GridGenerator<T>::GenerateGridsForFlag1 ()
00604 {
00605     //---------------------------------------------------------------
00606     AllocateGridsForFlag1();
00607     //---------------------------------------------------------------
00608 }
00609 //-----------------------------------------------------------------------------
00610 // Generate Vertex Interpolation Grids
00611 template <typename T>
00612 void GridGenerator<T>::GenerateGridsForVertexInterpolation ()
00613 {
00614     //---------------------------------------------------------------
00615     AllocateGridsForVertexInterpolation();
00616     //---------------------------------------------------------------
00617 }
00618 //-----------------------------------------------------------------------------
00619 // Allocate Position Grids
00620 template <typename T>
00621 void GridGenerator<T>::AllocateGridsForPosition ()
00622 {
00623     //DeleteGridsForPosition();
00624     //---------------------------------------------------------------
00625     m_atVertexPosition = new T***[ m_auiGridSize[0] ];
00626     assert( m_atVertexPosition );
00627     for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
00628         m_atVertexPosition[i] = new T**[ m_auiGridSize[1] ];
00629         assert( m_atVertexPosition[i] );
00630         for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
00631             m_atVertexPosition[i][j] = new T*[ m_auiGridSize[2] ];
00632             assert( m_atVertexPosition[i][j] );
00633             for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
00634                 m_atVertexPosition[i][j][k] = new T[ 3 ];
00635                 assert( m_atVertexPosition[i][j][k] );
00636             }
00637         }
00638     }
00639     //---------------------------------------------------------------
00640 }
00641 //-----------------------------------------------------------------------------
00642 // Allocate Normal Grids
00643 template <typename T>
00644 void GridGenerator<T>::AllocateGridsForNormal ()
00645 {
00646     //DeleteGridsForNormal();
00647     //---------------------------------------------------------------
00648     m_atVertexNormal = new T***[ m_auiGridSize[0] ];
00649     assert( m_atVertexNormal );
00650     for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
00651         m_atVertexNormal[i] = new T**[ m_auiGridSize[1] ];
00652         assert( m_atVertexNormal[i] );
00653         for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
00654             m_atVertexNormal[i][j] = new T*[ m_auiGridSize[2] ];
00655             assert( m_atVertexNormal[i][j] );
00656             for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
00657                 m_atVertexNormal[i][j][k] = new T[ 3 ];
00658                 assert( m_atVertexNormal[i][j][k] );
00659             }
00660         }
00661     }
00662     //---------------------------------------------------------------
00663 }
00664 //-----------------------------------------------------------------------------
00665 // Allocate Flag1 Grids
00666 template <typename T>
00667 void GridGenerator<T>::AllocateGridsForFlag1 ()
00668 {
00669     //DeleteGridsForFlag1();
00670     //---------------------------------------------------------------
00671     m_abVertexFlag1 = new VertexFlag **[ m_auiGridSize[0] ];
00672     assert( m_abVertexFlag1 );
00673     for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
00674         m_abVertexFlag1[i] = new VertexFlag *[ m_auiGridSize[1] ];
00675         assert( m_abVertexFlag1[i] );
00676         for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
00677             m_abVertexFlag1[i][j] = new VertexFlag [ m_auiGridSize[2] ];
00678             assert( m_abVertexFlag1[i][j] );
00679             for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
00680                 m_abVertexFlag1[i][j][k] = UNSET;
00681             }
00682         }
00683     }
00684     //---------------------------------------------------------------
00685 }
00686 //-----------------------------------------------------------------------------
00687 // Allocate Vertex Interpolation Grids
00688 template <typename T>
00689 void GridGenerator<T>::AllocateGridsForVertexInterpolation ()
00690 {
00691     //DeleteGridsForVertexInterpolation();
00692     //---------------------------------------------------------------
00693     m_atVertexInterpolation = new T***[ m_auiGridSize[0] ];
00694     assert( m_atVertexInterpolation );
00695     for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
00696         m_atVertexInterpolation[i] = new T**[ m_auiGridSize[1] ];
00697         assert( m_atVertexInterpolation[i] );
00698         for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
00699             m_atVertexInterpolation[i][j] = new T*[ m_auiGridSize[2] ];
00700             assert( m_atVertexInterpolation[i][j] );
00701             for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
00702                 m_atVertexInterpolation[i][j][k] = new T[ 3 ];
00703                 assert( m_atVertexInterpolation[i][j][k] );
00704             }
00705         }
00706     }
00707     //---------------------------------------------------------------
00708 }
00709 //-----------------------------------------------------------------------------
00710 
00711 
00712 //=============================================================================
00713 #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00714 //-----------------------------------------------------------------------------
00715 // Set Grid Points To Inside and Outside the Model
00716 template <typename T>
00717 void GridGenerator<T>::SetGridPointsToInsideOutsideModelBoundary_SW ()
00718 {
00719     if ( !m_pModel )    return;
00720     //---------------------------------------------------------------
00721     OpenGL::PolygonalModel<T> *     pPologonalModel 
00722             = dynamic_cast< OpenGL::PolygonalModel<T> * >( m_pModel );
00723     OpenGL::XPolygonalModel<T> *    pXPologonalModel 
00724             = dynamic_cast< OpenGL::XPolygonalModel<T> * >( m_pModel );
00725     OpenGL::HalfEdgeModel<T> *      pHalfEdgeModel 
00726             = dynamic_cast< OpenGL::HalfEdgeModel<T> * >( m_pModel );
00727     //---------------------------------------------------------------
00728     // For PolygonalModel
00729     if ( pPologonalModel ) {
00730         SetGridPointsToInsideOutsideModelBoundary_SW( pPologonalModel );
00731     }
00732     //---------------------------------------------------------------
00733     // For XPolygonalModel
00734     if ( pXPologonalModel ) {
00735         SetGridPointsToInsideOutsideModelBoundary_SW( pXPologonalModel );
00736     }
00737     //---------------------------------------------------------------
00738     // For HalfEdgeModel
00739     if ( pHalfEdgeModel ) {
00740         SetGridPointsToInsideOutsideModelBoundary_SW( pHalfEdgeModel );
00741     }
00742     //---------------------------------------------------------------
00743 }
00744 //-----------------------------------------------------------------------------
00745 // Set Grid Points To Inside and Outside the Model
00746 template <typename T>
00747 void GridGenerator<T>::SetGridPointsToInsideOutsideModelBoundary_SW ( 
00748     OpenGL::PolygonalModel<T> * pPModel )
00749 {
00750     Vertex<T> * vertexList = pPModel->GetVertexList();
00751     assert( false );
00752     // Find two vertices; (-x,0,0) and (x,0,0)
00753     // and faces that contain them
00754 }
00755 //-----------------------------------------------------------------------------
00756 // Set Grid Points To Inside and Outside the Model
00757 template <typename T>
00758 void GridGenerator<T>::SetGridPointsToInsideOutsideModelBoundary_SW ( 
00759     OpenGL::XPolygonalModel<T> * pXPModel )
00760 {
00761     //---------------------------------------------------------------
00762     XVertex<T> * vertex = pXPModel->GetVertexList();
00763     Face<T> * face = pXPModel->GetFaceList();
00764     int NumFaces = pXPModel->GetNoFaces();
00765     //---------------------------------------------------------------
00766     // Generate Grid by a Software Algorithm
00767     //-----------------------------------------------------
00768     Vector3<T> vMin, vMax, vPos, vAverage;
00769     //-----------------------------------------------------
00770     // For Each Face
00771     for ( int i = 0; i < NumFaces; ++i ) {
00772         //---------------------------------------
00773         // Find a box that contain this face
00774         vAverage = vMin = vMax = vertex[ face[i].GetVertexNo(0) ].GetPosition();
00775         for ( int n = 1; n < face[i].GetNoVertices(); ++n ) {
00776             vPos = vertex[ face[i].GetVertexNo(n) ].GetPosition();
00777             vAverage += vPos;
00778             for ( int d = 0; d < 3; ++d ) {
00779                 if      ( vMin[d] > vPos[d] )   vMin[d] = vPos[d];
00780                 else if ( vMax[d] < vPos[d] )   vMax[d] = vPos[d];
00781             }
00782         }
00783         //assert( vMin[0] <= vMax[0] );
00784         //assert( vMin[1] <= vMax[1] );
00785         //assert( vMin[2] <= vMax[2] );
00786         //---------------------------------------
00787         // Find a box of grids that contain this face
00788         // i.e. a bigger box that contain the face box
00789         int gMinNum[3], gMaxNum[3];
00790         T gMin, gMax;
00791         for ( int d = 0; d < 3; ++d ) {
00792             int step = m_auiGridSize[d] / 2;
00793             gMinNum[d] = gMaxNum[d] = step;
00794             gMin = m_tAABBMin[d] + gMinNum[d] * m_atGridDimension[d];
00795             T minOffset = vMin[d] - m_atGridDimension[d] / 2;
00796             T maxOffset = vMax[d] + m_atGridDimension[d] / 2;
00797             //T minOffset = vMin[d];
00798             //T maxOffset = vMax[d];
00799             //-------------------------
00800             // Find a box of this current face
00801             while ( ( gMin < minOffset || maxOffset < gMin ) && step > 1 ) {
00802                 step /= 2;
00803                 if ( gMin < minOffset ) {
00804                     gMinNum[d] += step;
00805                 }
00806                 else {
00807                     gMinNum[d] -= step;
00808                 }
00809                 gMin = m_tAABBMin[d] + gMinNum[d] * m_atGridDimension[d];
00810             }
00811             gMax = gMin;
00812             gMaxNum[d] = gMinNum[d];
00813 
00814             // DEBUG
00815             //gridBoxCenterPt[d] = gMinNum[d];
00816 
00817             //-------------------------
00818             // Find a grid box that covers the box of this current face
00819             //minMoveStep[d] = maxMoveStep[d] = 0;      // DEBUG
00820             //-------------------------
00821             // Find the low
00822             while ( vMin[d] < gMin ) {
00823             //while ( minOffset < gMin ) {
00824                 //--minMoveStep[d];     // DEBUG
00825                 --gMinNum[d];
00826                 gMin -= m_atGridDimension[d];
00827             }
00828             if ( gMinNum[d] < 0 ) {
00829                 gMinNum[d] = 0;
00830             }
00831             //-------------------------
00832             // Find the high
00833             while ( gMax < vMax[d] ) {
00834             //while ( gMax < maxOffset ) {
00835                 //++maxMoveStep[d];     // DEBUG
00836                 ++gMaxNum[d];
00837                 gMax += m_atGridDimension[d];
00838             }
00839             if ( gMaxNum[d] >= m_auiGridSize[d] ) {
00840                 gMaxNum[d] = m_auiGridSize[d] - 1;
00841             }
00842             //-----------------------------------
00843             //assert( gMinNum[d] <= gMaxNum[d] );
00844             //assert( 0 <= gMinNum[d] && gMinNum[d] < m_auiGridSize[d] );
00845             //assert( 0 <= gMaxNum[d] && gMaxNum[d] < m_auiGridSize[d] );
00846         }
00847         //-------------------------------------------------
00848         // Set Inside / Outside
00849         //vPos = vertex[ face[i].GetVertexNo(0) ].GetPosition();
00850         vAverage /= face[i].GetNoVertices();
00851         Vector3<T> normal = face[i].GetNormal();
00852         Vector3<T> direction;
00853         for ( int x = gMinNum[0]; x <= gMaxNum[0]; ++x ) {
00854             for ( int y = gMinNum[1]; y <= gMaxNum[1]; ++y ) {
00855                 for ( int z = gMinNum[2]; z <= gMaxNum[2]; ++z ) {
00856                     direction.SetXYZ( 
00857                         m_atVertexPosition[x][y][z][0] - vAverage[0],
00858                         m_atVertexPosition[x][y][z][1] - vAverage[1],
00859                         m_atVertexPosition[x][y][z][2] - vAverage[2]
00860                     );
00861                     //---------------------------
00862                     // If just outside boundary
00863                     if ( m_abVertexFlag1[x][y][z] == RIGHT_OUTSIDE_BOUNDARY ) continue;
00864                     //---------------------------
00865                     // The point is inside, on, or outside the boundary.
00866                     if ( normal * direction < 0.0 ) {
00867                         m_abVertexFlag1[x][y][z] = RIGHT_INSIDE_BOUNDARY;
00868                     }
00869                     else if ( normal * direction == 0.0 ) {
00870                         m_abVertexFlag1[x][y][z] = ON_BOUNDARY;
00871                     }
00872                     else {
00873                         m_abVertexFlag1[x][y][z] = RIGHT_OUTSIDE_BOUNDARY;
00874                     }
00875                 }
00876             }
00877         }
00878         //-------------------------------------------------
00879     }
00880     //---------------------------------------------------------------
00881     // End of Generating Grid by a Software Algorithm
00882 }
00883 //-----------------------------------------------------------------------------
00884 // Set Grid Points To Inside and Outside the Model
00885 // NOT IMPLEMENTED YET!
00886 template <typename T>
00887 void GridGenerator<T>::SetGridPointsToInsideOutsideModelBoundary_SW ( 
00888     OpenGL::HalfEdgeModel<T> * pHEModel )
00889 {
00890     assert( false );
00891     // Find two vertices; (-x,0,0) and (x,0,0)
00892     // and faces that contain them
00893 }
00894 //-----------------------------------------------------------------------------
00895 // Set (Flood-Fill) All Grid Points To Inside and Outside the Model
00896 template <typename T>
00897 void GridGenerator<T>::SetAllGridPointsToInsideOutsideModel_SW ()
00898 {
00899     //*
00900     int val = 0;
00901     for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
00902         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
00903             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
00904                 val = 0;
00905                 if ( m_abVertexFlag1[i][j][k] == UNSET ) {
00906                     if      ( m_abVertexFlag1[i+1][j][k] == RIGHT_OUTSIDE_BOUNDARY )    --val;
00907                     else if ( m_abVertexFlag1[i+1][j][k]  > RIGHT_OUTSIDE_BOUNDARY )    ++val;
00908                     if      ( m_abVertexFlag1[i-1][j][k] == RIGHT_OUTSIDE_BOUNDARY )    --val;
00909                     else if ( m_abVertexFlag1[i-1][j][k]  > RIGHT_OUTSIDE_BOUNDARY )    ++val;
00910                     if      ( m_abVertexFlag1[i][j+1][k] == RIGHT_OUTSIDE_BOUNDARY )    --val;
00911                     else if ( m_abVertexFlag1[i][j+1][k]  > RIGHT_OUTSIDE_BOUNDARY )    ++val;
00912                     if      ( m_abVertexFlag1[i][j-1][k] == RIGHT_OUTSIDE_BOUNDARY )    --val;
00913                     else if ( m_abVertexFlag1[i][j-1][k]  > RIGHT_OUTSIDE_BOUNDARY )    ++val;
00914                     if      ( m_abVertexFlag1[i][j][k+1] == RIGHT_OUTSIDE_BOUNDARY )    --val;
00915                     else if ( m_abVertexFlag1[i][j][k+1]  > RIGHT_OUTSIDE_BOUNDARY )    ++val;
00916                     if      ( m_abVertexFlag1[i][j][k-1] == RIGHT_OUTSIDE_BOUNDARY )    --val;
00917                     else if ( m_abVertexFlag1[i][j][k-1]  > RIGHT_OUTSIDE_BOUNDARY )    ++val;
00918                     if ( val > 0 ) {
00919                         m_abVertexFlag1[i][j][k] = INSIDE_MODEL;
00920                     }
00921                     else {
00922                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
00923                     }
00924                 }
00925             }
00926         }
00927     }
00928     //*/
00929 }
00930 //-----------------------------------------------------------------------------
00931 #endif  // of #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00932 //=============================================================================
00933 
00934 
00935 //=============================================================================
00936 #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00937 //-----------------------------------------------------------------------------
00938 // Set Grid Points To Inside and Outside the Model
00939 template <typename T>
00940 void GridGenerator<T>::SetAllGridPointsToInsideOutsideModel_HW ()
00941 {
00942     if ( !m_pModel )    return;
00943     //---------------------------------------------------------------
00944     OpenGL::PolygonalModel<T> *     pPologonalModel 
00945             = dynamic_cast< OpenGL::PolygonalModel<T> * >( m_pModel );
00946     OpenGL::XPolygonalModel<T> *    pXPologonalModel 
00947             = dynamic_cast< OpenGL::XPolygonalModel<T> * >( m_pModel );
00948     OpenGL::HalfEdgeModel<T> *      pHalfEdgeModel 
00949             = dynamic_cast< OpenGL::HalfEdgeModel<T> * >( m_pModel );
00950     //---------------------------------------------------------------
00951     // For PolygonalModel
00952     if ( pPologonalModel ) {
00953         SetAllGridPointsToInsideOutsideModel_HW( pPologonalModel );
00954     }
00955     //---------------------------------------------------------------
00956     // For XPolygonalModel
00957     if ( pXPologonalModel ) {
00958         SetAllGridPointsToInsideOutsideModel_HW( pXPologonalModel );
00959     }
00960     //---------------------------------------------------------------
00961     // For HalfEdgeModel
00962     if ( pHalfEdgeModel ) {
00963         SetAllGridPointsToInsideOutsideModel_HW( pHalfEdgeModel );
00964     }
00965     //---------------------------------------------------------------
00966 }
00967 //-----------------------------------------------------------------------------
00968 // Set Grid Points To Inside and Outside the Model
00969 // NOT IMPLEMENTED YET!
00970 template <typename T>
00971 void GridGenerator<T>::SetAllGridPointsToInsideOutsideModel_HW ( 
00972     OpenGL::PolygonalModel<T> * pPModel )
00973 {
00974     Vertex<T> * vertexList = pPModel->GetVertexList();
00975     assert( false );
00976     // Find two vertices; (-x,0,0) and (x,0,0)
00977     // and faces that contain them
00978 }
00979 //-----------------------------------------------------------------------------
00980 // Set Grid Points To Inside and Outside the Model
00981 template <typename T>
00982 void GridGenerator<T>::SetAllGridPointsToInsideOutsideModel_HW ( 
00983     OpenGL::XPolygonalModel<T> * pXPModel )
00984 {
00985     //---------------------------------------------------------------
00986     XVertex<T> * vertex = pXPModel->GetVertexList();
00987     Face<T> * face = pXPModel->GetFaceList();
00988     int NumFaces = pXPModel->GetNoFaces();
00989     //---------------------------------------------------------------
00990     // Check draw framebuffer size
00991     GLint bitSize[4];
00992     glGetIntegerv( GL_RED_BITS,   &bitSize[0] );
00993     glGetIntegerv( GL_GREEN_BITS, &bitSize[1] );
00994     glGetIntegerv( GL_BLUE_BITS,  &bitSize[2] );
00995     glGetIntegerv( GL_ALPHA_BITS, &bitSize[3] );
00996     //std::cout << "GL_RED_BITS:   " << bitSize[0] << "\n";
00997     //std::cout << "GL_GREEN_BITS: " << bitSize[1] << "\n";
00998     //std::cout << "GL_BLUE_BITS:  " << bitSize[2] << "\n";
00999     //std::cout << "GL_ALPHA_BITS: " << bitSize[3] << "\n";
01000     GLenum pixelFormat;
01001     GLfloat * drawData;
01002     GLint pixelSize;
01003     if ( bitSize[3] == 0 ) {    // No alpha channel
01004         pixelFormat = GL_RGB;
01005         pixelSize = 3;
01006     }
01007     else {
01008         pixelFormat = GL_RGBA;
01009         pixelSize = 4;
01010     }
01011     GLint startX = 0;
01012     GLint startY = 0;
01013     GLint width  = m_auiGridSize[0];
01014     GLint height = m_auiGridSize[1];
01015     GLenum dataType  = GL_FLOAT;
01016     drawData = new GLfloat[ width * height * pixelSize ];
01017     assert( drawData );
01018 
01019     // DEBUG
01020     //for ( int i = 0; i < width * height * pixelSize; ++i ) {
01021     //  drawData[i] = 1;
01022     //}
01023 
01024     // For each (Z) slice, create a clipping plane with the same 
01025     // position and orientation as the slice.
01026     glPushAttrib( GL_ALL_ATTRIB_BITS );
01027     glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
01028     GLdouble zPlane[4] = { 0.0, 0.0, -1.0, m_tAABBMin[2] };
01029     //-------------------------------------------
01030     // Set drawing area
01031     glMatrixMode( GL_PROJECTION );
01032     glPushMatrix();
01033     glLoadIdentity();
01034     //-------------------------------------------
01036     // Since the sampling position is at the center of the pixel
01037     //
01038     // +--------------------------+
01039     // | 0 | 1 | 2 | 3 | .... |n-1|
01040     // | 0 | 1 | 2 | 3 | .... |n-1|
01041     // | ........................ |
01042     // | ........................ |
01043     // | ........................ |
01044     // | 0 | 1 | 2 | 3 | .... |n-1|
01045     // +--------------------------+
01046     // ^ ^ ^   ^                ^ ^
01047     // | | |   |                | |
01048     // | | --h--                | |
01049     // | ---min & Max of model--- |
01050     // ---min & Max of ortho view--
01051     // h = ((Max - min) of model) / (n-1)
01052     // min of ortho view = min of model - h/2
01053     // Max of ortho view = Max of model + h/2
01055     T h_half[3];
01056     T orthoMin[3], orthoMax[3];
01057     for ( int i = 0; i < 3; ++i ) {
01058         int steps = m_auiGridSize[i] - 1;
01059         if ( steps >= 0 ) {
01060             h_half[i] = ( m_tAABBMax[i] - m_tAABBMin[i] ) / static_cast<T>(steps) / 2.0;
01061         }
01062         else {
01063             h_half[i] = 0;
01064         }
01065         orthoMin[i] = m_tAABBMin[i] - h_half[i];
01066         orthoMax[i] = m_tAABBMax[i] + h_half[i];
01067     }
01069     //glOrtho( m_tAABBMin[0], m_tAABBMax[0], 
01070     //       m_tAABBMin[1], m_tAABBMax[1], 
01071     //       m_tAABBMin[2]-100, m_tAABBMax[2]+100 );
01073     //glOrtho( 
01074     //  orthoMin[0], orthoMax[0], 
01075     //  orthoMin[1], orthoMax[1], 
01076     //  orthoMin[2]-100, orthoMax[2]+100 
01077     //);
01079     T orthoAdjustZ = ( orthoMax[2] - orthoMin[2] );
01080     glOrtho( 
01081         orthoMin[0], orthoMax[0], 
01082         orthoMin[1], orthoMax[1], 
01083         orthoMin[2] - orthoAdjustZ, orthoMax[2] + orthoAdjustZ 
01084     );
01085     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01087     glMatrixMode( GL_MODELVIEW );
01088     glPushMatrix();
01089     glLoadIdentity();
01090     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01091     //-------------------------------------------
01092     glViewport( 0, 0, width, height );
01093     TAPs::OpenGL::Fn::CHECK_GL_ERROR();
01094     //glDisable( GL_TEXTURE_1D );
01095     //glDisable( GL_TEXTURE_2D );
01096     //glDisable( GL_TEXTURE_3D );
01097     //glDisable( GL_BLEND );
01098     glDisable( GL_LIGHTING );
01099     glDisable( GL_DEPTH_TEST );
01100     glDrawBuffer( GL_BACK );
01101     glReadBuffer( GL_BACK );
01102     //-------------------------------------------
01103     for ( int z = 0; z < m_auiGridSize[2]; ++z )
01104     {
01105         //---------------------------------------
01106         // Setup clipping plane for the slice
01107         glClipPlane( GL_CLIP_PLANE0, zPlane );
01108         glEnable( GL_CLIP_PLANE0 );
01109         //---------------------------------------
01110         // Clear the slice with a background color
01111         glClearColor( 0.0, 0.0, 0.0, 0.0 );
01112         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
01113         //---------------------------------------
01114         // Render the back faces of the model in the foreground color
01115         glColor3f( 1.0, 0.0, 0.0 );
01116         glCullFace( GL_FRONT );
01117         glEnable( GL_CULL_FACE );
01118         for ( int i = 0; i < NumFaces; ++i ) {
01119             glBegin( GL_POLYGON );
01120             for ( int n = 0; n < face[i].GetNoVertices(); ++n ) {
01121                 Vector3<T> vPos = vertex[ face[i].GetVertexNo(n) ].GetPosition();
01122                 glVertex3d( vPos[0], vPos[1], vPos[2] );
01123             }
01124             glEnd();
01125         }
01126         //---------------------------------------
01127         // Render the back faces of the model in the background color
01128         glColor3f( 0.0, 0.0, 0.0 );
01129         glCullFace( GL_BACK );
01130         glEnable( GL_CULL_FACE );
01131         for ( int i = 0; i < NumFaces; ++i ) {
01132             glBegin( GL_POLYGON );
01133             for ( int n = 0; n < face[i].GetNoVertices(); ++n ) {
01134                 Vector3<T> vPos = vertex[ face[i].GetVertexNo(n) ].GetPosition();
01135                 glVertex3d( vPos[0], vPos[1], vPos[2] );
01136             }
01137             glEnd();
01138         }
01139         //---------------------------------------
01140         glDisable( GL_CLIP_PLANE0 );
01141         //---------------------------------------
01142         // Copy the data to m_abVertexFlag1
01143         glReadPixels( startX, startY, width, height, pixelFormat, dataType, drawData );
01144         int idx = 0;
01145         for ( int h = 0; h < height; ++h ) {
01146             for ( int w = 0; w < width; ++w ) {
01147                 if ( drawData[idx] > 0.01 ) {
01148                     m_abVertexFlag1[w][h][z] = RIGHT_INSIDE_BOUNDARY;
01149                 }
01150                 else {
01151                     m_abVertexFlag1[w][h][z] = RIGHT_OUTSIDE_BOUNDARY;
01152                 }
01153                 idx += pixelSize;
01154             }
01155         }
01156         //-----------------------------
01157         // Update the clip plane equation
01158         zPlane[3] += m_atGridDimension[2];
01159     } // end of for loop
01160     glPopMatrix();
01161     glMatrixMode( GL_PROJECTION );
01162     glPopMatrix();
01163     glMatrixMode( GL_MODELVIEW );
01164     glPopAttrib();
01165     delete [] drawData;
01166     //---------------------------------------------------------------
01167 }
01168 //-----------------------------------------------------------------------------
01169 // Set Grid Points To Inside and Outside the Model
01170 // NOT IMPLEMENTED YET!
01171 template <typename T>
01172 void GridGenerator<T>::SetAllGridPointsToInsideOutsideModel_HW ( 
01173     OpenGL::HalfEdgeModel<T> * pHEModel )
01174 {
01175     assert( false );
01176     // Find two vertices; (-x,0,0) and (x,0,0)
01177     // and faces that contain them
01178 }
01179 //-----------------------------------------------------------------------------
01180 #endif  // of #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
01181 //=============================================================================
01182 
01183 //=============================================================================
01184 // Clear extra right outside points from the generated grid data
01185 //-----------------------------------------------------------------------------
01186 template <typename T>
01187 void GridGenerator<T>::ClearExtraRightOutsidePoints ()
01188 {
01189     bool bNeedChange;
01190     //===============================================================
01191     // All elements not on bounding box's boundary
01192     //---------------------------------------------------------------
01193     for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01194         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01195             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01196                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01197                     bNeedChange = true;
01198                     //-----------------
01199                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01200                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01201                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01202                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01203                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01204                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01205                     //-----------------
01206                     if ( bNeedChange ) {
01207                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01208                     }
01209                 }
01210             }
01211         }
01212     }
01213     //---------------------------------------------------------------
01214     // END INSIDE BOUNDARY
01215     //===============================================================
01216 
01217     //===============================================================
01218     // All elements on the RIGHT boundary of bounding box
01219     //---------------------------------------------------------------
01220     for ( int i = m_auiGridSize[0]-1; i <= m_auiGridSize[0]-1; ++i ) {
01221         //-------------------------------------------------
01222         // Inside the RIGHT boundary
01223         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01224             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01225                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01226                     bNeedChange = true;
01227                     //-----------------
01228                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01229                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01230                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01231                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01232                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01233                     //else if   ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01234                     //-----------------
01235                     if ( bNeedChange ) {
01236                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01237                     }
01238                 }
01239             }
01240         }
01241         //-------------------------------------------------
01242         // Top row of the RIGHT boundary, except cornors
01243         for ( int j = m_auiGridSize[1]-1; j <= m_auiGridSize[1]-1; ++j ) {
01244             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01245                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01246                     bNeedChange = true;
01247                     //-----------------
01248                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01249                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01250                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01251                     //else if   ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01252                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01253                     //else if   ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01254                     //-----------------
01255                     if ( bNeedChange ) {
01256                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01257                     }
01258                 }
01259             }
01260         }
01261         //-------------------------------------------------
01262         // Bottom row of the RIGHT boundary, except cornors
01263         for ( int j = 0; j <= 0; ++j ) {
01264             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01265                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01266                     bNeedChange = true;
01267                     //-----------------
01268                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01269                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01270                     //else if   ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01271                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01272                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01273                     //else if   ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01274                     //-----------------
01275                     if ( bNeedChange ) {
01276                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01277                     }
01278                 }
01279             }
01280         }
01281         //-------------------------------------------------
01282         // Right-most column of the RIGHT boundary, except cornors
01283         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01284             for ( int k = m_auiGridSize[2]-1; k <= m_auiGridSize[2]-1; ++k ) {
01285                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01286                     bNeedChange = true;
01287                     //-----------------
01288                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01289                     //else if   ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01290                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01291                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01292                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01293                     //else if   ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01294                     //-----------------
01295                     if ( bNeedChange ) {
01296                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01297                     }
01298                 }
01299             }
01300         }
01301         //-------------------------------------------------
01302         // Left-most column of the RIGHT boundary, except cornors
01303         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01304             for ( int k = 0; k <= 0; ++k ) {
01305                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01306                     bNeedChange = true;
01307                     //-----------------
01308                     //if        ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01309                     if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01310                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01311                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01312                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01313                     //else if   ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01314                     //-----------------
01315                     if ( bNeedChange ) {
01316                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01317                     }
01318                 }
01319             }
01320         }
01321     }
01322     //---------------------------------------------------------------
01323     // END RIGHT BOUNDARY
01324     //===============================================================
01325 
01326     //===============================================================
01327     // All elements on the LEFT boundary of bounding box
01328     //---------------------------------------------------------------
01329     for ( int i = 0; i <= 0; ++i ) {
01330         //-------------------------------------------------
01331         // Inside the LEFT boundary
01332         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01333             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01334                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01335                     bNeedChange = true;
01336                     //-----------------
01337                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01338                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01339                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01340                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01341                     //else if   ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01342                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01343                     //-----------------
01344                     if ( bNeedChange ) {
01345                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01346                     }
01347                 }
01348             }
01349         }
01350         //-------------------------------------------------
01351         // Top row of the LEFT boundary, except cornors
01352         for ( int j = m_auiGridSize[1]-1; j <= m_auiGridSize[1]-1; ++j ) {
01353             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01354                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01355                     bNeedChange = true;
01356                     //-----------------
01357                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01358                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01359                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01360                     //else if   ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01361                     //else if   ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01362                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01363                     //-----------------
01364                     if ( bNeedChange ) {
01365                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01366                     }
01367                 }
01368             }
01369         }
01370         //-------------------------------------------------
01371         // Bottom row of the LEFT boundary, except cornors
01372         for ( int j = 0; j <= 0; ++j ) {
01373             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01374                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01375                     bNeedChange = true;
01376                     //-----------------
01377                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01378                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01379                     //else if   ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01380                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01381                     //else if   ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01382                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01383                     //-----------------
01384                     if ( bNeedChange ) {
01385                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01386                     }
01387                 }
01388             }
01389         }
01390         //-------------------------------------------------
01391         // Right-most column of the LEFT boundary, except cornors
01392         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01393             for ( int k = m_auiGridSize[2]-1; k <= m_auiGridSize[2]-1; ++k ) {
01394                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01395                     bNeedChange = true;
01396                     //-----------------
01397                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01398                     //else if   ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01399                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01400                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01401                     //else if   ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01402                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01403                     //-----------------
01404                     if ( bNeedChange ) {
01405                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01406                     }
01407                 }
01408             }
01409         }
01410         //-------------------------------------------------
01411         // Left-most column of the LEFT boundary, except cornors
01412         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01413             for ( int k = 0; k <= 0; ++k ) {
01414                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01415                     bNeedChange = true;
01416                     //-----------------
01417                     //if        ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01418                     if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01419                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01420                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01421                     //else if   ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01422                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01423                     //-----------------
01424                     if ( bNeedChange ) {
01425                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01426                     }
01427                 }
01428             }
01429         }
01430     }
01431     //---------------------------------------------------------------
01432     // END LEFT BOUNDARY
01433     //===============================================================
01434 
01435     //===============================================================
01436     // All elements on the TOP boundary of bounding box
01437     //---------------------------------------------------------------
01438     for ( int j = m_auiGridSize[1]-1; j <= m_auiGridSize[1]-1; ++j ) {
01439         //-------------------------------------------------
01440         // Inside the TOP boundary
01441         for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01442             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01443                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01444                     bNeedChange = true;
01445                     //-----------------
01446                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01447                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01448                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01449                     //else if   ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01450                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01451                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01452                     //-----------------
01453                     if ( bNeedChange ) {
01454                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01455                     }
01456                 }
01457             }
01458         }
01459         //-------------------------------------------------
01460         // Top row of the TOP boundary, except cornors
01461         for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01462             for ( int k = 0; k <= 0; ++k ) {
01463                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01464                     bNeedChange = true;
01465                     //-----------------
01466                     //if        ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01467                     if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01468                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01469                     //else if   ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01470                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01471                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01472                     //-----------------
01473                     if ( bNeedChange ) {
01474                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01475                     }
01476                 }
01477             }
01478         }
01479         //-------------------------------------------------
01480         // Bottom row of the TOP boundary, except cornors
01481         for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01482             for ( int k = m_auiGridSize[2]-1; k <= m_auiGridSize[2]-1; ++k ) {
01483                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01484                     bNeedChange = true;
01485                     //-----------------
01486                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01487                     //else if   ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01488                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01489                     //else if   ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01490                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01491                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01492                     //-----------------
01493                     if ( bNeedChange ) {
01494                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01495                     }
01496                 }
01497             }
01498         }
01499     }
01500     //---------------------------------------------------------------
01501     // END TOP BOUNDARY
01502     //===============================================================
01503 
01504     //===============================================================
01505     // All elements on the BOTTOM boundary of bounding box
01506     //---------------------------------------------------------------
01507     for ( int j = 0; j <= 0; ++j ) {
01508         //-------------------------------------------------
01509         // Inside the BOTTOM boundary
01510         for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01511             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01512                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01513                     bNeedChange = true;
01514                     //-----------------
01515                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01516                     else if ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01517                     //else if   ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01518                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01519                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01520                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01521                     //-----------------
01522                     if ( bNeedChange ) {
01523                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01524                     }
01525                 }
01526             }
01527         }
01528         //-------------------------------------------------
01529         // Top row of the BOTTOM boundary, except cornors
01530         for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01531             for ( int k = 0; k <= 0; ++k ) {
01532                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01533                     bNeedChange = true;
01534                     //-----------------
01535                     //if        ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01536                     if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01537                     //else if   ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01538                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01539                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01540                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01541                     //-----------------
01542                     if ( bNeedChange ) {
01543                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01544                     }
01545                 }
01546             }
01547         }
01548         //-------------------------------------------------
01549         // Bottom row of the BOTTOM boundary, except cornors
01550         for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01551             for ( int k = m_auiGridSize[2]-1; k <= m_auiGridSize[2]-1; ++k ) {
01552                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01553                     bNeedChange = true;
01554                     //-----------------
01555                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01556                     //else if   ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01557                     //else if   ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01558                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01559                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01560                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01561                     //-----------------
01562                     if ( bNeedChange ) {
01563                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01564                     }
01565                 }
01566             }
01567         }
01568     }
01569     //---------------------------------------------------------------
01570     // END BOTTOM BOUNDARY
01571     //===============================================================
01572 
01573     //===============================================================
01574     // All elements on the FRONT boundary of bounding box
01575     //---------------------------------------------------------------
01576     for ( int k = m_auiGridSize[2]-1; k <= m_auiGridSize[2]-1; ++k ) {
01577         //-------------------------------------------------
01578         // Inside the FRONT boundary
01579         for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01580             for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01581                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01582                     bNeedChange = true;
01583                     //-----------------
01584                     if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01585                     //else if   ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01586                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01587                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01588                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01589                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01590                     //-----------------
01591                     if ( bNeedChange ) {
01592                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01593                     }
01594                 }
01595             }
01596         }
01597     }
01598     //---------------------------------------------------------------
01599     // END FRONT BOUNDARY
01600     //===============================================================
01601 
01602     //===============================================================
01603     // All elements on the BACK boundary of bounding box
01604     //---------------------------------------------------------------
01605     for ( int k = 0; k <= 0; ++k ) {
01606         //-------------------------------------------------
01607         // Inside the BACK boundary
01608         for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01609             for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01610                 if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
01611                     bNeedChange = true;
01612                     //-----------------
01613                     //if        ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01614                     if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01615                     else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01616                     else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01617                     else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01618                     else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01619                     //-----------------
01620                     if ( bNeedChange ) {
01621                         m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01622                     }
01623                 }
01624             }
01625         }
01626     }
01627     //---------------------------------------------------------------
01628     // END BACK BOUNDARY
01629     //===============================================================
01630 
01631     //===============================================================
01632     // All of the eight corner points of the boundary of bounding box
01633     //---------------------------------------------------------------
01634     //    ^ j
01635     //    |
01636     //    |
01637     //    O----->i
01638     //   /
01639     //  /
01640     // k
01641     {
01642         //---------------------------------------
01643         int i = 0, j = 0, k = 0;
01644         bNeedChange = true;
01645         //-----------------
01646         if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01647         else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01648         else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01649         //-----------------
01650         if ( bNeedChange ) {
01651             m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01652         }
01653         //---------------------------------------
01654         k = m_auiGridSize[2]-1;
01655         bNeedChange = true;
01656         //-----------------
01657         if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01658         else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01659         else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01660         //-----------------
01661         if ( bNeedChange ) {
01662             m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01663         }
01664         //---------------------------------------
01665         j = m_auiGridSize[1]-1;
01666         bNeedChange = true;
01667         //-----------------
01668         if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01669         else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01670         else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01671         //-----------------
01672         if ( bNeedChange ) {
01673             m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01674         }
01675         //---------------------------------------
01676         k = 0;
01677         bNeedChange = true;
01678         //-----------------
01679         if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01680         else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01681         else if ( m_abVertexFlag1[i+1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01682         //-----------------
01683         if ( bNeedChange ) {
01684             m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01685         }
01686         //---------------------------------------
01687         i = m_auiGridSize[0]-1;
01688         j = 0;
01689         bNeedChange = true;
01690         //-----------------
01691         if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01692         else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01693         else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01694         //-----------------
01695         if ( bNeedChange ) {
01696             m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01697         }
01698         //---------------------------------------
01699         k = m_auiGridSize[2]-1;
01700         bNeedChange = true;
01701         //-----------------
01702         if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01703         else if ( m_abVertexFlag1[i][j+1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01704         else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01705         //-----------------
01706         if ( bNeedChange ) {
01707             m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01708         }
01709         //---------------------------------------
01710         j = m_auiGridSize[1]-1;
01711         bNeedChange = true;
01712         //-----------------
01713         if      ( m_abVertexFlag1[i][j][k-1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01714         else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01715         else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01716         //-----------------
01717         if ( bNeedChange ) {
01718             m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01719         }
01720         //---------------------------------------
01721         k = 0;
01722         bNeedChange = true;
01723         //-----------------
01724         if      ( m_abVertexFlag1[i][j][k+1] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01725         else if ( m_abVertexFlag1[i][j-1][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01726         else if ( m_abVertexFlag1[i-1][j][k] > RIGHT_OUTSIDE_BOUNDARY )     bNeedChange = false;
01727         //-----------------
01728         if ( bNeedChange ) {
01729             m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01730         }
01731         //---------------------------------------
01732     }
01733     //---------------------------------------------------------------
01734     // END THE EIGHT CORNERS BOUNDARY
01735     //===============================================================
01736 }
01737 //-----------------------------------------------------------------------------
01738 // END: ClearExtraRightOutsidePoints ()
01739 //=============================================================================
01740 
01741 //=============================================================================
01742 // Inverse Points -- Outside <--> Inside & Empty becomes Inside
01743 //-----------------------------------------------------------------------------
01744 template <typename T>
01745 void GridGenerator<T>::InverseInsideOutside ()
01746 {
01747     //===============================================================
01748     // All elements not on bounding box's boundary
01749     //---------------------------------------------------------------
01750     for ( int i = 1; i < m_auiGridSize[0]-1; ++i ) {
01751         for ( int j = 1; j < m_auiGridSize[1]-1; ++j ) {
01752             for ( int k = 1; k < m_auiGridSize[2]-1; ++k ) {
01753                 if      ( m_abVertexFlag1[i][j][k] == UNSET )                   m_abVertexFlag1[i][j][k] = INSIDE_MODEL;
01754                 else if ( m_abVertexFlag1[i][j][k] == OUTSIDE_MODEL )           m_abVertexFlag1[i][j][k] = INSIDE_MODEL;
01755                 //else if   ( m_abVertexFlag1[i][j][k] == ON_BOUNDARY )         m_abVertexFlag1[i][j][k] = ON_BOUNDARY;
01756                 else if ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY )  m_abVertexFlag1[i][j][k] = RIGHT_INSIDE_BOUNDARY;
01757                 else if ( m_abVertexFlag1[i][j][k] == RIGHT_INSIDE_BOUNDARY )   m_abVertexFlag1[i][j][k] = RIGHT_OUTSIDE_BOUNDARY;
01758                 else if ( m_abVertexFlag1[i][j][k] == INSIDE_MODEL )            m_abVertexFlag1[i][j][k] = OUTSIDE_MODEL;
01759             }
01760         }
01761     }
01762 }
01763 //-----------------------------------------------------------------------------
01764 // END: InverseInsideOutside ()
01765 //=============================================================================
01766 
01767 //=============================================================================
01768 // START the group functions of SetVertexInterpolationData
01769 //-----------------------------------------------------------------------------
01770 template <typename T>
01771 void GridGenerator<T>::SetVertexInterpolationData ( T tDefaultVal )
01772 {
01773     if ( !m_pModel )    return;
01774     //---------------------------------------------------------------
01775     // Set global default interpolation value
01776     m_bUseDefaultIntpVal = true;
01777     m_tDefaultIntpVal = tDefaultVal;
01778     //---------------------------------------------------------------
01779     OpenGL::PolygonalModel<T> *     pPologonalModel 
01780             = dynamic_cast< OpenGL::PolygonalModel<T> * >( m_pModel );
01781     OpenGL::XPolygonalModel<T> *    pXPologonalModel 
01782             = dynamic_cast< OpenGL::XPolygonalModel<T> * >( m_pModel );
01783     OpenGL::HalfEdgeModel<T> *      pHalfEdgeModel 
01784             = dynamic_cast< OpenGL::HalfEdgeModel<T> * >( m_pModel );
01785     //---------------------------------------------------------------
01786     // For PolygonalModel
01787     if ( pPologonalModel ) {
01788         SetVertexInterpolationData( pPologonalModel );
01789     }
01790     //---------------------------------------------------------------
01791     // For XPolygonalModel
01792     if ( pXPologonalModel ) {
01793         SetVertexInterpolationData( pXPologonalModel );
01794     }
01795     //---------------------------------------------------------------
01796     // For HalfEdgeModel
01797     if ( pHalfEdgeModel ) {
01798         SetVertexInterpolationData( pHalfEdgeModel );
01799     }
01800     //---------------------------------------------------------------
01801 }
01802 
01803 
01804 //=============================================================================
01805 // START the group functions of SetVertexInterpolationData
01806 //-----------------------------------------------------------------------------
01807 template <typename T>
01808 void GridGenerator<T>::SetVertexInterpolationData ()
01809 {
01810     if ( !m_pModel )    return;
01811     //---------------------------------------------------------------
01812     OpenGL::PolygonalModel<T> *     pPologonalModel 
01813             = dynamic_cast< OpenGL::PolygonalModel<T> * >( m_pModel );
01814     OpenGL::XPolygonalModel<T> *    pXPologonalModel 
01815             = dynamic_cast< OpenGL::XPolygonalModel<T> * >( m_pModel );
01816     OpenGL::HalfEdgeModel<T> *      pHalfEdgeModel 
01817             = dynamic_cast< OpenGL::HalfEdgeModel<T> * >( m_pModel );
01818     //---------------------------------------------------------------
01819     // For PolygonalModel
01820     if ( pPologonalModel ) {
01821         SetVertexInterpolationData( pPologonalModel );
01822     }
01823     //---------------------------------------------------------------
01824     // For XPolygonalModel
01825     if ( pXPologonalModel ) {
01826         SetVertexInterpolationData( pXPologonalModel );
01827     }
01828     //---------------------------------------------------------------
01829     // For HalfEdgeModel
01830     if ( pHalfEdgeModel ) {
01831         SetVertexInterpolationData( pHalfEdgeModel );
01832     }
01833     //---------------------------------------------------------------
01834 }
01835 //-----------------------------------------------------------------------------
01836 // Set Grid Points To Inside and Outside the Model
01837 template <typename T>
01838 void GridGenerator<T>::SetVertexInterpolationData ( 
01839     OpenGL::PolygonalModel<T> * pPModel )
01840 {
01841     Vertex<T> * vertexList = pPModel->GetVertexList();
01842     assert( false );
01843     // Find two vertices; (-x,0,0) and (x,0,0)
01844     // and faces that contain them
01845 }
01846 //-----------------------------------------------------------------------------
01847 // Set Grid Points To Inside and Outside the Model
01848 template <typename T>
01849 void GridGenerator<T>::SetVertexInterpolationData ( 
01850     OpenGL::XPolygonalModel<T> * pXPModel )
01851 {
01862     //---------------------------------------------------------------
01863     // Check if "use default interpolation value" is set
01864     if ( m_bUseDefaultIntpVal ) {
01865         // Set all interpolation data to m_tDefaultIntpVal
01866         for ( int x = 0; x < m_auiGridSize[0]; ++x ) {
01867             for ( int y = 0; y < m_auiGridSize[1]; ++y ) {
01868                 for ( int z = 0; z < m_auiGridSize[2]; ++z ) {
01869                     m_atVertexInterpolation[x][y][z][0] = 
01870                     m_atVertexInterpolation[x][y][z][1] = 
01871                     m_atVertexInterpolation[x][y][z][2] = m_tDefaultIntpVal;
01872                 }
01873             }
01874         }
01875         return;
01876     }
01877     else {
01878         // Set all interpolation data to random values
01879         srand( time(NULL) );
01880         for ( int x = 0; x < m_auiGridSize[0]; ++x ) {
01881             for ( int y = 0; y < m_auiGridSize[1]; ++y ) {
01882                 for ( int z = 0; z < m_auiGridSize[2]; ++z ) {
01883                     m_atVertexInterpolation[x][y][z][0] = 
01884                     m_atVertexInterpolation[x][y][z][1] = 
01885                     m_atVertexInterpolation[x][y][z][2] = 0.49;
01886 
01887                         //0.25 + static_cast<T>( rand() / static_cast<T>( RAND_MAX * 2 ) ); // range of [0.25-0.75]
01888 
01889                         //static_cast<T>( rand() / static_cast<T>( RAND_MAX ) );            // range of [0.00-0.99]
01890                 }
01891             }
01892         }
01893     }
01894 
01895     // DEBUG
01896     //return;       // No finding interpolation values for the surface
01897 
01898     //---------------------------------------------------------------
01899     XVertex<T> * vertex = pXPModel->GetVertexList();
01900     Face<T> * face = pXPModel->GetFaceList();
01901     int NumFaces = pXPModel->GetNoFaces();
01902     //---------------------------------------------------------------
01903     // Find interpolation values for the surface by a Software Algorithm
01904     //-----------------------------------------------------
01905     Vector3<T> vMin, vMax, vPos, vAverage;
01906     //-----------------------------------------------------
01907     // For Each Face
01908     for ( int i = 0; i < NumFaces; ++i ) {
01909         //---------------------------------------
01910         // Find a box that contain this face
01911         vAverage = vMin = vMax = vertex[ face[i].GetVertexNo(0) ].GetPosition();
01912         for ( int n = 1; n < face[i].GetNoVertices(); ++n ) {
01913             vPos = vertex[ face[i].GetVertexNo(n) ].GetPosition();
01914             vAverage += vPos;
01915             for ( int d = 0; d < 3; ++d ) {
01916                 if      ( vMin[d] > vPos[d] )   vMin[d] = vPos[d];
01917                 else if ( vMax[d] < vPos[d] )   vMax[d] = vPos[d];
01918             }
01919         }
01920         //assert( vMin[0] <= vMax[0] );
01921         //assert( vMin[1] <= vMax[1] );
01922         //assert( vMin[2] <= vMax[2] );
01923         //---------------------------------------
01924         // Find a box of grids that contain this face
01925         // i.e. a bigger box that contain the face box
01926         int gMinNum[3], gMaxNum[3];
01927         T gMin, gMax;
01928         for ( int d = 0; d < 3; ++d ) {
01929             int step = m_auiGridSize[d] / 2;
01930             gMinNum[d] = gMaxNum[d] = step;
01931             gMin = m_tAABBMin[d] + gMinNum[d] * m_atGridDimension[d];
01932             T minOffset = vMin[d] - m_atGridDimension[d] / 2;
01933             T maxOffset = vMax[d] + m_atGridDimension[d] / 2;
01934             //T minOffset = vMin[d];
01935             //T maxOffset = vMax[d];
01936             //-------------------------
01937             // Find a box of this current face
01938             while ( ( gMin < minOffset || maxOffset < gMin ) && step > 1 ) {
01939                 step /= 2;
01940                 if ( gMin < minOffset ) {
01941                     gMinNum[d] += step;
01942                 }
01943                 else {
01944                     gMinNum[d] -= step;
01945                 }
01946                 gMin = m_tAABBMin[d] + gMinNum[d] * m_atGridDimension[d];
01947             }
01948             gMax = gMin;
01949             gMaxNum[d] = gMinNum[d];
01950 
01951             // DEBUG
01952             //gridBoxCenterPt[d] = gMinNum[d];
01953 
01954             //-------------------------
01955             // Find a grid box that covers the box of this current face
01956             //minMoveStep[d] = maxMoveStep[d] = 0;      // DEBUG
01957             //-------------------------
01958             // Find the low
01959             while ( vMin[d] < gMin ) {
01960             //while ( minOffset < gMin ) {
01961                 //--minMoveStep[d];     // DEBUG
01962                 --gMinNum[d];
01963                 gMin -= m_atGridDimension[d];
01964             }
01965             if ( gMinNum[d] < 0 ) {
01966                 gMinNum[d] = 0;
01967             }
01968             //-------------------------
01969             // Find the high
01970             while ( gMax < vMax[d] ) {
01971             //while ( gMax < maxOffset ) {
01972                 //++maxMoveStep[d];     // DEBUG
01973                 ++gMaxNum[d];
01974                 gMax += m_atGridDimension[d];
01975             }
01976             if ( gMaxNum[d] >= m_auiGridSize[d] ) {
01977                 gMaxNum[d] = m_auiGridSize[d] - 1;
01978             }
01979             //-----------------------------------
01980             //assert( gMinNum[d] <= gMaxNum[d] );
01981             //assert( 0 <= gMinNum[d] && gMinNum[d] < m_auiGridSize[d] );
01982             //assert( 0 <= gMaxNum[d] && gMaxNum[d] < m_auiGridSize[d] );
01983         }
01984         //-------------------------------------------------
01985         // Set Inside / Outside
01986         //vPos = vertex[ face[i].GetVertexNo(0) ].GetPosition();
01987         vAverage /= face[i].GetNoVertices();
01988         Vector3<T> normal = face[i].GetNormal();
01989         Vector3<T> direction;
01990         for ( int x = gMinNum[0]; x <= gMaxNum[0]; ++x ) {
01991             for ( int y = gMinNum[1]; y <= gMaxNum[1]; ++y ) {
01992                 for ( int z = gMinNum[2]; z <= gMaxNum[2]; ++z ) {
01993 
01994                     /*
01995                     // RANDOM VALUES
01996                     T interpolationVal = static_cast<T>( rand() / static_cast<T>( RAND_MAX ) );
01997                     m_atVertexInterpolation[x][y][z][0] = ( interpolationVal - 0.5 ) / 2.0 + 0.5;
01998                     interpolationVal = static_cast<T>( rand() / static_cast<T>( RAND_MAX ) );
01999                     m_atVertexInterpolation[x][y][z][1] = ( interpolationVal - 0.5 ) / 2.0 + 0.5;
02000                     interpolationVal = static_cast<T>( rand() / static_cast<T>( RAND_MAX ) );
02001                     m_atVertexInterpolation[x][y][z][2] = ( interpolationVal - 0.5 ) / 2.0 + 0.5;
02002                     //*/
02003 
02004 
02005                     //*
02006                     // OFFSET FROM TRIANGLE SURFACE
02007                     if ( m_abVertexFlag1[x][y][z] > RIGHT_OUTSIDE_BOUNDARY ) {
02008                         T ratio;
02009                         Vector3<T> projPt;
02010                         //---------------------------------
02011                         // x-direction
02012                         bool result = false;
02013                         if ( x >= 0 ) { 
02014                             if ( m_abVertexFlag1[x+1][y][z] == RIGHT_OUTSIDE_BOUNDARY ) {
02015                                 result = CGMath<T>::FindIntersectionLineSegmentTriangle( 
02016                                             m_atVertexPosition[x][y][z], 
02017                                             m_atVertexPosition[x+1][y][z], 
02018                                             vertex[ face[i].GetVertexNo(0) ].GetPosition(), 
02019                                             vertex[ face[i].GetVertexNo(1) ].GetPosition(), 
02020                                             vertex[ face[i].GetVertexNo(2) ].GetPosition(), 
02021                                             ratio, projPt                           // O/P:
02022                                         );
02023                             }
02024                             if ( result ) {
02025                                 m_atVertexInterpolation[x][y][z][0]   = ratio;
02026                                 //m_atVertexInterpolation[x+1][y][z][0] = 1.0 - ratio;
02027                             }
02028                         }
02029                         if ( x < m_auiGridSize[0] ) {
02030                             if ( m_abVertexFlag1[x-1][y][z] == RIGHT_OUTSIDE_BOUNDARY ) {
02031                                 result = CGMath<T>::FindIntersectionLineSegmentTriangle( 
02032                                             m_atVertexPosition[x][y][z], 
02033                                             m_atVertexPosition[x-1][y][z], 
02034                                             vertex[ face[i].GetVertexNo(0) ].GetPosition(), 
02035                                             vertex[ face[i].GetVertexNo(1) ].GetPosition(), 
02036                                             vertex[ face[i].GetVertexNo(2) ].GetPosition(), 
02037                                             ratio, projPt                           // O/P:
02038                                         );
02039                             }
02040                             if ( result ) {
02041                                 m_atVertexInterpolation[x][y][z][0]   = ratio;
02042                                 //m_atVertexInterpolation[x-1][y][z][0] = 1.0 - ratio;
02043                             }
02044                         }
02045                         //---------------------------------
02046                         // y-direction
02047                         result = false;
02048                         if ( y >= 0 ) { 
02049                             if ( m_abVertexFlag1[x][y+1][z] == RIGHT_OUTSIDE_BOUNDARY ) {
02050                                 result = CGMath<T>::FindIntersectionLineSegmentTriangle( 
02051                                             m_atVertexPosition[x][y][z], 
02052                                             m_atVertexPosition[x][y+1][z], 
02053                                             vertex[ face[i].GetVertexNo(0) ].GetPosition(), 
02054                                             vertex[ face[i].GetVertexNo(1) ].GetPosition(), 
02055                                             vertex[ face[i].GetVertexNo(2) ].GetPosition(), 
02056                                             ratio, projPt                           // O/P:
02057                                         );
02058                             }
02059                             if ( result ) {
02060                                 m_atVertexInterpolation[x][y][z][1]   = ratio;
02061                                 //m_atVertexInterpolation[x][y+1][z][1] = 1.0 - ratio;
02062                             }
02063                         }
02064                         if ( y < m_auiGridSize[1] ) {
02065                             if ( m_abVertexFlag1[x][y-1][z] == RIGHT_OUTSIDE_BOUNDARY ) {
02066                                 result = CGMath<T>::FindIntersectionLineSegmentTriangle( 
02067                                             m_atVertexPosition[x][y][z], 
02068                                             m_atVertexPosition[x][y-1][z], 
02069                                             vertex[ face[i].GetVertexNo(0) ].GetPosition(), 
02070                                             vertex[ face[i].GetVertexNo(1) ].GetPosition(), 
02071                                             vertex[ face[i].GetVertexNo(2) ].GetPosition(), 
02072                                             ratio, projPt                           // O/P:
02073                                         );
02074                             }
02075                             if ( result ) {
02076                                 m_atVertexInterpolation[x][y][z][1]   = ratio;
02077                                 //m_atVertexInterpolation[x][y-1][z][1] = 1.0 - ratio;
02078                             }
02079                         }
02080                         //---------------------------------
02081                         // z-direction
02082                         result = false;
02083                         if ( z >= 0 ) { 
02084                             if ( m_abVertexFlag1[x][y][z+1] == RIGHT_OUTSIDE_BOUNDARY ) {
02085                                 result = CGMath<T>::FindIntersectionLineSegmentTriangle( 
02086                                             m_atVertexPosition[x][y][z], 
02087                                             m_atVertexPosition[x][y][z+1], 
02088                                             vertex[ face[i].GetVertexNo(0) ].GetPosition(), 
02089                                             vertex[ face[i].GetVertexNo(1) ].GetPosition(), 
02090                                             vertex[ face[i].GetVertexNo(2) ].GetPosition(), 
02091                                             ratio, projPt                           // O/P:
02092                                         );
02093                             }
02094                             if ( result ) {
02095                                 m_atVertexInterpolation[x][y][z][2]   = ratio;
02096                                 //m_atVertexInterpolation[x][y][z+1][2] = 1.0 - ratio;
02097                             }
02098                         }
02099                         if ( z < m_auiGridSize[2] ) {
02100                             if ( m_abVertexFlag1[x][y][z-1] == RIGHT_OUTSIDE_BOUNDARY ) {
02101                                 result = CGMath<T>::FindIntersectionLineSegmentTriangle( 
02102                                             m_atVertexPosition[x][y][z], 
02103                                             m_atVertexPosition[x][y][z-1], 
02104                                             vertex[ face[i].GetVertexNo(0) ].GetPosition(), 
02105                                             vertex[ face[i].GetVertexNo(1) ].GetPosition(), 
02106                                             vertex[ face[i].GetVertexNo(2) ].GetPosition(), 
02107                                             ratio, projPt                           // O/P:
02108                                         );
02109                             }
02110                             if ( result ) {
02111                                 m_atVertexInterpolation[x][y][z][2]   = ratio;
02112                                 //m_atVertexInterpolation[x][y][z-1][2] = 1.0 - ratio;
02113                             }
02114                         }
02115                         //---------------------------------
02116                     }
02117                     //*/
02118 
02119                     /*
02120                     direction.SetXYZ( 
02121                         m_atVertexPosition[x][y][z][0] - vAverage[0],
02122                         m_atVertexPosition[x][y][z][1] - vAverage[1],
02123                         m_atVertexPosition[x][y][z][2] - vAverage[2]
02124                     );
02125                     //---------------------------
02126                     // If just outside boundary
02127                     if ( m_abVertexFlag1[x][y][z] == RIGHT_OUTSIDE_BOUNDARY ) continue;
02128                     //---------------------------
02129                     // The point is inside, on, or outside the boundary.
02130                     if ( normal * direction < 0.0 ) {
02131                         m_abVertexFlag1[x][y][z] = RIGHT_INSIDE_BOUNDARY;
02132                     }
02133                     else if ( normal * direction == 0.0 ) {
02134                         m_abVertexFlag1[x][y][z] = ON_BOUNDARY;
02135                     }
02136                     else {
02137                         m_abVertexFlag1[x][y][z] = RIGHT_OUTSIDE_BOUNDARY;
02138                     }
02139                     //*/
02140                 }
02141             }
02142         }
02143         //-------------------------------------------------
02144     }
02145     //---------------------------------------------------------------
02146 }
02147 //-----------------------------------------------------------------------------
02148 // Set Grid Points To Inside and Outside the Model
02149 // NOT IMPLEMENTED YET!
02150 template <typename T>
02151 void GridGenerator<T>::SetVertexInterpolationData ( 
02152     OpenGL::HalfEdgeModel<T> * pHEModel )
02153 {
02154     assert( false );
02155     // Find two vertices; (-x,0,0) and (x,0,0)
02156     // and faces that contain them
02157 }
02158 //-----------------------------------------------------------------------------
02159 // END the group functions of SetVertexInterpolationData
02160 //=============================================================================
02161 
02162 //-----------------------------------------------------------------------------
02163 // Delete Grids
02164 template <typename T>
02165 void GridGenerator<T>::DeleteGrids ()
02166 {
02167     DeleteGridsForPosition();
02168     DeleteGridsForNormal();
02169     DeleteGridsForFlag1();
02170     DeleteGridsForVertexInterpolation();
02171 }
02172 //-----------------------------------------------------------------------------
02173 // Delete Position Grids
02174 template <typename T>
02175 void GridGenerator<T>::DeleteGridsForPosition ()
02176 {
02177     //---------------------------------------------------------------
02178     if ( m_atVertexPosition ) {
02179         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02180             for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02181                 for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
02182                     delete [] m_atVertexPosition[i][j][k];
02183                 }
02184                 delete [] m_atVertexPosition[i][j];
02185             }
02186             delete [] m_atVertexPosition[i];
02187         }
02188         delete m_atVertexPosition;
02189         m_atVertexPosition = NULL;
02190     }
02191 }
02192 //-----------------------------------------------------------------------------
02193 // Delete Normal Grids
02194 template <typename T>
02195 void GridGenerator<T>::DeleteGridsForNormal ()
02196 {
02197     //---------------------------------------------------------------
02198     if ( m_atVertexNormal ) {
02199         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02200             for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02201                 for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
02202                     delete [] m_atVertexNormal[i][j][k];
02203                 }
02204                 delete [] m_atVertexNormal[i][j];
02205             }
02206             delete [] m_atVertexNormal[i];
02207         }
02208         delete m_atVertexNormal;
02209         m_atVertexNormal = NULL;
02210     }
02211 }
02212 //-----------------------------------------------------------------------------
02213 // Delete Flag1 Grids
02214 template <typename T>
02215 void GridGenerator<T>::DeleteGridsForFlag1 ()
02216 {
02217     //---------------------------------------------------------------
02218     if ( m_abVertexFlag1 ) {
02219         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02220             for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02221                 delete [] m_abVertexFlag1[i][j];
02222             }
02223             delete [] m_abVertexFlag1[i];
02224         }
02225         delete m_abVertexFlag1;
02226         m_abVertexFlag1 = NULL;
02227     }
02228 }
02229 //-----------------------------------------------------------------------------
02230 // Delete Vertex Interpolation Grids
02231 template <typename T>
02232 void GridGenerator<T>::DeleteGridsForVertexInterpolation ()
02233 {
02234     //---------------------------------------------------------------
02235     if ( m_atVertexInterpolation ) {
02236         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02237             for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02238                 for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
02239                     delete [] m_atVertexInterpolation[i][j][k];
02240                 }
02241                 delete [] m_atVertexInterpolation[i][j];
02242             }
02243             delete [] m_atVertexInterpolation[i];
02244         }
02245         delete m_atVertexInterpolation;
02246         m_atVertexInterpolation = NULL;
02247     }
02248 }
02249 //-----------------------------------------------------------------------------
02250 #if defined(__gl_h_) || defined(__GL_H__)
02251 //=============================================================================
02252 // DrawByOpenGL
02253 //-----------------------------------------------------------------------------
02254 template <typename T> GLuint GridGenerator<T>::g_uiDisplayList = 0;
02255 template <typename T> GLenum GridGenerator<T>::g_eDrawStyle = GLU_LINE;
02256 //-----------------------------------------------------------------------------
02257 template <typename T>
02258 void GridGenerator<T>::DrawByOpenGL ( GLenum drawStyle, Vector4<T> color )
02259 {
02260     if ( !IsGridGenerated() )   return;
02261     //-----------------------------------------------------
02262     //std::cout << "Draw Grids\n";
02263     if ( drawStyle != g_eDrawStyle ) {
02264         g_eDrawStyle = drawStyle;
02265         if ( g_uiDisplayList ) {
02266             glDeleteLists( g_uiDisplayList, 1 );
02267             g_uiDisplayList = 0;
02268         }
02269     }
02270     if ( !g_uiDisplayList ) {
02271         g_uiDisplayList = glGenLists( 1 );
02272         GLUquadricObj *qObj = gluNewQuadric();
02273         glNewList( g_uiDisplayList, GL_COMPILE );
02274             // GLenum drawStyle
02275             //  GLU_FILL
02276             //  GLU_LINE
02277             //  GLU_SILHOUETTE
02278             //  GLU_POINT
02279             gluQuadricDrawStyle( qObj, drawStyle ); // wireframe
02280             gluSphere( qObj, 1, 20, 20 );
02281         glEndList();
02282         gluDeleteQuadric( qObj );
02283     }
02284     //-----------------------------------------------------
02285     glPushMatrix();
02286         //---------------------------------------
02287         if ( m_pModel ) m_pModel->GetTransform().TransformByOpenGLForDrawing();
02288         //---------------------------------------
02289         float factor = 32;
02290         float xScale = m_atGridDimension[0] / factor;
02291         float yScale = m_atGridDimension[1] / factor;
02292         float zScale = m_atGridDimension[2] / factor;
02293         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02294             for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02295                 for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
02296                     glPushMatrix();
02297                         glTranslatef( 
02298                             static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02299                             static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02300                             static_cast<float>( m_atVertexPosition[i][j][k][2] )
02301                         );
02302                         glScalef( xScale, yScale, zScale );
02303                         glCallList( g_uiDisplayList );
02304                     glPopMatrix();
02305                 }
02306             }
02307         }
02308     glPopMatrix();
02309 }
02310 //-----------------------------------------------------------------------------
02311 template <typename T>
02312 void GridGenerator<T>::DrawAsPointsByOpenGL ( Vector4<T> color )
02313 {
02314     if ( !IsGridGenerated() )   return;
02315     //-----------------------------------------------------
02316     glPushMatrix();
02317     glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT  | GL_POINT_BIT );
02318     //glDisable( GL_LIGHTING );
02319     glEnable( GL_LIGHTING );
02320     glEnable( GL_COLOR_MATERIAL );
02321     glColor4f( color[0], color[1], color[2], color[3] );
02322     //-----------------------------------------------------
02323     if ( m_pModel ) m_pModel->GetTransform().TransformByOpenGLForDrawing();
02324     //-----------------------------------------------------
02325     int pointSizeInside  = 3;
02326     int pointsizeOutside = 1;
02327     for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02328         for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02329             for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
02330                 if      ( m_abVertexFlag1[i][j][k] >= INSIDE_MODEL ) {
02331                     glPointSize( pointSizeInside );
02332                     glColor4f( color[0], color[1], color[2]/2, color[3] );
02333                 }
02334                 else if ( m_abVertexFlag1[i][j][k] >= RIGHT_INSIDE_BOUNDARY ) {
02335                     glPointSize( pointSizeInside );
02336                     glColor4f( color[0], color[1], color[2], color[3] );
02337                 }
02338                 else if ( m_abVertexFlag1[i][j][k] >= ON_BOUNDARY ) {
02339                     glPointSize( pointSizeInside );
02340                     glColor4f( color[0], color[1], color[2]*2, color[3] );
02341                 }
02342                 else if ( m_abVertexFlag1[i][j][k] >= RIGHT_OUTSIDE_BOUNDARY ) {
02343                     glPointSize( pointsizeOutside );
02344                     glColor4f( 0.75, 0, 0, 0.5 );
02345                 }
02346                 else if ( m_abVertexFlag1[i][j][k] >= OUTSIDE_MODEL ) {
02347                     glPointSize( pointsizeOutside );
02348                     glColor4f( 0.25, 0, 0, 0.5 );
02349                 }
02350                 else {
02351                     continue;
02352                 }
02353                 //-------------------------------
02354                 glBegin( GL_POINTS );
02355                 glVertex3f ( 
02356                     static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02357                     static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02358                     static_cast<float>( m_atVertexPosition[i][j][k][2] )
02359                 );
02360                 glEnd();
02361                 //-------------------------------
02362             }
02363         }
02364     }
02365     //-----------------------------------------------------
02366     glPopAttrib();
02367     glPopMatrix();
02368 }
02369 //-----------------------------------------------------------------------------
02370 template <typename T>
02371 void GridGenerator<T>::DrawAllOuterPointsByOpenGL ( Vector4<T> color )
02372 {
02373     if ( !IsGridGenerated() )   return;
02374     //-----------------------------------------------------
02375     glPushMatrix();
02376     glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT  | GL_POINT_BIT );
02377     //glDisable( GL_LIGHTING );
02378     glEnable( GL_LIGHTING );
02379     glEnable( GL_COLOR_MATERIAL );
02380     glColor4f( color[0], color[1], color[2], color[3] );
02381     glPointSize( 3 );
02382     //-----------------------------------------------------
02383     if ( m_pModel ) m_pModel->GetTransform().TransformByOpenGLForDrawing();
02384     //-----------------------------------------------------
02385     glBegin( GL_POINTS );
02386         //---------------------------------------
02387         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02388             for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02389                 for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
02390                     if      ( m_abVertexFlag1[i][j][k] == RIGHT_OUTSIDE_BOUNDARY ) {
02391                         glVertex3f ( 
02392                             static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02393                             static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02394                             static_cast<float>( m_atVertexPosition[i][j][k][2] )
02395                         );
02396                     }
02397                 }
02398             }
02399         }
02400         //---------------------------------------
02401     glEnd();
02402     //-----------------------------------------------------
02403     glPopAttrib();
02404     glPopMatrix();
02405 }
02406 //-----------------------------------------------------------------------------
02407 template <typename T>
02408 void GridGenerator<T>::DrawAllInnerPointsByOpenGL ( Vector4<T> color )
02409 {
02410     if ( !IsGridGenerated() )   return;
02411     //-----------------------------------------------------
02412     glPushMatrix();
02413     glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT  | GL_POINT_BIT );
02414     //glDisable( GL_LIGHTING );
02415     glEnable( GL_LIGHTING );
02416     glEnable( GL_COLOR_MATERIAL );
02417     glColor4f( color[0], color[1], color[2], color[3] );
02418     glPointSize( 3 );
02419     //-----------------------------------------------------
02420     if ( m_pModel ) m_pModel->GetTransform().TransformByOpenGLForDrawing();
02421     //-----------------------------------------------------
02422     glBegin( GL_POINTS );
02423         //---------------------------------------
02424         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02425             for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02426                 for ( int k = 0; k < m_auiGridSize[2]; ++k ) {
02427                     if      ( m_abVertexFlag1[i][j][k] > RIGHT_OUTSIDE_BOUNDARY ) {
02428                         glVertex3f ( 
02429                             static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02430                             static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02431                             static_cast<float>( m_atVertexPosition[i][j][k][2] )
02432                         );
02433                     }
02434                 }
02435             }
02436         }
02437         //---------------------------------------
02438     glEnd();
02439     //-----------------------------------------------------
02440     glPopAttrib();
02441     glPopMatrix();
02442 }
02443 //-----------------------------------------------------------------------------
02444 template <typename T>
02445 void GridGenerator<T>::DrawAsBoxesByOpenGL ( Vector4<T> color )
02446 {
02447     if ( !IsGridGenerated() )   return;
02448     //-----------------------------------------------------
02449     glPushMatrix();
02450     glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
02451     //glDisable( GL_LIGHTING );
02452     glEnable( GL_LIGHTING );
02453     glEnable( GL_COLOR_MATERIAL );
02454     glColor4f( color[0], color[1], color[2], color[3] );
02455     //-----------------------------------------------------
02456     if ( m_pModel ) m_pModel->GetTransform().TransformByOpenGLForDrawing();
02457     //-----------------------------------------------------
02458     int i, j, k;
02459     //---------------------------------
02460     // Z Lines
02461     for ( i = 0; i < m_auiGridSize[0]; ++i ) {
02462         for ( j = 0; j < m_auiGridSize[1]; ++j ) {
02463             glBegin( GL_LINE_STRIP );
02464             for ( k = 0; k < m_auiGridSize[2]; ++k ) {
02465                 glVertex3f ( 
02466                     static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02467                     static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02468                     static_cast<float>( m_atVertexPosition[i][j][k][2] )
02469                 );
02470             }
02471             glEnd();
02472         }
02473     }
02474     //---------------------------------
02475     // Y Lines
02476     for ( k = 0; k < m_auiGridSize[2]; ++k ) {
02477         for ( i = 0; i < m_auiGridSize[0]; ++i ) {
02478             glBegin( GL_LINE_STRIP );
02479             for ( j = 0; j < m_auiGridSize[1]; ++j ) {
02480                 glVertex3f ( 
02481                     static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02482                     static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02483                     static_cast<float>( m_atVertexPosition[i][j][k][2] )
02484                 );
02485             }
02486             glEnd();
02487         }
02488     }
02489     //---------------------------------
02490     // X Lines
02491     for ( j = 0; j < m_auiGridSize[1]; ++j ) {
02492         for ( k = 0; k < m_auiGridSize[2]; ++k ) {
02493             glBegin( GL_LINE_STRIP );
02494             for ( i = 0; i < m_auiGridSize[0]; ++i ) {
02495                 glVertex3f ( 
02496                     static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02497                     static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02498                     static_cast<float>( m_atVertexPosition[i][j][k][2] )
02499                 );
02500             }
02501             glEnd();
02502         }
02503     }
02504     //---------------------------------------------------------------
02505     glPopAttrib();
02506     glPopMatrix();
02507 }
02508 /*
02509 //-----------------------------------------------------------------------------
02510 template <typename T>
02511 void GridGenerator<T>::DrawAPlaneByOpenGL ( int planeNo, int plane )
02512 {
02513     // plane: 2: xy-, 0: yz-, 1: xz-plane
02514     //-----------------------------------------------------
02515     if      ( planeNo < 0 )
02516         planeNo = 0;
02517     else if ( planeNo >= m_auiGridSize[ plane ] )
02518         planeNo = m_auiGridSize[ plane ] - 1;
02519     //-----------------------------------------------------
02520     //glPushAttrib( GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_DEPTH_BUFFER_BIT );
02521     glPushAttrib( GL_CURRENT_BIT | GL_LIGHTING_BIT );
02522     glDisable( GL_LIGHTING );
02523     glMatrixMode( GL_PROJECTION );
02524     glPushMatrix();
02525     glLoadIdentity();
02526     glOrtho( -1, 1, -1, 1, -0.2, 0.2 );
02527     glMatrixMode( GL_MODELVIEW );
02528     glPushMatrix();
02529     glLoadIdentity();
02530     //gluLookAt( 0, 0, 2, 0, 0, 0, 0, 1, 0 );
02531     //-----------------------------------------------------
02532     DrawAsBoxesByOpenGL( Vector4<T>( 1, 0, 1, 1 ) );
02533     glColor3f( 0, 0, 1 );
02534     if ( m_pModel ) {
02535         OpenGL::OpenGLModel<T> * pGLModel = 
02536             dynamic_cast< OpenGL::OpenGLModel<T> * >( m_pModel );
02537         if ( pGLModel ) {
02538             pGLModel->DisplayGL( OpenGL::Enum::POLYGON );
02539         }
02540     }
02541     //-----------------------------------------------------
02542     glPopMatrix();
02543     glMatrixMode( GL_PROJECTION );
02544     glPopMatrix();
02545     glPopAttrib();
02546     //-----------------------------------------------------
02547     //-----------------------------------------------------
02548 }
02549 //*/
02550 //-----------------------------------------------------------------------------
02551 template <typename T>
02552 void GridGenerator<T>::DrawBoundingBoxByOpenGL ( Vector4<T> color )
02553 {
02554     if ( !IsGridGenerated() )   return;
02555     //-----------------------------------------------------
02556     glPushMatrix();
02557     glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT );
02558     //glDisable( GL_LIGHTING );
02559     glEnable( GL_LIGHTING );
02560     glEnable( GL_COLOR_MATERIAL );
02561     glColor4f( color[0], color[1], color[2], color[3] );
02562     glLineWidth( 1 );
02563     //-----------------------------------------------------
02564     if ( m_pModel ) m_pModel->GetTransform().TransformByOpenGLForDrawing();
02565     //-----------------------------------------------------
02566     int i, j, k;
02567     //---------------------------------
02568     // Z Lines
02569     for ( i = 0; i < m_auiGridSize[0]; i += m_auiGridSize[0]-1 ) {
02570         for ( j = 0; j < m_auiGridSize[1]; j += m_auiGridSize[1]-1 ) {
02571             glBegin( GL_LINE_STRIP );
02572             for ( k = 0; k < m_auiGridSize[2]; k += m_auiGridSize[2]-1 ) {
02573                 glVertex3f ( 
02574                     static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02575                     static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02576                     static_cast<float>( m_atVertexPosition[i][j][k][2] )
02577                 );
02578             }
02579             glEnd();
02580         }
02581     }
02582     //---------------------------------
02583     // Y Lines
02584     for ( k = 0; k < m_auiGridSize[2]; k += m_auiGridSize[2]-1 ) {
02585         for ( i = 0; i < m_auiGridSize[0]; i += m_auiGridSize[0]-1 ) {
02586             glBegin( GL_LINE_STRIP );
02587             for ( j = 0; j < m_auiGridSize[1]; j += m_auiGridSize[1]-1 ) {
02588                 glVertex3f ( 
02589                     static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02590                     static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02591                     static_cast<float>( m_atVertexPosition[i][j][k][2] )
02592                 );
02593             }
02594             glEnd();
02595         }
02596     }
02597     //---------------------------------
02598     // X Lines
02599     for ( j = 0; j < m_auiGridSize[1]; j += m_auiGridSize[1]-1 ) {
02600         for ( k = 0; k < m_auiGridSize[2]; k += m_auiGridSize[2]-1 ) {
02601             glBegin( GL_LINE_STRIP );
02602             for ( i = 0; i < m_auiGridSize[0]; i += m_auiGridSize[0]-1 ) {
02603                 glVertex3f ( 
02604                     static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02605                     static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02606                     static_cast<float>( m_atVertexPosition[i][j][k][2] )
02607                 );
02608             }
02609             glEnd();
02610         }
02611     }
02612     //---------------------------------------------------------------
02613 
02614     /*
02615     //===============================================================
02616     // Start DEBUG
02617     //---------------------------------------------------------------
02618     float pt[8][3];
02619     glLineWidth( 2 );
02620     // Draw Face Bounding Box
02621     pt[0][0] = vMin[0];     pt[0][1] = vMin[1];     pt[0][2] = vMin[2];
02622     pt[1][0] = vMax[0];     pt[1][1] = vMin[1];     pt[1][2] = vMin[2];
02623     pt[2][0] = vMax[0];     pt[2][1] = vMax[1];     pt[2][2] = vMin[2];
02624     pt[3][0] = vMin[0];     pt[3][1] = vMax[1];     pt[3][2] = vMin[2];
02625     pt[4][0] = vMin[0];     pt[4][1] = vMin[1];     pt[4][2] = vMax[2];
02626     pt[5][0] = vMax[0];     pt[5][1] = vMin[1];     pt[5][2] = vMax[2];
02627     pt[6][0] = vMax[0];     pt[6][1] = vMax[1];     pt[6][2] = vMax[2];
02628     pt[7][0] = vMin[0];     pt[7][1] = vMax[1];     pt[7][2] = vMax[2];
02629     glColor3f( 1, 0, 0 );
02630     glBegin( GL_LINES );
02631         //-------------------
02632         glVertex3fv ( pt[0] );
02633         glVertex3fv ( pt[1] );
02634         glVertex3fv ( pt[1] );
02635         glVertex3fv ( pt[2] );
02636         glVertex3fv ( pt[2] );
02637         glVertex3fv ( pt[3] );
02638         glVertex3fv ( pt[3] );
02639         glVertex3fv ( pt[0] );
02640         //-------------------
02641         glVertex3fv ( pt[4] );
02642         glVertex3fv ( pt[5] );
02643         glVertex3fv ( pt[5] );
02644         glVertex3fv ( pt[6] );
02645         glVertex3fv ( pt[6] );
02646         glVertex3fv ( pt[7] );
02647         glVertex3fv ( pt[7] );
02648         glVertex3fv ( pt[4] );
02649         //-------------------
02650         glVertex3fv ( pt[0] );
02651         glVertex3fv ( pt[4] );
02652         glVertex3fv ( pt[1] );
02653         glVertex3fv ( pt[5] );
02654         glVertex3fv ( pt[2] );
02655         glVertex3fv ( pt[6] );
02656         glVertex3fv ( pt[3] );
02657         glVertex3fv ( pt[7] );
02658         //-------------------
02659     glEnd();
02660     //-----------------------------------------------------
02661     //glPointSize( 7 );
02662     //glColor3f( 1, 0, 1 );
02663     //glBegin( GL_POINTS );
02664     //  glColor3f( 1, 0, 0 );
02665     //  glVertex3f ( vMin[0], vMin[1], vMin[2] );
02666     //  glColor3f( 0, 1, 0 );
02667     //  glVertex3f ( vMax[0], vMax[1], vMax[2] );
02668     //glEnd();
02669     //-----------------------------------------------------
02670     // Draw Grid Bounding Box
02671     glLineWidth( 3 );
02672     Vector3<T> vvMin, vvMax;
02673     for ( int d = 0; d < 3; ++d ) {
02674         vvMin[d] = m_tAABBMin[d] + gMinNum[d] * m_atGridDimension[d];
02675         vvMax[d] = m_tAABBMin[d] + gMaxNum[d] * m_atGridDimension[d];
02676     }
02677     pt[0][0] = vvMin[0];        pt[0][1] = vvMin[1];        pt[0][2] = vvMin[2];
02678     pt[1][0] = vvMax[0];        pt[1][1] = vvMin[1];        pt[1][2] = vvMin[2];
02679     pt[2][0] = vvMax[0];        pt[2][1] = vvMax[1];        pt[2][2] = vvMin[2];
02680     pt[3][0] = vvMin[0];        pt[3][1] = vvMax[1];        pt[3][2] = vvMin[2];
02681     pt[4][0] = vvMin[0];        pt[4][1] = vvMin[1];        pt[4][2] = vvMax[2];
02682     pt[5][0] = vvMax[0];        pt[5][1] = vvMin[1];        pt[5][2] = vvMax[2];
02683     pt[6][0] = vvMax[0];        pt[6][1] = vvMax[1];        pt[6][2] = vvMax[2];
02684     pt[7][0] = vvMin[0];        pt[7][1] = vvMax[1];        pt[7][2] = vvMax[2];
02685     glColor3f( 0, 1, 0 );
02686     glBegin( GL_LINES );
02687         //-------------------
02688         glVertex3fv ( pt[0] );
02689         glVertex3fv ( pt[1] );
02690         glVertex3fv ( pt[1] );
02691         glVertex3fv ( pt[2] );
02692         glVertex3fv ( pt[2] );
02693         glVertex3fv ( pt[3] );
02694         glVertex3fv ( pt[3] );
02695         glVertex3fv ( pt[0] );
02696         //-------------------
02697         glVertex3fv ( pt[4] );
02698         glVertex3fv ( pt[5] );
02699         glVertex3fv ( pt[5] );
02700         glVertex3fv ( pt[6] );
02701         glVertex3fv ( pt[6] );
02702         glVertex3fv ( pt[7] );
02703         glVertex3fv ( pt[7] );
02704         glVertex3fv ( pt[4] );
02705         //-------------------
02706         glVertex3fv ( pt[0] );
02707         glVertex3fv ( pt[4] );
02708         glVertex3fv ( pt[1] );
02709         glVertex3fv ( pt[5] );
02710         glVertex3fv ( pt[2] );
02711         glVertex3fv ( pt[6] );
02712         glVertex3fv ( pt[3] );
02713         glVertex3fv ( pt[7] );
02714         //-------------------
02715     glEnd();
02716     //-----------------------------------------------------
02717     //glPointSize( 7 );
02718     //glColor3f( 1, 0, 1 );
02719     //glBegin( GL_POINTS );
02720     //  glColor3f( 1, 0, 0 );
02721     //  glVertex3f ( vvMin[0], vvMin[1], vvMin[2] );
02722     //  glColor3f( 0, 1, 0 );
02723     //  glVertex3f ( vvMax[0], vvMax[1], vvMax[2] );
02724     //glEnd();
02725     //-----------------------------------------------------
02726     // Draw Grid Start Point
02727     glPointSize( 7 );
02728     glColor3f( 1, 0, 1 );
02729     pt[0][0] = m_tAABBMin[0] + gridBoxCenterPt[0] * m_atGridDimension[0];
02730     pt[0][1] = m_tAABBMin[1] + gridBoxCenterPt[1] * m_atGridDimension[1];
02731     pt[0][2] = m_tAABBMin[2] + gridBoxCenterPt[2] * m_atGridDimension[2];
02732     //glBegin( GL_POINTS );
02733     //  glVertex3fv ( pt[0] );
02734     //  glColor3f( 1, 0, 0 );
02735     //  glVertex3f ( m_tAABBMin[0], m_tAABBMin[1], m_tAABBMin[2] );
02736     //  glColor3f( 0, 1, 0 );
02737     //  glVertex3f ( m_tAABBMax[0], m_tAABBMax[1], m_tAABBMax[2] );
02738     //glEnd();
02739     //-----------------------------------------------------
02740     //glLineWidth( 5 );
02741     //glBegin( GL_LINES );
02742     //  glColor3f( 1, 0, 1 );
02743     //  glVertex3fv ( pt[0] );
02744     //  glVertex3f ( pt[0][0] + m_atGridDimension[0] * minMoveStep[0], pt[0][1], pt[0][2] );
02745     //  glVertex3fv ( pt[0] );
02746     //  glVertex3f ( pt[0][0], pt[0][1] + m_atGridDimension[1] * minMoveStep[1], pt[0][2] );
02747     //  glVertex3fv ( pt[0] );
02748     //  glVertex3f ( pt[0][0], pt[0][1], pt[0][2] + m_atGridDimension[2] * minMoveStep[2] );
02749 
02750     //  glColor3f( 0, 1, 1 );
02751     //  glVertex3fv ( pt[0] );
02752     //  glVertex3f ( pt[0][0] + m_atGridDimension[0] * maxMoveStep[0], pt[0][1], pt[0][2] );
02753     //  glVertex3fv ( pt[0] );
02754     //  glVertex3f ( pt[0][0], pt[0][1] + m_atGridDimension[1] * maxMoveStep[1], pt[0][2] );
02755     //  glVertex3fv ( pt[0] );
02756     //  glVertex3f ( pt[0][0], pt[0][1], pt[0][2] + m_atGridDimension[2] * maxMoveStep[2] );
02757     //glEnd();
02758     //---------------------------------------------------------------
02759     // End DEBUG
02760     //===============================================================
02761     //*/
02762 
02763     //---------------------------------------------------------------
02764     glPopAttrib();
02765     glPopMatrix();
02766 }
02767 //-----------------------------------------------------------------------------
02768 template <typename T>
02769 void GridGenerator<T>::DrawAPlaneByOpenGL ( 
02770     int drawDirection, Vector4<T> color )
02771 //  int planeNo, int drawDirection, Vector4<T> color )
02772 {
02773     if ( !IsGridGenerated() )   return;
02774     //-----------------------------------------------------
02775     glPushMatrix();
02776     glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT  | GL_POINT_BIT | GL_DEPTH_BUFFER_BIT );
02777     glDisable( GL_DEPTH_TEST );
02778     //glDisable( GL_LIGHTING );
02779     glEnable( GL_LIGHTING );
02780     glEnable( GL_COLOR_MATERIAL );
02781     glColor4f( color[0], color[1], color[2], color[3] );
02782     //-----------------------------------------------------
02783     if ( m_pModel ) m_pModel->GetTransform().TransformByOpenGLForDrawing();
02784     //-----------------------------------------------------
02785     int dim1 = 0, dim2 = 1;     // xy-plane
02786     switch ( drawDirection ) {
02787         case 0:                 // yz-plane
02788             dim1 = 1, dim2 = 2;
02789             break;
02790         case 1:                 // zx-plane
02791             dim1 = 2, dim2 = 0;
02792             break;
02793     }
02794     //-----------------------------------------------------
02795     // -- DEBUG --
02796 //  int k = planeNo;
02797 //  if      ( k < 0 )   k = 0;
02798 //  else if ( k > 
02799     static int k = 0;
02800     //-----------------------------------------------------
02801     for ( int i = 0; i < m_auiGridSize[dim1]; ++i ) {
02802         for ( int j = 0; j < m_auiGridSize[dim2]; ++j ) {
02803             if      ( m_abVertexFlag1[i][j][k] >= INSIDE_MODEL ) {
02804                 glPointSize( 3 );
02805                 //glColor4f( color[0], color[1], color[2]/2, color[3] );
02806                 glColor4f( 1, 1, 0, color[3] );
02807             }
02808             else if ( m_abVertexFlag1[i][j][k] >= RIGHT_INSIDE_BOUNDARY ) {
02809                 glPointSize( 3 );
02810                 glColor4f( color[0], color[1], color[2], color[3] );
02811             }
02812             else if ( m_abVertexFlag1[i][j][k] >= ON_BOUNDARY ) {
02813                 glPointSize( 3 );
02814                 //glColor4f( color[0], color[1], color[2]*2, color[3] );
02815                 glColor4f( 1, 1, 1, color[3] );
02816             }
02817             else if ( m_abVertexFlag1[i][j][k] >= RIGHT_OUTSIDE_BOUNDARY ) {
02818                 glPointSize( 1 );
02819                 glColor4f( 0.75, 0, 0, 0.5 );
02820             }
02821             else if ( m_abVertexFlag1[i][j][k] >= OUTSIDE_MODEL ) {
02822                 continue;
02823                 glPointSize( 1 );
02824                 glColor4f( 0.25, 0, 0, 0.5 );
02825             }
02826             else {
02827                 continue;
02828             }
02829 
02830             /*
02831             if      ( m_abVertexFlag1[i][j][k] > RIGHT_OUTSIDE_BOUNDARY ) {
02832                 glPointSize( 3 );
02833                 glColor4f( color[0], color[1], color[2], color[3] );
02834             }
02835             else if ( m_abVertexFlag1[i][j][k] > UNSET ) {
02836                 glPointSize( 1 );
02837                 glColor4f( 0.75, 0, 0, 0.5 );
02838             }
02839             else {
02840                 continue;
02841             }
02842             //*/
02843 
02844             //-------------------------------
02845             glBegin( GL_POINTS );
02846             glVertex3f ( 
02847                 static_cast<float>( m_atVertexPosition[i][j][k][0] ), 
02848                 static_cast<float>( m_atVertexPosition[i][j][k][1] ), 
02849                 static_cast<float>( m_atVertexPosition[i][j][k][2] )
02850             );
02851             glEnd();
02852             //-------------------------------
02853         }
02854     }
02855     ++k;
02856     //-----------------------------------------------------
02857     // -- DEBUG --
02858     if ( k >= m_auiGridSize[2] - 1 )    k = 0;
02859     //-----------------------------------------------------
02860     glPopAttrib();
02861     glPopMatrix();
02862 }
02863 //-----------------------------------------------------------------------------
02864 
02865 
02866 //=============================================================================
02867 // TAPs in TIPS
02868 #ifdef TAPs_IN_TIPS
02869 //-----------------------------------------------------------------------------
02870 template <typename T>
02871 void GridGenerator<T>::Draw8PtBoxByOpenGL ( T r, T g, T b, T a )
02872 {
02873     glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_POINT_BIT | GL_LINE_BIT );
02874     glPushMatrix();
02875     //---------------------------------------------------------------
02876     glPointSize( 5 );
02877     glEnable( GL_COLOR_MATERIAL );
02878     glColor4f( r+0.5, g+0.5, b+0.5, a+0.5 );
02879     //glEnable( GL_POLYGON_OFFSET_FILL );
02880     //glPolygonOffset( 1.0, 1.0 );
02881     //-----------------------------------------------------
02882     // Draw corner points
02883     glBegin( GL_POINTS );
02884     for ( int i = 0; i < 8; ++i ) {
02885         glVertex3fv( m_vBBPts[i].GetDataFloat() );
02886     }
02887     glEnd();
02888     //-----------------------------------------------------
02889     // Draw Box
02890     //-----------------------
02891     //     2---1
02892     //    /|  /|
02893     //   6-3-5-0
02894     //   |/  |/
02895     //   7---4
02896     //-----------------------
02897     glColor4f( r, g, b, a );
02898     glBegin( GL_QUAD_STRIP );
02899         // Top
02900         glNormal3f( 0, 1, 0 );
02901         glVertex3fv( m_vBBPts[6].GetDataFloat() );
02902         glVertex3fv( m_vBBPts[5].GetDataFloat() );
02903         glVertex3fv( m_vBBPts[2].GetDataFloat() );
02904         glVertex3fv( m_vBBPts[1].GetDataFloat() );
02905         // Back
02906         glNormal3f( 0, 0, -1 );
02907         glVertex3fv( m_vBBPts[3].GetDataFloat() );
02908         glVertex3fv( m_vBBPts[0].GetDataFloat() );
02909         // Bottom
02910         glNormal3f( 0, -1, 0 );
02911         glVertex3fv( m_vBBPts[7].GetDataFloat() );
02912         glVertex3fv( m_vBBPts[4].GetDataFloat() );
02913     glEnd();
02914     glBegin( GL_QUAD_STRIP );
02915         // Left
02916         glNormal3f( -1, 0, 0 );
02917         glVertex3fv( m_vBBPts[2].GetDataFloat() );
02918         glVertex3fv( m_vBBPts[3].GetDataFloat() );
02919         glVertex3fv( m_vBBPts[6].GetDataFloat() );
02920         glVertex3fv( m_vBBPts[7].GetDataFloat() );
02921         // Front
02922         glNormal3f( 0, 0, 1 );
02923         glVertex3fv( m_vBBPts[5].GetDataFloat() );
02924         glVertex3fv( m_vBBPts[4].GetDataFloat() );
02925         // Right
02926         glNormal3f( 1, 0, 0 );
02927         glVertex3fv( m_vBBPts[1].GetDataFloat() );
02928         glVertex3fv( m_vBBPts[0].GetDataFloat() );
02929     glEnd();
02930     //-----------------------------------------------------
02931 
02932     //*
02933     //-----------------------------------------------------
02934     {
02935         GLfloat matModView[16];
02936         glGetFloatv( GL_MODELVIEW_MATRIX, matModView );
02937         glDisable( GL_LIGHTING );
02938         glColor4f( 1, 1, 1, 1 );
02939         glLoadIdentity();
02940         //-----------------------------------------------------
02941         GLfloat pts[8][3];
02942         for ( int i = 0; i < 8; ++i ) {
02943             pts[i][0] = matModView[0]*m_vBBPts[i][0] + matModView[4]*m_vBBPts[i][1] + matModView[ 8]*m_vBBPts[i][2] + matModView[12];
02944             pts[i][1] = matModView[1]*m_vBBPts[i][0] + matModView[5]*m_vBBPts[i][1] + matModView[ 9]*m_vBBPts[i][2] + matModView[13];
02945             pts[i][2] = matModView[2]*m_vBBPts[i][0] + matModView[6]*m_vBBPts[i][1] + matModView[10]*m_vBBPts[i][2] + matModView[14];
02946         }
02947         //-----------------------------------------------------
02948         glPointSize( 11 );
02949         glBegin( GL_POINTS );
02950         for ( int i = 0; i < 8; ++i ) {
02951             glVertex3f( pts[i][0], pts[i][1], pts[i][2] );
02952         }
02953         glEnd();
02954     }
02955     //*/
02956 
02957     //-----------------------------------------------------
02958     //---------------------------------------------------------------
02959     glPopMatrix();
02960     glPopAttrib();
02961 }
02962 //-----------------------------------------------------------------------------
02963 template <typename T>
02964 void GridGenerator<T>::Draw8PtBoxCellsByOpenGL ( T r, T g, T b, T a )
02965 {
02966     glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_POINT_BIT | GL_LINE_BIT );
02967     glPushMatrix();
02968     //---------------------------------------------------------------
02969     glLineWidth( 1 );
02970     glColor4f( r, g, b, a );
02971     //glEnable( GL_COLOR_MATERIAL );
02972     //-----------------------------------------------------
02973     // Draw Cells in/on the box
02974     //-----------------------
02975     //     2---1
02976     //    /|  /|
02977     //   6-3-5-0
02978     //   |/  |/
02979     //   7---4
02980     //-----------------------
02981     // xy
02982     {
02983         glBegin( GL_LINES );
02984         GLfloat zStart = static_cast<GLfloat>( m_vBBPts[3][2] );
02985         GLfloat zEnd   = static_cast<GLfloat>( m_vBBPts[7][2] );
02986         GLfloat x = static_cast<GLfloat>( m_vBBPts[3][0] );
02987         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
02988             GLfloat y = static_cast<GLfloat>( m_vBBPts[3][1] );
02989             for ( int j = 0; j < m_auiGridSize[1]; ++j ) {
02990                 glVertex3f( x, y, zStart );
02991                 glVertex3f( x, y, zEnd );
02992                 y += m_atGridDimension[1];
02993             }
02994             x += m_atGridDimension[0];
02995         }
02996         glEnd();
02997     }
02998     //-----------------------
02999     // yz
03000     {
03001         glBegin( GL_LINES );
03002         GLfloat xStart = static_cast<GLfloat>( m_vBBPts[3][0] );
03003         GLfloat xEnd   = static_cast<GLfloat>( m_vBBPts[0][0] );
03004         GLfloat y = static_cast<GLfloat>( m_vBBPts[3][1] );
03005         for ( int i = 0; i < m_auiGridSize[1]; ++i ) {
03006             GLfloat z = static_cast<GLfloat>( m_vBBPts[3][2] );
03007             for ( int j = 0; j < m_auiGridSize[2]; ++j ) {
03008                 glVertex3f( xStart, y, z );
03009                 glVertex3f( xEnd,   y, z );
03010                 z += m_atGridDimension[2];
03011             }
03012             y += m_atGridDimension[1];
03013         }
03014         glEnd();
03015     }
03016     //-----------------------
03017     // xz
03018     {
03019         glBegin( GL_LINES );
03020         GLfloat yStart = static_cast<GLfloat>( m_vBBPts[3][1] );
03021         GLfloat yEnd   = static_cast<GLfloat>( m_vBBPts[2][1] );
03022         GLfloat x = static_cast<GLfloat>( m_vBBPts[3][0] );
03023         for ( int i = 0; i < m_auiGridSize[0]; ++i ) {
03024             GLfloat z = static_cast<GLfloat>( m_vBBPts[3][2] );
03025             for ( int j = 0; j < m_auiGridSize[2]; ++j ) {
03026                 glVertex3f( x, yStart, z );
03027                 glVertex3f( x, yEnd,   z );
03028                 z += m_atGridDimension[2];
03029             }
03030             x += m_atGridDimension[0];
03031         }
03032         glEnd();
03033     }
03034     //-----------------------------------------------------
03035     //---------------------------------------------------------------
03036     glPopMatrix();
03037     glPopAttrib();
03038 }
03039 //-----------------------------------------------------------------------------
03040 #endif // #ifdef TAPs_IN_TIPS
03041 //=============================================================================
03042 
03043 // DEBUG BY DRAWING
03044 //-----------------------------------------------------------------------------
03045 // Set Grid Points To Inside and Outside the Model
03046 template <typename T>
03047 void GridGenerator<T>::DRAW_SetGridPointsToInsideOutsideModelBoundary_SW ( 
03048     OpenGL::XPolygonalModel<T> * pXPModel )
03049 {
03050     if ( !IsGridGenerated() )   return;
03051     //---------------------------------------------------------------
03052     glPushAttrib( GL_ALL_ATTRIB_BITS );
03053     glDisable( GL_LIGHTING );
03054     glLineWidth( 3 );
03055     glPointSize( 7 );
03056     //---------------------------------------------------------------
03057     XVertex<T> * vertex = pXPModel->GetVertexList();
03058     Face<T> * face = pXPModel->GetFaceList();
03059     int NumFaces = pXPModel->GetNoFaces();
03060     //---------------------------------------------------------------
03061     // Generate Grid by a Software Algorithm
03062     //-----------------------------------------------------
03063     Vector3<T> vMin, vMax, vPos, vAverage;
03064     //-----------------------------------------------------
03065     // For Each Face
03066     static int faceNo = 0;
03067     static int count = 0;
03068     if ( ++count >= 1000 ) {
03069         count = 0;
03070         if ( ++faceNo >= NumFaces ) {
03071             faceNo = 0;
03072         }
03073     }
03074     //for ( int i = 0; i < NumFaces; ++i ) {
03075     //for ( int i = 0; i < 1; ++i ) {
03076     {   int i = faceNo;
03077         //---------------------------------------
03078         // Find a box that contain this face
03079         vAverage = vMin = vMax = vertex[ face[i].GetVertexNo(0) ].GetPosition();
03080         for ( int n = 1; n < face[i].GetNoVertices(); ++n ) {
03081             vPos = vertex[ face[i].GetVertexNo(n) ].GetPosition();
03082             vAverage += vPos;
03083             for ( int d = 0; d < 3; ++d ) {
03084                 if      ( vMin[d] > vPos[d] )   vMin[d] = vPos[d];
03085                 else if ( vMax[d] < vPos[d] )   vMax[d] = vPos[d];
03086             }
03087         }
03088         //assert( vMin[0] <= vMax[0] );
03089         //assert( vMin[1] <= vMax[1] );
03090         //assert( vMin[2] <= vMax[2] );
03091         //---------------------------------------
03092         // Find a box of grids that contain this face
03093         // i.e. a bigger box that contain the face box
03094         int gMinNum[3], gMaxNum[3];
03095         T gMin, gMax;
03096         for ( int d = 0; d < 3; ++d ) {
03097             int step = m_auiGridSize[d] / 2;
03098             gMinNum[d] = gMaxNum[d] = step;
03099             gMin = m_tAABBMin[d] + gMinNum[d] * m_atGridDimension[d];
03100             T minOffset = vMin[d] - m_atGridDimension[d] / 2;
03101             T maxOffset = vMax[d] + m_atGridDimension[d] / 2;
03102             //T minOffset = vMin[d];
03103             //T maxOffset = vMax[d];
03104             //-------------------------
03105             // Find a box of this current face
03106             while ( ( gMin < minOffset || maxOffset < gMin ) && step > 1 ) {
03107                 step /= 2;
03108                 if ( gMin < minOffset ) {
03109                     gMinNum[d] += step;
03110                 }
03111                 else {
03112                     gMinNum[d] -= step;
03113                 }
03114                 gMin = m_tAABBMin[d] + gMinNum[d] * m_atGridDimension[d];
03115             }
03116             gMax = gMin;
03117             gMaxNum[d] = gMinNum[d];
03118 
03119             // DEBUG
03120             //gridBoxCenterPt[d] = gMinNum[d];
03121 
03122             //-------------------------
03123             // Find a grid box that covers the box of this current face
03124             //minMoveStep[d] = maxMoveStep[d] = 0;      // DEBUG
03125             //-------------------------
03126             // Find the low
03127             while ( vMin[d] < gMin ) {
03128             //while ( minOffset < gMin ) {
03129                 //--minMoveStep[d];     // DEBUG
03130                 --gMinNum[d];
03131                 gMin -= m_atGridDimension[d];
03132             }
03133             if ( gMinNum[d] < 0 ) {
03134                 gMinNum[d] = 0;
03135             }
03136             //-------------------------
03137             // Find the high
03138             while ( gMax < vMax[d] ) {
03139             //while ( gMax < maxOffset ) {
03140                 //++maxMoveStep[d];     // DEBUG
03141                 ++gMaxNum[d];
03142                 gMax += m_atGridDimension[d];
03143             }
03144             if ( gMaxNum[d] >= m_auiGridSize[d] ) {
03145                 gMaxNum[d] = m_auiGridSize[d] - 1;
03146             }
03147             //-----------------------------------
03148             //assert( gMinNum[d] <= gMaxNum[d] );
03149             //assert( 0 <= gMinNum[d] && gMinNum[d] < m_auiGridSize[d] );
03150             //assert( 0 <= gMaxNum[d] && gMaxNum[d] < m_auiGridSize[d] );
03151         }
03152 
03153         //-------------------------------------------------
03154         // Set Inside / Outside
03155         //vPos = vertex[ face[i].GetVertexNo(0) ].GetPosition();
03156         vAverage /= face[i].GetNoVertices();
03157         Vector3<T> normal = face[i].GetNormal();
03158         Vector3<T> direction;
03159         for ( int x = gMinNum[0]; x <= gMaxNum[0]; ++x ) {
03160             for ( int y = gMinNum[1]; y <= gMaxNum[1]; ++y ) {
03161                 for ( int z = gMinNum[2]; z <= gMaxNum[2]; ++z ) {
03162                     direction.SetXYZ( 
03163                         m_atVertexPosition[x][y][z][0] - vAverage[0],
03164                         m_atVertexPosition[x][y][z][1] - vAverage[1],
03165                         m_atVertexPosition[x][y][z][2] - vAverage[2]
03166                     );
03167                     //---------------------------
03168                     // If just outside boundary
03169                     if ( m_abVertexFlag1[x][y][z] == RIGHT_OUTSIDE_BOUNDARY ) continue;
03170                     //---------------------------
03171                     // The point is inside, on, or outside the boundary.
03172                     if ( normal * direction < 0.0 ) {
03173                         m_abVertexFlag1[x][y][z] = RIGHT_INSIDE_BOUNDARY;
03174                     }
03175                     else if ( normal * direction == 0.0 ) {
03176                         m_abVertexFlag1[x][y][z] = ON_BOUNDARY;
03177                     }
03178                     else {
03179                         m_abVertexFlag1[x][y][z] = RIGHT_OUTSIDE_BOUNDARY;
03180                     }
03181                 }
03182             }
03183         }
03184         //-------------------------------------------------
03185 
03186         //=================================================
03187         // START: DRAWING
03188         //-------------------------------------------------
03189         { // Draw Triangle
03190             glColor3ub(  75, 255,  75 );
03191             glBegin( GL_TRIANGLES );
03192             //glBegin( GL_POINTS );
03193             for ( int n = 0; n < face[i].GetNoVertices(); ++n ) {
03194                 vPos = vertex[ face[i].GetVertexNo(n) ].GetPosition();
03195                 glVertex3f( vPos[0], vPos[1], vPos[2] );
03196             }
03197             glEnd();
03198         }
03199         { // Draw the box
03200             Vector3<T> boxMin( m_atVertexPosition[ gMinNum[0] ][ gMinNum[1] ][ gMinNum[2] ] );
03201             Vector3<T> boxMax( m_atVertexPosition[ gMaxNum[0] ][ gMaxNum[1] ][ gMaxNum[2] ] );
03202             //glColor3f( 
03203             //  rand() / static_cast<T>( RAND_MAX ), 
03204             //  rand() / static_cast<T>( RAND_MAX ), 
03205             //  rand() / static_cast<T>( RAND_MAX ) 
03206             //);
03207             glColor3ub( 15, 75, 15 );
03208             glBegin( GL_LINE_LOOP );
03209                 glVertex3f( boxMin[0], boxMin[1], boxMin[2] );
03210                 glVertex3f( boxMax[0], boxMin[1], boxMin[2] );
03211                 glVertex3f( boxMax[0], boxMax[1], boxMin[2] );
03212                 glVertex3f( boxMin[0], boxMax[1], boxMin[2] );
03213             glEnd();
03214             glBegin( GL_LINE_LOOP );
03215                 glVertex3f( boxMin[0], boxMin[1], boxMax[2] );
03216                 glVertex3f( boxMax[0], boxMin[1], boxMax[2] );
03217                 glVertex3f( boxMax[0], boxMax[1], boxMax[2] );
03218                 glVertex3f( boxMin[0], boxMax[1], boxMax[2] );
03219             glEnd();
03220             glBegin( GL_LINES );
03221                 glVertex3f( boxMin[0], boxMin[1], boxMin[2] );
03222                 glVertex3f( boxMin[0], boxMin[1], boxMax[2] );
03223                 glVertex3f( boxMax[0], boxMin[1], boxMin[2] );
03224                 glVertex3f( boxMax[0], boxMin[1], boxMax[2] );
03225                 glVertex3f( boxMax[0], boxMax[1], boxMin[2] );
03226                 glVertex3f( boxMax[0], boxMax[1], boxMax[2] );
03227                 glVertex3f( boxMin[0], boxMax[1], boxMin[2] );
03228                 glVertex3f( boxMin[0], boxMax[1], boxMax[2] );
03229             glEnd();
03230         }
03231         { // Draw Points
03232             glBegin( GL_POINTS );
03233             for ( int x = gMinNum[0]; x <= gMaxNum[0]; ++x ) {
03234                 for ( int y = gMinNum[1]; y <= gMaxNum[1]; ++y ) {
03235                     for ( int z = gMinNum[2]; z <= gMaxNum[2]; ++z ) {
03236                         if ( m_abVertexFlag1[x][y][z] == UNSET )                    glColor3ub(   0,   0,   0 );
03237                         if ( m_abVertexFlag1[x][y][z] == OUTSIDE_MODEL )            glColor3ub( 127,   0,   0 );
03238                         if ( m_abVertexFlag1[x][y][z] == RIGHT_OUTSIDE_BOUNDARY )   glColor3ub( 255,   0,   0 );
03239                         if ( m_abVertexFlag1[x][y][z] == ON_BOUNDARY )              glColor3ub(   0, 255,   0 );
03240                         if ( m_abVertexFlag1[x][y][z] == RIGHT_INSIDE_BOUNDARY )    glColor3ub(   0,   0, 127 );
03241                         if ( m_abVertexFlag1[x][y][z] == INSIDE_MODEL )             glColor3ub(   0,   0, 255 );
03242                         glVertex3f( m_atVertexPosition[x][y][z][0], 
03243                                     m_atVertexPosition[x][y][z][1], 
03244                                     m_atVertexPosition[x][y][z][2] );
03245                     }
03246                 }
03247             }
03248             glEnd();
03249         }
03250         //-------------------------------------------------
03251         // END: DRAWING
03252         //=================================================
03253     }
03254     //---------------------------------------------------------------
03255     glPopAttrib();
03256     // End of Generating Grid by a Software Algorithm
03257 }
03258 
03259 
03260 //-----------------------------------------------------------------------------
03261 #endif
03262 //=============================================================================
03263 END_NAMESPACE_TAPs
03264 //-----------------------------------------------------------------------------
03265 //34567890123456789012345678901234567890123456789012345678901234567890123456789
03266 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines