← Back to team overview

yade-dev team mailing list archive

[svn] r1673 - in trunk: gui/py pkg/common pkg/common/Engine/FilterEngine

 

Author: sega
Date: 2009-02-20 20:21:34 +0100 (Fri, 20 Feb 2009)
New Revision: 1673

Added:
   trunk/pkg/common/Engine/FilterEngine/ColorizedTimeFilter.cpp
   trunk/pkg/common/Engine/FilterEngine/ColorizedTimeFilter.hpp
   trunk/pkg/common/Engine/FilterEngine/PythonRunnerFilter.cpp
   trunk/pkg/common/Engine/FilterEngine/PythonRunnerFilter.hpp
Modified:
   trunk/gui/py/utils.py
   trunk/pkg/common/Engine/FilterEngine/FilterEngine.cpp
   trunk/pkg/common/SConscript
Log:
Add two new filters: ColorizedTimeFilter and PythonRunnerFilter


Modified: trunk/gui/py/utils.py
===================================================================
--- trunk/gui/py/utils.py	2009-02-20 16:49:51 UTC (rev 1672)
+++ trunk/gui/py/utils.py	2009-02-20 19:21:34 UTC (rev 1673)
@@ -389,6 +389,16 @@
 		
 
 def ColorizedVelocityFilter(isFilterActivated=True,autoScale=True,minValue=0,maxValue=0,posX=0,posY=0.2,width=0.05,height=0.5,title='Velocity, m/s'):
-	f = DeusExMachina('ColorizedVelocityFilter',{'isFilterActivated':isFilterActivated,'autoScale':autoScale,'minValue':minValue,'maxValue':maxValue,'posX':posX,'posY':posY,'width':width,'height':height,'title':title})
-	O.engines+=[f]
-	return f
+    f = DeusExMachina('ColorizedVelocityFilter',{'isFilterActivated':isFilterActivated,'autoScale':autoScale,'minValue':minValue,'maxValue':maxValue,'posX':posX,'posY':posY,'width':width,'height':height,'title':title})
+    O.engines+=[f]
+    return f
+
+def ColorizedTimeFilter(point=[0,0,0],normal=[0,1,0],isFilterActivated=True,autoScale=True,minValue=0,maxValue=0,posX=0,posY=0.2,width=0.05,height=0.5,title='Time, m/s'):
+    f = DeusExMachina('ColorizedTimeFilter',{'point':point,'normal':normal,'isFilterActivated':isFilterActivated,'autoScale':autoScale,'minValue':minValue,'maxValue':maxValue,'posX':posX,'posY':posY,'width':width,'height':height,'title':title})
+    O.engines+=[f]
+    return f
+
+def PythonRunnerFilter(command='pass',isFilterActivated=True):
+    f = DeusExMachina('PythonRunnerFilter',{'command':command,'isFilterActivated':isFilterActivated})
+    O.engines+=[f]
+    return f

Added: trunk/pkg/common/Engine/FilterEngine/ColorizedTimeFilter.cpp
===================================================================
--- trunk/pkg/common/Engine/FilterEngine/ColorizedTimeFilter.cpp	2009-02-20 16:49:51 UTC (rev 1672)
+++ trunk/pkg/common/Engine/FilterEngine/ColorizedTimeFilter.cpp	2009-02-20 19:21:34 UTC (rev 1673)
@@ -0,0 +1,178 @@
+/*************************************************************************
+*  Copyright (C) 2008 by Sergei Dorofeenko                               *
+*  sega@xxxxxxxxxxxxxxxx                                                 *
+*                                                                        *
+*  This program is free software; it is licensed under the terms of the  *
+*  GNU General Public License v2 or later. See file LICENSE for details. *
+*************************************************************************/
+
+#include"ColorizedTimeFilter.hpp"
+#include <yade/pkg-common/ColorScale.hpp>
+
+CREATE_LOGGER(ColorizedTimeFilter);
+
+ColorizedTimeFilter::ColorizedTimeFilter() : FilterEngine() 
+{
+	first=true;
+	point=Vector3r(0,0,0);
+	normal=Vector3r(0,1,0);
+	autoScale=true;
+	minValue=0;
+	maxValue=0;
+	onlyDynamic=true;
+	prevSimulTime=0;
+	subscrBodies.clear();
+
+	posX=0;
+	posY=0.2;
+	width=0.05;
+	height=0.5;
+	title="Time, s";
+}
+
+
+ColorizedTimeFilter::~ColorizedTimeFilter()
+{
+}
+
+bool ColorizedTimeFilter::isActivated()
+{
+	return FilterEngine::isActivated();
+}
+
+void ColorizedTimeFilter::registerAttributes()
+{
+	FilterEngine::registerAttributes();
+	REGISTER_ATTRIBUTE(point);
+	REGISTER_ATTRIBUTE(normal);
+	REGISTER_ATTRIBUTE(autoScale);
+	REGISTER_ATTRIBUTE(onlyDynamic);
+	REGISTER_ATTRIBUTE(minValue);
+	REGISTER_ATTRIBUTE(maxValue);
+
+	REGISTER_ATTRIBUTE(posX);
+	REGISTER_ATTRIBUTE(posY);
+	REGISTER_ATTRIBUTE(width);
+	REGISTER_ATTRIBUTE(height);
+	REGISTER_ATTRIBUTE(title);
+}
+
+void ColorizedTimeFilter::applyCondition(MetaBody* ncb)
+{
+
+	if (first) { initialize(ncb); return; }
+	
+	Real currSimulTime = Omega::instance().getSimulationTime();
+	shared_ptr<BodyContainer>& bodies = ncb->bodies;
+	for (int i=0,e=subscrBodies.size(); i<e; ++i)
+	{
+		shared_ptr<Body> b =(*bodies)[subscrBodies[i]]; 
+		Vector3r pos = b->physicalParameters->se3.position;
+		if ( values[i] != -1 )
+			values[i]+=currSimulTime-prevSimulTime;
+		else if ( (pos-point).Dot(normal)<0 ) 
+			values[i]=0;
+	}
+	prevSimulTime = currSimulTime;
+	makeScale();
+	updateColorScale();
+	for (int i=0,e=subscrBodies.size(); i<e; ++i)
+	{
+		shared_ptr<Body> b =(*bodies)[subscrBodies[i]]; 
+		if (values[i]>0)
+			b->geometricalModel->diffuseColor = getColor4Value(values[i]);
+	}
+}
+
+void ColorizedTimeFilter::updateColorScale()
+{
+	legend->posX = posX; 
+	legend->posY = posY; 
+	legend->width = width; 
+	legend->height = height; 
+
+	if (cur_minValue==minValue && cur_maxValue==maxValue) return;
+
+	cur_minValue=minValue;
+	cur_maxValue=maxValue;
+	legend->colors.resize(3);
+	Real h = (maxValue-minValue)/(legend->colors.size()-1);
+	for(unsigned int i=0; i<legend->colors.size(); ++i)
+		legend->colors[i] = getColor4Value(minValue+h*i);
+
+	legend->labels.resize(5);
+	h = (maxValue-minValue)/(legend->labels.size()-1);
+	for(unsigned int i=0; i<legend->labels.size(); ++i)
+	{
+		char tmp[64];
+		sprintf(tmp, "%6.3f", minValue+h*i);
+		legend->labels[i]=string(tmp);
+	}
+}
+
+void ColorizedTimeFilter::makeScale()
+{
+	if (autoScale)
+	{
+		minValue=maxValue=0;
+		for(int i=0,e=values.size(); i<e; ++i)
+		{
+			if (values[i]>0 && values[i]<minValue) minValue = values[i];
+			if (values[i]>maxValue) maxValue = values[i];
+		}
+		LOG_INFO("minValue:" << minValue << '\t' << "maxValue:" << maxValue);
+	}
+}
+
+Vector3r ColorizedTimeFilter::getColor4Value(Real v)
+{
+	Vector3r color;
+	Real midValue = (minValue+maxValue)/2;
+	if (v<midValue) 
+	{
+		//color[0] = 0;
+		color[0] = -1/(midValue-minValue)*(v-minValue)+1;
+		color[1] = 1/(midValue-minValue)*(v-minValue);
+		color[2] = 0;
+		//color[2] = -1/(midValue-minValue)*(v-minValue)+1;
+	}
+	else 
+	{
+		//color[0] = 1/(maxValue-midValue)*(v-midValue);
+		color[0] = 0;
+		color[1] = -1/(maxValue-midValue)*(v-midValue)+1;
+		color[2] = 1/(maxValue-midValue)*(v-midValue);
+		//color[2] = 0;
+	}
+	return color;
+}
+
+void ColorizedTimeFilter::initialize(MetaBody* ncb)
+{
+	first=false;
+
+	widget = shared_ptr<Body>(new Body);
+	legend = shared_ptr<ColorScale>(new ColorScale);
+	widget->geometricalModel = legend;
+	legend->title = title;
+	widget->physicalParameters = shared_ptr<PhysicalParameters>(new PhysicalParameters);
+	widget->isDynamic=false;
+	ncb->bodies->insert(widget);
+
+	if (onlyDynamic)
+	{
+		FOREACH(shared_ptr<Body> b, *ncb->bodies) { if(b->isDynamic) subscrBodies.push_back(b->getId()); }
+	}
+	else
+		subscrBodies.assign(subscribedBodies.begin(),subscribedBodies.end());
+
+	values.resize(subscrBodies.size());
+	shared_ptr<BodyContainer>& bodies = ncb->bodies;
+	for(int i=0,e=subscrBodies.size(); i<e; ++i)
+		values[i] = ( ((*bodies)[subscrBodies[i]]->physicalParameters->se3.position-point).Dot(normal) > 0 ) ? -1:0;
+	
+	prevSimulTime = Omega::instance().getSimulationTime();
+}
+
+
+YADE_PLUGIN();

Added: trunk/pkg/common/Engine/FilterEngine/ColorizedTimeFilter.hpp
===================================================================
--- trunk/pkg/common/Engine/FilterEngine/ColorizedTimeFilter.hpp	2009-02-20 16:49:51 UTC (rev 1672)
+++ trunk/pkg/common/Engine/FilterEngine/ColorizedTimeFilter.hpp	2009-02-20 19:21:34 UTC (rev 1673)
@@ -0,0 +1,60 @@
+/*************************************************************************
+*  Copyright (C) 2008 by Sergei Dorofeenko                               *
+*  sega@xxxxxxxxxxxxxxxx                                                 *
+*                                                                        *
+*  This program is free software; it is licensed under the terms of the  *
+*  GNU General Public License v2 or later. See file LICENSE for details. *
+*************************************************************************/
+
+#pragma once
+
+#include<yade/pkg-common/FilterEngine.hpp>
+#include<yade/core/MetaBody.hpp>
+
+class ColorScale;
+
+// Assign a sphere's color with the time when it cross the plane.
+class ColorizedTimeFilter : public FilterEngine {
+	protected:
+		Real prevSimulTime;
+		vector<Real> values;
+		vector<long int> subscrBodies;
+		bool first;
+		void initialize(MetaBody*);
+		void makeScale();
+		void updateColorScale();
+		Vector3r getColor4Value(Real v);
+		shared_ptr<Body> widget;
+		shared_ptr<ColorScale> legend;
+		Real cur_minValue;
+		Real cur_maxValue;
+	public :
+		bool autoScale;
+		bool onlyDynamic;
+		Real minValue;
+		Real maxValue;
+
+		// plane 
+		Vector3r point;
+		Vector3r normal;
+
+		// legend
+		Real posX,posY;
+		Real width,height;
+		string title;
+
+		ColorizedTimeFilter();
+		virtual ~ColorizedTimeFilter();
+	
+		virtual bool isActivated();
+		virtual void applyCondition(MetaBody*);
+	
+		virtual void registerAttributes();
+		DECLARE_LOGGER;
+	REGISTER_CLASS_NAME(ColorizedTimeFilter);
+	REGISTER_BASE_CLASS_NAME(FilterEngine);
+};
+
+REGISTER_SERIALIZABLE(ColorizedTimeFilter);
+
+

Modified: trunk/pkg/common/Engine/FilterEngine/FilterEngine.cpp
===================================================================
--- trunk/pkg/common/Engine/FilterEngine/FilterEngine.cpp	2009-02-20 16:49:51 UTC (rev 1672)
+++ trunk/pkg/common/Engine/FilterEngine/FilterEngine.cpp	2009-02-20 19:21:34 UTC (rev 1673)
@@ -10,7 +10,7 @@
 
 bool FilterEngine::isFiltrationActivated = false;
 
-FilterEngine::FilterEngine() : DeusExMachina(), isFilterActivated(false)
+FilterEngine::FilterEngine() : DeusExMachina(), isFilterActivated(true)
 {
 }
 

Added: trunk/pkg/common/Engine/FilterEngine/PythonRunnerFilter.cpp
===================================================================
--- trunk/pkg/common/Engine/FilterEngine/PythonRunnerFilter.cpp	2009-02-20 16:49:51 UTC (rev 1672)
+++ trunk/pkg/common/Engine/FilterEngine/PythonRunnerFilter.cpp	2009-02-20 19:21:34 UTC (rev 1673)
@@ -0,0 +1,2 @@
+#include"PythonRunnerFilter.hpp"
+YADE_PLUGIN();

Added: trunk/pkg/common/Engine/FilterEngine/PythonRunnerFilter.hpp
===================================================================
--- trunk/pkg/common/Engine/FilterEngine/PythonRunnerFilter.hpp	2009-02-20 16:49:51 UTC (rev 1672)
+++ trunk/pkg/common/Engine/FilterEngine/PythonRunnerFilter.hpp	2009-02-20 19:21:34 UTC (rev 1673)
@@ -0,0 +1,32 @@
+/*************************************************************************
+*  Copyright (C) 2009 by Sergei Dorofeenko                               *
+*  sega@xxxxxxxxxxxxxxxx                                                 *
+*                                                                        *
+*  This program is free software; it is licensed under the terms of the  *
+*  GNU General Public License v2 or later. See file LICENSE for details. *
+*************************************************************************/
+#pragma once
+#include<yade/core/MetaBody.hpp>
+#include<yade/pkg-common/FilterEngine.hpp>
+
+// Run python command
+class PythonRunnerFilter: public FilterEngine {
+	private:
+		string command;
+	public :
+		PythonRunnerFilter(): command("pass"){};
+		virtual void action(MetaBody* b){
+			PyGILState_STATE gstate;
+				gstate = PyGILState_Ensure();
+				PyRun_SimpleString(command.c_str()); 
+			PyGILState_Release(gstate);
+		}
+		virtual void registerAttributes(){ FilterEngine::registerAttributes(); REGISTER_ATTRIBUTE(command); }
+	protected :
+		virtual void postProcessAttributes(bool deserializing){}
+	REGISTER_CLASS_NAME(PythonRunnerFilter);
+	REGISTER_BASE_CLASS_NAME(FilterEngine);
+};
+
+REGISTER_SERIALIZABLE(PythonRunnerFilter);
+

Modified: trunk/pkg/common/SConscript
===================================================================
--- trunk/pkg/common/SConscript	2009-02-20 16:49:51 UTC (rev 1672)
+++ trunk/pkg/common/SConscript	2009-02-20 19:21:34 UTC (rev 1673)
@@ -112,6 +112,10 @@
 		LIBS=env['LIBS']+['FilterEngine']),
 	env.SharedLibrary('ColorizedVelocityFilter',['Engine/FilterEngine/ColorizedVelocityFilter.cpp'],
 		LIBS=env['LIBS']+['FilterEngine','ColorScale']),
+	env.SharedLibrary('ColorizedTimeFilter',['Engine/FilterEngine/ColorizedTimeFilter.cpp'],
+		LIBS=env['LIBS']+['FilterEngine','ColorScale']),
+	env.SharedLibrary('PythonRunnerFilter',['Engine/FilterEngine/PythonRunnerFilter.cpp'],
+		LIBS=env['LIBS']+['FilterEngine']),
 	env.SharedLibrary('BoundingVolumeMetaEngine',['Engine/MetaEngine/BoundingVolumeMetaEngine.cpp']),
 	env.SharedLibrary('GeometricalModelMetaEngine',['Engine/MetaEngine/GeometricalModelMetaEngine.cpp']),
 	env.SharedLibrary('InteractingGeometryMetaEngine',['Engine/MetaEngine/InteractingGeometryMetaEngine.cpp']),