- fixed geometry init (object id init was missing)
[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-2005 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 // API
34
35 // reset elbeemSimulationSettings struct with defaults
36 extern "C" 
37 void elbeemResetSettings(elbeemSimulationSettings *set) {
38         if(!set) return;
39   set->version = 3;
40   set->domainId = 0;
41         for(int i=0 ; i<3; i++) set->geoStart[i] = 0.0;
42         for(int i=0 ; i<3; i++) set->geoSize[i] = 1.0;
43   set->resolutionxyz = 64;
44   set->previewresxyz = 24;
45   set->realsize = 1.0;
46   set->viscosity = 0.000001;
47
48         for(int i=0 ; i<2; i++) set->gravity[i] = 0.0;
49         set->gravity[2] = -9.81;
50
51   set->animStart = 0; 
52         set->aniFrameTime = 0.01;
53         set->noOfFrames = 10;
54   set->gstar = 0.005;
55   set->maxRefine = -1;
56   set->generateParticles = 0.0;
57   set->numTracerParticles = 0;
58   strcpy(set->outputPath,"./elbeemdata_");
59
60         set->channelSizeFrameTime=0;
61         set->channelFrameTime=NULL;
62         set->channelSizeViscosity=0;
63         set->channelViscosity=NULL;
64         set->channelSizeGravity=0;
65         set->channelGravity=NULL; 
66
67         set->obstacleType= FLUIDSIM_OBSTACLE_NOSLIP;
68         set->obstaclePartslip= 0.;
69         set->generateVertexVectors = 0;
70         set->surfaceSmoothing = 1.;
71
72         set->farFieldSize = 0.;
73         set->runsimCallback = NULL;
74         set->runsimUserData = NULL;
75
76         // init identity
77         for(int i=0; i<16; i++) set->surfaceTrafo[i] = 0.0;
78         for(int i=0; i<4; i++) set->surfaceTrafo[i*4+i] = 1.0;
79 }
80
81 // start fluidsim init
82 extern "C" 
83 int elbeemInit() {
84         setElbeemState( SIMWORLD_INITIALIZING );
85         setElbeemErrorString("[none]");
86
87         elbeemCheckDebugEnv();
88         debMsgStd("performElbeemSimulation",DM_NOTIFY,"El'Beem Simulation Init Start as Plugin, debugLevel:"<<gDebugLevel<<" ...\n", 2);
89         
90         // create world object with initial settings
91         ntlBlenderDumper *elbeem = new ntlBlenderDumper(); 
92         gpWorld = elbeem;
93         return 0;
94 }
95
96 // start fluidsim init
97 extern "C" 
98 int elbeemAddDomain(elbeemSimulationSettings *settings) {
99         // has to be inited...
100         if((getElbeemState() == SIMWORLD_INVALID) && (!gpWorld)) { elbeemInit(); }
101         if(getElbeemState() != SIMWORLD_INITIALIZING) { errFatal("elbeemAddDomain","Unable to init simulation world",SIMWORLD_INITERROR); }
102         // create domain with given settings
103         gpWorld->addDomain(settings);
104         return 0;
105 }
106
107 // error message access
108 extern "C" 
109 void elbeemGetErrorString(char *buffer) {
110         if(!buffer) return;
111         strncpy(buffer,getElbeemErrorString(),256);
112 }
113
114 // reset elbeemMesh struct with zeroes
115 extern "C" 
116 void elbeemResetMesh(elbeemMesh *mesh) {
117         if(!mesh) return;
118   mesh->type = 0;
119   mesh->parentDomainId = 0;
120   mesh->numVertices = 0;
121         mesh->vertices = NULL;
122         mesh->numTriangles = 0;
123   mesh->triangles = NULL;
124
125         mesh->channelSizeTranslation = 0;
126         mesh->channelTranslation = NULL;
127         mesh->channelSizeRotation = 0;
128         mesh->channelRotation = NULL;
129         mesh->channelSizeScale = 0;
130         mesh->channelScale = NULL;
131         mesh->channelSizeActive = 0;
132         mesh->channelActive = NULL;
133         mesh->channelSizeInitialVel = 0;
134         mesh->channelInitialVel = NULL;
135         mesh->localInivelCoords = 0;
136
137         mesh->obstacleType= FLUIDSIM_OBSTACLE_NOSLIP;
138         mesh->volumeInitType= OB_VOLUMEINIT_VOLUME;
139         mesh->obstaclePartslip= 0.;
140
141         mesh->channelSizeVertices = 0;
142         mesh->channelVertices = NULL;
143
144         mesh->name = "[unnamed]";
145 }
146
147 int globalMeshCounter = 1;
148 // add mesh as fluidsim object
149 extern "C" 
150 int elbeemAddMesh(elbeemMesh *mesh) {
151         int initType = -1;
152         if(getElbeemState() != SIMWORLD_INITIALIZING) { errFatal("elbeemAddMesh","World and domain not initialized, call elbeemInit and elbeemAddDomain before...", SIMWORLD_INITERROR); }
153
154         switch(mesh->type) {
155                 case OB_FLUIDSIM_OBSTACLE: 
156                         if     (mesh->obstacleType==FLUIDSIM_OBSTACLE_PARTSLIP) initType = FGI_BNDPART; 
157                         else if(mesh->obstacleType==FLUIDSIM_OBSTACLE_FREESLIP) initType = FGI_BNDFREE; 
158                         else /*if(mesh->obstacleType==FLUIDSIM_OBSTACLE_NOSLIP)*/ initType = FGI_BNDNO; 
159                         break;
160                 case OB_FLUIDSIM_FLUID: initType = FGI_FLUID; break;
161                 case OB_FLUIDSIM_INFLOW: initType = FGI_MBNDINFLOW; break;
162                 case OB_FLUIDSIM_OUTFLOW: initType = FGI_MBNDOUTFLOW; break;
163         }
164         // invalid type?
165         if(initType<0) return 1;
166         
167         ntlGeometryObjModel *obj = new ntlGeometryObjModel( );
168         gpWorld->getRenderGlobals()->getSimScene()->addGeoClass( obj );
169         obj->initModel(
170                         mesh->numVertices, mesh->vertices, mesh->numTriangles, mesh->triangles,
171                         mesh->channelSizeVertices, mesh->channelVertices );
172         if(mesh->name) obj->setName(string(mesh->name));
173         else {
174                 char meshname[100];
175                 snprintf(meshname,100,"mesh%04d",globalMeshCounter);
176                 obj->setName(string(meshname));
177         }
178         globalMeshCounter++;
179         obj->setGeoInitId( mesh->parentDomainId+1 );
180         obj->setGeoInitIntersect(true);
181         obj->setGeoInitType(initType);
182         obj->setGeoPartSlipValue(mesh->obstaclePartslip);
183         if((mesh->volumeInitType<VOLUMEINIT_VOLUME)||(mesh->volumeInitType>VOLUMEINIT_BOTH)) mesh->volumeInitType = VOLUMEINIT_VOLUME;
184         obj->setVolumeInit(mesh->volumeInitType);
185         // use channel instead, obj->setInitialVelocity( ntlVec3Gfx(mesh->iniVelocity[0], mesh->iniVelocity[1], mesh->iniVelocity[2]) );
186         obj->initChannels(
187                         mesh->channelSizeTranslation, mesh->channelTranslation, 
188                         mesh->channelSizeRotation,    mesh->channelRotation, 
189                         mesh->channelSizeScale,       mesh->channelScale,
190                         mesh->channelSizeActive,      mesh->channelActive,
191                         mesh->channelSizeInitialVel,  mesh->channelInitialVel
192                 );
193         obj->setLocalCoordInivel( mesh->localInivelCoords );
194
195         debMsgStd("elbeemAddMesh",DM_MSG,"Added elbeem mesh: "<<obj->getName()<<" type="<<initType<<" "<<obj->getIsAnimated(), 9 );
196         return 0;
197 }
198
199 // do the actual simulation
200 extern "C" 
201 int elbeemSimulate(void) {
202         if(!gpWorld) return 1;
203
204         gpWorld->finishWorldInit();
205         if( isSimworldOk() ) {
206                 myTime_t timestart = getTime();
207                 gpWorld->renderAnimation();
208                 myTime_t timeend = getTime();
209
210                 if(getElbeemState() != SIMWORLD_STOP) {
211                         // ok, we're done...
212                         delete gpWorld;
213                         gpWorld = NULL;
214                         debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
215                 } else {
216                         debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation stopped, time so far: "<<getTimeString(timeend-timestart)<<".", 2 ); 
217                 }
218                 return 0;
219         } 
220
221         // failure...
222         return 1;
223 }
224
225
226 // continue a previously stopped simulation
227 extern "C" 
228 int elbeemContinueSimulation(void) {
229
230         if(getElbeemState() != SIMWORLD_STOP) {
231                 errMsg("elbeemContinueSimulation","No running simulation found! Aborting...");
232                 if(gpWorld) delete gpWorld;
233                 return 1;
234         }
235
236         myTime_t timestart = getTime();
237         gpWorld->renderAnimation();
238         myTime_t timeend = getTime();
239
240         if(getElbeemState() != SIMWORLD_STOP) {
241                 // ok, we're done...
242                 delete gpWorld;
243                 gpWorld = NULL;
244                 debMsgStd("elbeemContinueSimulation",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
245         } else {
246                 debMsgStd("elbeemContinueSimulation",DM_NOTIFY, "El'Beem simulation stopped, time so far: "<<getTimeString(timeend-timestart)<<".", 2 ); 
247         }
248         return 0;
249
250
251
252 // global vector to flag values to remove
253 vector<int> gKeepVal;
254
255 #define SIMPLIFY_FLOAT_EPSILON (1e-6f)
256 #define SIMPLIFY_DOUBLE_EPSILON (1e-12f)
257 #define SFLOATEQ(x,y)  (ABS((x)-(y)) < SIMPLIFY_FLOAT_EPSILON)
258 #define SDOUBLEEQ(x,y) (ABS((x)-(y)) < SIMPLIFY_DOUBLE_EPSILON)
259 #define SVECFLOATEQ(x,y)  ( \
260         (ABS((x)[0]-(y)[0]) < SIMPLIFY_FLOAT_EPSILON) && \
261         (ABS((x)[1]-(y)[1]) < SIMPLIFY_FLOAT_EPSILON) && \
262         (ABS((x)[2]-(y)[2]) < SIMPLIFY_FLOAT_EPSILON) )
263
264 // helper function - simplify animation channels
265 extern "C" 
266 int elbeemSimplifyChannelFloat(float *channel, int* size) {
267         bool changed = false;
268         int nsize = *size;
269         int orgsize = *size;
270         if(orgsize<1) return false;
271         gKeepVal.resize( orgsize );
272         for(int i=0; i<orgsize; i++) { gKeepVal[i] = true; }
273         const bool debugSF = false;
274
275         float last = channel[0 + 0];
276         for(int i=1; i<orgsize; i++) {
277                 float curr = channel[2*i + 0];
278                 bool remove = false;
279                 if(SFLOATEQ(curr,last)) remove = true;
280                 // dont remove if next value is different
281                 if((remove)&&(i<orgsize-1)) {
282                         float next = channel[2*(i+1)+0];
283                         if(!SFLOATEQ(next,curr)) remove = false;
284                 }
285                 if(remove) {
286                         changed = true;
287                         gKeepVal[i] = false;
288                         nsize--;
289                 }
290                 if(debugSF) errMsg("elbeemSimplifyChannelFloat","i"<<i<<"/"<<orgsize<<" v"<<channel[ (i*2) + 0 ]<<" t"<<channel[ (i*2) + 1 ]<<"   nsize="<<nsize<<" r"<<remove );
291                 last = curr;
292         }
293
294         if(changed) {
295                 nsize = 1;
296                 for(int i=1; i<orgsize; i++) {
297                         if(gKeepVal[i]) {
298                                 channel[ (nsize*2) + 0 ] = channel[ (i*2) + 0 ];
299                                 channel[ (nsize*2) + 1 ] = channel[ (i*2) + 1 ];
300                                 nsize++;
301                         }
302                 }
303                 *size = nsize;
304         }
305
306         if(debugSF) for(int i=1; i<nsize; i++) {
307                 errMsg("elbeemSimplifyChannelFloat","n i"<<i<<"/"<<nsize<<" v"<<channel[ (i*2) + 0 ]<<" t"<<channel[ (i*2) + 1 ] );
308         }
309
310         return changed;
311 }
312
313 extern "C" 
314 int elbeemSimplifyChannelVec3(float *channel, int* size) {
315         bool changed = false;
316         int nsize = *size;
317         int orgsize = *size;
318         if(orgsize<1) return false;
319         gKeepVal.resize( orgsize );
320         for(int i=0; i<orgsize; i++) { gKeepVal[i] = true; }
321         const bool debugVF = false;
322
323         ntlVec3f last( channel[0 + 0], channel[0 + 1], channel[0 + 2] );
324         for(int i=1; i<orgsize; i++) {
325                 ntlVec3f curr( channel[4*i + 0], channel[4*i + 1], channel[4*i + 2]);
326                 bool remove = false;
327                 if(SVECFLOATEQ(curr,last)) remove = true;
328                 // dont remove if next value is different
329                 if((remove)&&(i<orgsize-1)) {
330                         ntlVec3f next( channel[4*(i+1)+0], channel[4*(i+1)+1], channel[4*(i+1)+2]);
331                         if(!SVECFLOATEQ(next,curr)) remove = false;
332                 }
333                 if(remove) {
334                         changed = true;
335                         gKeepVal[i] = false;
336                         nsize--;
337                 }
338                 if(debugVF) errMsg("elbeemSimplifyChannelVec3","i"<<i<<"/"<<orgsize<<" v"<<
339                                 channel[ (i*4) + 0 ]<<","<< channel[ (i*4) + 1 ]<<","<< channel[ (i*4) + 2 ]<<
340                                 " t"<<channel[ (i*4) + 3 ]<<"   nsize="<<nsize<<" r"<<remove );
341                 last = curr;
342         }
343
344         if(changed) {
345                 nsize = 1;
346                 for(int i=1; i<orgsize; i++) {
347                         if(gKeepVal[i]) {
348                                 for(int j=0; j<4; j++){ channel[ (nsize*4) + j ] = channel[ (i*4) + j ]; }
349                                 nsize++;
350                         }
351                 }
352                 *size = nsize;
353         }
354
355         if(debugVF) for(int i=1; i<nsize; i++) {
356                 errMsg("elbeemSimplifyChannelVec3","n i"<<i<<"/"<<nsize<<" v"<<
357                                 channel[ (i*4) + 0 ]<<","<< channel[ (i*4) + 1 ]<<","<< channel[ (i*4) + 2 ]<<
358                                 " t"<<channel[ (i*4) + 3 ] );
359         }
360
361         return changed;
362 }
363
364
365 #undef SIMPLIFY_FLOAT_EPSILON
366 #undef SIMPLIFY_DOUBLE_EPSILON
367 #undef SFLOATEQ
368 #undef SDOUBLEEQ
369