UnivFFviaFM.cpp 28.1 KB
Newer Older
Arne Graf's avatar
Arne Graf committed
1 2 3 4 5 6 7 8 9 10 11 12
//
// Created by arne on 5/9/17.
//

#include "UnivFFviaFM.h"
#include "../../geometry/Line.h"
#include "../../geometry/Building.h"
#include "../../geometry/Room.h"
#include "../../geometry/SubRoom.h"
#include "mesh/RectGrid.h"


Arne Graf's avatar
Arne Graf committed
13 14
UnivFFviaFM::UnivFFviaFM(Room* r, Building* b, double hx, double wallAvoid, bool useWallAvoid)
          : UnivFFviaFM(r, b->GetConfig(), hx, wallAvoid, useWallAvoid) {
Arne Graf's avatar
Arne Graf committed
15 16 17
     _building = b;
}

Arne Graf's avatar
Arne Graf committed
18 19
UnivFFviaFM::UnivFFviaFM(SubRoom* sr, Building* b, double hx, double wallAvoid, bool useWallAvoid)
          : UnivFFviaFM(sr, b->GetConfig(), hx, wallAvoid, useWallAvoid) {
Arne Graf's avatar
Arne Graf committed
20 21 22
     _building = b;
}

Arne Graf's avatar
Arne Graf committed
23 24 25 26 27
UnivFFviaFM::UnivFFviaFM(Room *r, const Configuration *conf, double hx, double wallAvoid, bool useWallAvoid)
          : UnivFFviaFM(r, conf, hx, wallAvoid, useWallAvoid, std::vector<int>()){
}

UnivFFviaFM::UnivFFviaFM(Room *roomArg, const Configuration *confArg, double hx, double wallAvoid, bool useWallAvoid, std::vector<int> wantedDoors) {
Arne Graf's avatar
Arne Graf committed
28 29 30 31 32 33
     //build the vector with walls(wall or obstacle), the map with <UID, Door(Cross or Trans)>, the vector with targets(UIDs)
     //then call other constructor including the mode

     std::vector<Line> lines;
     std::map<int, Line> tmpDoors;

Arne Graf's avatar
Arne Graf committed
34
     for (auto& subroomMap : roomArg->GetAllSubRooms()) {
Arne Graf's avatar
Arne Graf committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
          SubRoom* subRoomPtr = subroomMap.second.get();
          std::vector<Wall> walls = std::vector<Wall>(subRoomPtr->GetAllWalls());
          for (auto& wall : walls) {
               lines.emplace_back((Line)wall);
          }

          std::vector<Obstacle*> tmpObsPtrVec = subRoomPtr->GetAllObstacles();
          for (Obstacle* ptrObs : tmpObsPtrVec) {
               const std::vector<Wall> obsWalls = ptrObs->GetAllWalls();
               for (auto& owall : obsWalls) {
                    lines.emplace_back((Line)owall);
               }
          }

          const std::vector<Crossing*> tmpCross = subRoomPtr->GetAllCrossings();
          const std::vector<Transition*> tmpTrans = subRoomPtr->GetAllTransitions();

Arne Graf's avatar
Arne Graf committed
52
          int uidNotConst = 0;
Arne Graf's avatar
Arne Graf committed
53
          for (auto& cross : tmpCross) {
Arne Graf's avatar
Arne Graf committed
54 55 56 57
               uidNotConst = cross->GetUniqueID();
               if (tmpDoors.count(uidNotConst) == 0) {
                    tmpDoors.emplace(std::make_pair(uidNotConst, (Line) *cross));
               }
Arne Graf's avatar
Arne Graf committed
58 59
          }
          for (auto& trans : tmpTrans) {
Arne Graf's avatar
Arne Graf committed
60 61 62 63
               uidNotConst = trans->GetUniqueID();
               if (tmpDoors.count(uidNotConst) == 0) {
                    tmpDoors.emplace(std::make_pair(uidNotConst, (Line) *trans));
               }
Arne Graf's avatar
Arne Graf committed
64 65 66
          }
     }

Arne Graf's avatar
Arne Graf committed
67 68 69 70 71 72
     create(lines, tmpDoors, wantedDoors, FF_HOMO_SPEED, hx, wallAvoid, useWallAvoid);
     writeFF("UnivFFRoom.vtk", this->getKnownDoorUIDs());
}

UnivFFviaFM::UnivFFviaFM(SubRoom* sr, const Configuration* conf, double hx, double wallAvoid, bool useWallAvoid)
          : UnivFFviaFM(sr, conf, hx, wallAvoid, useWallAvoid, std::vector<int>()){
Arne Graf's avatar
Arne Graf committed
73 74
}

Arne Graf's avatar
Arne Graf committed
75
UnivFFviaFM::UnivFFviaFM(SubRoom* subRoomArg, const Configuration* confArg, double hx, double wallAvoid, bool useWallAvoid, std::vector<int> wantedDoors) {
Arne Graf's avatar
Arne Graf committed
76 77 78 79
     //build the vector with walls(wall or obstacle), the map with <UID, Door(Cross or Trans)>, the vector with targets(UIDs)
     //then call other constructor including the mode

     std::vector<Line> lines;
Arne Graf's avatar
Arne Graf committed
80 81 82
     std::map<int, Line> tmpDoors;

     std::vector<Wall> walls = std::vector<Wall> (subRoomArg->GetAllWalls());
Arne Graf's avatar
Arne Graf committed
83 84 85 86
     for (auto& wall : walls) {
          lines.emplace_back((Line)wall);
     }

Arne Graf's avatar
Arne Graf committed
87
     std::vector<Obstacle*> tmpObsPtrVec = subRoomArg->GetAllObstacles();
Arne Graf's avatar
Arne Graf committed
88 89 90 91 92 93 94
     for (Obstacle* ptrObs : tmpObsPtrVec) {
          const std::vector<Wall> obsWalls = ptrObs->GetAllWalls();
          for (auto& owall : obsWalls) {
               lines.emplace_back((Line)owall);
          }
     }

Arne Graf's avatar
Arne Graf committed
95 96
     const std::vector<Crossing*> tmpCross = subRoomArg->GetAllCrossings();
     const std::vector<Transition*> tmpTrans = subRoomArg->GetAllTransitions();
Arne Graf's avatar
Arne Graf committed
97

Arne Graf's avatar
Arne Graf committed
98
     int uidNotConst = 0;
Arne Graf's avatar
Arne Graf committed
99
     for (auto& cross : tmpCross) {
Arne Graf's avatar
Arne Graf committed
100 101
          uidNotConst = cross->GetUniqueID();
          tmpDoors.emplace(std::make_pair(uidNotConst, (Line) *cross));
Arne Graf's avatar
Arne Graf committed
102 103
     }
     for (auto& trans : tmpTrans) {
Arne Graf's avatar
Arne Graf committed
104 105
          uidNotConst = trans->GetUniqueID();
          tmpDoors.emplace(std::make_pair(uidNotConst, (Line) *trans));
Arne Graf's avatar
Arne Graf committed
106 107
     }

Arne Graf's avatar
Arne Graf committed
108 109
     create(lines, tmpDoors, wantedDoors, FF_HOMO_SPEED, hx, wallAvoid, useWallAvoid);
     writeFF("UnivFFSubroom.vtk", this->getKnownDoorUIDs());
Arne Graf's avatar
Arne Graf committed
110 111
}

Arne Graf's avatar
Arne Graf committed
112 113 114 115 116 117 118
void UnivFFviaFM::create(std::vector<Line>& walls, std::map<int, Line>& doors, std::vector<int> targetUIDs, int mode,
                         double spacing, double wallAvoid, bool useWallAvoid) {

     _wallAvoidDistance = wallAvoid;
     _useWallAvoidance = useWallAvoid;
     _speedmode = mode;

Arne Graf's avatar
Arne Graf committed
119 120 121 122 123 124 125
     //find circumscribing rectangle (x_min/max, y_min/max) //create RectGrid
     createRectGrid(walls, doors, spacing);
     _nPoints = _grid->GetnPoints();

     //allocate _gridCode and  _speedFieldSelector and initialize them ("draw" walls and doors)
     _gridCode = new int[_nPoints];
     processGeometry(walls, doors);
Arne Graf's avatar
Arne Graf committed
126
     _speedFieldSelector.emplace(_speedFieldSelector.begin()+INITIAL_SPEED, new double[_nPoints]);
Arne Graf's avatar
Arne Graf committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
     std::fill(_speedFieldSelector[INITIAL_SPEED], _speedFieldSelector[INITIAL_SPEED]+_nPoints, 1.0);

     //allocate _initalSpeed and maybe _modifiedSpeed
     if (mode == FF_WALL_AVOID) {
          double* cost_alias_walldistance = new double[_nPoints];
          _costFieldWithKey[0] = cost_alias_walldistance;
          Point* gradient_alias_walldirection = new Point[_nPoints];
          _directionFieldWithKey[0] = gradient_alias_walldirection;

          //create wall distance field
          drawLinesOnGrid(walls, cost_alias_walldistance, magicnum(TARGET_REGION));
          calcFF(cost_alias_walldistance, gradient_alias_walldirection, _speedFieldSelector[INITIAL_SPEED]);

          double* temp_reduWallSpeed = new double[_nPoints];
          if (_speedFieldSelector[REDU_WALL_SPEED]) { //free memory before overwriting
               delete[] _speedFieldSelector[REDU_WALL_SPEED];
          }
          _speedFieldSelector[REDU_WALL_SPEED] = temp_reduWallSpeed;
          //init _reducedWallSpeed by using distance field
          //@todo: @ar.graf @newFF
     }

     for (auto targetUID : targetUIDs ) {
          Line tempTargetLine = Line(doors[targetUID]);
          Point tempCenterPoint = Point(tempTargetLine.GetCentre());

          //this allocation must be on shared heap! to be accessable by any thread later (head should be shared in openmp)
          double* newArrayDBL = new double[_nPoints];
          Point* newArrayPt = nullptr;
          if (_user == DISTANCE_AND_DIRECTIONS_USED) {
               newArrayPt = new Point[_nPoints];
          }

          if (_costFieldWithKey[targetUID])
               delete[] _costFieldWithKey[targetUID];
          _costFieldWithKey[targetUID] = newArrayDBL;

          //init costarray
          for (int i = 0; i < _nPoints; ++i) {
               if (_gridCode[i] == WALL) {
                    newArrayDBL[i] = magicnum(WALL_ON_COSTARRAY);
               } else {
                    newArrayDBL[i] = magicnum(UNKNOWN_COST);
               }
          }

          if (_directionFieldWithKey[targetUID])
               delete[] _directionFieldWithKey[targetUID];
Arne Graf's avatar
Arne Graf committed
175 176
          if (newArrayPt)
               _directionFieldWithKey[targetUID] = newArrayPt;
Arne Graf's avatar
Arne Graf committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

          //initialize start area
          if (_mode == LINESEGMENT) {
               drawLinesOnGrid(tempTargetLine, newArrayDBL, magicnum(TARGET_REGION));
          }
          if (_mode == CENTERPOINT) {
               newArrayDBL[_grid->getKeyAtPoint(tempCenterPoint)] = magicnum(TARGET_REGION);
          }

          if (mode == FF_WALL_AVOID) {
               calcFF(newArrayDBL, newArrayPt, _speedFieldSelector[REDU_WALL_SPEED]);
          } else if (mode == FF_HOMO_SPEED) {
               calcFF(newArrayDBL, newArrayPt, _speedFieldSelector[INITIAL_SPEED]);
          }
          _uids.emplace_back(targetUID);
     }    //loop over targets



}

void UnivFFviaFM::createRectGrid(std::vector<Line>& walls, std::map<int, Line>& doors, double spacing) {
     double x_min = DBL_MAX;  double x_max = DBL_MIN;
     double y_min = DBL_MAX;  double y_max = DBL_MIN;

     for(auto& wall : walls) {
          if (wall.GetPoint1()._x < x_min) x_min = wall.GetPoint1()._x;
          if (wall.GetPoint1()._y < y_min) y_min = wall.GetPoint1()._y;
          if (wall.GetPoint2()._x < x_min) x_min = wall.GetPoint2()._x;
          if (wall.GetPoint2()._y < y_min) y_min = wall.GetPoint2()._y;

          if (wall.GetPoint1()._x > x_max) x_max = wall.GetPoint1()._x;
          if (wall.GetPoint1()._y > y_max) y_max = wall.GetPoint1()._y;
          if (wall.GetPoint2()._x > x_max) x_max = wall.GetPoint2()._x;
          if (wall.GetPoint2()._y > y_max) y_max = wall.GetPoint2()._y;
     }

     for(auto& doorPair:doors) {
          Line& door = doorPair.second;
          if (door.GetPoint1()._x < x_min) x_min = door.GetPoint1()._x;
          if (door.GetPoint1()._y < y_min) y_min = door.GetPoint1()._y;
          if (door.GetPoint2()._x < x_min) x_min = door.GetPoint2()._x;
          if (door.GetPoint2()._y < y_min) y_min = door.GetPoint2()._y;

          if (door.GetPoint1()._x > x_max) x_max = door.GetPoint1()._x;
          if (door.GetPoint1()._y > y_max) y_max = door.GetPoint1()._y;
          if (door.GetPoint2()._x > x_max) x_max = door.GetPoint2()._x;
          if (door.GetPoint2()._y > y_max) y_max = door.GetPoint2()._y;
     }

     //create Rect Grid
     _grid = new RectGrid();
     _grid->setBoundaries(x_min, y_min, x_max, y_max);
     _grid->setSpacing(spacing, spacing);
     _grid->createGrid();
}

void UnivFFviaFM::processGeometry(std::vector<Line>&walls, std::map<int, Line>& doors) {
     for (int i = 0; i < _nPoints; ++i) {
          _gridCode[i] = OUTSIDE;
     }

Arne Graf's avatar
Arne Graf committed
239 240 241 242
     for (auto mapentry : doors) {
          _doors.insert(mapentry);
     }
     //_doors = doors;
Arne Graf's avatar
Arne Graf committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

     drawLinesOnGrid<int>(walls, _gridCode, WALL);
     drawLinesOnGrid(doors, _gridCode); //UIDs of doors will be drawn on _gridCode
}

void UnivFFviaFM::drawLinesOnGrid(std::map<int, Line>& doors, int *const grid) {
     for (auto&& doorPair : doors) {
          int tempUID = doorPair.first;
          Line tempDoorLine = Line(doorPair.second);
          drawLinesOnGrid(tempDoorLine, grid, tempUID);
     }
}

template <typename T>
void UnivFFviaFM::drawLinesOnGrid(std::vector<Line>& wallArg, T* const target, const T value) { //no init, plz init elsewhere

     for (auto& line : wallArg) {
          drawLinesOnGrid(line, target, value);
     } //loop over all walls

} //drawLinesOnGrid

template <typename T>
void UnivFFviaFM::drawLinesOnGrid(Line& line, T* const target, const T value) { //no init, plz init elsewhere
// i~x; j~y;
//http://stackoverflow.com/questions/10060046/drawing-lines-with-bresenhams-line-algorithm
//src in answer of "Avi"; adapted to fit this application

     //grid handeling local vars:
     long int iMax  = _grid->GetiMax();

     long int iStart, iEnd;
     long int jStart, jEnd;
     long int iDot, jDot;
     long int key;
     long int deltaX, deltaY, deltaX1, deltaY1, px, py, xe, ye, i; //Bresenham Algorithm


     key = _grid->getKeyAtPoint(line.GetPoint1());
     iStart = (long) _grid->get_i_fromKey(key);
     jStart = (long) _grid->get_j_fromKey(key);

     key = _grid->getKeyAtPoint(line.GetPoint2());
     iEnd = (long) _grid->get_i_fromKey(key);
     jEnd = (long) _grid->get_j_fromKey(key);

     deltaX = (int) (iEnd - iStart);
     deltaY = (int) (jEnd - jStart);
     deltaX1 = abs( (int) (iEnd - iStart));
     deltaY1 = abs( (int) (jEnd - jStart));

     px = 2*deltaY1 - deltaX1;
     py = 2*deltaX1 - deltaY1;

     if(deltaY1<=deltaX1) {
          if(deltaX>=0) {
               iDot = iStart;
               jDot = jStart;
               xe = iEnd;
          } else {
               iDot = iEnd;
               jDot = jEnd;
               xe = iStart;
          }
          if ((_gridCode[jDot*iMax + iDot] != WALL) && (_gridCode[jDot*iMax + iDot] != CLOSED_CROSSING) && (_gridCode[jDot*iMax + iDot] != CLOSED_TRANSITION)) {
               target[jDot * iMax + iDot] = value;
          }
          for (i=0; iDot < xe; ++i) {
               ++iDot;
               if(px<0) {
                    px+=2*deltaY1;
               } else {
                    if((deltaX<0 && deltaY<0) || (deltaX>0 && deltaY>0)) {
                         ++jDot;
                    } else {
                         --jDot;
                    }
                    px+=2*(deltaY1-deltaX1);
               }
               if ((_gridCode[jDot*iMax + iDot] != WALL) && (_gridCode[jDot*iMax + iDot] != CLOSED_CROSSING) && (_gridCode[jDot*iMax + iDot] != CLOSED_TRANSITION)) {
                    target[jDot * iMax + iDot] = value;
               }
          }
     } else {
          if(deltaY>=0) {
               iDot = iStart;
               jDot = jStart;
               ye = jEnd;
          } else {
               iDot = iEnd;
               jDot = jEnd;
               ye = jStart;
          }
          if ((_gridCode[jDot*iMax + iDot] != WALL) && (_gridCode[jDot*iMax + iDot] != CLOSED_CROSSING) && (_gridCode[jDot*iMax + iDot] != CLOSED_TRANSITION)) {
               target[jDot * iMax + iDot] = value;
          }
          for(i=0; jDot<ye; ++i) {
               ++jDot;
               if (py<=0) {
                    py+=2*deltaX1;
               } else {
                    if((deltaX<0 && deltaY<0) || (deltaX>0 && deltaY>0)) {
                         ++iDot;
                    } else {
                         --iDot;
                    }
                    py+=2*(deltaX1-deltaY1);
               }
               if ((_gridCode[jDot*iMax + iDot] != WALL) && (_gridCode[jDot*iMax + iDot] != CLOSED_CROSSING) && (_gridCode[jDot*iMax + iDot] != CLOSED_TRANSITION)) {
                    target[jDot * iMax + iDot] = value;
               }
          }
     }
} //drawLinesOnGrid

void UnivFFviaFM::calcFF(double* costOutput, Point* directionOutput, const double *const speed) {
     CompareCost comp = CompareCost(costOutput);
     std::priority_queue<long int, std::vector<long int>, CompareCost> trialfield(costOutput); //pass the argument for the constr of CompareCost
     std::priority_queue<long int, std::vector<long int>, CompareCost> trialfield2(comp);      //pass the CompareCost object directly

     directNeighbor local_neighbor = _grid->getNeighbors(0);
     long int aux = 0;
     //init trial field
     for (long int i = 0; i < _nPoints; ++i) {
          if (costOutput[i] == 0.0) {
               //check for negative neighbours, calc that ones and add to queue trialfield
               local_neighbor = _grid->getNeighbors(i);

               //check for valid neigh
               aux = local_neighbor.key[0];
               if ((aux != -2) && (_gridCode[aux] != WALL) && (costOutput[aux] < 0.0)) {
                    calcCost(aux, costOutput, directionOutput, speed);
                    trialfield.emplace(aux);
                    trialfield2.emplace(aux);
               }
               aux = local_neighbor.key[1];
               if ((aux != -2) && (_gridCode[aux] != WALL) && (costOutput[aux] < 0.0)) {
                    calcCost(aux, costOutput, directionOutput, speed);
                    trialfield.emplace(aux);
                    trialfield2.emplace(aux);
               }
               aux = local_neighbor.key[2];
               if ((aux != -2) && (_gridCode[aux] != WALL) && (costOutput[aux] < 0.0)) {
                    calcCost(aux, costOutput, directionOutput, speed);
                    trialfield.emplace(aux);
                    trialfield2.emplace(aux);
               }
               aux = local_neighbor.key[3];
               if ((aux != -2) && (_gridCode[aux] != WALL) && (costOutput[aux] < 0.0)) {
                    calcCost(aux, costOutput, directionOutput, speed);
                    trialfield.emplace(aux);
                    trialfield2.emplace(aux);
               }
          }
     }

     while(!trialfield.empty()) {
          local_neighbor = _grid->getNeighbors(trialfield.top());
          trialfield.pop();

          //check for valid neigh
          aux = local_neighbor.key[0];
          if ((aux != -2) && (_gridCode[aux] != WALL) && (costOutput[aux] < 0.0)) {
               calcCost(aux, costOutput, directionOutput, speed);
               trialfield.emplace(aux);
               trialfield2.emplace(aux);
          }
          aux = local_neighbor.key[1];
          if ((aux != -2) && (_gridCode[aux] != WALL) && (costOutput[aux] < 0.0)) {
               calcCost(aux, costOutput, directionOutput, speed);
               trialfield.emplace(aux);
               trialfield2.emplace(aux);
          }
          aux = local_neighbor.key[2];
          if ((aux != -2) && (_gridCode[aux] != WALL) && (costOutput[aux] < 0.0)) {
               calcCost(aux, costOutput, directionOutput, speed);
               trialfield.emplace(aux);
               trialfield2.emplace(aux);
          }
          aux = local_neighbor.key[3];
          if ((aux != -2) && (_gridCode[aux] != WALL) && (costOutput[aux] < 0.0)) {
               calcCost(aux, costOutput, directionOutput, speed);
               trialfield.emplace(aux);
               trialfield2.emplace(aux);
          }
     }
}

void UnivFFviaFM::calcCost(const long int key, double* cost, Point* dir, const double* const speed) {
     //adapt from calcFloorfield
     double row = DBL_MAX;
     double col = DBL_MAX;
     long int aux = -1; //will be set below
     bool pointsUp = false;
     bool pointsRight = false;

     directNeighbor dNeigh = _grid->getNeighbors(key);

     aux = dNeigh.key[0];
     //hint: trialfield[i].cost = dist2Wall + i; <<< set in resetGoalAndCosts
     if  ((aux != -2) &&                                                                         //neighbor is a gridpoint
          (cost[aux] != magicnum(UNKNOWN_COST)) && (cost[aux] != magicnum(UNKNOWN_DISTANCE)) &&  //gridpoint holds a calculated value
          (_gridCode[aux] != WALL))                                                              //gridpoint holds a calculated value
     {
          row = cost[aux];
          pointsRight = true;
          if (row < 0) {
               std::cerr << "hier ist was schief " << row << " " << aux << " " <<  std::endl;
               row = DBL_MAX;
          }
     }
     aux = dNeigh.key[2];
     if  ((aux != -2) &&                                                         //neighbor is a gridpoint
          (cost[aux] != magicnum(UNKNOWN_COST)) && (cost[aux] != magicnum(UNKNOWN_DISTANCE)) &&  //gridpoint holds a calculated value
          (_gridCode[aux] != WALL) &&
          (cost[aux] < row))                                       //calculated value promises smaller cost
     {
          row = cost[aux];
          pointsRight = false;
     }

     aux = dNeigh.key[1];
     //hint: trialfield[i].cost = dist2Wall + i; <<< set in parseBuilding after linescan call
     if  ((aux != -2) &&                                                         //neighbor is a gridpoint
          (cost[aux] != magicnum(UNKNOWN_COST)) && (cost[aux] != magicnum(UNKNOWN_DISTANCE)) &&  //gridpoint holds a calculated value
          (_gridCode[aux] != WALL))
     {
          col = cost[aux];
          pointsUp = true;
          if (col < 0) {
               std::cerr << "hier ist was schief " << col << " " << aux << " "  << std::endl;
               col = DBL_MAX;
          }
     }
     aux = dNeigh.key[3];
     if  ((aux != -2) &&                                                         //neighbor is a gridpoint
          (cost[aux] != magicnum(UNKNOWN_COST)) && (cost[aux] != magicnum(UNKNOWN_DISTANCE)) &&  //gridpoint holds a calculated value
          (_gridCode[aux] != WALL) &&
          (cost[aux] < col))                                       //calculated value promises smaller cost
     {
          col = cost[aux];
          pointsUp = false;
     }
     if (col == DBL_MAX) { //one sided update with row
          cost[key] = onesidedCalc(row, _grid->Gethx()/speed[key]);
          //flag[key] = FM_SINGLE;
          if (pointsRight) {
               dir[key]._x = (-(cost[key+1]-cost[key])/_grid->Gethx());
               dir[key]._y = (0.);
          } else {
               dir[key]._x = (-(cost[key]-cost[key-1])/_grid->Gethx());
               dir[key]._y = (0.);
          }
          dir[key] = dir[key].Normalized(); //@todo: ar.graf: what yields better performance? scale every point here or scale each read value? more points or more calls to any element of dir2Wall
          return;
     }

     if (row == DBL_MAX) { //one sided update with col
          cost[key] = onesidedCalc(col, _grid->Gethy()/speed[key]);
          //flag[key] = FM_SINGLE;
          if (pointsUp) {
               dir[key]._x = (0.);
               dir[key]._y = (-(cost[key+(_grid->GetiMax())]-cost[key])/_grid->Gethy());
          } else {
               dir[key]._x = (0.);
               dir[key]._y = (-(cost[key]-cost[key-(_grid->GetiMax())])/_grid->Gethy());
          }
          dir[key] = dir[key].Normalized();
          return;
     }

     //two sided update
     double precheck = twosidedCalc(row, col, _grid->Gethx()/speed[key]);
     if (precheck >= 0) {
          cost[key] = precheck;
          //flag[key] = FM_DOUBLE;
          if (pointsUp && pointsRight) {
               dir[key]._x = (-(cost[key+1]-cost[key])/_grid->Gethx());
               dir[key]._y = (-(cost[key+(_grid->GetiMax())]-cost[key])/_grid->Gethy());
          }
          if (pointsUp && !pointsRight) {
               dir[key]._x = (-(cost[key]-cost[key-1])/_grid->Gethx());
               dir[key]._y = (-(cost[key+(_grid->GetiMax())]-cost[key])/_grid->Gethy());
          }
          if (!pointsUp && pointsRight) {
               dir[key]._x = (-(cost[key+1]-cost[key])/_grid->Gethx());
               dir[key]._y = (-(cost[key]-cost[key-(_grid->GetiMax())])/_grid->Gethy());
          }
          if (!pointsUp && !pointsRight) {
               dir[key]._x = (-(cost[key]-cost[key-1])/_grid->Gethx());
               dir[key]._y = (-(cost[key]-cost[key-(_grid->GetiMax())])/_grid->Gethy());
          }
     } else {
          std::cerr << "else in twosided Dist " << std::endl;
     }
     dir[key] = dir[key].Normalized();
}

inline double UnivFFviaFM::onesidedCalc(double xy, double hDivF) {
     //if ( (xy+hDivF) > 10000) std::cerr << "error in onesided " << xy << std::endl;
     return xy + hDivF;
}

inline double UnivFFviaFM::twosidedCalc(double x, double y, double hDivF) { //on error return -2
     double determinante = (2*hDivF*hDivF - (x-y)*(x-y));
     if (determinante >= 0) {
          return (x + y + sqrt(determinante))/2;
     } else {
          return (x < y) ? (x + hDivF) : (y + hDivF);
     }
     std::cerr << "error in two-sided 2!!!!!!!!!!!!!!!!!!!!!!! o_O??" << std::endl;
     return -2.; //this line should never execute
} //twosidedCalc

Arne Graf's avatar
Arne Graf committed
557 558 559
void UnivFFviaFM::addTarget(const int uid) {
     Line tempTargetLine = Line(_doors[uid]);
     Point tempCenterPoint = Point(tempTargetLine.GetCentre());
Arne Graf's avatar
Arne Graf committed
560

Arne Graf's avatar
Arne Graf committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
     //this allocation must be on shared heap! to be accessable by any thread later (head should be shared in openmp)
     double* newArrayDBL = new double[_nPoints];
     Point* newArrayPt = nullptr;
     if (_user == DISTANCE_AND_DIRECTIONS_USED) {
          newArrayPt = new Point[_nPoints];
     }

     if (_costFieldWithKey[uid])
          delete[] _costFieldWithKey[uid];
     _costFieldWithKey[uid] = newArrayDBL;

     //init costarray
     for (int i = 0; i < _nPoints; ++i) {
          if (_gridCode[i] == WALL) {
               newArrayDBL[i] = magicnum(WALL_ON_COSTARRAY);
          } else {
               newArrayDBL[i] = magicnum(UNKNOWN_COST);
          }
     }

     if (_directionFieldWithKey[uid])
          delete[] _directionFieldWithKey[uid];
     if (newArrayPt)
          _directionFieldWithKey[uid] = newArrayPt;
Arne Graf's avatar
Arne Graf committed
585

Arne Graf's avatar
Arne Graf committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
     //initialize start area
     if (_mode == LINESEGMENT) {
          drawLinesOnGrid(tempTargetLine, newArrayDBL, magicnum(TARGET_REGION));
     }
     if (_mode == CENTERPOINT) {
          newArrayDBL[_grid->getKeyAtPoint(tempCenterPoint)] = magicnum(TARGET_REGION);
     }

     if (_speedmode == FF_WALL_AVOID) {
          calcFF(newArrayDBL, newArrayPt, _speedFieldSelector[REDU_WALL_SPEED]);
     } else if (_speedmode == FF_HOMO_SPEED) {
          calcFF(newArrayDBL, newArrayPt, _speedFieldSelector[INITIAL_SPEED]);
     }
     _uids.emplace_back(uid);
}

void UnivFFviaFM::addAllTargets() {
     for (auto uidmap : _doors) {
          addTarget(uidmap.first);
     }
Arne Graf's avatar
Arne Graf committed
606
}
Arne Graf's avatar
Arne Graf committed
607

Arne Graf's avatar
Arne Graf committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
std::vector<int> UnivFFviaFM::getKnownDoorUIDs(){
     return _uids;
}

void UnivFFviaFM::writeFF(const std::string& filename, std::vector<int> targetID) {
    Log->Write("INFO: \tWrite Floorfield to file");
    Log->Write(filename);
    std::ofstream file;

    Log->Write("FloorfieldViaFM::writeFF(): writing to file %s: There are %d targets.", filename.c_str(), targetID.size());

    int numX = (int) ((_grid->GetxMax()-_grid->GetxMin())/_grid->Gethx());
    int numY = (int) ((_grid->GetyMax()-_grid->GetyMin())/_grid->Gethy());
    int numTotal = numX * numY;
    //std::cerr << numTotal << " numTotal" << std::endl;
    //std::cerr << grid->GetnPoints() << " grid" << std::endl;
    file.open(filename);

    file << "# vtk DataFile Version 3.0" << std::endl;
    file << "Testdata: Fast Marching: Test: " << std::endl;
    file << "ASCII" << std::endl;
    file << "DATASET STRUCTURED_POINTS" << std::endl;
    file << "DIMENSIONS " <<
                                std::to_string(_grid->GetiMax()) <<
                                " " <<
                                std::to_string(_grid->GetjMax()) <<
                                " 1" << std::endl;
    file << "ORIGIN " << _grid->GetxMin() << " " << _grid->GetyMin() << " 0" << std::endl;
    file << "SPACING " << std::to_string(_grid->Gethx()) << " " << std::to_string(_grid->Gethy()) << " 1" << std::endl;
    file << "POINT_DATA " << std::to_string(numTotal) << std::endl;
    file << "SCALARS GCode float 1" << std::endl;
    file << "LOOKUP_TABLE default" << std::endl;
Arne Graf's avatar
Arne Graf committed
640 641 642
    if (!_gridCode) {
         return;
    }
Arne Graf's avatar
Arne Graf committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    for (long int i = 0; i < _grid->GetnPoints(); ++i) {
         file << _gridCode[i] << std::endl;
    }

    if (_directionFieldWithKey[0]) {
         file << "VECTORS Dir2Wall float" << std::endl;
         for (long int i = 0; i < _grid->GetnPoints(); ++i) {
              file << _directionFieldWithKey[0][i]._x << " " << _directionFieldWithKey[0][i]._y << " 0.0" << std::endl;
         }

         file << "SCALARS Dist2Wall float 1" << std::endl;
         file << "LOOKUP_TABLE default" << std::endl;
         for (long int i = 0; i < _grid->GetnPoints(); ++i) {
              file << _costFieldWithKey[0][i] << std::endl; //@todo: change target to all dist2wall
         }
    }

//    file << "SCALARS SubroomPtr float 1" << std::endl;
//    file << "LOOKUP_TABLE default" << std::endl;
//    for (long int i = 0; i < _grid->GetnPoints(); ++i) {
//        if (_subrooms[i]) {
//            file << _subrooms[i]->GetUID() << std::endl;
//        } else {
//            file << 0.0 << std::endl;
//        }
//    }
Arne Graf's avatar
Arne Graf committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
    if (!targetID.empty()) {
         for (unsigned int iTarget = 0; iTarget < targetID.size(); ++iTarget) {
              Log->Write("%s: target number %d: UID %d", filename.c_str(), iTarget, targetID[iTarget]);
              if (_directionFieldWithKey.count(targetID[iTarget]) == 0) {
                   continue;
              }

              Point *gradarray = _directionFieldWithKey[targetID[iTarget]];
              if (gradarray == nullptr) {
                   continue;
              }

              std::string name = _building->GetTransOrCrossByUID(targetID[iTarget])->GetCaption() + "-" +
                                 std::to_string(targetID[iTarget]);
              std::replace(name.begin(), name.end(), ' ', '_');
              file << "VECTORS GradientTarget" << name << " float" << std::endl;
              for (int i = 0; i < _grid->GetnPoints(); ++i) {
                   file << gradarray[i]._x << " " << gradarray[i]._y << " 0.0" << std::endl;
              }

              double *costarray = _costFieldWithKey[targetID[iTarget]];
              file << "SCALARS CostTarget" << name << " float 1" << std::endl;
              file << "LOOKUP_TABLE default" << std::endl;
              for (long int i = 0; i < _grid->GetnPoints(); ++i) {
                   file << costarray[i] << std::endl;
              }
         }
Arne Graf's avatar
Arne Graf committed
696 697 698
    }
    file.close();
}