simulator.cpp 6.3 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
#include "simulator.h"

Thomas's avatar
Thomas committed
3
Simulator::Simulator() : globalTime_(0.0f), timeStep_(0.1f)
Pierre Kraemer's avatar
Pierre Kraemer committed
4
{
Pierre Kraemer's avatar
Pierre Kraemer committed
5
	srand(10);
Pierre Kraemer's avatar
Pierre Kraemer committed
6
7
	envMap_.init();
	std::cout << "setup scenario" << std::endl;
8
	importAgents("myAgents.pos");
9
10
//	setupScenario(15000);
	addPathsToAgents();
Pierre Kraemer's avatar
Pierre Kraemer committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	for(unsigned int i = 0; i < agents_.size(); ++i)
	{
		agents_[i]->updateObstacleNeighbors();
		agents_[i]->updateAgentNeighbors();
	}
}

Simulator::~Simulator()
{
	for (unsigned int i = 0; i < agents_.size(); ++i)
		delete agents_[i];
}

void Simulator::doStep()
{
	envMap_.clearUpdateCandidates();

Pierre Kraemer's avatar
Pierre Kraemer committed
28
	envMap_.map.setCurrentLevel(0);
Pierre Kraemer's avatar
Pierre Kraemer committed
29
30
31
32
33
	for (unsigned int i = 0; i < agents_.size(); ++i)
	{
		agents_[i]->computePrefVelocity();
		agents_[i]->computeNewVelocity();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
34
	envMap_.map.setCurrentLevel(envMap_.map.getMaxLevel());
Pierre Kraemer's avatar
Pierre Kraemer committed
35
36
37
38
39

	for (unsigned int i = 0; i < agents_.size(); ++i)
	{
		Dart oldFace = agents_[i]->part_.d;
		agents_[i]->update();
Pierre Kraemer's avatar
Pierre Kraemer committed
40
41
42
43
//		if(envMap_.map.getEmbedding(oldFace, FACE_ORBIT) != envMap_.map.getEmbedding(agents_[i]->part_.d, FACE_ORBIT))
		if(agents_[i]->part_.crossCell != CGoGN::Algo::MovingObjects::NO_CROSS)
//		switch(agents_[i]->part_.crossCell)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
44
//			case CGoGN::Algo::MovingObjects::CROSS_EDGE :
Pierre Kraemer's avatar
Pierre Kraemer committed
45
46
47
//				envMap_.agentChangeFaceThroughEdge(agents_[i]);
//				envMap_.addCoarsenCandidate(oldFace);
//				envMap_.addRefineCandidate(agents_[i]->part_.d);
Pierre Kraemer's avatar
Pierre Kraemer committed
48
49
50
//				break;
//			case CGoGN::Algo::MovingObjects::CROSS_OTHER :
				envMap_.agentChangeFace(agents_[i], oldFace);
Pierre Kraemer's avatar
Pierre Kraemer committed
51
52
				envMap_.addCoarsenCandidate(oldFace);
				envMap_.addRefineCandidate(agents_[i]->part_.d);
Pierre Kraemer's avatar
Pierre Kraemer committed
53
//				break;
Pierre Kraemer's avatar
Pierre Kraemer committed
54
55
56
		}

		envMap_.map.setCurrentLevel(0);
Pierre Kraemer's avatar
Pierre Kraemer committed
57
		agents_[i]->updateObstacleNeighbors();
Pierre Kraemer's avatar
Pierre Kraemer committed
58
59
		envMap_.map.setCurrentLevel(envMap_.map.getMaxLevel());
		
Pierre Kraemer's avatar
Pierre Kraemer committed
60
61
62
		agents_[i]->updateAgentNeighbors();
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
63
	envMap_.updateMap();
Pierre Kraemer's avatar
Pierre Kraemer committed
64
65
66
67

	globalTime_ += timeStep_;
}

68
void Simulator::setupScenario(unsigned int nbMaxAgent)
Pierre Kraemer's avatar
Pierre Kraemer committed
69
70
71
72
73
74
75
76
{
	/*
	* Add agents, specifying their start position, and store their goals on the
	* opposite side of the environment.
	*/
	Dart d = envMap_.map.begin();
	DartMarkerStore filled(envMap_.map);

77
78
79
80
81
82
	unsigned int nbx = 3;
	unsigned int nby = 3;

	unsigned int bMax = nbMaxAgent/(nbx*nby);

	for (unsigned int i = 0; i < bMax && d != envMap_.map.end(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
85
86
87
88
89
90
91
92
	{
		bool found = false;
		VEC3 pos;
		Dart dCell;
		while(!found && d != envMap_.map.end())
		{
			if(!filled.isMarked(d) && !envMap_.buildingMark.isMarked(d))
			{
				filled.markOrbit(FACE_ORBIT, d);
				pos = Algo::Geometry::faceCentroid<PFP>(envMap_.map, d, envMap_.position);
93
				pos[2] = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
				dCell = d;
				found = true;
			}
			envMap_.map.next(d);
		}

		if(found)
		{
			float ecart = 3.0f;
			VEC3 posinit = VEC3(pos[0] - (float(nbx)/2.0f * ecart), pos[1] - (float(nby)/2.0f * ecart), pos[2]);
			for(unsigned int curx = 0; curx < nbx; ++curx)
			{			
				for(unsigned int cury = 0; cury < nby; ++cury)
				{
					VEC3 posagent = posinit + VEC3(ecart * curx, ecart * cury, 0.0f);
					agents_.push_back(new Agent(this, posagent, dCell));
					agents_.back()->goals_.push_back(posagent);
					agents_.back()->goals_.push_back(-1.0f * posagent);
Pierre Kraemer's avatar
Pierre Kraemer committed
112
					agents_.back()->curGoal_ = 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
113
114
115
116
117
118
119
120
121
				}
			}
		}
	}
	std::cout << "nb agents : " << agents_.size() << std::endl;

	swapAgentsGoals();

	for(unsigned int i = 0; i < agents_.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
122
		envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d);
Pierre Kraemer's avatar
Pierre Kraemer committed
123
}
124

Pierre Kraemer's avatar
Pierre Kraemer committed
125
126
void Simulator::addPathsToAgents()
{
127
	unsigned int dartDistForPath=50;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
129
130
	for(unsigned int i = 0; i < agents_.size(); ++i)
	{
		agents_[i]->goals_.clear();
131

Pierre Kraemer's avatar
Pierre Kraemer committed
132
133
		Dart dStart = agents_[i]->part_.d;
		Dart dStop = dStart;
134
		for(unsigned int j = 0; envMap_.buildingMark.isMarked(dStop) || j < dartDistForPath+rand()*20; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
135
136
137
138
139
		{
			if(dStop == envMap_.map.end())
				dStop = envMap_.map.begin();
			envMap_.map.next(dStop);
		}
140
141
142

		std::vector<Dart> path = CGoGN::PathFinder::pathFindAStar<PFP>(envMap_.map, envMap_.position, dStart, dStop, envMap_.buildingMark);

Pierre Kraemer's avatar
Pierre Kraemer committed
143
144
		for(std::vector<Dart>::iterator it = path.begin(); it!=path.end();++it)
			agents_[i]->goals_.push_back(Algo::Geometry::faceCentroid<PFP>(envMap_.map, *it, envMap_.position));
145

Pierre Kraemer's avatar
Pierre Kraemer committed
146
		Dart dStop2 = dStop;
147
		for(unsigned int j = 0; envMap_.buildingMark.isMarked(dStop2) || j < dartDistForPath+rand()*20; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
148
149
150
151
152
		{
			if(dStop2 == envMap_.map.end())
				dStop2 = envMap_.map.begin();
			envMap_.map.next(dStop2);
		}
153
154
155

		path = CGoGN::PathFinder::pathFindAStar<PFP>(envMap_.map, envMap_.position, dStop, dStop2, envMap_.buildingMark);

Pierre Kraemer's avatar
Pierre Kraemer committed
156
157
		for(std::vector<Dart>::iterator it = path.begin(); it!=path.end();++it)
			agents_[i]->goals_.push_back(Algo::Geometry::faceCentroid<PFP>(envMap_.map, *it, envMap_.position));
158
159
160

		path = CGoGN::PathFinder::pathFindAStar<PFP>(envMap_.map, envMap_.position, dStop2, dStart, envMap_.buildingMark);

Pierre Kraemer's avatar
Pierre Kraemer committed
161
162
163
164
		for(std::vector<Dart>::iterator it = path.begin(); it!=path.end();++it)
			agents_[i]->goals_.push_back(Algo::Geometry::faceCentroid<PFP>(envMap_.map, *it, envMap_.position));
	}
}
165
166


Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
170
171
172
173
174
175
176
177
bool Simulator::importAgents(std::string filename)
{
	std::ifstream myfile(filename.c_str(), std::ios::in);

	if (!myfile.good())
	{
		std::cerr << "(export) Unable to open file " << filename << std::endl;
		return false;
	}

	std::string line,token;
Pierre Kraemer's avatar
Pierre Kraemer committed
178
179
	while ( myfile.good() )
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
180
181
		getline(myfile,line);

Pierre Kraemer's avatar
Pierre Kraemer committed
182
183
		if(line.size() > 1)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
184
185
186
			std::istringstream iss(line);
			float x,y,z;

Pierre Kraemer's avatar
Pierre Kraemer committed
187
188
189
190
191
			iss >> x;
			iss >> y;
			iss >> z;

			VEC3 pos(x, y, z);
Pierre Kraemer's avatar
Pierre Kraemer committed
192
193
194
195
196
			Dart dCell = envMap_.getBelongingCell(pos);

			agents_.push_back(new Agent(this, pos, dCell));
			agents_.back()->goals_.push_back(pos);
			agents_.back()->goals_.push_back(-1.0f * pos);
Pierre Kraemer's avatar
Pierre Kraemer committed
197
			agents_.back()->curGoal_ = 1;
Pierre Kraemer's avatar
Pierre Kraemer committed
198
199
200
		}
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
201
202
	swapAgentsGoals();

Pierre Kraemer's avatar
Pierre Kraemer committed
203
	for (unsigned int i = 0; i < agents_.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
204
		envMap_.pushAgentInCells(agents_[i], agents_[i]->part_.d);
Pierre Kraemer's avatar
Pierre Kraemer committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

	myfile.close();
	return true;
}

bool Simulator::exportAgents(std::string filename)
{
	std::ofstream out(filename.c_str(), std::ios::out);
	if (!out.good()) {
		std::cerr << "(export) Unable to open file " << filename << std::endl;
		return false;
	}

	for (unsigned int i = 0; i < agents_.size(); ++i) {
		out << agents_[i]->part_.m_position << std::endl;
	}

	out.close();
	return true;
}

void Simulator::swapAgentsGoals()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
228
229
230
231
232
	unsigned int nbAgents = agents_.size();
	for(unsigned int i = 0; i < nbAgents; ++i)
	{
		unsigned int r = rand() % nbAgents;

233
234
235
236
237
		if(agents_[i]->goals_[1][0]!=agents_[r]->goals_[1][0] || agents_[i]->goals_[1][1]!=agents_[r]->goals_[1][1]) {
			VEC3 g = agents_[i]->goals_[1];
			agents_[i]->goals_[1] = agents_[r]->goals_[1];
			agents_[r]->goals_[1] = g;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
238
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
239
}