← Back to team overview

yade-dev team mailing list archive

[svn] r1908 - in trunk: core gui/qt3 pkg/snow/RenderingEngine py/yadeWrapper scripts

 

Author: eudoxos
Date: 2009-07-31 12:53:44 +0200 (Fri, 31 Jul 2009)
New Revision: 1908

Modified:
   trunk/core/MetaBody.cpp
   trunk/core/MetaBody.hpp
   trunk/core/Omega.cpp
   trunk/core/Omega.hpp
   trunk/core/yade.cpp
   trunk/gui/qt3/GLViewer.cpp
   trunk/gui/qt3/QtGUI-python.cpp
   trunk/pkg/snow/RenderingEngine/Ef1_BshSnowGrain_glDraw.cpp
   trunk/pkg/snow/RenderingEngine/Ef1_IstSnowLayersContact_glDraw.cpp
   trunk/py/yadeWrapper/yadeWrapper.cpp
   trunk/scripts/simple-scene-player.py
Log:
1. Add infrastructure for getting temporary filenames in a secure manner to Omega
2. move selectedBOyd to MetaBody; initialize it properly to avoid possible crash.


Modified: trunk/core/MetaBody.cpp
===================================================================
--- trunk/core/MetaBody.cpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/core/MetaBody.cpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -46,6 +46,7 @@
 	stopAtVirtTime=0; // not yet implemented either
 	isDynamic=false;
 	dt=1e-8;
+	selectedBody=-1;
 
 	// fill default tags
 	struct passwd* pw;

Modified: trunk/core/MetaBody.hpp
===================================================================
--- trunk/core/MetaBody.hpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/core/MetaBody.hpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -53,6 +53,8 @@
 		Real stopAtRealTime;
 
 		bool needsInitializers;
+		// for GL selection
+		body_id_t selectedBody;
 	protected :
 		virtual void postProcessAttributes(bool deserializing);
 	REGISTER_ATTRIBUTES(Body,

Modified: trunk/core/Omega.cpp
===================================================================
--- trunk/core/Omega.cpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/core/Omega.cpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -72,6 +72,23 @@
 time_duration Omega::getComputationDuration(){return microsec_clock::local_time()-msStartingSimulationTime-simulationPauseDuration;}
 
 
+void Omega::initTemps(){
+	char dirTemplate[]="/tmp/yade-XXXXXX";
+	tmpFileDir=mkdtemp(dirTemplate);
+	tmpFileCounter=0;
+}
+
+void Omega::cleanupTemps(){
+	filesystem::path tmpPath(tmpFileDir);
+	filesystem::remove_all(tmpPath);
+}
+
+std::string Omega::tmpFilename(){
+	if(tmpFileDir.empty()) throw runtime_error("tmpFileDir empty; Omega::initTemps not yet called()?");
+	boost::mutex::scoped_lock lock(tmpFileCounterMutex);
+	return tmpFileDir+lexical_cast<string>(tmpFileCounter++);
+}
+
 void Omega::reset(){
 	//finishSimulationLoop();
 	joinSimulationLoop();

Modified: trunk/core/Omega.hpp
===================================================================
--- trunk/core/Omega.hpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/core/Omega.hpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -88,11 +88,14 @@
 
 		map<string,string> memSavedSimulations;
 
+		boost::mutex tmpFileCounterMutex;
+		long tmpFileCounter;
+		std::string tmpFileDir;
+
 	public :
 		shared_ptr<Preferences> preferences;
 		string 				 yadeConfigPath;	// FIXME - must be private and more clean
 		string 				 yadeVersionName;	// FIXME - public ?
-		body_id_t			 selectedBody;
 	
 		// FIXME this is a hack. See  GLViewer:86
 		// problem is that currently there is no way to transmit arguments between UI and GLDraw* methods.
@@ -159,6 +162,11 @@
 		void init();
 		void timeInit();
 
+		void initTemps();
+		void cleanupTemps();
+		//! Return unique temporary filename. May be deleted by the user; if not, will be deleted at shutdown.
+		string tmpFilename();
+
 		void reset();
 
 		DECLARE_LOGGER;

Modified: trunk/core/yade.cpp
===================================================================
--- trunk/core/yade.cpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/core/yade.cpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -261,6 +261,8 @@
 		Omega::instance().scanPlugins(string(PREFIX "/lib/yade" SUFFIX "/gui" ));
 	Omega::instance().init();
 
+	// make directory for temporaries
+	Omega::instance().initTemps();
 	Omega::instance().setSimulationFileName(simulationFileName); //init() resets to "";
 
 	// recovery file pattern
@@ -289,6 +291,8 @@
  	// for(int i=0;i<argc; i++)cerr<<"Argument "<<i<<": "<<argv[i]<<endl;
 	int ok = frontEnd->run(argc,argv);
 
+	// remove all remaining temporary files
+	Omega::instance().cleanupTemps();
 	#ifdef YADE_PYTHON
 		/* pyFinalize crashes with boost:python<=1.35
 		 * http://www.boost.org/libs/python/todo.html#pyfinalize-safety has explanation 

Modified: trunk/gui/qt3/GLViewer.cpp
===================================================================
--- trunk/gui/qt3/GLViewer.cpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/gui/qt3/GLViewer.cpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -191,24 +191,24 @@
 
 string GLViewer::getState(){
 	QString origStateFileName=stateFileName();
-	char tmpnam_str [L_tmpnam]; char* result=tmpnam(tmpnam_str); if(!result) throw runtime_error("Failure getting temporary filename.");
-	setStateFileName(tmpnam_str); saveStateToFile(); setStateFileName(origStateFileName);
-	LOG_DEBUG("State saved to temp file "<<tmpnam_str);
+	string tmpFile=Omega::instance().tmpFilename();
+	setStateFileName(tmpFile); saveStateToFile(); setStateFileName(origStateFileName);
+	LOG_DEBUG("State saved to temp file "<<tmpFile);
 	// read tmp file contents and return it as string
 	// this will replace all whitespace by space (nowlines will disappear, which is what we want)
-	ifstream in(tmpnam_str); string ret; while(!in.eof()){string ss; in>>ss; ret+=" "+ss;}; in.close();
-	boost::filesystem::remove(boost::filesystem::path(tmpnam_str));
+	ifstream in(tmpFile.c_str()); string ret; while(!in.eof()){string ss; in>>ss; ret+=" "+ss;}; in.close();
+	boost::filesystem::remove(boost::filesystem::path(tmpFile));
 	return ret;
 }
 
 void GLViewer::setState(string state){
-	char tmpnam_str [L_tmpnam]; char* result=tmpnam(tmpnam_str); if(!result) throw runtime_error("Failure getting temporary filename.");
-	std::ofstream out(tmpnam_str);
-	if(!out.good()){ LOG_ERROR("Error opening temp file `"<<tmpnam_str<<"', loading aborted."); return; }
+	string tmpFile=Omega::instance().tmpFilename();
+	std::ofstream out(tmpFile.c_str());
+	if(!out.good()){ LOG_ERROR("Error opening temp file `"<<tmpFile<<"', loading aborted."); return; }
 	out<<state; out.close();
-	LOG_DEBUG("Will load state from temp file "<<tmpnam_str);
-	QString origStateFileName=stateFileName(); setStateFileName(tmpnam_str); restoreStateFromFile(); setStateFileName(origStateFileName);
-	boost::filesystem::remove(boost::filesystem::path(tmpnam_str));
+	LOG_DEBUG("Will load state from temp file "<<tmpFile);
+	QString origStateFileName=stateFileName(); setStateFileName(tmpFile); restoreStateFromFile(); setStateFileName(origStateFileName);
+	boost::filesystem::remove(boost::filesystem::path(tmpFile));
 }
 
 void GLViewer::keyPressEvent(QKeyEvent *e)
@@ -250,7 +250,7 @@
 	/* letters alphabetically */
 	else if(e->key()==Qt::Key_C && selectedName() >= 0 && (*(Omega::instance().getRootBody()->bodies)).exists(selectedName())) setSceneCenter(manipulatedFrame()->position());
 	else if(e->key()==Qt::Key_C && (e->state() & AltButton)){ displayMessage("Median centering"); centerMedianQuartile(); }
-	else if(e->key()==Qt::Key_D &&(e->state() & AltButton)){ body_id_t id; if((id=Omega::instance().selectedBody)>=0){ const shared_ptr<Body>& b=Body::byId(id); b->isDynamic=!b->isDynamic; LOG_INFO("Body #"<<id<<" now "<<(b->isDynamic?"":"NOT")<<" dynamic"); } }
+	else if(e->key()==Qt::Key_D &&(e->state() & AltButton)){ body_id_t id; if((id=Omega::instance().getRootBody()->selectedBody)>=0){ const shared_ptr<Body>& b=Body::byId(id); b->isDynamic=!b->isDynamic; LOG_INFO("Body #"<<id<<" now "<<(b->isDynamic?"":"NOT")<<" dynamic"); } }
 	else if(e->key()==Qt::Key_D) {timeDispMask+=1; if(timeDispMask>(TIME_REAL|TIME_VIRT|TIME_ITER))timeDispMask=0; }
 	else if(e->key()==Qt::Key_G) {bool anyDrawn=drawGridXYZ[0]||drawGridXYZ[1]||drawGridXYZ[2]; for(int i=0; i<3; i++)drawGridXYZ[i]=!anyDrawn; }
 	else if (e->key()==Qt::Key_M && selectedName() >= 0){
@@ -439,7 +439,7 @@
 	if(selection<0){
 		if(isMoving){
 			displayMessage("Moving finished"); mouseMovesCamera(); isMoving=false;
-			Omega::instance().selectedBody = -1;
+			Omega::instance().getRootBody()->selectedBody = -1;
 		}
 		return;
 	}
@@ -457,7 +457,7 @@
 		Quaternionr& q = Body::byId(selection)->physicalParameters->se3.orientation;
 		Vector3r&    v = Body::byId(selection)->physicalParameters->se3.position;
 		manipulatedFrame()->setPositionAndOrientation(qglviewer::Vec(v[0],v[1],v[2]),qglviewer::Quaternion(q[0],q[1],q[2],q[3]));
-		Omega::instance().selectedBody = selection;
+		Omega::instance().getRootBody()->selectedBody = selection;
 		#ifdef YADE_PYTHON
 			try{
 				PyGILState_STATE gstate;

Modified: trunk/gui/qt3/QtGUI-python.cpp
===================================================================
--- trunk/gui/qt3/QtGUI-python.cpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/gui/qt3/QtGUI-python.cpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -83,7 +83,12 @@
 	shared_ptr<QtSimulationPlayer> player=ensuredMainWindow()->player;
 	GLSimulationPlayerViewer* glv=player->glSimulationPlayerViewer;
 	string snapBase2(snapBase);
-	if(snapBase2.empty()){ char tmpnam_str [L_tmpnam]; char* ret=tmpnam(tmpnam_str); if(ret!=tmpnam_str) throw runtime_error("tmpnam failed."); snapBase2=tmpnam_str; LOG_INFO("Using "<<snapBase2<<" as temporary basename for snapshots."); }
+	if(snapBase2.empty()){
+		char tmpl[]="/tmp/yade-player-XXXXXX";
+		snapBase2=mkdtemp(tmpl);
+		snapBase2+="/frame-";
+		LOG_INFO("Using "<<snapBase2<<" as temporary basename for snapshots.");
+	}
 	glv->stride=stride;
 	glv->load(savedSim); // Omega locks rendering here for us
 	glv->saveSnapShots=true;

Modified: trunk/pkg/snow/RenderingEngine/Ef1_BshSnowGrain_glDraw.cpp
===================================================================
--- trunk/pkg/snow/RenderingEngine/Ef1_BshSnowGrain_glDraw.cpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/pkg/snow/RenderingEngine/Ef1_BshSnowGrain_glDraw.cpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -171,7 +171,7 @@
 /*
 	// plot depth tetrahedron of selected surface
 
-//	int me = (int)(Omega::instance().selectedBody);
+//	int me = (int)(Omega::instance().getRootBody()->selectedBody);
 //	if(me > 0 && me < Omega::instance().getRootBody()->bodies->size())
 //	{
 //		BshSnowGrain* m = dynamic_cast<BshSnowGrain*>((*(Omega::instance().getRootBody()->bodies))[me]->geometricalModel.get());
@@ -224,7 +224,7 @@
 //	std::vector<Vector3r> me_inside;me_inside.clear();
 //	std::vector<Vector3r> oth_inside;oth_inside.clear();
 
-	int me = (int)(Omega::instance().selectedBody);
+	int me = (int)(Omega::instance().getRootBody()->selectedBody);
 	if(me > 0 && me < Omega::instance().getRootBody()->bodies->size())
 	{
 		BshSnowGrain* m = dynamic_cast<BshSnowGrain*>((*(Omega::instance().getRootBody()->bodies))[me]->geometricalModel.get());
@@ -277,7 +277,7 @@
 			if(gr->slices[0][0] == m->slices[0][0])
 			{
 				std::cerr << "got body " << me << "\n";
-				int other=(int)(Omega::instance().selectedBody);
+				int other=(int)(Omega::instance().getRootBody()->selectedBody);
 				if(other > 0 && other < Omega::instance().getRootBody()->bodies->size())
 				{
 				BshSnowGrain* oth = dynamic_cast<BshSnowGrain*>((*(Omega::instance().getRootBody()->bodies))[other]->geometricalModel.get());
@@ -317,7 +317,7 @@
 	// check current grain insides
 //if(!surface)
 //{
-//	int me = (int)(Omega::instance().selectedBody);
+//	int me = (int)(Omega::instance().getRootBody()->selectedBody);
 //	if(me > 0 && me < Omega::instance().getRootBody()->bodies->size())
 //	{
 //		BshSnowGrain* m = dynamic_cast<BshSnowGrain*>((*(Omega::instance().getRootBody()->bodies))[me]->geometricalModel.get());
@@ -345,7 +345,7 @@
 if(!surface)
 {
 //	glBegin(GL_POINTS);
-	int me = (int)(Omega::instance().selectedBody);
+	int me = (int)(Omega::instance().getRootBody()->selectedBody);
 	if(me > 0 && me < Omega::instance().getRootBody()->bodies->size())
 	{
 		BshSnowGrain* m = dynamic_cast<BshSnowGrain*>((*(Omega::instance().getRootBody()->bodies))[me]->geometricalModel.get());

Modified: trunk/pkg/snow/RenderingEngine/Ef1_IstSnowLayersContact_glDraw.cpp
===================================================================
--- trunk/pkg/snow/RenderingEngine/Ef1_IstSnowLayersContact_glDraw.cpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/pkg/snow/RenderingEngine/Ef1_IstSnowLayersContact_glDraw.cpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -229,7 +229,7 @@
 
 
 /*
-	if(!(id1 == (int)(Omega::instance().selectedBody) || id2 == (int)(Omega::instance().selectedBody)))
+	if(!(id1 == (int)(Omega::instance().getRootBody()->selectedBody) || id2 == (int)(Omega::instance().getRootBody()->selectedBody)))
 		return;
 
 	assert(Omega::instance().getRootBody()->bodies->exists(id1));

Modified: trunk/py/yadeWrapper/yadeWrapper.cpp
===================================================================
--- trunk/py/yadeWrapper/yadeWrapper.cpp	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/py/yadeWrapper/yadeWrapper.cpp	2009-07-31 10:53:44 UTC (rev 1908)
@@ -565,7 +565,7 @@
 long Interaction_getId2(const shared_ptr<Interaction>& i){ return (long)i->getId2(); }
 
 void FileGenerator_generate(const shared_ptr<FileGenerator>& fg, string outFile){ fg->setFileName(outFile); fg->setSerializationLibrary("XMLFormatManager"); bool ret=fg->generateAndSave(); LOG_INFO((ret?"SUCCESS:\n":"FAILURE:\n")<<fg->message); if(ret==false) throw runtime_error("Generator reported error: "+fg->message); };
-void FileGenerator_load(const shared_ptr<FileGenerator>& fg){ char tmpnam_str [L_tmpnam]; char* result=tmpnam(tmpnam_str); if(result!=tmpnam_str) throw runtime_error(__FILE__ ": tmpnam(char*) failed!");  string xml(tmpnam_str+string(".xml.bz2")); LOG_DEBUG("Using temp file "<<xml); FileGenerator_generate(fg,xml); pyOmega().load(xml); }
+void FileGenerator_load(const shared_ptr<FileGenerator>& fg){ string xml(Omega::instance().tmpFilename()+".xml.bz2"); LOG_DEBUG("Using temp file "<<xml); FileGenerator_generate(fg,xml); pyOmega().load(xml); }
 
 // many thanks to http://markmail.org/message/s4ksg6nfspw2wxwd
 namespace boost { namespace python { namespace detail {

Modified: trunk/scripts/simple-scene-player.py
===================================================================
--- trunk/scripts/simple-scene-player.py	2009-07-31 10:07:59 UTC (rev 1907)
+++ trunk/scripts/simple-scene-player.py	2009-07-31 10:53:44 UTC (rev 1908)
@@ -30,7 +30,7 @@
 
 def setWire():
 	o=Omega()
-	for b in o.bodies: b.shape['wire']=True
+	for b in o.bodies: b.shape['wire']=False
 # you could have saved the viewer state by using Alt-S in the view...
 from yade import qt
 qt.makePlayerVideo('/tmp/player.sqlite','/tmp/player.ogg','/tmp/qglviewerState.xml',stride=10,fps=12,postLoadHook='setWire()')