bool
ParticleSimulator::Simulate(float endtime, float delta, bool bdebug)
{
	ParticleFactory* factory = ParticleFactory::getInstance();
	float snapTime = delta;
	bool bSuccess = true;
	for (set<MovingParticle*>::iterator it = factory->activeSet.begin(); it != factory->activeSet.end(); ++it)
	{
		(*it)->updateEvent();
	}
	{
		vector<Snapshot> shots = Snapshot::TakeSnapshot(time);
		snapshots.insert(snapshots.end(), shots.begin(), shots.end());
	}
	int iter = 0;
	while (time < endtime)
	{
		iter++;
		vector<Snapshot> shots = Snapshot::TakeSnapshot(time); //temporary
		MovingParticle* p = MovingParticle::getNextEvent();
		if (p == NULL) break;
		if (p->getEvent().t > endtime) break;
		if (p->id == 255)
			p->id += 0;

		for (set<MovingParticle*>::iterator it = factory->activeSet.begin(); it != factory->activeSet.end(); ++it)
		{
			(*it)->update(p->getEvent().t - time);
		}
		time = p->getEvent().t;
		if (p->applyEvent() == false) break;
		p->getEvent().print();

		MovingParticle::removeUnstable();
		if (time >= snapTime)
		{
			vector<Snapshot> shots = Snapshot::TakeSnapshot(time);
			snapshots.insert(snapshots.end(), shots.begin(), shots.end());
			snapTime += delta;
		}
		MovingParticle::quickFinish();
		if (bdebug && MovingParticle::sanityCheck() == false)
		{
			printf("Violation of sanity check found at %f.\n", time);
			vector<Snapshot> shots = Snapshot::TakeSnapshot(time);
			snapshots.insert(snapshots.end(), shots.begin(), shots.end());
			bSuccess = false;
			break;
		}

		//find closed regions
		vector<vector<MovingParticle*>> regions = MovingParticle::clusterParticles();
		for (int i = 0; i < regions.size(); ++i)
		{
			//if (clockWise(regions[i]) > 0)
			{
				vector<MovingParticle*> tr = MovingParticle::traceBackPolygon(regions[i]);
				vector<vector<MovingParticle*>> areas = MovingParticle::closedRegions(tr);
				for (int j = 0; j < areas.size(); ++j)
				{
					Snapshot shot(0.0f, areas[j]);
					if (find(closedRegions.begin(), closedRegions.end(), shot) == closedRegions.end())
					{
						closedRegions.push_back(shot);
					}
				}
				//if (tr.size() > 10) //forget small ones
				{
					Snapshot shot(0.0f, tr);
					if (find(traces.begin(), traces.end(), shot) == traces.end())
					{
						traces.push_back(Snapshot(0, tr));
						polygons.push_back(Snapshot(time, regions[i]));
					}
				}
			}
		}

		doneEvents.push_back(p->getEvent());
		//for (set<MovingParticle*>::iterator it = factory->updateQueue.begin(); it != factory->updateQueue.end(); ++it)
		for (set<MovingParticle*>::iterator it = factory->activeSet.begin(); it != factory->activeSet.end(); ++it)
		{
			(*it)->updateEvent();
		}
		factory->updateQueue.clear();
	}
	for (int i = 0; i < factory->particles.size(); ++i)
	{
		MovingParticle* p = factory->particles[i];
		if (p->bActive == false && p->time > p->init_event_time && p->init_event_time >= 0)
		{
			printf("%d %f %f %f %d\n", p->id, p->created, p->time, p->init_event_time, p->event.type);
		}
	}
	return bSuccess;
}
Ejemplo n.º 2
0
bool
ParticleSimulator::Simulate(float endtime, float delta, bool bdebug)
{
	ParticleFactory& factory = ParticleFactory::getInstance();
	PolygonFactory& pfactory = PolygonFactory::getInstance();
	float snapTime = delta;
	bool bSuccess = true;
	for (set<MovingParticle*>::iterator it = factory.activeSet.begin(); it != factory.activeSet.end(); ++it)
	{
		(*it)->updateEvent();
	}
	{
		vector<Snapshot> shots = Snapshot::TakeSnapshot(time);
		snapshots.insert(snapshots.end(), shots.begin(), shots.end());
	}
	int iter = 0;
	while (time < endtime)
	{
		iter++;
		MovingParticle* p = MovingParticle::getNextEvent();
		if (p == NULL) break;
		if (p->getEvent().t > endtime) break;
		if (p->id == 19 && p->event.q->id == 22)
			iter += 0;

		for (set<MovingParticle*>::iterator it = factory.activeSet.begin(); it != factory.activeSet.end(); ++it)
		{
			(*it)->update(p->getEvent().t - time);
		}
		time = p->getEvent().t;
		pair<MovingParticle*, MovingParticle*> pnew = p->applyEvent();
		/*if (p->event.type == SplitEvent || p->event.type == CollisionEvent)
		{
			if (p->polygon->getId() == p->event.q->polygon->getId())
			{
				//split the same polygon into two
				dag.add(p->polygon, pnew.first->polygon);
				dag.add(p->polygon, pnew.second->polygon);
			}
			else {
				//merge two polygons into one
				dag.add(pnew.first->polygon, p->polygon);
				dag.add(pnew.first->polygon, p->event.q->polygon);
			}
		}*/
		p->getEvent().print();

		//MovingParticle::removeUnstable();
		if (time >= snapTime)
		{
			vector<Snapshot> shots = Snapshot::TakeSnapshot(time);
			snapshots.insert(snapshots.end(), shots.begin(), shots.end());
			snapTime += delta;
		}
		MovingParticle::quickFinish();
		if (bdebug && MovingParticle::sanityCheck() == false)
		{
			printf("Violation of sanity check found at %f.\n", time);
			vector<Snapshot> shots = Snapshot::TakeSnapshot(time);
			snapshots.insert(snapshots.end(), shots.begin(), shots.end());
			bSuccess = false;
			break;
		}

		//find closed regions
		vector<vector<MovingParticle*>> regions = MovingParticle::clusterParticles();
		for (int i = 0; i < regions.size(); ++i)
		{
			vector<MovingParticle*> tr = MovingParticle::traceBackPolygon(regions[i]);
			vector<vector<MovingParticle*>> areas = MovingParticle::closedRegions(tr);

			Snapshot shot0(time, time, regions[i]);
			for (int j = 0; j < areas.size(); ++j)
			{
				Snapshot shot(time, 0.0f, areas[j]);
				if (find(closedRegions.begin(), closedRegions.end(), shot) == closedRegions.end())
				{
					closedRegions.push_back(shot);
					//Polygon* poly = pfactory.makePolygon(areas[j], time);
					traces.push_back(shot0);
				}
			}
			
			/*if (find(traces.begin(), traces.end(), shot0) == traces.end())
			{
				polygons.push_back(Snapshot(time, time, regions[i]));
			}*/
		}

		doneEvents.push_back(p->getEvent());
		//for (set<MovingParticle*>::iterator it = factory->updateQueue.begin(); it != factory->updateQueue.end(); ++it)
		for (set<MovingParticle*>::iterator it = factory.activeSet.begin(); it != factory.activeSet.end(); ++it)
		{
			(*it)->updateEvent();
		}
		factory.updateQueue.clear();
	}
	for (int i = 0; i < factory.particles.size(); ++i)
	{
		//factory.particles[i]->printParentTree("\t");
	}
	return bSuccess;
}