doxygen: add newline after \file
[blender.git] / intern / elbeem / intern / elbeem.cpp
index 8e96879..01e4801 100644 (file)
@@ -1,9 +1,12 @@
+/** \file
+ * \ingroup elbeem
+ */
 /******************************************************************************
  *
  * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
  * All code distributed as part of El'Beem is covered by the version 2 of the 
  * GNU General Public License. See the file COPYING for details.
- * Copyright 2003-2005 Nils Thuerey
+ * Copyright 2003-2006 Nils Thuerey
  *
  * Main program functions
  */
@@ -30,13 +33,15 @@ int guiRoiMaxLev=6, guiRoiMinLev=0;
 ntlWorld *gpWorld = NULL;
 
 
+
 // API
 
 // reset elbeemSimulationSettings struct with defaults
 extern "C" 
 void elbeemResetSettings(elbeemSimulationSettings *set) {
        if(!set) return;
-  set->version = 2;
+  set->version = 3;
+  set->domainId = 0;
        for(int i=0 ; i<3; i++) set->geoStart[i] = 0.0;
        for(int i=0 ; i<3; i++) set->geoSize[i] = 1.0;
   set->resolutionxyz = 64;
@@ -52,7 +57,8 @@ void elbeemResetSettings(elbeemSimulationSettings *set) {
        set->noOfFrames = 10;
   set->gstar = 0.005;
   set->maxRefine = -1;
-  set->generateParticles = 1.0;
+  set->generateParticles = 0.0;
+  set->numTracerParticles = 0;
   strcpy(set->outputPath,"./elbeemdata_");
 
        set->channelSizeFrameTime=0;
@@ -62,10 +68,15 @@ void elbeemResetSettings(elbeemSimulationSettings *set) {
        set->channelSizeGravity=0;
        set->channelGravity=NULL; 
 
-       set->obstacleType= FLUIDSIM_OBSTACLE_NOSLIP;
-       set->obstaclePartslip= 0.;
+       set->domainobsType= FLUIDSIM_OBSTACLE_NOSLIP;
+       set->domainobsPartslip= 0.;
        set->generateVertexVectors = 0;
        set->surfaceSmoothing = 1.;
+       set->surfaceSubdivs = 1;
+
+       set->farFieldSize = 0.;
+       set->runsimCallback = NULL;
+       set->runsimUserData = NULL;
 
        // init identity
        for(int i=0; i<16; i++) set->surfaceTrafo[i] = 0.0;
@@ -74,48 +85,113 @@ void elbeemResetSettings(elbeemSimulationSettings *set) {
 
 // start fluidsim init
 extern "C" 
-int elbeemInit(elbeemSimulationSettings *settings) {
-       gElbeemState = SIMWORLD_INVALID;
-       strcpy(gElbeemErrorString,"[none]");
+int elbeemInit() {
+       setElbeemState( SIMWORLD_INITIALIZING );
+       setElbeemErrorString("[none]");
+       resetGlobalColorSetting();
 
        elbeemCheckDebugEnv();
        debMsgStd("performElbeemSimulation",DM_NOTIFY,"El'Beem Simulation Init Start as Plugin, debugLevel:"<<gDebugLevel<<" ...\n", 2);
        
        // create world object with initial settings
-       ntlBlenderDumper *elbeem = new ntlBlenderDumper(settings);
+       ntlBlenderDumper *elbeem = new ntlBlenderDumper(); 
        gpWorld = elbeem;
        return 0;
 }
 
+// fluidsim end
+extern "C" 
+int elbeemFree() {
+       
+       return 0;
+}
+
+// start fluidsim init
+extern "C" 
+int elbeemAddDomain(elbeemSimulationSettings *settings) {
+       // has to be inited...
+       if((getElbeemState() == SIMWORLD_INVALID) && (!gpWorld)) { elbeemInit(); }
+       if(getElbeemState() != SIMWORLD_INITIALIZING) { errFatal("elbeemAddDomain","Unable to init simulation world",SIMWORLD_INITERROR); }
+       // create domain with given settings
+       gpWorld->addDomain(settings);
+       return 0;
+}
+
+// error message access
+extern "C" 
+void elbeemGetErrorString(char *buffer) {
+       if(!buffer) return;
+       strncpy(buffer,getElbeemErrorString(),256);
+}
+
 // reset elbeemMesh struct with zeroes
 extern "C" 
 void elbeemResetMesh(elbeemMesh *mesh) {
        if(!mesh) return;
+       // init typedef struct elbeemMesh
   mesh->type = 0;
+
+       mesh->parentDomainId = 0;
+
+       /* vertices */
   mesh->numVertices = 0;
        mesh->vertices = NULL;
+
+       mesh->channelSizeVertices = 0;
+       mesh->channelVertices = NULL;
+
+       /* triangles */
        mesh->numTriangles = 0;
   mesh->triangles = NULL;
+
+       /* animation channels */
        mesh->channelSizeTranslation = 0;
        mesh->channelTranslation = NULL;
        mesh->channelSizeRotation = 0;
        mesh->channelRotation = NULL;
        mesh->channelSizeScale = 0;
        mesh->channelScale = NULL;
+
+       /* active channel */
        mesh->channelSizeActive = 0;
        mesh->channelActive = NULL;
+
        mesh->channelSizeInitialVel = 0;
        mesh->channelInitialVel = NULL;
+
        mesh->localInivelCoords = 0;
+
        mesh->obstacleType= FLUIDSIM_OBSTACLE_NOSLIP;
        mesh->obstaclePartslip= 0.;
+       mesh->obstacleImpactFactor= 1.;
+
+       mesh->volumeInitType= OB_VOLUMEINIT_VOLUME;
+
+       /* name of the mesh, mostly for debugging */
        mesh->name = "[unnamed]";
+       
+       /* fluid control settings */
+       mesh->cpsTimeStart = 0;
+       mesh->cpsTimeEnd = 0;
+       mesh->cpsQuality = 0;
+       
+       mesh->channelSizeAttractforceStrength = 0;
+       mesh->channelAttractforceStrength = NULL;
+       mesh->channelSizeAttractforceRadius = 0;
+       mesh->channelAttractforceRadius = NULL;
+       mesh->channelSizeVelocityforceStrength = 0;
+       mesh->channelVelocityforceStrength = NULL;
+       mesh->channelSizeVelocityforceRadius = 0;
+       mesh->channelVelocityforceRadius = NULL;
 }
 
+int globalMeshCounter = 1;
 // add mesh as fluidsim object
 extern "C" 
 int elbeemAddMesh(elbeemMesh *mesh) {
-       int initType = -1;
+       int initType;
+       if(getElbeemState() != SIMWORLD_INITIALIZING) { errFatal("elbeemAddMesh","World and domain not initialized, call elbeemInit and elbeemAddDomain before...", SIMWORLD_INITERROR); }
+
        switch(mesh->type) {
                case OB_FLUIDSIM_OBSTACLE: 
                        if     (mesh->obstacleType==FLUIDSIM_OBSTACLE_PARTSLIP) initType = FGI_BNDPART; 
@@ -125,26 +201,54 @@ int elbeemAddMesh(elbeemMesh *mesh) {
                case OB_FLUIDSIM_FLUID: initType = FGI_FLUID; break;
                case OB_FLUIDSIM_INFLOW: initType = FGI_MBNDINFLOW; break;
                case OB_FLUIDSIM_OUTFLOW: initType = FGI_MBNDOUTFLOW; break;
+               case OB_FLUIDSIM_CONTROL: initType = FGI_CONTROL; break;
+               default: return 1; // invalid type
        }
-       // invalid type?
-       if(initType<0) return 1;
        
        ntlGeometryObjModel *obj = new ntlGeometryObjModel( );
        gpWorld->getRenderGlobals()->getSimScene()->addGeoClass( obj );
-       obj->initModel(mesh->numVertices, mesh->vertices, mesh->numTriangles, mesh->triangles);
-       if(mesh->name) obj->setName(std::string(mesh->name));
-       else obj->setName(std::string("[unnamed]"));
-       obj->setGeoInitId(1);
+       gpWorld->getRenderGlobals()->getRenderScene()->addGeoClass(obj);
+       obj->initModel(
+                       mesh->numVertices, mesh->vertices, mesh->numTriangles, mesh->triangles,
+                       mesh->channelSizeVertices, mesh->channelVertices );
+       if(mesh->name) obj->setName(string(mesh->name));
+       else {
+               char meshname[100];
+               snprintf(meshname,100,"mesh%04d",globalMeshCounter);
+               obj->setName(string(meshname));
+       }
+       globalMeshCounter++;
+       obj->setGeoInitId( mesh->parentDomainId+1 );
        obj->setGeoInitIntersect(true);
        obj->setGeoInitType(initType);
-       obj->setGeoPartSlipValue(mesh->obstaclePartslip);
+       
+       // abuse partslip value for control fluid: reverse control keys or not
+       if(initType == FGI_CONTROL)
+               obj->setGeoPartSlipValue(mesh->obstacleType);
+       else
+               obj->setGeoPartSlipValue(mesh->obstaclePartslip);
+       
+       obj->setGeoImpactFactor(mesh->obstacleImpactFactor);
+       
+       /* fluid control features */
+       obj->setCpsTimeStart(mesh->cpsTimeStart);
+       obj->setCpsTimeEnd(mesh->cpsTimeEnd);
+       obj->setCpsQuality(mesh->cpsQuality);
+       
+       if((mesh->volumeInitType<VOLUMEINIT_VOLUME)||(mesh->volumeInitType>VOLUMEINIT_BOTH)) mesh->volumeInitType = VOLUMEINIT_VOLUME;
+       obj->setVolumeInit(mesh->volumeInitType);
        // use channel instead, obj->setInitialVelocity( ntlVec3Gfx(mesh->iniVelocity[0], mesh->iniVelocity[1], mesh->iniVelocity[2]) );
+       
        obj->initChannels(
                        mesh->channelSizeTranslation, mesh->channelTranslation, 
                        mesh->channelSizeRotation,    mesh->channelRotation, 
                        mesh->channelSizeScale,       mesh->channelScale,
                        mesh->channelSizeActive,      mesh->channelActive,
-                       mesh->channelSizeInitialVel,  mesh->channelInitialVel
+                       mesh->channelSizeInitialVel,  mesh->channelInitialVel,
+                       mesh->channelSizeAttractforceStrength,  mesh->channelAttractforceStrength,
+                       mesh->channelSizeAttractforceRadius,  mesh->channelAttractforceRadius,
+                       mesh->channelSizeVelocityforceStrength,  mesh->channelVelocityforceStrength,
+                       mesh->channelSizeVelocityforceRadius,  mesh->channelVelocityforceRadius
                );
        obj->setLocalCoordInivel( mesh->localInivelCoords );
 
@@ -158,17 +262,20 @@ int elbeemSimulate(void) {
        if(!gpWorld) return 1;
 
        gpWorld->finishWorldInit();
-       
-       if(SIMWORLD_OK()) {
-               gElbeemState = SIMWORLD_INITED;
+       if( isSimworldOk() ) {
                myTime_t timestart = getTime();
                gpWorld->renderAnimation();
                myTime_t timeend = getTime();
-               debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
 
-               // ok, we're done...
-               delete gpWorld;
-               gpWorld = NULL;
+               if(getElbeemState() != SIMWORLD_STOP) {
+                       // ok, we're done...
+                       delete gpWorld;
+                       
+                       gpWorld = NULL;
+                       debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
+               } else {
+                       debMsgStd("elbeemSimulate",DM_NOTIFY, "El'Beem simulation stopped, time so far: "<<getTimeString(timeend-timestart)<<".", 2 ); 
+               }
                return 0;
        } 
 
@@ -177,6 +284,32 @@ int elbeemSimulate(void) {
 }
 
 
+// continue a previously stopped simulation
+extern "C" 
+int elbeemContinueSimulation(void) {
+
+       if(getElbeemState() != SIMWORLD_STOP) {
+               errMsg("elbeemContinueSimulation","No running simulation found! Aborting...");
+               if(gpWorld) delete gpWorld;
+               return 1;
+       }
+
+       myTime_t timestart = getTime();
+       gpWorld->renderAnimation();
+       myTime_t timeend = getTime();
+
+       if(getElbeemState() != SIMWORLD_STOP) {
+               // ok, we're done...
+               delete gpWorld;
+               gpWorld = NULL;
+               debMsgStd("elbeemContinueSimulation",DM_NOTIFY, "El'Beem simulation done, time: "<<getTimeString(timeend-timestart)<<".\n", 2 ); 
+       } else {
+               debMsgStd("elbeemContinueSimulation",DM_NOTIFY, "El'Beem simulation stopped, time so far: "<<getTimeString(timeend-timestart)<<".", 2 ); 
+       }
+       return 0;
+} 
+
+
 // global vector to flag values to remove
 vector<int> gKeepVal;