TAPs 0.7.7.3
TAPsGridGenerator_ForTheDefModBasedOnMSS.hpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsGridGenerator_ForTheDefModBasedOnMSS.hpp
00003 ******************************************************************************/
00014 /******************************************************************************
00015 SUKITTI PUNAK   (07/22/2007)
00016 UPDATE          (01/07/2010)
00017 ******************************************************************************/
00018 #ifndef TAPs_GRID_GENERATOR_HPP
00019 #define TAPs_GRID_GENERATOR_HPP
00020 
00021 #include "../Core/TAPsLib.hpp"
00022 #include "../OpenGLModel/TAPsModel.hpp"
00023 #include "../OpenGLModel/TAPsOpenGLModelList.hpp"
00024 
00025 //=============================================================================
00026 // DEFINES / CHOICES
00027 //=============================================================================
00028 //-----------------------------------------------------------------------------
00029 #define TAPs_GRID_GENERATOR_BY_SOFTWARE
00030 #define TAPs_GRID_GENERATOR_BY_HARDWARE
00031 //-----------------------------------------------------------------------------
00032 //=============================================================================
00033 
00034 //=============================================================================
00035 // TAPs in TIPS
00036 //#define TAPs_IN_TIPS
00037 #ifdef TAPs_IN_TIPS
00038 //#include <TIPS_MyMod/lib/mesh/polymesh.h>
00039 #include <TIPS/lib/haptic/mesh.h>
00040 #endif // TAPs_IN_TIPS
00041 //=============================================================================
00042 
00043 BEGIN_NAMESPACE_TAPs
00044 //=============================================================================
00045 // Forward Declaration
00046 //-----------------------------------------------------------------------------
00047 //-----------------------------------------------------------------------------
00048 //=============================================================================
00049 
00050 //=============================================================================
00051 // Class GridGenerator
00052 //-----------------------------------------------------------------------------
00053 template <typename T>
00054 class GridGenerator {
00055 public:
00056     //---------------------------------------------------------------
00058     GridGenerator ( 
00059         OpenGL::Model<T> * pModel = NULL, 
00060         bool useSoftware  = false, 
00061         bool genInversely = false, 
00062         bool useDefaultIntpValue = false,   
00063         //bool useDefaultIntpValue = true,  //!< Use default global interpolation value
00064         T    defaultIntpValue = 0.5 
00065     );
00067     ~GridGenerator ();
00068     //---------------------------------------------------------------
00069     // Operation(s)
00070     bool ReGenerateGrids ();
00071     bool GenerateGridsWithDimension ( 
00072         T iGridWidth, T iGridHeight, T iGridDepth, 
00073         Vector3<T> * lowPoint  = NULL, 
00074         Vector3<T> * highPoint = NULL 
00075     );
00076     bool GenerateGridsWithSize ( 
00077         int iNumOfXSlices, int iNumOfYSlices, int iNumOfZSlices, 
00078         Vector3<T> * lowPoint  = NULL, 
00079         Vector3<T> * highPoint = NULL 
00080     );
00081 
00082     //*
00083     // DEBUG for Checking Marching Cubes Tables
00084     bool GenerateDefinedGrids2x2x2 ( 
00085         unsigned char config = 1, 
00086         int iNumOfXSlices = 2, int iNumOfYSlices = 2, int iNumOfZSlices = 2, 
00087         Vector3<T> * lowPoint  = NULL, 
00088         Vector3<T> * highPoint = NULL 
00089     );  // flags of cube's vertices
00090     //*/
00091 
00092     //---------------------------------------------------------------
00093     // Grid Info Accessor(s)
00094     //inline Vector3<T> GetGridDimension () { return Vector3<T>( m_atGridDimension ) };
00095     //inline Vector3<T> GetGridSize ()      { return Vector3<T>( m_atGridSize ) };
00096     inline T GetGridDimensionX () const { return m_atGridDimension[0]; }
00097     inline T GetGridDimensionY () const { return m_atGridDimension[1]; }
00098     inline T GetGridDimensionZ () const { return m_atGridDimension[2]; }
00099     inline int GetGridSizeX () const    { return m_auiGridSize[0]; }
00100     inline int GetGridSizeY () const    { return m_auiGridSize[1]; }
00101     inline int GetGridSizeZ () const    { return m_auiGridSize[2]; }
00102     inline Vector3<T> GetAABBMinPt () const { return Vector3<T>( m_tAABBMin ); }
00103     inline Vector3<T> GetAABBMaxPt () const { return Vector3<T>( m_tAABBMax ); }
00104     //-------------
00105     //inline void SetGridDimensionX ( T v ) { m_atGridDimension[0] = v; ... }
00106     //inline void SetGridDimensionY ( T v ) { m_atGridDimension[1] = v; ... }
00107     //inline void SetGridDimensionZ ( T v ) { m_atGridDimension[2] = v; ... }
00108     //inline void SetGridSizeX ( T v )  { m_auiGridSize[0] = v; ... }
00109     //inline void SetGridSizeY ( T v )  { m_auiGridSize[1] = v; ... }
00110     //inline void SetGridSizeZ ( T v )  { m_auiGridSize[2] = v; ... }
00111     //---------------------------------------------------------------
00112 
00113     //===============================================================
00114     // Adjustable Parameters -- User Interaction
00115     //---------------------------------------------------------------
00116     bool DoesGridGeneratorUseSoftware () const { return m_bUseSoftwareToGenGrid; }
00117     bool DoesGridGeneratorUseHardware () const { return !m_bUseSoftwareToGenGrid; }
00118     void SetGridGeneratorToUseSoftware () { m_bUseSoftwareToGenGrid = true; }
00119     void SetGridGeneratorToUseHardware () { m_bUseSoftwareToGenGrid = false; }
00120     //---------------------------------------------------------------
00121     bool DoesGridGeneratorGenerateGridInversely () const { return m_bGenGridInversely; }
00122     bool DoesGridGeneratorGenerateGridNormally  () const { return !m_bGenGridInversely; }
00123     void SetGridGeneratorToGenerateGridInversely () { m_bGenGridInversely = true; }
00124     void SetGridGeneratorToGenerateGridNormally  () { m_bGenGridInversely = false; }
00125     //---------------------------------------------------------------
00142     bool DoesGridGeneratorUseDefaultIntpVal () const        { return m_bUseDefaultIntpVal; }
00143     bool DoesGridGeneratorUseMeshOffsetForIntpVal () const  { return !m_bUseDefaultIntpVal; }
00144     void SetGridGeneratorUseDefaultIntpVal ()       { m_bUseDefaultIntpVal = true; }
00145     void SetGridGeneratorUseMeshOffsetForIntpVal () { m_bUseDefaultIntpVal = false; }
00146     void GetDefaultIntpVal () const     { return m_tDefaultIntpVal; }
00147     void SetDefaultIntpVal ( T val )    { m_tDefaultIntpVal = val; }
00148     //---------------------------------------------------------------
00149     //===============================================================
00150 
00151     //---------------------------------------------------------------
00152     OpenGL::Model<T> * GetModelUseToGenGrid () const { return m_pModel; }
00153     void SetModelUseToGenGrid ( OpenGL::Model<T> * pModel ) {
00154         if ( m_pModel != pModel ) {
00155             DeleteGrids();
00156             m_atGridDimension[0] = m_atGridDimension[1] = m_atGridDimension[2] = 0;
00157             m_auiGridSize[0] = m_auiGridSize[1] = m_auiGridSize[2] = 0;
00158         }
00159     }
00160     //---------------------------------------------------------------
00161     void DeleteGrids ();    // delete all grid info
00162     //---------------------------------------------------------------
00163     inline bool IsGridGenerated ()  { return m_atVertexPosition != NULL; }
00164     //---------------------------------------------------------------
00165     // For Vertex Flag
00166     static enum VertexFlag {
00167         UNSET                   = 0,
00168         OUTSIDE_MODEL,
00169         RIGHT_OUTSIDE_BOUNDARY,
00170         ON_BOUNDARY,
00171         RIGHT_INSIDE_BOUNDARY,
00172         INSIDE_MODEL,
00173         DUMMY
00174     };
00175     //---------------------------------------------------------------
00176     // Data Accessor(s)
00180     inline T **** const         ReturnPtrToVertexPositionData ()        { return m_atVertexPosition; }
00181     inline T const **** const   ReturnPtrToVertexPositionData () const  { return m_atVertexPosition; }
00185     inline T **** const         ReturnPtrToVertexNormalData ()          { return m_atVertexNormal; }
00186     inline T const **** const   ReturnPtrToVertexNormalData () const    { return m_atVertexNormal; }
00190     inline VertexFlag  *** const        ReturnPtrToVertexFlagData ()        { return m_abVertexFlag1; }
00191     inline VertexFlag const *** const   ReturnPtrToVertexFlagData () const  { return m_abVertexFlag1; }
00195     inline T **** const         ReturnPtrToVertexInterpolationData ()       { return m_atVertexInterpolation; }
00196     inline T const **** const   ReturnPtrToVertexInterpolationData () const { return m_atVertexInterpolation; }
00197 #ifdef TAPs_GRID_WITH_TWO_INTERPOLATIONS
00198     inline T **** const         ReturnPtrToVertexInterpolationDataToo ()        { return m_atVertexInterpolationToo; }
00199     inline T const **** const   ReturnPtrToVertexInterpolationDataToo () const  { return m_atVertexInterpolationToo; }
00200 #endif
00201     //---------------------------------------------------------------
00202 private:
00203     //===============================================================
00204     // MEMBER FUNCTION(s)
00205     //---------------------------------------------------------------
00206     void FindBoundingBoxDimension ( Vector3<T> * lowPoint, Vector3<T> * highPoint );
00207     //-----------------------------------------------------
00208 
00209 //=============================================================================
00210 // TAPs in TIPS
00211 #ifdef TAPs_IN_TIPS
00212 public:
00213     void GenerateGridsInTIPS ();
00214 private:
00215         void GenerateGridsForPositionInTIPS ( Vector3<T> pts[8] );
00216     #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00217         void SetGridPtsToInOutBoundary_SW_InTIPS();
00218         void SetAllGridPtsToInOut_SW_InTIPs();
00219     #endif // #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00220     #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00221         void SetAllGridPtsToInOut_HW_InTIPs();
00222     #endif // #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00223         void SetVertexInterpolationDataInTIPS ();
00224 #endif // #ifdef TAPs_IN_TIPS
00225 //=============================================================================
00226     void GenerateGrids ();
00227     //
00228     void GenerateGridsForPosition ();
00229     void GenerateGridsForNormal ();
00230     void GenerateGridsForFlag1 ();
00231     void GenerateGridsForVertexInterpolation ();
00232     //
00233     void AllocateGridsForPosition ();
00234     void AllocateGridsForNormal ();
00235     void AllocateGridsForFlag1 ();
00236     void AllocateGridsForVertexInterpolation ();
00237     //
00238     void DeleteGridsForPosition ();
00239     void DeleteGridsForNormal ();
00240     void DeleteGridsForFlag1 ();
00241     void DeleteGridsForVertexInterpolation ();
00242     //-----------------------------------------------------
00243     // Sampling grid point values by Software
00244 #ifdef TAPs_GRID_GENERATOR_BY_SOFTWARE
00245     void SetGridPointsToInsideOutsideModelBoundary_SW ();
00246     void SetGridPointsToInsideOutsideModelBoundary_SW ( OpenGL::PolygonalModel<T> * pPModel );  // NOT IMPLEMENTED YET!
00247     void SetGridPointsToInsideOutsideModelBoundary_SW ( OpenGL::XPolygonalModel<T> * pXPModel );
00248     void SetGridPointsToInsideOutsideModelBoundary_SW ( OpenGL::HalfEdgeModel<T> * pHEModel );  // NOT IMPLEMENTED YET!
00249     void SetAllGridPointsToInsideOutsideModel_SW ();
00250 #endif
00251     //-----------------------------------------------------
00252     // Sampling grid point values by Hardware (OpenGL)
00253 #ifdef TAPs_GRID_GENERATOR_BY_HARDWARE
00254     void SetAllGridPointsToInsideOutsideModel_HW ();
00255     void SetAllGridPointsToInsideOutsideModel_HW ( OpenGL::PolygonalModel<T> * pPModel );   // NOT IMPLEMENTED YET!
00256     void SetAllGridPointsToInsideOutsideModel_HW ( OpenGL::XPolygonalModel<T> * pXPModel );
00257     void SetAllGridPointsToInsideOutsideModel_HW ( OpenGL::HalfEdgeModel<T> * pHEModel );   // NOT IMPLEMENTED YET!
00258 #endif
00259 
00260 public:
00261     //===============================================================
00262     // DEBUG BY DRAWING
00263     // Check SetGridPointsToInsideOutsideModelBoundary_SW ( OpenGL::XPolygonalModel<T> * pXPModel )
00264     void DRAW_SetGridPointsToInsideOutsideModelBoundary_SW ( OpenGL::XPolygonalModel<T> * pXPModel );
00265     //===============================================================
00266 private:
00267 
00268     //-----------------------------------------------------
00269     // Clear extra right outside points from the generated grid data
00270     void ClearExtraRightOutsidePoints ();
00271     //-----------------------------------------------------
00272     // Inverse Points -- Outside <--> Inside & Empty becomes Inside
00273     void InverseInsideOutside ();
00274     //-----------------------------------------------------
00275     // This one makes the grid generator uses the passed default value 
00276     // for the global interpolation value.
00277     void SetVertexInterpolationData ( T tDefaultVal );
00278     //
00279     // This one makes the grid generator uses either the (already set) 
00280     // default value or uses randomly generated values for interpolation 
00281     // values.
00282     void SetVertexInterpolationData ();
00283     void SetVertexInterpolationData ( OpenGL::PolygonalModel<T> * pPModel );    // NOT IMPLEMENTED YET!
00284     void SetVertexInterpolationData ( OpenGL::XPolygonalModel<T> * pXPModel );
00285     void SetVertexInterpolationData ( OpenGL::HalfEdgeModel<T> * pHEModel );    // NOT IMPLEMENTED YET!
00286     //---------------------------------------------------------------
00287     //===============================================================
00288     // DATA MEMBERs
00289     //---------------------------------------------------------------
00290     T   m_tAABBMin[3];      // For Bounding Box
00291     T   m_tAABBMax[3];      // For Bounding Box
00292     //-------------------------------------------
00293 
00294 //=============================================================================
00295 // TAPs in TIPS
00299     //-----------------------
00300     //     2---1
00301     //    /|  /|
00302     //   6-3-5-0
00303     //   |/  |/
00304     //   7---4
00305     // The default box is a cube center at (0,0,0).
00306     //-----------------------
00307 #ifdef TAPs_IN_TIPS
00308     Vector3<T>  m_vBBPts[8];
00309     std::vector< ::SurfLab::haptic::Mesh * >    m_pTIPSMeshes;
00310 #endif // #ifdef TAPs_IN_TIPS
00311 //=============================================================================
00312 
00313     //-------------------------------------------
00314     T   m_atGridDimension[3];   // width, height, depth of each cell
00315     int m_auiGridSize[3];       // number of cells in x, y, and z
00316 
00317     //===============================================================
00318     // Adjustable Parameters -- User Interaction
00319     //---------------------------------------------------------------
00320     bool m_bUseSoftwareToGenGrid;   // if set use SW, else use HW
00321     bool m_bGenGridInversely;       // if set grid points are generated inversely
00322                                     // e.g. outside pts become inside pts
00323 
00324     // Fop Marching Cubes Algorithm
00325     bool m_bUseDefaultIntpVal;  // all vertices will have the same default interpolation value
00326     T    m_tDefaultIntpVal;     // default interpolation value for each vertex
00327     //---------------------------------------------------------------
00328     //===============================================================
00329 
00330     //---------------------------------------------------------------
00331     // Unmanaged Pointer(s)
00332     OpenGL::Model<T> *  m_pModel;       // pointer to a model
00333     //---------------------------------------------------------------
00334     // Managed Pointer(s)
00335     //T *** tempData;           // temp data
00336     //-----------------------------------------------------
00337     // Vertex Attributes
00341     T ****          m_atVertexPosition;         // vertex position
00342     T ****          m_atVertexNormal;           // vertex normal
00343     VertexFlag ***  m_abVertexFlag1;            // vertex flag1
00344                                                 // See enum VertexFlag above
00354     T ****          m_atVertexInterpolation;    // interpolation value (pos)
00355 #ifdef TAPs_GRID_WITH_TWO_INTERPOLATIONS
00356     T ****          m_atVertexInterpolationToo; // interpolation value (neg)
00357 #endif
00358     //---------------------------------------------------------------
00359 //=============================================================================
00360 //-----------------------------------------------------------------------------
00361 // With OpenGL
00362 #if defined(__gl_h_) || defined(__GL_H__)
00363 public:
00364     //---------------------------------------------------------------
00365     // DrawByOpenGL
00366     // GLenum drawStyle; GLU_FILL, GLU_LINE, GLU_SILHOUETTE, and GLU_POINT
00367     // Expensive Drawing, since it draws each particles using sphere.
00368     virtual void DrawByOpenGL (
00369                 GLenum drawStyle = GLU_LINE, 
00370                 Vector4<T> color = Vector4<T>( 0.77, 0.85, 0.75, 0.75 ) 
00371     );
00372     //---------------------------------------------------------------
00373     virtual void DrawBoundingBoxByOpenGL ( 
00374         Vector4<T> color = Vector4<T>( 0, 0, 1, 0.5 ) );
00375     //---------------------------------------------------------------
00376     virtual void DrawAsPointsByOpenGL ( 
00377                 Vector4<T> color = Vector4<T>( 0.77, 0.85, 0.75, 0.75 ) 
00378     );
00379     //---------------------------------------------------------------
00380     virtual void DrawAsBoxesByOpenGL ( 
00381                 Vector4<T> color = Vector4<T>( 0.77, 0.85, 0.75, 0.75 ) 
00382     );
00383     //---------------------------------------------------------------
00384     virtual void DrawAllOuterPointsByOpenGL ( 
00385         Vector4<T> color = Vector4<T>( 1, 0, 0, 0.5 ) );
00386     //---------------------------------------------------------------
00387     virtual void DrawAllInnerPointsByOpenGL ( 
00388         Vector4<T> color = Vector4<T>( 0, 1, 0, 0.5 ) );
00389     //---------------------------------------------------------------
00403     virtual void DrawAPlaneByOpenGL ( int drawDirection = 2, 
00404         Vector4<T> color = Vector4<T>( 0, 1, 0, 0.5 ) );
00405 //  virtual void DrawAPlaneByOpenGL ( int planeNo, int drawDirection = 2, 
00406 //      Vector4<T> color = Vector4<T>( 0, 1, 0, 0.5 ) );
00407     //---------------------------------------------------------------
00408 //  virtual void DrawAPlaneByOpenGL ( 
00409 //              int planeNo,
00410 //              int plane = 1       // 2: xy-, 0: yz-, 1: xz-plane
00411 //  );
00412 
00413 
00414     /*
00415     //---------------------------------------------------------------
00416     // DEBUG
00417     int gMinNum[3], gMaxNum[3];
00418     Vector3<T> vMin, vMax, vPos;
00419     Vector3<T> gridBoxCenterPt;
00420     T minMoveStep[3], maxMoveStep[3];
00421     //---------------------------------------------------------------
00422     //*/
00423 
00424 //=============================================================================
00425 // TAPs in TIPS
00426 #ifdef TAPs_IN_TIPS
00427 //-------------------------------------------------------------------
00428     //-----------------------------------------------------
00429     // Draw Fns
00430     void Draw8PtBoxByOpenGL ( 
00431         T r = 0.8, T g = 0.8, T b = 0.5, T a = 0.25 );
00432     void Draw8PtBoxCellsByOpenGL ( 
00433         T r = 1.0, T g = 1.0, T b = 1.0, T a = 0.5 );
00434     //-----------------------------------------------------
00435     // Get/Set Fns
00436     void GetGridSize( int &x, int &y, int &z ) const {
00437         x = m_auiGridSize[0];
00438         y = m_auiGridSize[1];
00439         z = m_auiGridSize[2];
00440     }
00441     void GetGridWidth( T &x, T &y, T &z ) const {
00442         x = (m_vBBPts[0] - m_vBBPts[3]).Length();
00443         y = (m_vBBPts[2] - m_vBBPts[3]).Length();
00444         z = (m_vBBPts[7] - m_vBBPts[3]).Length();
00445     }
00446     void GetCellDimension( T &x, T &y, T &z ) const {
00447         x = m_atGridDimension[0];
00448         y = m_atGridDimension[1];
00449         z = m_atGridDimension[2];
00450     }
00451     void SetGridSize( int x, int y, int z ) {
00452         int minSize = 3;
00453         if ( x < minSize ) x = minSize;
00454         if ( y < minSize ) y = minSize;
00455         if ( z < minSize ) z = minSize;
00456         m_auiGridSize[0] = x;
00457         m_auiGridSize[1] = y;
00458         m_auiGridSize[2] = z;
00459         //-------------------
00460         m_atGridDimension[0] = (m_vBBPts[0] - m_vBBPts[3]).Length() / (x-1.0);
00461         m_atGridDimension[1] = (m_vBBPts[2] - m_vBBPts[3]).Length() / (y-1.0);
00462         m_atGridDimension[2] = (m_vBBPts[7] - m_vBBPts[3]).Length() / (z-1.0);
00463     }
00464     void SetGridWidth( T x, T y, T z ) {
00465         int minWidth = 0.001;
00466         if ( x < minWidth ) x = minWidth;
00467         if ( y < minWidth ) y = minWidth;
00468         if ( z < minWidth ) z = minWidth;
00469         T halfX = static_cast<T>( x / 2.0 );
00470         T halfY = static_cast<T>( y / 2.0 );
00471         T halfZ = static_cast<T>( z / 2.0 );
00472         m_vBBPts[0].SetXYZ(  halfX, -halfY, -halfZ );
00473         m_vBBPts[1].SetXYZ(  halfX,  halfY, -halfZ );
00474         m_vBBPts[2].SetXYZ( -halfX,  halfY, -halfZ );
00475         m_vBBPts[3].SetXYZ( -halfX, -halfY, -halfZ );
00476         m_vBBPts[4].SetXYZ(  halfX, -halfY,  halfZ );
00477         m_vBBPts[5].SetXYZ(  halfX,  halfY,  halfZ );
00478         m_vBBPts[6].SetXYZ( -halfX,  halfY,  halfZ );
00479         m_vBBPts[7].SetXYZ( -halfX, -halfY,  halfZ );
00480         //-------------------
00481         m_atGridDimension[0] = (m_vBBPts[0] - m_vBBPts[3]).Length() / (m_auiGridSize[0]-1.0);
00482         m_atGridDimension[1] = (m_vBBPts[2] - m_vBBPts[3]).Length() / (m_auiGridSize[1]-1.0);
00483         m_atGridDimension[2] = (m_vBBPts[7] - m_vBBPts[3]).Length() / (m_auiGridSize[2]-1.0);
00484     }
00485 //-------------------------------------------------------------------
00486 #endif // #ifdef TAPs_IN_TIPS
00487 //=============================================================================
00488 
00489     //---------------------------------------------------------------
00490     static GLuint   g_uiDisplayList;    // for OpenGL drawing
00491     static GLenum   g_eDrawStyle;       // for OpenGL drawing
00492 #endif
00493 //-----------------------------------------------------------------------------
00494 //=============================================================================
00495 };  // End Class GridGenerator
00496 //=============================================================================
00497 END_NAMESPACE_TAPs
00498 //-----------------------------------------------------------------------------
00499 // Include definition if TAPs_USE_EXPORT is not defined
00500 //#if !defined( TAPs_USE_EXPORT )
00501     #include "TAPsGridGenerator_ForTheDefModBasedOnMSS.cpp"
00502 //#endif
00503 //-----------------------------------------------------------------------------
00504 #endif // TAPs_GRID_GENERATOR_IN_TIPS_HPP
00505 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00506 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines