Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / sph / common / mesh.h
1 /*
2   FLUIDS v.1 - SPH Fluid Simulator for CPU and GPU
3   Copyright (C) 2009. Rama Hoetzlein, http://www.rchoetzlein.com
4
5   ZLib license
6   This software is provided 'as-is', without any express or implied
7   warranty.  In no event will the authors be held liable for any damages
8   arising from the use of this software.
9
10   Permission is granted to anyone to use this software for any purpose,
11   including commercial applications, and to alter it and redistribute it
12   freely, subject to the following restrictions:
13
14   1. The origin of this software must not be misrepresented; you must not
15      claim that you wrote the original software. If you use this software
16      in a product, an acknowledgment in the product documentation would be
17      appreciated but is not required.
18   2. Altered source versions must be plainly marked as such, and must not be
19      misrepresented as being the original software.
20   3. This notice may not be removed or altered from any source distribution.
21 */
22
23 #ifndef DEF_MESH
24         #define DEF_MESH
25
26         #include <vector>
27         #include <gl/glut.h>
28
29         #include "geomx.h"
30         #include "mesh_info.h"  
31         #include "vector.h"
32
33         //#include "mfile.h"
34
35         //#define MESH_DEBUG
36
37         #ifdef MESH_DEBUG
38                 #define VERT_DELTA              10000
39                 #define EDGE_DELTA              20000
40                 #define FACE_DELTA              30000
41         #else
42                 #define VERT_DELTA              0
43                 #define EDGE_DELTA              0
44                 #define FACE_DELTA              0
45         #endif
46
47         #define PLY_UINT                        0
48         #define PLY_INT                         1
49         #define PLY_FLOAT                       2
50         #define PLY_LIST                        3
51         #define PLY_VERTS                       4
52         #define PLY_FACES                       5
53
54         struct PlyProperty {
55                 char                                            type;
56                 std::string                                     name;
57         };
58         struct PlyElement {
59                 int                                                     num;
60                 char                                            type;           // 0 = vert, 1 = face
61                 std::vector<PlyProperty>        prop_list;
62         };
63
64         class Mesh : public GeomX, public MeshInfo {
65         public:
66                 Mesh ();        
67
68                 //virtual objType GetType ()                    { return 'mesh'; }
69                 
70                 // Distributed functions                
71                 //virtual void onUpdate ( objData dat, mint::Event* e );
72                 //void UpdateMesh ();
73
74                 // Generic functions
75                 void InitStatic ();
76                 void DrawGL ( float* viewmat );
77                 void DrawFaceGL ( float* viewmat );
78                 void Measure ();
79                 Mesh& operator= ( Mesh& op2 );
80
81                 // Load PLY mesh
82                 void LoadPly ( char* fname, float s );
83                 void AddPlyElement ( char typ, int n );
84                 void AddPlyProperty ( char typ, std::string name ); 
85                 void LoadPlyVerts ();
86                 void LoadPlyFaces ();
87                 int FindPlyElem ( char typ );
88                 int FindPlyProp ( int elem, std::string name );
89
90                 // Vertex, Face, Edge functions
91                 xref AddVert (float x, float y, float z )               { return (this->*m_AddVertFunc) (x, y, z); }
92                 xref AddFaceFast (xref v1, xref v2, xref v3 )   { return (this->*m_AddFaceFast3Func) (v1, v2, v3); }
93                 xref AddFaceFast (xref v1, xref v2, xref v3, xref v4 )  { return (this->*m_AddFaceFast4Func) (v1, v2, v3, v4); }
94                 xref (Mesh::*m_AddVertFunc) (float x, float y, float z);
95                 xref (Mesh::*m_AddFaceFast3Func) (xref v1, xref v2, xref v3);
96                 xref (Mesh::*m_AddFaceFast4Func) (xref v1, xref v2, xref v3, xref v4);
97                 
98                 int NumVert ()  { return NumElem ( m_Vbuf ); }
99                 int NumEdge ()  { return NumElem ( m_Ebuf ); }
100                 int NumFace ()  { return NumElem ( m_Fbuf ); }
101                 
102                 void IncFace ( int n )          { m_CurrF += n; }
103                 void DebugHeap ();
104
105                 // FVF - Face-Vertex-Face Mesh
106                 void CreateFVF ();
107                 void ClearFVF ();
108                 void SetFuncFVF ();
109                 xref AddVertFVF ( float x, float y, float z );
110                 xref AddFaceFast3FVF ( xref v1, xref v2, xref v3 );
111                 xref AddFaceFast4FVF ( xref v1, xref v2, xref v3, xref v4 );
112                 VertFVF* GetVertFVF ( int n )           { return (VertFVF*) (mBuf[m_Vbuf].data + n*mBuf[m_Vbuf].stride); }
113                 FaceFVF* GetFaceFVF ( int n )           { return (FaceFVF*) (mBuf[m_Fbuf].data + n*mBuf[m_Fbuf].stride); }
114                 void* GetExtraFVF ( VertFVF* v )        { return ((char*) v + miBufSize[(int) FVF][BVert]); }
115                 void ComputeNormalsFVF ();
116                 void SetNormalFVF ( int n, Vector3DF norm );
117                 void SetColorFVF ( int n, DWORD clr );
118                 void SmoothFVF ( int iter );
119                 void DebugFVF ();
120                 void DrawVertsFVF ( float* viewmat, int a, int b );
121                 void DrawFacesFVF ( float* viewmat, int a, int b );     
122
123                 // CM - Connected Mesh
124                 void CreateCM ();
125                 void SetFuncCM ();
126                 xref AddVertCM ( float x, float y, float z );
127                 xref AddFaceFast3CM ( xref v1, xref v2, xref v3 );
128                 xref AddFaceFast4CM ( xref v1, xref v2, xref v3, xref v4 );
129                 xref AddEdgeCM ( xref v1, xref v2 );
130                 xref FindEdgeCM ( xref v1, xref v2 );   
131                 VertCM* GetVertCM ( int n )             { return (VertCM*) (mBuf[m_Vbuf].data + n*mBuf[m_Vbuf].stride); }
132                 EdgeCM* GetEdgeCM ( int n )             { return (EdgeCM*) (mBuf[m_Ebuf].data + n*mBuf[m_Ebuf].stride); }
133                 FaceCM* GetFaceCM ( int n )             { return (FaceCM*) (mBuf[m_Fbuf].data + n*mBuf[m_Fbuf].stride); }
134                 void* GetExtraCM ( VertCM* v )  { return ((char*) v + miBufSize[(int) CM][BVert] ); }
135                 void DebugCM ();
136                 void DrawVertsCM ( float* viewmat, int a, int b );
137                 void DrawFacesCM ( float* viewmat, int a, int b );      
138                 void DrawEdgesCM ( float* viewmat, int a, int b );      
139
140                 MFormat GetMeshBufs ( char& v, char& e, char& f )       { v = m_Vbuf; e = m_Ebuf; f = m_Fbuf; return m_Mform; }
141
142         protected:
143                 MFormat         m_Mform;                        // Mesh format
144                 char            m_Vbuf;
145                 char            m_Ebuf;
146                 char            m_Fbuf;
147
148                 int                     m_CurrF;
149
150                 std::vector< PlyElement* >      m_Ply;
151                 //File          m_File;
152                 int                     m_PlyCurrElem;
153
154                 static bool             mbInitStatic;
155
156                 Vector3DF       mT;
157         };
158
159 #endif
160