XMLReader.cpp
Go to the documentation of this file.
1 
7 #include "XMLReader.h"
8 
9 
10 XMLReader::XMLReader(const std::string &fileName)
11 {
12  this->fileName = fileName;
13  this->doc = new XMLDocument();
14  this->allAgents = new std::vector<Agent *>();
15 
16  this->map = nullptr;
17  this->options = nullptr;
18  this->grid = nullptr;
19  this->obstacles = nullptr;
20  this->root = nullptr;
21  this->plannertype = 0;
22 }
23 
24 
26 {
27  this->fileName = "";
28  this->doc = nullptr;
29  this->allAgents = nullptr;
30 
31  this->map = nullptr;
32  this->options = nullptr;
33  this->grid = nullptr;
34  this->obstacles = nullptr;
35  this->root = nullptr;
36  this->plannertype = 0;
37 }
38 
39 
41 {
42 
43  fileName = obj.fileName;
44  if(doc != nullptr)
45  {
46  delete doc;
47  doc = nullptr;
48  }
49 
50 
51  if(obj.doc != nullptr)
52  {
53  doc = new XMLDocument();
54  obj.doc->DeepCopy(doc);
55  root = doc->FirstChildElement(CNS_TAG_ROOT);
56 
57  }
58 
59 
60  plannertype = obj.plannertype;
61  allAgents = (obj.allAgents == nullptr) ? nullptr : new std::vector<Agent *>(*(obj.allAgents));
62  map = (obj.map == nullptr) ? nullptr : new Map(*obj.map);
63  options = (obj.options == nullptr) ? nullptr : new EnvironmentOptions(*obj.options);
64  grid = (obj.grid == nullptr) ? nullptr : new std::vector<std::vector<int>>(*obj.grid);
65  obstacles = (obj.obstacles == nullptr) ? nullptr : new std::vector<std::vector<Point>>(*obj.obstacles);
66 }
67 
68 
70 {
71 
72  if(allAgents != nullptr)
73  {
74  delete allAgents;
75  allAgents = nullptr;
76  }
77 
78  if(options != nullptr)
79  {
80  delete options;
81  options = nullptr;
82  }
83 
84  if(map != nullptr)
85  {
86  delete map;
87  map = nullptr;
88  }
89  if(grid != nullptr)
90  {
91  delete grid;
92  grid = nullptr;
93  }
94  if(obstacles != nullptr)
95  {
96  delete obstacles;
97  obstacles = nullptr;
98  }
99 
100  if(doc != nullptr)
101  {
102  delete doc;
103  doc = nullptr;
104  root = nullptr;
105  }
106 
107 }
108 
109 
111 {
112  if(this->map != nullptr)
113  {
114  *map = this->map;
115  this->map = nullptr;
116  return true;
117  }
118 
119  return false;
120 }
121 
122 
124 {
125  if(this->options != nullptr)
126  {
127  *envOpt = this->options;
128  this->options = nullptr;
129  return true;
130  }
131  return false;
132 }
133 
134 
135 bool XMLReader::GetAgents(std::vector<Agent *> &agents, const int &numThreshold)
136 {
137 
138  if(allAgents == nullptr || allAgents->size() >= numThreshold)
139  {
140  agents.clear();
141  for(int i = 0; i < numThreshold; i++)
142  {
143  agents.push_back((*allAgents)[i]);
144  }
145  for(int i = numThreshold; i < allAgents->size(); i++)
146  {
147  delete (*allAgents)[i];
148  }
149  allAgents->clear();
150  delete allAgents;
151  allAgents = nullptr;
152 
153  return true;
154  }
155  return false;
156 }
157 
158 
160 {
161 
162  if(doc == nullptr || allAgents == nullptr)
163  {
164  return false;
165  }
166 
167  // Чтение XML-файла с заданием / Reading task from XML-file //
168 
169  if(doc->LoadFile(fileName.c_str()) != XMLError::XML_SUCCESS)
170  {
171  std::cout << "Error opening input XML file\n";
172  return false;
173  }
174 
175  root = doc->FirstChildElement(CNS_TAG_ROOT);
176  if (!root)
177  {
178  std::cout <<CNS_TAG_ROOT <<" element not found in XML file\n";
179  return false;
180  }
181 
182  return ReadMap() && ReadAlgorithmOptions() && ReadAgents();
183 }
184 
186 {
187  if (this != &obj)
188  {
189  fileName = obj.fileName;
190  if(doc != nullptr)
191  {
192  delete doc;
193  }
194 
195  if(obj.doc != nullptr)
196  {
197  doc = new XMLDocument();
198  obj.doc->DeepCopy(doc);
199  root = root = doc->FirstChildElement(CNS_TAG_ROOT);
200  }
201  else
202  {
203  doc = nullptr;
204  root = nullptr;
205  }
206 
207  if(allAgents != nullptr)
208  {
209  delete allAgents;
210  }
211  allAgents = (obj.allAgents == nullptr) ? nullptr : new std::vector<Agent *>(*(obj.allAgents));
212 
213  if(map != nullptr)
214  {
215  delete map;
216  }
217  map = (obj.map == nullptr) ? nullptr : new Map(*obj.map);
218 
219  if(options != nullptr)
220  {
221  delete options;
222  }
223  options = (obj.options == nullptr) ? nullptr : new EnvironmentOptions(*obj.options);
224 
225  if(grid != nullptr)
226  {
227  delete grid;
228  }
229  grid = (obj.grid == nullptr) ? nullptr : new std::vector<std::vector<int>>(*obj.grid);
230 
231  if(obstacles != nullptr)
232  {
233  delete obstacles;
234  }
235  obstacles = (obj.obstacles == nullptr) ? nullptr : new std::vector<std::vector<Point>>(*obj.obstacles);
236 
237  plannertype = obj.plannertype;
238  }
239  return *this;
240 }
241 
243 {
244  return new XMLReader(*this);
245 }
246 
247 bool XMLReader::ReadMap()
248 {
249  XMLElement *tmpElement;
250  // Чтение информации о карте и препятствиях / Reading information about map and obstacles //
251 
252  XMLElement *mapTag = 0, *element = 0, *mapnode, *obsts = 0, *obstElem = 0;
253 
254  std::string value;
255  std::stringstream stream;
256 
257  bool hasGridMem = false, hasGrid = false, hasHeight = false, hasWidth = false, hasCellSize = false;
258  int height = -1, width = -1, rowIter = 0, gridI = 0, gridJ = 0, obstNum = -1, count = 0;
259  float cellSize;
260 
261  // Чтение карты / Map reading //
262 
263  mapTag = root->FirstChildElement(CNS_TAG_MAP);
264  if (!mapTag)
265  {
266  std::cout << CNS_TAG_MAP <<" element not found in XML file\n";
267  return false;
268  }
269 
270  for (mapnode = mapTag->FirstChildElement(); mapnode; mapnode = mapnode->NextSiblingElement())
271  {
272  element = mapnode->ToElement();
273  value = mapnode->Value();
274  std::transform(value.begin(), value.end(), value.begin(), ::tolower);
275 
276  stream.str("");
277  stream.clear();
278 
279  if(value != CNS_TAG_GRID)
280  {
281  stream << element->GetText();
282  }
283 
284  if (!hasGridMem && hasHeight && hasWidth)
285  {
286  grid = new std::vector<std::vector<int>>(height);
287  for (int i = 0; i < height; ++i)
288  (*grid)[i] = std::vector<int>(width);
289  hasGridMem = true;
290  }
291 
292  if (value == CNS_TAG_HEIGHT)
293  {
294  if (hasHeight)
295  {
296 #if FULL_OUTPUT
297  std::cout << "Warning! Duplicate '" << CNS_TAG_HEIGHT << "' encountered." << std::endl;
298  std::cout << "Only first value of '" << CNS_TAG_HEIGHT << "' =" << height << "will be used." << std::endl;
299 #endif
300  }
301  else
302  {
303  if (!((stream >> height) && (height > 0)))
304  {
305 #if FULL_OUTPUT
306  std::cout << "Warning! Invalid value of '" << CNS_TAG_HEIGHT
307  << "' tag encountered (or could not convert to integer)." << std::endl;
308  std::cout << "Value of '" << CNS_TAG_HEIGHT << "' tag should be an integer >=0" << std::endl;
309  std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_HEIGHT
310  << "' tag will be encountered later..." << std::endl;
311 #endif
312  }
313  else
314  {
315  hasHeight = true;
316  }
317  }
318  }
319  else if (value == CNS_TAG_WIDTH)
320  {
321  if (hasWidth)
322  {
323 #if FULL_OUTPUT
324  std::cout << "Warning! Duplicate '" << CNS_TAG_WIDTH << "' encountered." << std::endl;
325  std::cout << "Only first value of '" << CNS_TAG_WIDTH << "' =" << width << "will be used." << std::endl;
326 #endif
327  }
328  else
329  {
330  if (!((stream >> width) && (width > 0)))
331  {
332 #if FULL_OUTPUT
333  std::cout << "Warning! Invalid value of '" << CNS_TAG_WIDTH
334  << "' tag encountered (or could not convert to integer)." << std::endl;
335  std::cout << "Value of '" << CNS_TAG_WIDTH << "' tag should be an integer AND >0" << std::endl;
336  std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_WIDTH
337  << "' tag will be encountered later..." << std::endl;
338 #endif
339  }
340  else
341  {
342  hasWidth = true;
343  }
344  }
345  }
346  else if (value == CNS_TAG_CELLSIZE)
347  {
348  if (hasCellSize)
349  {
350 #if FULL_OUTPUT
351  std::cout << "Warning! Duplicate '" << CNS_TAG_CELLSIZE << "' encountered." << std::endl;
352  std::cout << "Only first value of '" << CNS_TAG_CELLSIZE << "' =" << cellSize << "will be used."
353  << std::endl;
354 #endif
355  }
356  else {
357  if (!((stream >> cellSize) && (cellSize > 0))) {
358 #if FULL_OUTPUT
359  std::cout << "Warning! Invalid value of '" << CNS_TAG_CELLSIZE
360  << "' tag encountered (or could not convert to double)." << std::endl;
361  std::cout << "Value of '" << CNS_TAG_CELLSIZE
362  << "' tag should be double AND >0. By default it is defined to '1'" << std::endl;
363  std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_CELLSIZE
364  << "' tag will be encountered later..." << std::endl;
365 #endif
366  }
367  else
368  hasCellSize = true;
369  }
370  }
371  else if (value == CNS_TAG_GRID)
372  {
373  hasGrid = true;
374  if (!(hasHeight && hasWidth))
375  {
376  std::cout << "Error! No '" << CNS_TAG_WIDTH << "' tag or '" << CNS_TAG_HEIGHT << "' tag before '"
377  << CNS_TAG_GRID << "'tag encountered!" << std::endl;
378  return false;
379  }
380  element = mapnode->FirstChildElement();
381  while (gridI < height)
382  {
383  if (!element) {
384  std::cout << "Error! Not enough '" << CNS_TAG_ROW << "' tags inside '" << CNS_TAG_GRID << "' tag."
385  << std::endl;
386  std::cout << "Number of '" << CNS_TAG_ROW
387  << "' tags should be equal (or greater) than the value of '" << CNS_TAG_HEIGHT
388  << "' tag which is " << height << std::endl;
389  return false;
390  }
391  std::string str = element->GetText();
392  std::vector<std::string> elems;
393  std::stringstream ss(str);
394  std::string item;
395  while (std::getline(ss, item, ' '))
396  {
397  elems.push_back(item);
398  }
399  rowIter = gridJ = 0;
400  int val;
401  if (elems.size() > 0)
402  {
403  for(gridJ = 0; gridJ < width; ++gridJ)
404  {
405  if(gridJ == elems.size())
406  {
407  break;
408  }
409  stream.str("");
410  stream.clear();
411  stream << elems[gridJ];
412  stream >> val;
413  (*grid)[gridI][gridJ] = val;
414  }
415  }
416  if (gridJ != width)
417  {
418  std::cout << "Invalid value on " << CNS_TAG_GRID << " in the " << gridI + 1 << " " << CNS_TAG_ROW
419  << std::endl;
420  return false;
421  }
422  ++gridI;
423 
424  element = element->NextSiblingElement();
425  }
426  }
427  }
428 
429  if (!hasGrid)
430  {
431  std::cout << CNS_TAG_GRID <<" element not found in XML file\n";
432  return false;
433  }
434 
435  // Чтение препятствий / Obstacles reading //
436  obsts = root->FirstChildElement(CNS_TAG_OBSTS);
437  if (!obsts)
438  {
439  std::cout << CNS_TAG_OBSTS <<" element not found in XML file\n";
440  return false;
441  }
442 
443  if (obsts->QueryIntAttribute(CNS_TAG_ATTR_NUM, &obstNum) != XMLError::XML_SUCCESS)
444  {
445  std::cout << CNS_TAG_ATTR_NUM <<" element not found at " << CNS_TAG_OBSTS << " tag in XML file\n";
446  return false;
447  }
448 
449  obstacles = new std::vector<std::vector<Point>>(obstNum);
450  float oX,oY;
451  obstElem = obsts->FirstChildElement(CNS_TAG_OBST);
452  for(count = 0; obstElem && count < obstNum; obstElem = obstElem->NextSiblingElement(CNS_TAG_OBST), count++)
453  {
454  for(tmpElement = obstElem->FirstChildElement(CNS_TAG_VERTEX); tmpElement; tmpElement = tmpElement->NextSiblingElement(CNS_TAG_VERTEX))
455  {
456  if (tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_X, &oX) != XMLError::XML_SUCCESS)
457  {
458  std::cout << CNS_TAG_ATTR_X <<" element not found at " << CNS_TAG_OBST <<" #" << count + 1 << " tag in XML file\n";
459  return false;
460  }
461  if (tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_Y, &oY) != XMLError::XML_SUCCESS)
462  {
463  std::cout << CNS_TAG_ATTR_Y <<" element not found at " << CNS_TAG_OBST <<" #" << count + 1 << " tag in XML file\n";
464  return false;
465  }
466  (*obstacles)[count].push_back(Point(oX, oY));
467  }
468 
469  }
470 
471  if(count != obstNum)
472  {
473  std::cout << "The number of obstacles does not match the stated in "<< CNS_TAG_ATTR_NUM << "attribute of " << CNS_TAG_OBSTS << " tag\n";
474  return false;
475  }
476 
477  map = new Map(cellSize, *grid, *obstacles);
478  return true;
479 }
480 
481 bool XMLReader::ReadAlgorithmOptions()
482 {
483  XMLElement *tmpElement;
484  bool tagFlag;
485 
486  // Чтение информации о параметрах окружения и алгоритма// Reading options of the environment and algorithm //
487 
490 
491  XMLElement *alg = root->FirstChildElement(CNS_TAG_ALG);
492  if(!alg)
493  {
494  std::cout <<CNS_TAG_ALG <<" element not found in XML file\n";
495  return false;
496  }
497 
498  // Нет необходимости в считывании типа эвристики / Metric type not necessary //
499 
500 // tagFlag = (tmpElement = alg->FirstChildElement(CNS_TAG_MT)) && (tmpElement->QueryIntText(&(options->metrictype)) == XMLError::XML_SUCCESS);
501 // if(!tagFlag)
502 // {
503 // std::cout <<CNS_TAG_MT <<" element not found in XML file. It was defined to "<< CN_DEFAULT_METRIC_TYPE<<"\n";
504 // }
505 
506  plannertype = CN_DEFAULT_ST;
507  tagFlag = (tmpElement = alg->FirstChildElement(CNS_TAG_ST)) && tmpElement->GetText();
508  if(!tagFlag)
509  {
510 #if FULL_OUTPUT
511  std::cout <<CNS_TAG_ST <<" element not found in XML file. It was defined to "<< CNS_DEFAULT_ST <<"\n";
512 #endif
513  }
514  else
515  {
516  auto tmpst = std::string(tmpElement->GetText());
517  if (tmpst == CNS_SP_ST_THETA)
518  {
519  plannertype = CN_SP_ST_THETA;
520  }
521  else if(tmpst == CNS_SP_ST_DIR)
522  {
523  plannertype = CN_SP_ST_DIR;
524  }
525  else
526  {
527  std::cout <<CNS_TAG_ST <<" element are incorrect. It was defined to "<< CNS_DEFAULT_ST <<"\n";
528  }
529  }
530 
531  tagFlag = (tmpElement = alg->FirstChildElement(CNS_TAG_BT)) && (tmpElement->QueryBoolText(&(options->breakingties)) == XMLError::XML_SUCCESS);
532  if(!tagFlag)
533  {
534 #if FULL_OUTPUT
535  std::cout <<CNS_TAG_BT <<" element not found in XML file. It was defined to "<< CN_DEFAULT_BREAKINGTIES<<"\n";
536 #endif
537  }
538 
539  tagFlag = (tmpElement = alg->FirstChildElement(CNS_TAG_AS)) && (tmpElement->QueryBoolText(&(options->allowsqueeze)) == XMLError::XML_SUCCESS);
540  if(!tagFlag)
541  {
542 #if FULL_OUTPUT
543  std::cout <<CNS_TAG_AS <<" element not found in XML file. It was defined to "<< CN_DEFAULT_ALLOWSQUEEZE<<"\n";
544 #endif
545  }
546 
547  tagFlag = (tmpElement = alg->FirstChildElement(CNS_TAG_CC)) && (tmpElement->QueryBoolText(&(options->cutcorners)) == XMLError::XML_SUCCESS);
548  if(!tagFlag)
549  {
550 #if FULL_OUTPUT
551  std::cout <<CNS_TAG_CC <<" element not found in XML file. It was defined to "<< CN_DEFAULT_CUTCORNERS<<"\n";
552 #endif
553  }
554 
555  tagFlag = (tmpElement = alg->FirstChildElement(CNS_TAG_HW)) && (tmpElement->QueryFloatText(&(options->hweight)) == XMLError::XML_SUCCESS);
556  if(!tagFlag)
557  {
558 #if FULL_OUTPUT
559  std::cout <<CNS_TAG_HW <<" element not found in XML file. It was defined to "<< CN_DEFAULT_HWEIGHT<<"\n";
560 #endif
561  }
562 
563  tagFlag = (tmpElement = alg->FirstChildElement(CNS_TAG_TS)) && (tmpElement->QueryFloatText(&(options->timestep)) == XMLError::XML_SUCCESS);
564  if(!tagFlag)
565  {
566 #if FULL_OUTPUT
567  std::cout <<CNS_TAG_TS <<" element not found in XML file. It was defined to "<< CN_DEFAULT_TIME_STEP<<"\n";
568 #endif
569  }
570 
571  tagFlag = (tmpElement = alg->FirstChildElement(CNS_TAG_DEL)) && (tmpElement->QueryFloatText(&(options->delta)) == XMLError::XML_SUCCESS);
572  if(!tagFlag)
573  {
574 #if FULL_OUTPUT
575  std::cout <<CNS_TAG_DEL <<" element not found in XML file. It was defined to "<< CN_DEFAULT_DELTA<<"\n";
576 #endif
577  }
578  return true;
579 }
580 
581 bool XMLReader::ReadAgents()
582 {
583  XMLElement *tmpElement;
584  int count;
585  // Чтение информации о агентах// Reading agents information //
586 
587  AgentParam defaultParam = AgentParam();
588  int agentsNumber;
589  const char* agType = CNS_DEFAULT_AGENT_TYPE;
590 
591  XMLElement *agents = root->FirstChildElement(CNS_TAG_AGENTS);
592  if(!agents)
593  {
594  std::cout <<CNS_TAG_AGENTS <<" element not found in XML file\n";
595  return false;
596  }
597 
598  if(agents->QueryIntAttribute(CNS_TAG_ATTR_NUM, &agentsNumber) != XMLError::XML_SUCCESS)
599  {
600  std::cout <<CNS_TAG_ATTR_NUM <<" element not found at " << CNS_TAG_AGENTS << " tag in XML file\n";
601  return false;
602  }
603  if(agents->QueryStringAttribute(CNS_TAG_ATTR_TYPE, &agType) != XMLError::XML_SUCCESS)
604  {
605 #if FULL_OUTPUT
606  std::cout <<CNS_TAG_ATTR_TYPE <<" element not found at " << CNS_TAG_AGENTS << " tag in XML file. It was defined to "<< CNS_DEFAULT_AGENT_TYPE<<"\n";
607 #endif
608  }
609 
610 
611  tmpElement = agents->FirstChildElement(CNS_TAG_DEF_PARAMS);
612  if (!tmpElement)
613  {
614 #if FULL_OUTPUT
615  std::cout <<CNS_TAG_DEF_PARAMS <<" element not found in XML file. The following default values was defined\n";
616  std::cout << "Default " <<CNS_TAG_ATTR_SIZE << " = " << CN_DEFAULT_SIZE <<"\n";
617  std::cout << "Default " <<CNS_TAG_ATTR_MAXSPEED << " = " << CN_DEFAULT_MAX_SPEED <<"\n";
618  std::cout << "Default " <<CNS_TAG_ATTR_AGENTSMAXNUM << " = " << CN_DEFAULT_AGENTS_MAX_NUM <<"\n";
619  std::cout << "Default " <<CNS_TAG_ATTR_TIMEBOUNDARY << " = " << CN_DEFAULT_TIME_BOUNDARY <<"\n";
620  std::cout << "Default " <<CNS_TAG_ATTR_SIGHTRADIUS << " = " << CN_DEFAULT_RADIUS_OF_SIGHT <<"\n";
621  std::cout << "Default " <<CNS_TAG_ATTR_TIMEBOUNDARYOBST << " = " << CN_DEFAULT_OBS_TIME_BOUNDARY <<"\n";
622  std::cout << "Default " <<CNS_TAG_ATTR_REPS << " = " << CN_DEFAULT_REPS <<"\n";
623 #endif
624  }
625  else
626  {
627  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_SIZE, &defaultParam.radius) != XMLError::XML_SUCCESS)
628  {
629 #if FULL_OUTPUT
630  std::cout <<CNS_TAG_ATTR_SIZE <<" element not found at "<< CNS_TAG_DEF_PARAMS << " tag. It was defined to "<< CN_DEFAULT_SIZE<<"\n";
631 #endif
632  }
633  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_MAXSPEED, &defaultParam.maxSpeed) != XMLError::XML_SUCCESS)
634  {
635 #if FULL_OUTPUT
636  std::cout <<CNS_TAG_ATTR_MAXSPEED <<" element not found at "<< CNS_TAG_DEF_PARAMS << " tag. It was defined to "<< CN_DEFAULT_MAX_SPEED<<"\n";
637 #endif
638  }
639  if(tmpElement->QueryIntAttribute(CNS_TAG_ATTR_AGENTSMAXNUM, &defaultParam.agentsMaxNum) != XMLError::XML_SUCCESS)
640  {
641 #if FULL_OUTPUT
642  std::cout <<CNS_TAG_ATTR_AGENTSMAXNUM <<" element not found at "<< CNS_TAG_DEF_PARAMS << " tag. It was defined to "<< CN_DEFAULT_AGENTS_MAX_NUM<<"\n";
643 #endif
644  }
645  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_TIMEBOUNDARY, &defaultParam.timeBoundary) != XMLError::XML_SUCCESS)
646  {
647 #if FULL_OUTPUT
648  std::cout <<CNS_TAG_ATTR_TIMEBOUNDARY <<" element not found at "<< CNS_TAG_DEF_PARAMS << " tag. It was defined to "<< CN_DEFAULT_TIME_BOUNDARY<<"\n";
649 #endif
650  }
651  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_SIGHTRADIUS, &defaultParam.sightRadius) != XMLError::XML_SUCCESS)
652  {
653 #if FULL_OUTPUT
654  std::cout <<CNS_TAG_ATTR_SIGHTRADIUS <<" element not found at "<< CNS_TAG_DEF_PARAMS << " tag. It was defined to "<< CN_DEFAULT_RADIUS_OF_SIGHT<<"\n";
655 #endif
656  }
657  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_TIMEBOUNDARYOBST, &defaultParam.timeBoundaryObst) != XMLError::XML_SUCCESS)
658  {
659 #if FULL_OUTPUT
660  std::cout <<CNS_TAG_ATTR_TIMEBOUNDARYOBST <<" element not found at "<< CNS_TAG_DEF_PARAMS << " tag. It was defined to "<< CN_DEFAULT_OBS_TIME_BOUNDARY<<"\n";
661 #endif
662  }
663  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_REPS, &defaultParam.rEps) != XMLError::XML_SUCCESS)
664  {
665 #if FULL_OUTPUT
666  std::cout <<CNS_TAG_ATTR_REPS <<" element not found at "<< CNS_TAG_DEF_PARAMS << " tag. It was defined to "<< CN_DEFAULT_REPS<<"\n";
667 #endif
668  }
669  }
670  tmpElement = agents->FirstChildElement(CNS_TAG_AGENT);
671 
672  int id;
673  float stx = 0, sty = 0, gx = 0, gy = 0;
674 
675  for(count = 0; tmpElement; tmpElement = tmpElement->NextSiblingElement(CNS_TAG_AGENT), count++)
676  {
677  AgentParam param = AgentParam(defaultParam);
678 
679  bool correct = true;
680 
681  if (tmpElement->QueryIntAttribute(CNS_TAG_ATTR_ID, &id) != XMLError::XML_SUCCESS)
682  {
683  std::cout <<CNS_TAG_ATTR_ID <<" element not found in XML file at agent №"<<count<<"\n";
684  return false;
685  }
686 
687  for(auto agent : *allAgents)
688  {
689  if(id == agent->GetID())
690  {
691  std::cout <<"There is an agent with same ID ("<<id<<")\n";
692  correct = false;
693  }
694  }
695 
696  if (tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_STX, &stx) != XMLError::XML_SUCCESS)
697  {
698  std::cout <<CNS_TAG_ATTR_STX <<" element not found in XML file at agent №"<<count<<"\n";
699  return false;
700  }
701  if (tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_STY, &sty) != XMLError::XML_SUCCESS)
702  {
703  std::cout <<CNS_TAG_ATTR_STY <<" element not found in XML file at agent "<<id<<"\n";
704  return false;
705  }
706  if (tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_GX, &gx) != XMLError::XML_SUCCESS)
707  {
708  std::cout <<CNS_TAG_ATTR_GX <<" element not found in XML file at agent "<<id<<"\n";
709  return false;
710  }
711  if (tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_GY, &gy) != XMLError::XML_SUCCESS)
712  {
713  std::cout <<CNS_TAG_ATTR_GY <<" element not found in XML file at agent "<<id<<"\n";
714  return false;
715  }
716  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_SIZE, &param.radius) != XMLError::XML_SUCCESS || param.radius <= 0)
717  {
718 #if FULL_OUTPUT
719  std::cout <<CNS_TAG_ATTR_SIZE <<" element not found in XML file (or it is incorrect) at agent "<<id<<"\n";
720 #endif
721  param.radius = defaultParam.radius;
722  }
723  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_MAXSPEED, &param.maxSpeed) != XMLError::XML_SUCCESS || param.maxSpeed <= 0)
724  {
725 #if FULL_OUTPUT
726  std::cout <<CNS_TAG_ATTR_MAXSPEED <<" element not found in XML file (or it is incorrect) at agent "<<id<<"\n";
727 #endif
728  param.maxSpeed = defaultParam.maxSpeed;
729  }
730  if(tmpElement->QueryIntAttribute(CNS_TAG_ATTR_AGENTSMAXNUM, &param.agentsMaxNum) != XMLError::XML_SUCCESS || param.agentsMaxNum <= 0)
731  {
732 #if FULL_OUTPUT
733  std::cout <<CNS_TAG_ATTR_AGENTSMAXNUM <<" element not found in XML file (or it is incorrect) at agent "<<id<<"\n";
734 #endif
735  param.agentsMaxNum = defaultParam.agentsMaxNum;
736  }
737  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_TIMEBOUNDARY, &param.timeBoundary) != XMLError::XML_SUCCESS || param.timeBoundary <= 0)
738  {
739 #if FULL_OUTPUT
740  std::cout <<CNS_TAG_ATTR_TIMEBOUNDARY <<" element not found in XML file (or it is incorrect) at agent "<<id<<"\n";
741 #endif
742  param.timeBoundary = defaultParam.timeBoundary;
743  }
744  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_SIGHTRADIUS, &param.sightRadius) != XMLError::XML_SUCCESS || param.sightRadius <= 0)
745  {
746 #if FULL_OUTPUT
747  std::cout <<CNS_TAG_ATTR_SIGHTRADIUS <<" element not found in XML file (or it is incorrect) at agent "<<id<<"\n";
748 #endif
749  param.sightRadius = defaultParam.sightRadius;
750  }
751  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_TIMEBOUNDARYOBST, &param.timeBoundaryObst) != XMLError::XML_SUCCESS || param.timeBoundaryObst <= 0)
752  {
753 #if FULL_OUTPUT
754  std::cout <<CNS_TAG_ATTR_TIMEBOUNDARYOBST <<" element not found in XML file (or it is incorrect) at agent "<<id<<"\n";
755 #endif
756  param.timeBoundaryObst = defaultParam.timeBoundaryObst;
757  }
758  if(tmpElement->QueryFloatAttribute(CNS_TAG_ATTR_REPS, &param.rEps) != XMLError::XML_SUCCESS || param.rEps < 0)
759  {
760 #if FULL_OUTPUT
761  std::cout <<CNS_TAG_ATTR_REPS <<" element not found in XML file (or it is incorrect) at agent "<<id<<"\n";
762 #endif
763  param.rEps = defaultParam.rEps;
764  }
765 
766 
767 
768 
769  if(stx <= param.radius || sty <= param.radius ||
770  gx <= param.radius || gy <= param.radius ||
771  stx >= (map->GetWidth() * map->GetCellSize()) - param.radius ||
772  gx >= (map->GetWidth() * map->GetCellSize()) - param.radius ||
773  sty >= (map->GetHeight() * map->GetCellSize()) - param.radius ||
774  gy >= (map->GetHeight() * map->GetCellSize()) - param.radius)
775  {
776  std::cout <<"Start or goal position of agent "<<id<<" is out of map or too close to boundaries\n";
777  correct = false;
778  }
779 
780 
781  for(auto agent : *allAgents)
782  {
783  float sqRadiusSum = static_cast<float>(std::pow((agent->GetRadius() + param.radius), 2.0));
784  float sqDist = (Point(stx, sty) - agent->GetPosition()).SquaredEuclideanNorm();
785  if(sqDist <= sqRadiusSum)
786  {
787  std::cout <<"Position of agent "<<id<<" is too close to another agent\n";
788  correct = false;
789  break;
790  }
791  }
792 
793  Node tmpStNode = map->GetClosestNode(Point(stx, sty));
794  Node tmpGlNode = map->GetClosestNode(Point(gx, gy));
795  LineOfSight positionChecker(param.radius/ map->GetCellSize());
796 
797  if(!positionChecker.checkTraversability(tmpStNode.i, tmpStNode.j, *map) || !positionChecker.checkTraversability(tmpGlNode.i, tmpGlNode.j, *map))
798  {
799  std::cout <<"Position of agent "<<id<<" is too close to some obstacle\n";
800  correct = false;
801  }
802 
803  if(!correct)
804  {
805 #if FULL_OUTPUT
806  std::cout<<"Agent "<<id<< " was skipped\n\n";
807 #endif
808  continue;
809  }
810 #if FULL_OUTPUT
811  std::cout<<"Agent "<<id<< " was added at position "<< Point(stx, sty).ToString()<<"\n";
812 #endif
813  Agent *a;
814  std::string agTypeStr = std::string(agType);
815  if(agTypeStr == CNS_AT_ST_ORCA)
816  {
817  a = new ORCAAgent(id, Point(stx, sty), Point(gx, gy), *map, *options, param);
818  }
819  else
820  {
821  a = new ORCAAgent(id, Point(stx, sty), Point(gx, gy), *map, *options, param);
822  }
823 
824 
825  switch(plannertype)
826  {
827  case CN_SP_ST_THETA:
828  {
829  a->SetPlanner(ThetaStar(*map, *options, Point(stx, sty), Point(gx, gy), param.radius + param.rEps));
830  break;
831  }
832  case CN_SP_ST_DIR:
833  {
834  a->SetPlanner(DirectPlanner(*map, *options, Point(stx, sty), Point(gx, gy), param.radius + param.rEps));
835  break;
836  }
837  default:
838  {
839  a->SetPlanner(ThetaStar(*map, *options, Point(stx, sty), Point(gx, gy), param.radius + param.rEps));
840  break;
841  }
842  }
843  allAgents->push_back(a);
844  }
845  return true;
846 }
#define CN_DEFAULT_HWEIGHT
Default H-weight for Theta* value.
Definition: Const.h:124
#define CNS_TAG_ROW
XML tag or attribute.
Definition: Const.h:58
#define CNS_TAG_ATTR_SIGHTRADIUS
XML tag or attribute.
Definition: Const.h:31
#define CN_DEFAULT_AGENTS_MAX_NUM
Default agent max neighbours number value.
Definition: Const.h:116
#define CNS_TAG_DEF_PARAMS
XML tag or attribute.
Definition: Const.h:26
XMLReader & operator=(const XMLReader &obj)
Assignment operator.
Definition: XMLReader.cpp:185
float sightRadius
Radius in which the agent takes neighbors into account.
Definition: Agent.h:72
#define CNS_TAG_HW
XML tag or attribute.
Definition: Const.h:49
#define CNS_TAG_OBSTS
XML tag or attribute.
Definition: Const.h:60
#define CNS_TAG_ATTR_REPS
XML tag or attribute.
Definition: Const.h:33
The Node class defines a cell of grid (see Map class)
Definition: Geom.h:21
#define CNS_TAG_ATTR_STX
XML tag or attribute.
Definition: Const.h:38
#define CNS_TAG_VERTEX
XML tag or attribute.
Definition: Const.h:62
float radius
Size of the agent (radius of the agent).
Definition: Agent.h:75
#define CNS_TAG_ATTR_MAXSPEED
XML tag or attribute.
Definition: Const.h:28
bool GetMap(Map **map) override
Creates object with static environment data.
Definition: XMLReader.cpp:110
DirectPlanner class implements simple planner, which creates direct path from start to goal position...
Definition: DirectPlanner.h:17
File contains XMLReader class.
#define CNS_SP_ST_DIR
XML text constant.
Definition: Const.h:93
bool ReadData() override
Starts input data reading from XML-file.
Definition: XMLReader.cpp:159
float rEps
Buffer size (more about buffer see Main page).
Definition: Agent.h:76
XMLReader()
XMLReader default constructor.
Definition: XMLReader.cpp:25
bool GetAgents(std::vector< Agent * > &agents, const int &numThreshold) override
Creates object for all agents.
Definition: XMLReader.cpp:135
#define CNS_TAG_ATTR_GX
XML tag or attribute.
Definition: Const.h:40
The Point class defines a position (or euclidean vector from (0,0)) in 2D space.
Definition: Geom.h:61
float timeBoundaryObst
Time within which ORCA algorithm ensures collision avoidance with neighbor obstacles.
Definition: Agent.h:74
#define CN_DEFAULT_CUTCORNERS
Default Theta* CUTCORNERS value.
Definition: Const.h:123
int agentsMaxNum
Number of neighbors, that the agent takes into account.
Definition: Agent.h:78
#define CNS_TAG_ST
XML tag or attribute.
Definition: Const.h:44
void SetPlanner(const Planner &pl)
Set global planner object.
Definition: Agent.h:267
#define CN_DEFAULT_REPS
Default agent buffer size value.
Definition: Const.h:127
Class AgentParam contains agent and algoritms parameters.
Definition: Agent.h:38
#define CN_DEFAULT_RADIUS_OF_SIGHT
Default agent radius of sight value.
Definition: Const.h:119
#define CNS_TAG_MAP
XML tag or attribute.
Definition: Const.h:53
#define CNS_DEFAULT_AGENT_TYPE
Default collision avoidance algorithm.
Definition: Const.h:132
#define CNS_TAG_CELLSIZE
XML tag or attribute.
Definition: Const.h:54
#define CNS_TAG_OBST
XML tag or attribute.
Definition: Const.h:61
#define CN_DEFAULT_TIME_BOUNDARY
Default ORCA time boundary value.
Definition: Const.h:117
#define CN_DEFAULT_METRIC_TYPE
Default Theta* heuristic-function value.
Definition: Const.h:120
#define CNS_TAG_CC
XML tag or attribute.
Definition: Const.h:48
Agent class implements base agent interface and behavior.
Definition: Agent.h:95
XMLReader * Clone() const override
Method for cloning objects. Create copy of object in memmory and return pointer to copy...
Definition: XMLReader.cpp:242
#define CNS_TAG_ATTR_TIMEBOUNDARY
XML tag or attribute.
Definition: Const.h:30
std::string ToString() const
Creates STL string, which contains x,y values.
Definition: Geom.cpp:48
#define CNS_TAG_HEIGHT
XML tag or attribute.
Definition: Const.h:56
~XMLReader() override
XMLReader destructor.
Definition: XMLReader.cpp:69
#define CNS_AT_ST_ORCA
XML text constant.
Definition: Const.h:95
float timeBoundary
Time within which ORCA algorithm ensures collision avoidance with neighbor agents.
Definition: Agent.h:73
#define CN_DEFAULT_MAX_SPEED
Default agent max speed value.
Definition: Const.h:115
The XMLReader class implements interface for input data reading from XML-file with a specific structu...
Definition: XMLReader.h:73
#define CN_SP_ST_DIR
Direct planning algorithm constant.
Definition: Const.h:108
#define CN_DEFAULT_OBS_TIME_BOUNDARY
Default ORCA time boundary for obstacles value.
Definition: Const.h:118
#define CNS_TAG_ATTR_GY
XML tag or attribute.
Definition: Const.h:41
#define CNS_TAG_ATTR_SIZE
XML tag or attribute.
Definition: Const.h:27
#define CNS_TAG_ATTR_X
XML tag or attribute.
Definition: Const.h:63
#define CNS_TAG_ALG
XML tag or attribute.
Definition: Const.h:43
Class EnvironmentOptions contains environment and algoritms parameters.
#define CNS_DEFAULT_ST
Default path planning algorithm.
Definition: Const.h:131
Map class describes static environment.
Definition: Map.h:36
#define CNS_TAG_TS
XML tag or attribute.
Definition: Const.h:50
#define CNS_TAG_BT
XML tag or attribute.
Definition: Const.h:46
float maxSpeed
Maximum speed of agent.
Definition: Agent.h:77
int i
The number of row. Part of (i,j) address of cell on grid.
Definition: Geom.h:24
#define CN_DEFAULT_SIZE
Default agent size value.
Definition: Const.h:114
#define CNS_TAG_ROOT
Enable full output to std stream.
Definition: Const.h:20
#define CNS_TAG_ATTR_ID
XML tag or attribute.
Definition: Const.h:37
ThetaStar class implements Theta* algorithm, which creates path from start to goal position...
Definition: ThetaStar.h:22
#define CN_DEFAULT_ST
Default path planning algorithm.
Definition: Const.h:130
#define CNS_TAG_AGENT
XML tag or attribute.
Definition: Const.h:36
ORCAAgent class implements agent with ORCA algoritm behavior.
Definition: ORCAAgent.h:26
#define CNS_TAG_DEL
XML tag or attribute.
Definition: Const.h:51
#define CNS_TAG_ATTR_TYPE
XML tag or attribute.
Definition: Const.h:24
#define CNS_TAG_ATTR_AGENTSMAXNUM
XML tag or attribute.
Definition: Const.h:29
int j
The number of column. Part of (i,j) address of cell on grid.
Definition: Geom.h:25
#define CNS_TAG_WIDTH
XML tag or attribute.
Definition: Const.h:55
This class implements line-of-sight function between two cells on the grid for a variable size of age...
Definition: LineOfSight.h:30
#define CNS_SP_ST_THETA
XML text constant.
Definition: Const.h:92
#define CNS_TAG_ATTR_TIMEBOUNDARYOBST
XML tag or attribute.
Definition: Const.h:32
#define CN_SP_ST_THETA
Theta* path planning algorithm constant.
Definition: Const.h:107
#define CNS_TAG_AS
XML tag or attribute.
Definition: Const.h:47
#define CNS_TAG_GRID
XML tag or attribute.
Definition: Const.h:57
#define CNS_TAG_AGENTS
XML tag or attribute.
Definition: Const.h:22
#define CNS_TAG_ATTR_Y
XML tag or attribute.
Definition: Const.h:64
#define CNS_TAG_ATTR_STY
XML tag or attribute.
Definition: Const.h:39
#define CNS_TAG_ATTR_NUM
XML tag or attribute.
Definition: Const.h:23
bool GetEnvironmentOptions(EnvironmentOptions **envOpt) override
Creates object with algorithms and environment parameters.
Definition: XMLReader.cpp:123
#define CN_DEFAULT_BREAKINGTIES
Default Theta* BREAKINGTIES value.
Definition: Const.h:121
#define CN_DEFAULT_TIME_STEP
Default simulation timestep value.
Definition: Const.h:125
#define CN_DEFAULT_ALLOWSQUEEZE
Default Theta* ALLOWSQUEEZE value.
Definition: Const.h:122
#define CN_DEFAULT_DELTA
Default delta value.
Definition: Const.h:126


ORCAStar
Author(s): Stepan Drgachev
autogenerated on Wed Jul 15 2020 16:13:14