Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / sph / common / point_set.h
1 /*
2   FLUIDS v.1 - SPH Fluid Simulator for CPU and GPU
3   Copyright (C) 2008. 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_POINT_SET
24         #define DEF_POINT_SET
25
26         #include <iostream>
27         #include <vector>
28         #include <stdio.h>
29         #include <stdlib.h>
30         #include <math.h>
31         
32         #include "common_defs.h"
33         #include "geomx.h"
34         #include "vector.h"     
35
36         typedef signed int              xref;
37         
38         #define MAX_NEIGHBOR            80
39         
40         #define MAX_PARAM                       21
41
42         // Scalar params
43         #define PNT_DRAWMODE            0
44                 #define PNT_SPHERE                      0
45                 #define PNT_POINT                       1
46         #define PNT_DRAWSIZE            1               
47         #define POINT_GRAV                      2
48         #define PLANE_GRAV                      3
49
50         // Vector params
51         #define EMIT_POS                        0
52         #define EMIT_ANG                        1
53         #define EMIT_DANG                       2
54         #define EMIT_SPREAD                     3
55         #define EMIT_RATE                       4
56         #define POINT_GRAV_POS          5       
57         #define PLANE_GRAV_DIR          6       
58
59
60         #define BPOINT                          0
61         #define BPARTICLE                       1
62
63         struct Point {
64                 Vector3DF               pos;
65                 DWORD                   clr;
66                 int                             next;
67         };
68         
69         struct Particle {       
70                 Vector3DF               pos;
71                 DWORD                   clr;
72                 int                             next;
73                 Vector3DF               vel;                    
74                 Vector3DF               vel_eval;               
75                 unsigned short  age;
76         };
77
78         class PointSet : public GeomX {
79         public:
80                 PointSet ();
81
82                 // Point Sets
83                 
84                 virtual void Initialize ( int mode, int max );
85                 virtual void Draw ( float* view_mat, float rad );               
86                 virtual void Reset ();          
87                 virtual int AddPoint ();                
88                 virtual int AddPointReuse ();
89                 Point* GetPoint ( int n )               { return (Point*) GetElem(0, n); }              
90                 int     NumPoints ()                            { return NumElem(0); }          
91
92                 // Metablobs
93                 virtual float GetValue ( float x, float y, float z );
94                 virtual Vector3DF GetGradient ( float x, float y, float z );
95         //      virtual float GetValue ( float x, float y, float z, Vector3DF& dir );
96                 virtual DWORD GetColor ( float x, float y, float z );
97                 
98                 // Particle system              
99                 virtual void Run ();
100                 virtual void Advance ();                
101                 virtual void Emit ( float spacing );                    
102
103                 // Misc
104                 virtual void AddVolume ( Vector3DF min, Vector3DF max, float spacing );
105
106                 // Parameters                   
107                 void SetParam (int p, float v )         { m_Param[p] = v; }
108                 void SetParam (int p, int v )           { m_Param[p] = (float) v; }
109                 float GetParam ( int p )                        { return (float) m_Param[p]; }
110                 Vector3DF GetVec ( int p )                      { return m_Vec[p]; }
111                 void SetVec ( int p, Vector3DF v )      { m_Vec[p] = v; }
112                 void Toggle ( int p )                           { m_Toggle[p] = !m_Toggle[p]; }         
113                 bool GetToggle ( int p )                        { return m_Toggle[p]; }
114
115                 float GetDT()                                           { return (float) m_DT; }
116
117                 // Spatial Subdivision
118                 void Grid_Setup ( Vector3DF min, Vector3DF max, float sim_scale, float cell_size, float border );               
119                 void Grid_Create ();
120                 void Grid_InsertParticles ();   
121                 void Grid_Draw ( float* view_mat );             
122                 void Grid_FindCells ( Vector3DF p, float radius );
123                 int Grid_FindCell ( Vector3DF p );
124                 Vector3DF GetGridRes ()         { return m_GridRes; }
125                 Vector3DF GetGridMin ()         { return m_GridMin; }
126                 Vector3DF GetGridMax ()         { return m_GridMax; }
127                 Vector3DF GetGridDelta ()       { return m_GridDelta; }
128                 int GetGridCell ( int x, int y, int z );
129                 Point* firstGridParticle ( int gc, int& p );
130                 Point* nextGridParticle ( int& p );
131                 unsigned short* getNeighborTable ( int n, int& cnt );
132
133         protected:
134                 int                                                     m_Frame;                
135
136                 // Parameters
137                 double                                          m_Param [ MAX_PARAM ];                  // see defines above
138                 Vector3DF                                       m_Vec [ MAX_PARAM ];
139                 bool                                            m_Toggle [ MAX_PARAM ];
140                 
141                 // Particle System
142                 double                                          m_DT;
143                 double                                          m_Time;
144
145                 // Spatial Grid
146                 std::vector< int >                      m_Grid;
147                 std::vector< int >                      m_GridCnt;
148                 int                                                     m_GridTotal;                    // total # cells
149                 Vector3DF                                       m_GridMin;                              // volume of grid (may not match domain volume exactly)
150                 Vector3DF                                       m_GridMax;
151                 Vector3DF                                       m_GridRes;                              // resolution in each axis
152                 Vector3DF                                       m_GridSize;                             // physical size in each axis
153                 Vector3DF                                       m_GridDelta;
154                 float                                           m_GridCellsize;
155                 int                                                     m_GridCell[27];
156
157                 // Neighbor Table
158                 unsigned short                          m_NC[65536];                    // neighbor table (600k)
159                 unsigned short                          m_Neighbor[65536][MAX_NEIGHBOR];        
160                 float                                           m_NDist[65536][MAX_NEIGHBOR];
161
162                 static int m_pcurr;
163         };
164
165 #endif