env_map.h 3.4 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#ifndef ENVMAP_H
#define ENVMAP_H

#include <iostream>
#include <algorithm>

#include "Topology/generic/parameters.h"
#include "Topology/map/map2.h"
#include "Topology/generic/embeddedMap2.h"

#include "Algo/ImplicitHierarchicalMesh/ihm.h"
#include "Algo/ImplicitHierarchicalMesh/subdivision.h"

#include "Algo/Modelisation/polyhedron.h"
#include "Algo/Modelisation/extrusion.h"
#include "Algo/Modelisation/subdivision.h"
#include "Algo/Geometry/centroid.h"
#include "Algo/Geometry/area.h"

#include "Container/fakeAttribute.h"

#include "env_generator.h"

#include "Algo/Parallel/parallel_foreach.h"

using namespace CGoGN;

class Agent;

struct PFP: public PFP_STANDARD
{
	// definition de la carte
// 	typedef EmbeddedMap2<Map2> MAP;
 	typedef Algo::IHM::ImplicitHierarchicalMap MAP;

	// definition des listes d'agent
	typedef std::vector<Agent*> AGENTS;
	typedef std::vector<Dart> OBSTACLES;
	typedef NoMathIONameAttribute<AGENTS> AGENTVECT;
	typedef NoMathIONameAttribute<OBSTACLES> OBSTACLEVECT;
	typedef AttributeHandler<AGENTVECT> TAB_AGENTVECT;
	typedef AttributeHandler<OBSTACLEVECT> TAB_OBSTACLEVECT;
};

typedef PFP::VEC3 VEC3;

class EnvMap
{
public :
	EnvMap();
	
	unsigned int mapMemoryCost();
	Dart getBelongingCell(const PFP::VEC3& pos);
	
	void init();
	
	void registerObstaclesInFaces();
	void addNeighborObstacles(PFP::OBSTACLES& obst, Dart d, bool edgeNeighbor);
	
	void setAgentNeighborsAndObstacles(Agent* agent);

	void addNeighborAgents(Agent* agent, Dart d);
	
//	void agentChangeFaceThroughEdge(Agent* agent, Dart oldFace);
	void agentChangeFace(Agent* agent, Dart oldFace);

	void pushAgentInCell(Agent* agent, Dart d);
	void popAgentInCell(Agent* agent, Dart d);
	
	void addRefineCandidate(Dart d);
	void addCoarsenCandidate(Dart d);
	void clearUpdateCandidates();
	
	void updateMap();

	void resetAgentInFace(Agent* agent);
	
	PFP::MAP map;

	PFP::TVEC3 position;
	PFP::TVEC3 normal;

	PFP::TAB_AGENTVECT agentvect;
	PFP::TAB_OBSTACLEVECT obstvect;

	CellMarker obstacleMark;
	CellMarker buildingMark;

	static const unsigned int nbAgentsToSubdivide = 12;
	static const unsigned int nbAgentsToSimplify = 8;

	std::map<unsigned int, Dart> refineCandidate;
	std::map<unsigned int, Dart> coarsenCandidate;
};

/**************************************
*           INLINE FUNCTIONS          *
**************************************/

inline void EnvMap::pushAgentInCell(Agent* agent, Dart d)
{
	assert(map.getCurrentLevel() == map.getMaxLevel());
	assert(std::find(agentvect[d].begin(), agentvect[d].end(), agent) == agentvect[d].end());
	
	agentvect[d].push_back(agent);
	addRefineCandidate(d);
}

inline void EnvMap::popAgentInCell(Agent* agent, Dart d)
{
	assert(map.getCurrentLevel() == map.getMaxLevel());
	assert(std::find(agentvect[d].begin(), agentvect[d].end(), agent) != agentvect[d].end());
	
	PFP::AGENTS::iterator end = agentvect[d].end();
	for(PFP::AGENTS::iterator it = agentvect[d].begin(); it != end; ++it)
	{
		if(*it == agent)
		{
			*it = agentvect[d].back();
			agentvect[d].pop_back();
			break;
		}
	}
	addCoarsenCandidate(d);
}

inline void EnvMap::addRefineCandidate(Dart d)
{
	refineCandidate.insert(std::pair<unsigned int, Dart>(map.getEmbedding(d, FACE_ORBIT), d)) ;
}

inline void EnvMap::addCoarsenCandidate(Dart d)
{
	coarsenCandidate.insert(std::pair<unsigned int, Dart>(map.getEmbedding(d, FACE_ORBIT), d)) ;
}

inline void EnvMap::clearUpdateCandidates()
{
	refineCandidate.clear();
	coarsenCandidate.clear();
}

#endif