Random drop filter refined

This commit is contained in:
Paolo Cignoni cignoni 2010-01-02 12:59:36 +00:00
parent b2b53b979c
commit cd6c5b87f5
5 changed files with 32 additions and 28 deletions

View File

@ -37,15 +37,14 @@ void GravitySubFilter::initialize(MeshDocument& md, RichParameterSet& par){
m_engine.clear();
m_engine.setGlobalForce(gravity);
for(unsigned int i = 0; i < md.size(); i++)
for(int i = 0; i < md.size(); i++)
m_engine.registerTriMesh(*md.getMesh(i), m_currentSceneryLayer == i ? true : false);
m_layersTrans.clear();
for(unsigned int i = 0; i < md.size(); i++)
m_layersTrans.push_back(LayerTransformations());
m_layersTrans.resize(md.size());
for(int i = 0; i <= m_steps; i++){
for(unsigned int j = 0; j < md.size(); j++){
for(int j = 0; j < md.size(); j++){
m_layersTrans[j].push_back(m_engine.getTransformationMatrix(*md.getMesh(j)));
}
m_engine.integrate(m_stepSize);

View File

@ -1,6 +1,6 @@
#include "MeshSubFilter.h"
MeshSubFilter::MeshSubFilter() : m_stepSize(0.01), m_steps(-1), m_seconds(-1){
MeshSubFilter::MeshSubFilter() : m_steps(-1), m_seconds(-1), m_stepSize(0.01), m_stepsPerSecond(1.0f/m_stepSize) {
}
void MeshSubFilter::initParameterSet(QAction* action,MeshDocument& md, RichParameterSet & par){

View File

@ -28,6 +28,7 @@ protected:
int m_steps;
int m_seconds;
const float m_stepSize;
const unsigned int m_stepsPerSecond;
LayersTransformations m_layersTrans;
};

View File

@ -7,7 +7,7 @@
using namespace std;
RandomDropFilter::RandomDropFilter() : m_randomLayer(-1), m_distance(-1), m_dropRate(-1){
RandomDropFilter::RandomDropFilter() : m_randomLayer(-1), m_dropRate(-1), m_distance(-1){
}
void RandomDropFilter::initParameterSet(QAction* action,MeshDocument& md, RichParameterSet & par){
@ -17,8 +17,8 @@ void RandomDropFilter::initParameterSet(QAction* action,MeshDocument& md, RichPa
for(int i = 1; i <= md.size(); i++){
layers.push_back(QString::number(i));
}
par.addParam(new RichEnum("randomLayer", 1, layers, "Random object layer", "Select the layer that contains the object that will spawn randomly"));
par.addParam(new RichEnum("randomLayer", 0, layers, "Random object layer", "Select the layer that contains the object that will spawn randomly"));
par.addParam(new RichFloat("distance", 1.0f, "Random radius interval", "The object will spawn in a random position contained in the specified radius"));
par.addParam(new RichInt("dropRate", 2, "Drop rate", "The drop rate"));
}
@ -26,14 +26,14 @@ void RandomDropFilter::initParameterSet(QAction* action,MeshDocument& md, RichPa
bool RandomDropFilter::applyFilter(QAction* filter, MeshDocument &md, RichParameterSet& par, vcg::CallBackPos* cb){
MeshSubFilter::applyFilter(filter, md, par, cb);
int currentStep = (par.getDynamicFloat("timeline") * m_steps) / 100;
unsigned int rndObjects = m_seconds/m_dropRate;
unsigned int currentRndObject = md.size() - rndObjects + currentStep/(100*m_dropRate);
int currentStep = (par.getDynamicFloat("timeline") * m_steps) / m_stepsPerSecond;
int randomObjects = m_seconds/m_dropRate;
int currentRandomObject = md.size() - randomObjects + currentStep/(m_stepsPerSecond*m_dropRate);
for(int i = 0; i < md.size(); i++){
md.getMesh(i)->cm.Tr = m_layersTrans[i][currentStep];
if(i < currentRndObject)
if(i < currentRandomObject)
md.getMesh(i)->visible = true;
else
md.getMesh(i)->visible = false;
@ -50,30 +50,34 @@ void RandomDropFilter::initialize(MeshDocument& md, RichParameterSet& par){
m_engine.setGlobalForce(gravity);
srand((unsigned)time(0));
int randomObjects = m_seconds/m_dropRate;
m_steps = m_seconds/m_stepSize;
unsigned int rndObjects = m_seconds/m_dropRate;
for(unsigned int i = 0; i < rndObjects; i++)
foreach(MeshModel *mesh, md.meshList){
if(mesh->fileName.find("randomMesh") == 0)
md.delMesh(mesh);
}
for(int i = 0; i < randomObjects; i++)
addRandomObject(md);
for(unsigned int i = 0; i < md.size() - rndObjects; i++)
for(int i = 0; i < md.size() - randomObjects; i++)
m_engine.registerTriMesh(*md.getMesh(i), m_randomLayer == i ? false : true);
m_layersTrans.clear();
for(unsigned int i = 0; i < md.size(); i++)
m_layersTrans.push_back(LayerTransformations());
m_layersTrans.resize(md.size());
m_steps = m_seconds/m_stepSize;
for(int i = 0; i <= m_steps; i++){
int currentRndObject = md.size() - rndObjects + i/(100*m_dropRate);
int currentRndObject = md.size() - randomObjects + i/(m_stepsPerSecond*m_dropRate);
if(i != 0 && i % (100*m_dropRate) == 0)
if(i != 0 && i % (m_stepsPerSecond*m_dropRate) == 0)
m_engine.registerTriMesh(*md.getMesh(currentRndObject - 1), false);
for(unsigned int j = 0; j < currentRndObject; j++){
for(int j = 0; j < currentRndObject; j++){
m_layersTrans[j].push_back(m_engine.getTransformationMatrix(*md.getMesh(j)));
}
for(unsigned int j = currentRndObject; j < md.size(); j++){
for(int j = currentRndObject; j < md.size(); j++){
m_layersTrans[j].push_back(vcg::Matrix44<float>::Identity());
}
@ -82,16 +86,16 @@ void RandomDropFilter::initialize(MeshDocument& md, RichParameterSet& par){
}
void RandomDropFilter::addRandomObject(MeshDocument& md){
MeshModel* meshCopy = md.addNewMesh("rndMesh");
MeshModel* meshCopy = md.addNewMesh("randomMesh");
vcg::tri::Append<CMeshO,CMeshO>::Mesh(meshCopy->cm, md.getMesh(m_randomLayer)->cm, false, true);
meshCopy->cm.Tr = md.getMesh(m_randomLayer)->cm.Tr;
float x = (float) meshCopy->cm.Tr.GetColumn3(3).X() + m_distance/2.0f - rand()/(RAND_MAX*m_distance);
float y = (float) meshCopy->cm.Tr.GetColumn3(3).Y() + m_distance/2.0f - rand()/(RAND_MAX*m_distance);
float z = (float) meshCopy->cm.Tr.GetColumn3(3).Z() + m_distance/2.0f - rand()/(RAND_MAX*m_distance);
float x = meshCopy->cm.Tr.GetColumn3(3).X() + m_distance/2.0f - static_cast<float>(rand())/RAND_MAX*m_distance;
float y = meshCopy->cm.Tr.GetColumn3(3).Y() + m_distance/2.0f - static_cast<float>(rand())/RAND_MAX*m_distance;
float z = meshCopy->cm.Tr.GetColumn3(3).Z() + m_distance/2.0f - static_cast<float>(rand())/RAND_MAX*m_distance;
meshCopy->cm.Tr.SetRotateRad(rand(), vcg::Point3<float>(x, y, z));
meshCopy->cm.Tr.SetTranslate(x, y, z);
meshCopy->cm.Tr.SetRotateDeg(rand(), vcg::Point3f(x, y, z));
meshCopy->cm.Tr.SetColumn(3, vcg::Point3f(x, y, z));
meshCopy->visible = false;
}

View File

@ -18,7 +18,7 @@ public:
protected:
virtual void initialize(MeshDocument&, RichParameterSet&);
virtual bool configurationHasChanged(RichParameterSet& par);
void addRandomObject(MeshDocument& md);
virtual void addRandomObject(MeshDocument& md);
private:
int m_randomLayer;