svn merge -r 15649:15800 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[blender.git] / intern / elbeem / intern / elbeem.cpp
1 /******************************************************************************
2  *
3  * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
4  * All code distributed as part of El'Beem is covered by the version 2 of the 
5  * GNU General Public License. See the file COPYING for details.
6  * Copyright 2003-2006 Nils Thuerey
7  *
8  * Main program functions
9  */
10
11 #include "elbeem.h"
12 #include "ntl_blenderdumper.h"
13 extern "C" void elbeemCheckDebugEnv(void);
14
15 #include "ntl_world.h"
16 #include "ntl_geometrymodel.h"
17
18 /*****************************************************************************/
19 // region of interest global vars
20 // currently used by e.g. fsgr solver
21 double guiRoiSX = 0.0;
22 double guiRoiSY = 0.0;
23 double guiRoiSZ = 0.0;
24 double guiRoiEX = 1.0;
25 double guiRoiEY = 1.0;
26 double guiRoiEZ = 1.0;
27 int guiRoiMaxLev=6, guiRoiMinLev=0;
28
29 //! global raytracer pointer (=world)
30 ntlWorld *gpWorld = NULL;
31
32
33
34 // API
35
36 // reset elbeemSimulationSettings struct with defaults
37 extern "C" 
38 void elbeemResetSettings(elbeemSimulationSettings *set) {
39         if(!set) return;
40   set->version = 3;
41   set->domainId = 0;
42         for(int i=0 ; i<3; i++) set->geoStart[i] = 0.0;
43         for(int i=0 ; i<3; i++) set->geoSize[i] = 1.0;
44   set->resolutionxyz = 64;
45   set->previewresxyz = 24;
46   set->realsize = 1.0;
47   set->viscosity = 0.000001;
48
49         for(int i=0 ; i<2; i++) set->gravity[i] = 0.0;
50         set->gravity[2] = -9.81;
51
52   set->animStart = 0; 
53         set->aniFrameTime = 0.01;
54         set->noOfFrames = 10;
55   set->gstar = 0.005;
56   set->maxRefine = -1;
57   set->generateParticles = 0.0;
58   set->numTracerParticles = 0;
59   strcpy(set->outputPath,"./elbeemdata_");
60
61         set->channelSizeFrameTime=0;
62         set->channelFrameTime=NULL;
63         set->channelSizeViscosity=0;
64         set->channelViscosity=NULL;
65         set->channelSizeGravity=0;
66         set->channelGravity=NULL; 
67
68         set->domainobsType= FLUIDSIM_OBSTACLE_NOSLIP;
69         set->domainobsPartslip= 0.;
70         set->generateVertexVectors = 0;
71         set->surfaceSmoothing = 1.;
72         set->surfaceSubdivs = 1;
73
74         set->farFieldSize = 0.;
75         set->runsimCallback = NULL;
76         set->runsimUserData = NULL;
77
78         // init identity
79         for(int i=0; i<16; i++) set->surfaceTrafo[i] = 0.0;
80         for(int i=0; i<4; i++) set->surfaceTrafo[i*4+i] = 1.0;
81 }
82
83 // start fluidsim init
84 extern "C" 
85 int elbeemInit() {
86         setElbeemState( SIMWORLD_INITIALIZING );
87         setElbeemErrorString("[none]");
88         resetGlobalColorSetting();
89
90         elbeemCheckDebugEnv();
91         debMsgStd("performElbeemSimulation",DM_NOTIFY,"El'Beem Simulation Init Start as Plugin, debugLevel:"<<gDebugLevel<<" ...\n", 2);
92         
93         // create world object with initial settings
94         ntlBlenderDumper *elbeem = new ntlBlenderDumper(); 
95         gpWorld = elbeem;
96         return 0;
97 }
98
99 // fluidsim end
100 extern "C" 
101 int elbeemFree() {
102         
103         return 0;
104 }
105
106 // start fluidsim init
107 extern "C" 
108 int elbeemAddDomain(elbeemSimulationSettings *settings) {
109         // has to be inited...
110         if((getElbeemState() == SIMWORLD_INVALID) && (!gpWorld)) { elbeemInit(); }
111         if(getElbeemState() != SIMWORLD_INITIALIZING) { errFatal("elbeemAddDomain","Unable to init simulation world",SIMWORLD_INITERROR); }
112         // create domain with given settings
113         gpWorld->addDomain(settings);
114         return 0;
115 }
116
117 // error message access
118 extern "C" 
119 void elbeemGetErrorString(char *buffer) {
120         if(!buffer) return;
121         strncpy(buffer,getElbeemErrorString(),256);
122 }
123
124 // reset elbeemMesh struct with zeroes
125 extern "C" 
126 void elbeemResetMesh(elbeemMesh *mesh) {
127         if(!mesh) return;
128         // init typedef struct elbeemMesh
129   mesh->type = 0;
130
131         mesh->parentDomainId = 0;
132
133         /* vertices */
134   mesh->numVertices = 0;
135         mesh->vertices = NULL;
136
137         mesh->channelSizeVertices = 0;
138         mesh->channelVertices = NULL;
139
140         /* triangles */
141         mesh->numTriangles = 0;
142   mesh->triangles = NULL;
143
144         /* animation channels */
145         mesh->channelSizeTranslation = 0;
146         mesh->channelTranslation = NULL;
147         mesh->channelSizeRotation = 0;
148         mesh->channelRotation = NULL;
149         mesh->channelSizeScale = 0;
150         mesh->channelScale = NULL;
151
152         /* active channel */
153         mesh->channelSizeActive = 0;
154         mesh->channelActive = NULL;
155
156         mesh->channelSizeInitialVel = 0;
157         mesh->channelInitialVel = NULL;
158
159         mesh->localInivelCoords = 0;
160
161         mesh->obstacleType= FLUIDSIM_OBSTACLE_NOSLIP;
162         mesh->obstaclePartslip= 0.;
163         mesh->obstacleImpactFactor= 1.;
164
165         mesh->volumeInitType= OB_VOLUMEINIT_VOLUME;
166
167         /* name of the mesh, mostly for debugging */
168         mesh->name = "[unnamed]";
169         
170         /* fluid control settings */
171         mesh->cpsTimeStart = 0;
172         mesh->cpsTimeEnd = 0;
173         mesh->cpsQuality = 0;
174         
175         mesh->channelSizeAttractforceStrength = 0;
176         mesh->channelAttractforceStrength = NULL;
177         mesh->channelSizeAttractforceRadius = 0;
178         mesh->channelAttractforceRadius = NULL;
179         mesh->channelSizeVelocityforceStrength = 0;
180         mesh->channelVelocityforceStrength = NULL;
181         mesh->channelSizeVelocityforceRadius = 0;
182         mesh->channelVelocityforceRadius = NULL;
183 }
184
185 int globalMeshCounter = 1;
186 // add mesh as fluidsim object
187 extern "C" 
188 int elbeemAddMesh(elbeemMesh *mesh) {
189         int initType;
190         if(getElbeemState() != SIMWORLD_INITIALIZING) { errFatal("elbeemAddMesh","World and domain not initialized, call elbeemInit and elbeemAddDomain before...", SIMWORLD_INITERROR); }
191
192         switch(mesh->type) {
193                 case OB_FLUIDSIM_OBSTACLE: 
194                         if     (mesh->obstacleType==FLUIDSIM_OBSTACLE_PARTSLIP) initType = FGI_BNDPART; 
195                         else if(mesh->obstacleType==FLUIDSIM_OBSTACLE_FREESLIP) initType = FGI_BNDFREE; 
196                         else /*if(mesh->obstacleType==FLUIDSIM_OBSTACLE_NOSLIP)*/ initType = FGI_BNDNO; 
197                         break;
198                 case OB_FLUIDSIM_FLUID: initType = FGI_FLUID; break;
199                 case OB_FLUIDSIM_INFLOW: initType = FGI_MBNDINFLOW; break;
200                 case OB_FLUIDSIM_OUTFLOW: initType = FGI_MBNDOUTFLOW; break;
201                 case OB_FLUIDSIM_CONTROL: initType = FGI_CONTROL; break;
202                 default: return 1; // invalid type
203         }
204         
205         ntlGeometryObjModel *obj = new ntlGeometryObjModel( );
206         gpWorld->getRenderGlobals()->getSimScene()->addGeoClass( obj );
207         obj->initModel(
208                         mesh->numVertices, mesh->vertices, mesh->numTriangles, mesh->triangles,
209                         mesh->channelSizeVertices, mesh->channelVertices );
210         if(mesh->name) obj->setName(string(mesh->name));
211         else {
212                 char meshname[100];
213                 snprintf(meshname,100,"mesh%04d",globalMeshCounter);
214                 obj->setName(string(meshname));
215         }
216         globalMeshCounter++;
217         obj->setGeoInitId( mesh->parentDomainId+1 );
218         obj->setGeoInitIntersect(true);
219         obj->setGeoInitType(initType);
220         obj->setGeoPartSlipValue(mesh->obstaclePartslip);
221         obj->setGeoImpactFactor(mesh->obstacleImpactFactor);
222         
223         /* fluid control features */
224         obj->setCpsTimeStart(mesh->cpsTimeStart);
225         obj->setCpsTimeEnd(mesh->cpsTimeEnd);
226         obj->setCpsQuality(mesh->cpsQuality);
227         
228         if((mesh->volumeInitType<VOLUMEINIT_VOLUME)||(mesh->volumeInitType>VOLUMEINIT_BOTH)) mesh->volumeInitType = VOLUMEINIT_VOLUME;
229         obj->setVolumeInit(mesh->volumeInitType);
230         // use channel instead, obj->setInitialVelocity( ntlVec3Gfx(mesh->iniVelocity[0], mesh->iniVelocity[1], mesh->iniVelocity[2]) );
231         
232         obj->initChannels(
233                         mesh->channelSizeTranslation, mesh->channelTranslation, 
234                         mesh->channelSizeRotation,    mesh->channelRotation, 
235                         mesh->channelSizeScale,       mesh->channelScale,
236                         mesh->channelSizeActive,      mesh->channelActive,
237                         mesh->channelSizeInitialVel,  mesh->channelInitialVel,
238                         mesh->channelSizeAttractforceStrength,  mesh->channelAttractforceStrength,
239                         mesh->channelSizeAttractforceRadius,  mesh->channelAttractforceRadius,
240                         mesh->channelSizeVelocityforceStrength,  mesh->channelVelocityforceStrength,
241                         mesh->channelSizeVelocityforceRadius,  mesh->channelVelocityforceRadius
242                 );
243         obj->setLocalCoordInivel( mesh->localInivelCoords );
244
245         debMsgStd("elbeemAddMesh",DM_MSG,"Added elbeem mesh: "<<obj->getName()<<" type="<<initType<<" "<<obj->getIsAnimated(), 9 );
246         return 0;
247 }
248
249 // do the actual simulation
250 extern "C" 
251 int elbeemSimulate(void) {
252         if(!gpWorld) return 1;
253
254         gpWorld->finishWorldInit();
255         if( isSimworldOk() ) {
256                 myTime_t timestart = getTime();
257                 gpWorld->renderAnimation();
258                 myTime_t timeend = getTime();
259
260                 if(getElbeemState() != SIMWORLD_STOP) {
261                         // ok, we're done...
262                         delete gpWorld;
263                         
264                         gpWorld = NULL;
265                         debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
266                 } else {
267                         debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation stopped, time so far: "<<getTimeString(timeend-timestart)<<".", 2 ); 
268                 }
269                 return 0;
270         } 
271
272         // failure...
273         return 1;
274 }
275
276
277 // continue a previously stopped simulation
278 extern "C" 
279 int elbeemContinueSimulation(void) {
280
281         if(getElbeemState() != SIMWORLD_STOP) {
282                 errMsg("elbeemContinueSimulation","No running simulation found! Aborting...");
283                 if(gpWorld) delete gpWorld;
284                 return 1;
285         }
286
287         myTime_t timestart = getTime();
288         gpWorld->renderAnimation();
289         myTime_t timeend = getTime();
290
291         if(getElbeemState() != SIMWORLD_STOP) {
292                 // ok, we're done...
293                 delete gpWorld;
294                 gpWorld = NULL;
295                 debMsgStd("elbeemContinueSimulation",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
296         } else {
297                 debMsgStd("elbeemContinueSimulation",DM_NOTIFY, "El'Beem simulation stopped, time so far: "<<getTimeString(timeend-timestart)<<".", 2 ); 
298         }
299         return 0;
300
301
302
303 // global vector to flag values to remove
304 vector<int> gKeepVal;
305
306 #define SIMPLIFY_FLOAT_EPSILON (1e-6f)
307 #define SIMPLIFY_DOUBLE_EPSILON (1e-12f)
308 #define SFLOATEQ(x,y)  (ABS((x)-(y)) < SIMPLIFY_FLOAT_EPSILON)
309 #define SDOUBLEEQ(x,y) (ABS((x)-(y)) < SIMPLIFY_DOUBLE_EPSILON)
310 #define SVECFLOATEQ(x,y)  ( \
311         (ABS((x)[0]-(y)[0]) < SIMPLIFY_FLOAT_EPSILON) && \
312         (ABS((x)[1]-(y)[1]) < SIMPLIFY_FLOAT_EPSILON) && \
313         (ABS((x)[2]-(y)[2]) < SIMPLIFY_FLOAT_EPSILON) )
314
315 // helper function - simplify animation channels
316 extern "C" 
317 int elbeemSimplifyChannelFloat(float *channel, int* size) {
318         bool changed = false;
319         int nsize = *size;
320         int orgsize = *size;
321         if(orgsize<1) return false;
322         gKeepVal.resize( orgsize );
323         for(int i=0; i<orgsize; i++) { gKeepVal[i] = true; }
324         const bool debugSF = false;
325
326         float last = channel[0 + 0];
327         for(int i=1; i<orgsize; i++) {
328                 float curr = channel[2*i + 0];
329                 bool remove = false;
330                 if(SFLOATEQ(curr,last)) remove = true;
331                 // dont remove if next value is different
332                 if((remove)&&(i<orgsize-1)) {
333                         float next = channel[2*(i+1)+0];
334                         if(!SFLOATEQ(next,curr)) remove = false;
335                 }
336                 if(remove) {
337                         changed = true;
338                         gKeepVal[i] = false;
339                         nsize--;
340                 }
341                 if(debugSF) errMsg("elbeemSimplifyChannelFloat","i"<<i<<"/"<<orgsize<<" v"<<channel[ (i*2) + 0 ]<<" t"<<channel[ (i*2) + 1 ]<<"   nsize="<<nsize<<" r"<<remove );
342                 last = curr;
343         }
344
345         if(changed) {
346                 nsize = 1;
347                 for(int i=1; i<orgsize; i++) {
348                         if(gKeepVal[i]) {
349                                 channel[ (nsize*2) + 0 ] = channel[ (i*2) + 0 ];
350                                 channel[ (nsize*2) + 1 ] = channel[ (i*2) + 1 ];
351                                 nsize++;
352                         }
353                 }
354                 *size = nsize;
355         }
356
357         if(debugSF) for(int i=1; i<nsize; i++) {
358                 errMsg("elbeemSimplifyChannelFloat","n i"<<i<<"/"<<nsize<<" v"<<channel[ (i*2) + 0 ]<<" t"<<channel[ (i*2) + 1 ] );
359         }
360
361         return changed;
362 }
363
364 extern "C" 
365 int elbeemSimplifyChannelVec3(float *channel, int* size) {
366         bool changed = false;
367         int nsize = *size;
368         int orgsize = *size;
369         if(orgsize<1) return false;
370         gKeepVal.resize( orgsize );
371         for(int i=0; i<orgsize; i++) { gKeepVal[i] = true; }
372         const bool debugVF = false;
373
374         ntlVec3f last( channel[0 + 0], channel[0 + 1], channel[0 + 2] );
375         for(int i=1; i<orgsize; i++) {
376                 ntlVec3f curr( channel[4*i + 0], channel[4*i + 1], channel[4*i + 2]);
377                 bool remove = false;
378                 if(SVECFLOATEQ(curr,last)) remove = true;
379                 // dont remove if next value is different
380                 if((remove)&&(i<orgsize-1)) {
381                         ntlVec3f next( channel[4*(i+1)+0], channel[4*(i+1)+1], channel[4*(i+1)+2]);
382                         if(!SVECFLOATEQ(next,curr)) remove = false;
383                 }
384                 if(remove) {
385                         changed = true;
386                         gKeepVal[i] = false;
387                         nsize--;
388                 }
389                 if(debugVF) errMsg("elbeemSimplifyChannelVec3","i"<<i<<"/"<<orgsize<<" v"<<
390                                 channel[ (i*4) + 0 ]<<","<< channel[ (i*4) + 1 ]<<","<< channel[ (i*4) + 2 ]<<
391                                 " t"<<channel[ (i*4) + 3 ]<<"   nsize="<<nsize<<" r"<<remove );
392                 last = curr;
393         }
394
395         if(changed) {
396                 nsize = 1;
397                 for(int i=1; i<orgsize; i++) {
398                         if(gKeepVal[i]) {
399                                 for(int j=0; j<4; j++){ channel[ (nsize*4) + j ] = channel[ (i*4) + j ]; }
400                                 nsize++;
401                         }
402                 }
403                 *size = nsize;
404         }
405
406         if(debugVF) for(int i=1; i<nsize; i++) {
407                 errMsg("elbeemSimplifyChannelVec3","n i"<<i<<"/"<<nsize<<" v"<<
408                                 channel[ (i*4) + 0 ]<<","<< channel[ (i*4) + 1 ]<<","<< channel[ (i*4) + 2 ]<<
409                                 " t"<<channel[ (i*4) + 3 ] );
410         }
411
412         return changed;
413 }
414
415
416 #undef SIMPLIFY_FLOAT_EPSILON
417 #undef SIMPLIFY_DOUBLE_EPSILON
418 #undef SFLOATEQ
419 #undef SDOUBLEEQ
420