Introduced quality to GUI, not yet tested (WIP commit)
[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 // start fluidsim init
100 extern "C" 
101 int elbeemAddDomain(elbeemSimulationSettings *settings) {
102         // has to be inited...
103         if((getElbeemState() == SIMWORLD_INVALID) && (!gpWorld)) { elbeemInit(); }
104         if(getElbeemState() != SIMWORLD_INITIALIZING) { errFatal("elbeemAddDomain","Unable to init simulation world",SIMWORLD_INITERROR); }
105         // create domain with given settings
106         gpWorld->addDomain(settings);
107         return 0;
108 }
109
110 // error message access
111 extern "C" 
112 void elbeemGetErrorString(char *buffer) {
113         if(!buffer) return;
114         strncpy(buffer,getElbeemErrorString(),256);
115 }
116
117 // reset elbeemMesh struct with zeroes
118 extern "C" 
119 void elbeemResetMesh(elbeemMesh *mesh) {
120         if(!mesh) return;
121         // init typedef struct elbeemMesh
122   mesh->type = 0;
123
124         mesh->parentDomainId = 0;
125
126         /* vertices */
127   mesh->numVertices = 0;
128         mesh->vertices = NULL;
129
130         mesh->channelSizeVertices = 0;
131         mesh->channelVertices = NULL;
132
133         /* triangles */
134         mesh->numTriangles = 0;
135   mesh->triangles = NULL;
136
137         /* animation channels */
138         mesh->channelSizeTranslation = 0;
139         mesh->channelTranslation = NULL;
140         mesh->channelSizeRotation = 0;
141         mesh->channelRotation = NULL;
142         mesh->channelSizeScale = 0;
143         mesh->channelScale = NULL;
144
145         /* active channel */
146         mesh->channelSizeActive = 0;
147         mesh->channelActive = NULL;
148
149         mesh->channelSizeInitialVel = 0;
150         mesh->channelInitialVel = NULL;
151
152         mesh->localInivelCoords = 0;
153
154         mesh->obstacleType= FLUIDSIM_OBSTACLE_NOSLIP;
155         mesh->obstaclePartslip= 0.;
156         mesh->obstacleImpactFactor= 1.;
157
158         mesh->volumeInitType= OB_VOLUMEINIT_VOLUME;
159
160         /* name of the mesh, mostly for debugging */
161         mesh->name = "[unnamed]";
162         
163         /* fluid control settings */
164         mesh->cpsTimeStart = 0;
165         mesh->cpsTimeEnd = 0;
166         mesh->cpsQuality = 0;
167         
168         mesh->channelSizeAttractforceStrength = 0;
169         mesh->channelAttractforceStrength = NULL;
170         mesh->channelSizeAttractforceRadius = 0;
171         mesh->channelAttractforceRadius = NULL;
172         mesh->channelSizeVelocityforceStrength = 0;
173         mesh->channelVelocityforceStrength = NULL;
174         mesh->channelSizeVelocityforceRadius = 0;
175         mesh->channelVelocityforceRadius = NULL;
176 }
177
178 int globalMeshCounter = 1;
179 // add mesh as fluidsim object
180 extern "C" 
181 int elbeemAddMesh(elbeemMesh *mesh) {
182         int initType;
183         if(getElbeemState() != SIMWORLD_INITIALIZING) { errFatal("elbeemAddMesh","World and domain not initialized, call elbeemInit and elbeemAddDomain before...", SIMWORLD_INITERROR); }
184
185         switch(mesh->type) {
186                 case OB_FLUIDSIM_OBSTACLE: 
187                         if     (mesh->obstacleType==FLUIDSIM_OBSTACLE_PARTSLIP) initType = FGI_BNDPART; 
188                         else if(mesh->obstacleType==FLUIDSIM_OBSTACLE_FREESLIP) initType = FGI_BNDFREE; 
189                         else /*if(mesh->obstacleType==FLUIDSIM_OBSTACLE_NOSLIP)*/ initType = FGI_BNDNO; 
190                         break;
191                 case OB_FLUIDSIM_FLUID: initType = FGI_FLUID; break;
192                 case OB_FLUIDSIM_INFLOW: initType = FGI_MBNDINFLOW; break;
193                 case OB_FLUIDSIM_OUTFLOW: initType = FGI_MBNDOUTFLOW; break;
194                 case OB_FLUIDSIM_CONTROL: initType = FGI_CONTROL; break;
195                 default: return 1; // invalid type
196         }
197         
198         ntlGeometryObjModel *obj = new ntlGeometryObjModel( );
199         gpWorld->getRenderGlobals()->getSimScene()->addGeoClass( obj );
200         obj->initModel(
201                         mesh->numVertices, mesh->vertices, mesh->numTriangles, mesh->triangles,
202                         mesh->channelSizeVertices, mesh->channelVertices );
203         if(mesh->name) obj->setName(string(mesh->name));
204         else {
205                 char meshname[100];
206                 snprintf(meshname,100,"mesh%04d",globalMeshCounter);
207                 obj->setName(string(meshname));
208         }
209         globalMeshCounter++;
210         obj->setGeoInitId( mesh->parentDomainId+1 );
211         obj->setGeoInitIntersect(true);
212         obj->setGeoInitType(initType);
213         obj->setGeoPartSlipValue(mesh->obstaclePartslip);
214         obj->setGeoImpactFactor(mesh->obstacleImpactFactor);
215         
216         /* fluid control features */
217         obj->setCpsTimeStart(mesh->cpsTimeStart);
218         obj->setCpsTimeEnd(mesh->cpsTimeEnd);
219         obj->setCpsQuality(mesh->cpsQuality);
220         
221         if((mesh->volumeInitType<VOLUMEINIT_VOLUME)||(mesh->volumeInitType>VOLUMEINIT_BOTH)) mesh->volumeInitType = VOLUMEINIT_VOLUME;
222         obj->setVolumeInit(mesh->volumeInitType);
223         // use channel instead, obj->setInitialVelocity( ntlVec3Gfx(mesh->iniVelocity[0], mesh->iniVelocity[1], mesh->iniVelocity[2]) );
224         
225         obj->initChannels(
226                         mesh->channelSizeTranslation, mesh->channelTranslation, 
227                         mesh->channelSizeRotation,    mesh->channelRotation, 
228                         mesh->channelSizeScale,       mesh->channelScale,
229                         mesh->channelSizeActive,      mesh->channelActive,
230                         mesh->channelSizeInitialVel,  mesh->channelInitialVel,
231                         mesh->channelSizeAttractforceStrength,  mesh->channelAttractforceStrength,
232                         mesh->channelSizeAttractforceRadius,  mesh->channelAttractforceRadius,
233                         mesh->channelSizeVelocityforceStrength,  mesh->channelVelocityforceStrength,
234                         mesh->channelSizeVelocityforceRadius,  mesh->channelVelocityforceRadius
235                 );
236         obj->setLocalCoordInivel( mesh->localInivelCoords );
237
238         debMsgStd("elbeemAddMesh",DM_MSG,"Added elbeem mesh: "<<obj->getName()<<" type="<<initType<<" "<<obj->getIsAnimated(), 9 );
239         return 0;
240 }
241
242 // do the actual simulation
243 extern "C" 
244 int elbeemSimulate(void) {
245         if(!gpWorld) return 1;
246
247         gpWorld->finishWorldInit();
248         if( isSimworldOk() ) {
249                 myTime_t timestart = getTime();
250                 gpWorld->renderAnimation();
251                 myTime_t timeend = getTime();
252
253                 if(getElbeemState() != SIMWORLD_STOP) {
254                         // ok, we're done...
255                         delete gpWorld;
256                         gpWorld = NULL;
257                         debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
258                 } else {
259                         debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation stopped, time so far: "<<getTimeString(timeend-timestart)<<".", 2 ); 
260                 }
261                 return 0;
262         } 
263
264         // failure...
265         return 1;
266 }
267
268
269 // continue a previously stopped simulation
270 extern "C" 
271 int elbeemContinueSimulation(void) {
272
273         if(getElbeemState() != SIMWORLD_STOP) {
274                 errMsg("elbeemContinueSimulation","No running simulation found! Aborting...");
275                 if(gpWorld) delete gpWorld;
276                 return 1;
277         }
278
279         myTime_t timestart = getTime();
280         gpWorld->renderAnimation();
281         myTime_t timeend = getTime();
282
283         if(getElbeemState() != SIMWORLD_STOP) {
284                 // ok, we're done...
285                 delete gpWorld;
286                 gpWorld = NULL;
287                 debMsgStd("elbeemContinueSimulation",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
288         } else {
289                 debMsgStd("elbeemContinueSimulation",DM_NOTIFY, "El'Beem simulation stopped, time so far: "<<getTimeString(timeend-timestart)<<".", 2 ); 
290         }
291         return 0;
292
293
294
295 // global vector to flag values to remove
296 vector<int> gKeepVal;
297
298 #define SIMPLIFY_FLOAT_EPSILON (1e-6f)
299 #define SIMPLIFY_DOUBLE_EPSILON (1e-12f)
300 #define SFLOATEQ(x,y)  (ABS((x)-(y)) < SIMPLIFY_FLOAT_EPSILON)
301 #define SDOUBLEEQ(x,y) (ABS((x)-(y)) < SIMPLIFY_DOUBLE_EPSILON)
302 #define SVECFLOATEQ(x,y)  ( \
303         (ABS((x)[0]-(y)[0]) < SIMPLIFY_FLOAT_EPSILON) && \
304         (ABS((x)[1]-(y)[1]) < SIMPLIFY_FLOAT_EPSILON) && \
305         (ABS((x)[2]-(y)[2]) < SIMPLIFY_FLOAT_EPSILON) )
306
307 // helper function - simplify animation channels
308 extern "C" 
309 int elbeemSimplifyChannelFloat(float *channel, int* size) {
310         bool changed = false;
311         int nsize = *size;
312         int orgsize = *size;
313         if(orgsize<1) return false;
314         gKeepVal.resize( orgsize );
315         for(int i=0; i<orgsize; i++) { gKeepVal[i] = true; }
316         const bool debugSF = false;
317
318         float last = channel[0 + 0];
319         for(int i=1; i<orgsize; i++) {
320                 float curr = channel[2*i + 0];
321                 bool remove = false;
322                 if(SFLOATEQ(curr,last)) remove = true;
323                 // dont remove if next value is different
324                 if((remove)&&(i<orgsize-1)) {
325                         float next = channel[2*(i+1)+0];
326                         if(!SFLOATEQ(next,curr)) remove = false;
327                 }
328                 if(remove) {
329                         changed = true;
330                         gKeepVal[i] = false;
331                         nsize--;
332                 }
333                 if(debugSF) errMsg("elbeemSimplifyChannelFloat","i"<<i<<"/"<<orgsize<<" v"<<channel[ (i*2) + 0 ]<<" t"<<channel[ (i*2) + 1 ]<<"   nsize="<<nsize<<" r"<<remove );
334                 last = curr;
335         }
336
337         if(changed) {
338                 nsize = 1;
339                 for(int i=1; i<orgsize; i++) {
340                         if(gKeepVal[i]) {
341                                 channel[ (nsize*2) + 0 ] = channel[ (i*2) + 0 ];
342                                 channel[ (nsize*2) + 1 ] = channel[ (i*2) + 1 ];
343                                 nsize++;
344                         }
345                 }
346                 *size = nsize;
347         }
348
349         if(debugSF) for(int i=1; i<nsize; i++) {
350                 errMsg("elbeemSimplifyChannelFloat","n i"<<i<<"/"<<nsize<<" v"<<channel[ (i*2) + 0 ]<<" t"<<channel[ (i*2) + 1 ] );
351         }
352
353         return changed;
354 }
355
356 extern "C" 
357 int elbeemSimplifyChannelVec3(float *channel, int* size) {
358         bool changed = false;
359         int nsize = *size;
360         int orgsize = *size;
361         if(orgsize<1) return false;
362         gKeepVal.resize( orgsize );
363         for(int i=0; i<orgsize; i++) { gKeepVal[i] = true; }
364         const bool debugVF = false;
365
366         ntlVec3f last( channel[0 + 0], channel[0 + 1], channel[0 + 2] );
367         for(int i=1; i<orgsize; i++) {
368                 ntlVec3f curr( channel[4*i + 0], channel[4*i + 1], channel[4*i + 2]);
369                 bool remove = false;
370                 if(SVECFLOATEQ(curr,last)) remove = true;
371                 // dont remove if next value is different
372                 if((remove)&&(i<orgsize-1)) {
373                         ntlVec3f next( channel[4*(i+1)+0], channel[4*(i+1)+1], channel[4*(i+1)+2]);
374                         if(!SVECFLOATEQ(next,curr)) remove = false;
375                 }
376                 if(remove) {
377                         changed = true;
378                         gKeepVal[i] = false;
379                         nsize--;
380                 }
381                 if(debugVF) errMsg("elbeemSimplifyChannelVec3","i"<<i<<"/"<<orgsize<<" v"<<
382                                 channel[ (i*4) + 0 ]<<","<< channel[ (i*4) + 1 ]<<","<< channel[ (i*4) + 2 ]<<
383                                 " t"<<channel[ (i*4) + 3 ]<<"   nsize="<<nsize<<" r"<<remove );
384                 last = curr;
385         }
386
387         if(changed) {
388                 nsize = 1;
389                 for(int i=1; i<orgsize; i++) {
390                         if(gKeepVal[i]) {
391                                 for(int j=0; j<4; j++){ channel[ (nsize*4) + j ] = channel[ (i*4) + j ]; }
392                                 nsize++;
393                         }
394                 }
395                 *size = nsize;
396         }
397
398         if(debugVF) for(int i=1; i<nsize; i++) {
399                 errMsg("elbeemSimplifyChannelVec3","n i"<<i<<"/"<<nsize<<" v"<<
400                                 channel[ (i*4) + 0 ]<<","<< channel[ (i*4) + 1 ]<<","<< channel[ (i*4) + 2 ]<<
401                                 " t"<<channel[ (i*4) + 3 ] );
402         }
403
404         return changed;
405 }
406
407
408 #undef SIMPLIFY_FLOAT_EPSILON
409 #undef SIMPLIFY_DOUBLE_EPSILON
410 #undef SFLOATEQ
411 #undef SDOUBLEEQ
412