6 std::vector<Node*> mNodes;
14 Cube(
int index,
GridGraph* graph,
int tries = 0,
int found = 0);
16 Cube& operator = (
const Cube& other) {
return(*
this); }
19 int sample(std::vector<Node*>& new_nodes,
int n,
int limit);
20 void addNode(
Node* N);
22 double outgoingMean();
26 template<
class T,
class STREAM>
27 void print_vector(std::vector<T>& v, STREAM& stream)
32 for(
typename std::vector<T>::iterator it = v.begin()+1, end = v.end(); it < end; it++)
34 stream <<
" " << (*it);
39 template<
class T,
class STREAM>
40 void read_vector(std::vector<T>& v, STREAM& stream)
43 while(stream.peek() !=
'\n')
51 typedef bool (*Compare)(p3d_node* n1, p3d_node* n2);
53 extern void p3d_shoot_bounded(p3d_rob* R, std::vector<DofSpec>& params, shared_ptr<Configuration> q,
bool sample_passive);
59 std::vector<double> mOrig, mEnd;
60 std::vector<uint> mDegrees;
61 std::vector<uint> mFactor;
62 std::vector<uint> mCumulFactor;
64 std::vector<Cube*> mCubes;
65 std::vector<bool> mFreeSpace;
70 GridGraph(
double length, std::vector<double> orig, std::vector<double> end, std::vector<uint> degrees,
72 Graph(g, r, pathFactory, sampler),
73 mLength(length), mOrig(orig), mEnd(end), mDegrees(degrees),
74 mDim(mDegrees.size()), mFreeSpaceN(0), mMaxDist(maxDist)
76 for(uint i = 0; i < mDim; i++)
78 mFactor.push_back((uint)((mEnd[i] - mOrig[i]) / mLength) + 1);
79 mCumulFactor.push_back(i == 0 ? 1 : mCumulFactor[i-1] * mFactor[i-1]);
82 mCubesN = mDim == 0 ? 0 : mCumulFactor.back() * mFactor.back();
84 for(uint i(0); i < mCubesN; i++)
86 mCubes.push_back(
new Cube(i,
this));
87 mFreeSpace.push_back(
false);
93 Graph(g, r, pathFactory, sampler),
94 mFreeSpaceN(0), mMaxDist(maxDist)
96 shared_ptr<std::fstream> gridFile = open_file(filename, std::ios_base::in);
100 *gridFile >> mCubesN;
101 *gridFile >> mLength;
102 (*gridFile).ignore(1);
103 read_vector(mDegrees, *gridFile);
104 read_vector(mOrig, *gridFile);
105 read_vector(mEnd, *gridFile);
106 read_vector(mFactor, *gridFile);
107 read_vector(mCumulFactor, *gridFile);
109 while(*gridFile >> freeCube) {
112 for(uint i = mCubes.size(); i < freeCube && i < mCubesN; i++) {
113 mCubes.push_back(
new Cube(i,
this, 0, 0));
114 mFreeSpace.push_back(
false);
116 mCubes.push_back(
new Cube(freeCube,
this, tries, found));
117 mFreeSpace.push_back(
true);
125 for(uint i(0); i < mCubes.size(); i++)
127 this->Graph::freeResources();
132 std::vector<DofSpec> params;
133 for(uint i(0); i < mDim; i++) {
134 params.push_back(DofSpec(mDegrees[i], mOrig[i], mEnd[i]));
136 mSampler =
new GridSampler(mR, mR->copyConfig(mR->mR->ROBOT_GOTO), params,
true);
139 void writeGrid(
const char* filename)
141 shared_ptr<std::fstream> gridFile = open_file(filename, std::ios_base::out);
142 *gridFile << mDim << endl;
143 *gridFile << mCubesN << endl;
144 *gridFile << mLength << endl;
145 print_vector(mDegrees, *gridFile);
146 print_vector(mOrig, *gridFile);
147 print_vector(mEnd, *gridFile);
148 print_vector(mFactor, *gridFile);
149 print_vector(mCumulFactor, *gridFile);
150 for(uint i = 0; i < mCubesN; i++)
154 *gridFile << i <<
" " << mCubes[i]->mTries <<
" " << mCubes[i]->mFound << endl;
159 bool withinBounds(std::vector<double> point)
163 for(uint i(0); i < mDim && !b; i++)
165 b = b || (point[i] < mOrig[i] || point[i] > mEnd[i]);
175 for(uint i(0); i < mDim; i++)
177 n += int ((q.mQ[mDegrees[i]] - mOrig[i]) / mLength) * mCumulFactor[i];
180 if(n < 0 || n >= (
int) mCubesN)
186 std::vector<double> n_to_coords(
int n)
188 std::vector<double> coords(mDim);
190 for(
int i = mDim - 1; i >= 0; i--)
192 coords[i] = (double) (n / mCumulFactor[i]) * mLength + mOrig[i];
193 n = n % mCumulFactor[i];
198 void sampleGrid(
int tries)
200 int remaining(tries);
201 double completion(0.01);
204 if(p3d_GetStopValue())
210 shared_ptr<Configuration> q = mSampler->sample();
211 if(!mR->isInCollision(*q))
212 this->markCubeFree(*q);
214 if(completion < ((
double) tries - (
double) remaining) / (double) tries)
216 cout <<
"done: " << completion << endl;
224 std::vector<double> coords;
226 for(uint i = 0; i < mDim; i++)
228 coords.push_back(q.mQ[mDegrees[i]]);
230 if(this->withinBounds(coords))
232 int n = this->coords_to_n(q);
234 mFreeSpace[n] =
true;
236 cout <<
"cube " << mFreeSpaceN;
237 for(uint i = 0; i < mDim; i++)
239 cout <<
", " << coords[i];
246 { cout <<
"out of bounds";
247 for(uint i = 0; i < mDim; i++)
248 { cout <<
", " << coords[i]; }
254 void sampleUnderThreshold(std::vector<Node*>& newNodes,
double max_neighbours,
int max_tries, uint max_nodes,
int tries) {
255 for(uint i = 0; i < mCubes.size(); i++)
259 double cube_mean = mCubes[i]->outgoingMean();
260 if(! terminationCube(*mCubes[i], max_neighbours, max_tries, max_nodes))
262 mCubes[i]->sample(newNodes, 1, tries);
264 cout <<
"cube: " << i <<
", mean: " << cube_mean <<
", size: " << mCubes[i]->mNodes.size() << endl;
269 void sampleIso(std::vector<Node*>& newNodes, uint n_per_cube,
int tries)
271 for(uint i(0); i < mCubes.size(); i++)
275 mCubes[i]->sample(newNodes, n_per_cube, tries);
280 bool terminationCube(
Cube& c,
double max_neighbours,
int max_tries, uint max_nodes)
282 return((! (c.outgoingMean() < max_neighbours &&
283 c.mTries < max_tries && c.mNodes.size() < max_nodes)) ||
287 bool terminationCondition(
double max_neighbours,
int max_tries, uint max_nodes)
289 for(uint i(0); i < mCubes.size(); i++)
291 if(mFreeSpace[i] && ! this->terminationCube(*mCubes[i], max_neighbours, max_tries, max_nodes))
299 Node* insertNode(shared_ptr<Configuration> q)
304 if((n = this->coords_to_n(*q)) != -1)
306 int cube_n = this->markCubeFree(*q);
311 mCubes[cube_n]->addNode(node);
Classe représentant un Node d'un Graph.
Definition: node.hpp:39
Definition: GridCube.hpp:55
This class holds a the robot represented by a kinematic chain.
Definition: robot.hpp:42
Node * insertNode(Node *node)
insert un Node dans le Graph
Definition: graph.cpp:700
Definition: GridCube.hpp:4
The sampling API.
Definition: sampling_api.hpp:19
Classe représentant une Configuration d'un Robot.
Definition: configuration.hpp:25
Creares local paths.
Definition: localpath_factory.hpp:16