Exemplo n.º 1
0
/******************************************************************************
 *! dump particles if desired 
 *****************************************************************************/
void ParticleTracer::notifyOfDump(int dumptype, int frameNr,char *frameNrStr,string outfilename, double simtime) {
	debMsgStd("ParticleTracer::notifyOfDump",DM_MSG,"obj:"<<this->getName()<<" frame:"<<frameNrStr<<" dumpp"<<mDumpParts<<" t"<<simtime, 10); // DEBUG

	if(
			(dumptype==DUMP_FULLGEOMETRY)&&
			(mDumpParts>0)) {
		// dump to binary file
		std::ostringstream boutfilename("");
		boutfilename << outfilename <<"_particles_" << frameNrStr;
		if(glob_mpactive) {
			if(glob_mpindex>0) { boutfilename << "mp"<<glob_mpindex; }
		}
		boutfilename << ".gz";
		debMsgStd("ParticleTracer::notifyOfDump",DM_MSG,"B-Dumping: "<< this->getName() <<", particles:"<<mParts.size()<<" "<< " to "<<boutfilename.str()<<" #"<<frameNr , 7);
		//debMsgStd("ParticleTracer::notifyOfDump",DM_MSG,"B-Dumping: partgeodeb sim:"<<mSimStart<<","<<mSimEnd<<" geosize:"<<mStart<<","<<mEnd,2 );

		// output to zipped file
		gzFile gzf;
		gzf = gzopen(boutfilename.str().c_str(), "wb1");
		if(gzf) {
			int numParts;
			if(sizeof(numParts)!=4) { errMsg("ParticleTracer::notifyOfDump","Invalid int size"); return; }
			// only dump active particles
			numParts = 0;
			for(size_t i=0; i<mParts.size(); i++) {
				if(!mParts[i].getActive()) continue;
				numParts++;
			}
			gzwrite(gzf, &numParts, sizeof(numParts));
			for(size_t i=0; i<mParts.size(); i++) {
				if(!mParts[i].getActive()) { continue; }
				ParticleObject *p = &mParts[i];
				//int type = p->getType();  // export whole type info
				int type = p->getFlags(); // debug export whole type & status info
				ntlVec3Gfx pos = p->getPos();
				float size = p->getSize();

				if(type&PART_FLOAT) { // WARNING same handling for dump!
					// add one gridcell offset
					//pos[2] += 1.0; 
				} 
				// display as drop for now externally
				//else if(type&PART_TRACER) { type |= PART_DROP; }

				pos = (*mpTrafo) * pos;

				ntlVec3Gfx v = p->getVel();
				v[0] *= mpTrafo->value[0][0];
				v[1] *= mpTrafo->value[1][1];
				v[2] *= mpTrafo->value[2][2];
				// FIXME check: pos = (*mpTrafo) * pos;
				gzwrite(gzf, &type, sizeof(type)); 
				gzwrite(gzf, &size, sizeof(size)); 
				for(int j=0; j<3; j++) { gzwrite(gzf, &pos[j], sizeof(float)); }
				for(int j=0; j<3; j++) { gzwrite(gzf, &v[j], sizeof(float)); }
			}
			gzclose( gzf );
		}
	} // dump?
}
Exemplo n.º 2
0
void ParticleTracer::checkDumpTextPositions(double simtime) {
	// dfor partial & full dump
	if(mDumpTextInterval>0.) {
		debMsgStd("ParticleTracer::checkDumpTextPositions",DM_MSG,"t="<<simtime<<" last:"<<mDumpTextLastTime<<" inter:"<<mDumpTextInterval,7);
	}

	if((mDumpTextInterval>0.) && (simtime>mDumpTextLastTime+mDumpTextInterval)) {
		// dump to binary file
		std::ostringstream boutfilename("");
		if(mDumpTextFile.length()>1) {   
			boutfilename << mDumpTextFile <<  ".cpart2"; 
		} else {                           
			boutfilename << "_particles" <<  ".cpart2"; 
		}
		debMsgStd("ParticleTracer::checkDumpTextPositions",DM_MSG,"T-Dumping: "<< this->getName() <<", particles:"<<mParts.size()<<" "<< " to "<<boutfilename.str()<<" " , 7);

		int numParts = 0;
		// only dump bubble particles
		for(size_t i=0; i<mParts.size(); i++) {
			//if(!mParts[i].getActive()) continue;
			//if(!(mParts[i].getType()&PART_BUBBLE)) continue;
			numParts++;
		}

		// output to text file
		//gzFile gzf;
		FILE *stf;
		if(mDumpTextCount==0) {
			//gzf = gzopen(boutfilename.str().c_str(), "w0");
			stf = fopen(boutfilename.str().c_str(), "w");

			fprintf( stf, "\n\n# cparts generated by elbeem \n# no. of parts \nN %d \n\n",numParts);
			// fixed time scale for now
			fprintf( stf, "T %f \n\n", 1.0);
		} else {
			//gzf = gzopen(boutfilename.str().c_str(), "a+0");
			stf = fopen(boutfilename.str().c_str(), "a+");
		}

		// add current set
		if(stf) {
			fprintf( stf, "\n\n# new set at frame %d,t%f,p%d --------------------------------- \n\n", mDumpTextCount, simtime, numParts );
			fprintf( stf, "S %f \n\n", simtime );
			
			for(size_t i=0; i<mParts.size(); i++) {
				ParticleObject *p = &mParts[i];
				ntlVec3Gfx pos = p->getPos();
				float size = p->getSize();
				float infl = 1.;
				//if(!mParts[i].getActive()) { size=0.; } // switch "off"
				if(!mParts[i].getActive()) { infl=0.; } // switch "off"
				if(!mParts[i].getInFluid()) { infl=0.; } // switch "off"
				if(mParts[i].getLifeTime()<0.) { infl=0.; } // not yet active...

				pos = (*mpTrafo) * pos;
				ntlVec3Gfx v = p->getVel();
				v[0] *= mpTrafo->value[0][0];
				v[1] *= mpTrafo->value[1][1];
				v[2] *= mpTrafo->value[2][2];
				
				fprintf( stf, "P %f %f %f \n", pos[0],pos[1],pos[2] );
				if(size!=1.0) fprintf( stf, "s %f \n", size );
				if(infl!=1.0) fprintf( stf, "i %f \n", infl );
				fprintf( stf, "\n" );
			}

			fprintf( stf, "# %d end  ", mDumpTextCount );
			//gzclose( gzf );
			fclose( stf );

			mDumpTextCount++;
		}

		mDumpTextLastTime += mDumpTextInterval;
	}

}
Exemplo n.º 3
0
/******************************************************************************
 * Only dump time dep. objects to file
 *****************************************************************************/
int ntlBlenderDumper::renderScene( void )
{
    char nrStr[5];								/* nr conversion */
    ntlRenderGlobals *glob = mpGlob;
    ntlScene *scene = mpGlob->getSimScene();
    bool debugOut = false;
    bool debugRender = false;
#if ELBEEM_PLUGIN==1
    debugOut = false;
#endif // ELBEEM_PLUGIN==1

    vector<string> gmName; 	 // gm names
    vector<string> gmMat;    // materials for gm
    int numGMs = 0;					 // no. of .obj models created

    if(debugOut) debMsgStd("ntlBlenderDumper::renderScene",DM_NOTIFY,"Dumping geometry data", 1);
    long startTime = getTime();
    snprintf(nrStr, 5, "%04d", glob->getAniCount() );

    // local scene vars
    vector<ntlTriangle> Triangles;
    vector<ntlVec3Gfx>  Vertices;
    vector<ntlVec3Gfx>  VertNormals;

    // check geo objects
    int idCnt = 0;          // give IDs to objects
    for (vector<ntlGeometryClass*>::iterator iter = scene->getGeoClasses()->begin();
            iter != scene->getGeoClasses()->end(); iter++) {
        if(!(*iter)->getVisible()) continue;
        int tid = (*iter)->getTypeId();

        if(tid & GEOCLASSTID_OBJECT) {
            // normal geom. objects -> ignore
        }
        if(tid & GEOCLASSTID_SHADER) {
            ntlGeometryShader *geoshad = (ntlGeometryShader*)(*iter); //dynamic_cast<ntlGeometryShader*>(*iter);
            string outname = geoshad->getOutFilename();
            if(outname.length()<1) outname = mpGlob->getOutFilename();
            geoshad->notifyShaderOfDump(DUMP_FULLGEOMETRY, glob->getAniCount(),nrStr,outname);

            for (vector<ntlGeometryObject*>::iterator siter = geoshad->getObjectsBegin();
                    siter != geoshad->getObjectsEnd();
                    siter++) {
                if(debugOut) debMsgStd("ntlBlenderDumper::BuildScene",DM_MSG,"added shader geometry "<<(*siter)->getName(), 8);

                (*siter)->notifyOfDump(DUMP_FULLGEOMETRY, glob->getAniCount(),nrStr,outname, this->mSimulationTime);
                bool doDump = false;
                bool isPreview = false;
                // only dump final&preview surface meshes
                if( (*siter)->getName().find( "final" ) != string::npos) {
                    doDump = true;
                } else if( (*siter)->getName().find( "preview" ) != string::npos) {
                    doDump = true;
                    isPreview = true;
                }
                if(!doDump) continue;

                // dont quit, some objects need notifyOfDump call
                if((glob_mpactive) && (glob_mpindex>0)) {
                    continue; //return 0;
                }

                // only dump geo shader objects
                Triangles.clear();
                Vertices.clear();
                VertNormals.clear();
                (*siter)->initialize( mpGlob );
                (*siter)->getTriangles(this->mSimulationTime, &Triangles, &Vertices, &VertNormals, idCnt);
                idCnt ++;

                // WARNING - this is dirty, but simobjs are the only geoshaders right now
                SimulationObject *sim = (SimulationObject *)geoshad;
                LbmSolverInterface *lbm = sim->getSolver();


                // always dump mesh, even empty ones...

                // dump to binary file
                std::ostringstream boutfilename("");
                //boutfilename << ecrpath.str() << outname <<"_"<< (*siter)->getName() <<"_" << nrStr << ".obj";
                boutfilename << outname <<"_"<< (*siter)->getName() <<"_" << nrStr;
                if(debugOut) debMsgStd("ntlBlenderDumper::renderScene",DM_MSG,"B-Dumping: "<< (*siter)->getName()
                                           <<", triangles:"<<Triangles.size()<<", vertices:"<<Vertices.size()<<
                                           " to "<<boutfilename.str() , 7);
                gzFile gzf;

                // output velocities if desired
                if((!isPreview) && (lbm->getDumpVelocities())) {
                    std::ostringstream bvelfilename;
                    bvelfilename << boutfilename.str();
                    bvelfilename << ".bvel.gz";
                    gzf = gzopen(bvelfilename.str().c_str(), "wb9");
                    if(gzf) {
                        int numVerts;
                        if(sizeof(numVerts)!=4) {
                            errMsg("ntlBlenderDumper::renderScene","Invalid int size");
                            return 1;
                        }
                        numVerts = Vertices.size();
                        gzwrite(gzf, &numVerts, sizeof(numVerts));
                        for(size_t i=0; i<Vertices.size(); i++) {
                            // returns smoothed velocity, scaled by frame time
                            ntlVec3Gfx v = lbm->getVelocityAt( Vertices[i][0], Vertices[i][1], Vertices[i][2] );
                            // translation not necessary, test rotation & scaling?
                            for(int j=0; j<3; j++) {
                                float vertp = v[j];
                                //if(i<20) errMsg("ntlBlenderDumper","DUMP_VEL final "<<i<<" = "<<v);
                                gzwrite(gzf, &vertp, sizeof(vertp));
                            }
                        }
                        gzclose( gzf );
                    }
                }

                // compress all bobj's
                boutfilename << ".bobj.gz";
                gzf = gzopen(boutfilename.str().c_str(), "wb1"); // wb9 is slow for large meshes!
                if (!gzf) {
                    errMsg("ntlBlenderDumper::renderScene","Unable to open output '"<<boutfilename<<"' ");
                    return 1;
                }

                // dont transform velocity output, this is handled in blender
                // current transform matrix
                ntlMatrix4x4<gfxReal> *trafo;
                trafo = lbm->getDomainTrafo();
                if(trafo) {
                    // transform into source space
                    for(size_t i=0; i<Vertices.size(); i++) {
                        Vertices[i] = (*trafo) * Vertices[i];
                    }
                }
                // rotate vertnormals
                ntlMatrix4x4<gfxReal> rottrafo;
                rottrafo.initId();
                if(lbm->getDomainTrafo()) {
                    // dont modifiy original!
                    rottrafo = *lbm->getDomainTrafo();
                    ntlVec3Gfx rTrans,rScale,rRot,rShear;
                    rottrafo.decompose(rTrans,rScale,rRot,rShear);
                    rottrafo.initRotationXYZ(rRot[0],rRot[1],rRot[2]);
                    // only rotate here...
                    for(size_t i=0; i<Vertices.size(); i++) {
                        VertNormals[i] = rottrafo * VertNormals[i];
                        normalize(VertNormals[i]); // remove scaling etc.
                    }
                }


                // write to file
                int numVerts;
                if(sizeof(numVerts)!=4) {
                    errMsg("ntlBlenderDumper::renderScene","Invalid int size");
                    return 1;
                }
                numVerts = Vertices.size();
                gzwrite(gzf, &numVerts, sizeof(numVerts));
                for(size_t i=0; i<Vertices.size(); i++) {
                    for(int j=0; j<3; j++) {
                        float vertp = Vertices[i][j];
                        gzwrite(gzf, &vertp, sizeof(vertp));
                    }
                }

                // should be the same as Vertices.size
                if(VertNormals.size() != (size_t)numVerts) {
                    errMsg("ntlBlenderDumper::renderScene","Normals have to have same size as vertices!");
                    VertNormals.resize( Vertices.size() );
                }
                gzwrite(gzf, &numVerts, sizeof(numVerts));
                for(size_t i=0; i<VertNormals.size(); i++) {
                    for(int j=0; j<3; j++) {
                        float normp = VertNormals[i][j];
                        gzwrite(gzf, &normp, sizeof(normp));
                    }
                }

                int numTris = Triangles.size();
                gzwrite(gzf, &numTris, sizeof(numTris));
                for(size_t i=0; i<Triangles.size(); i++) {
                    for(int j=0; j<3; j++) {
                        int triIndex = Triangles[i].getPoints()[j];
                        gzwrite(gzf, &triIndex, sizeof(triIndex));
                    }
                }
                gzclose( gzf );
                debMsgStd("ntlBlenderDumper::renderScene",DM_NOTIFY," Wrote: '"<<boutfilename.str()<<"' ", 2);
                numGMs++;
            }
        }

    }

    // output ecr config file
    if(numGMs>0) {
        if(debugOut) debMsgStd("ntlBlenderDumper::renderScene",DM_MSG,"Objects dumped: "<<numGMs, 10);
    } else {
        if((glob_mpactive) && (glob_mpindex>0)) {
            // ok, nothing to do anyway...
        } else {
            errFatal("ntlBlenderDumper::renderScene","No objects to dump! Aborting...",SIMWORLD_INITERROR);
            return 1;
        }
    }

    // debug timing
    long stopTime = getTime();
    debMsgStd("ntlBlenderDumper::renderScene",DM_MSG,"Scene #"<<nrStr<<" dump time: "<< getTimeString(stopTime-startTime) <<" ", 10);

    // still render for preview...
    if(debugRender) {
        debMsgStd("ntlBlenderDumper::renderScene",DM_NOTIFY,"Performing preliminary render", 1);
        ntlWorld::renderScene();
    }
    else {
        // next frame
        glob->setAniCount( glob->getAniCount() +1 );
    }

    return 0;
}