Map.cpp 3.9 KB
Newer Older
Martin Kröning's avatar
Martin Kröning committed
1 2
#include "Map.hpp"
#include "Junction.hpp"
3
#include "MapAppAPIWrapper.hpp"
Martin Kröning's avatar
Martin Kröning committed
4 5
#include <algorithm>
#include <chrono>
6
#include <cstddef>
Martin Kröning's avatar
Martin Kröning committed
7 8 9 10 11 12 13 14 15 16
#include <limits>
#include <memory>
#include <ratio>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
#include <utility>
#include <vector>

17 18
Map::Map(std::istream &is, const std::string &windowTitle)
    : time(0), mapApp(windowTitle) {
Martin Kröning's avatar
Martin Kröning committed
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
  std::string type;
  while (is >> type) {
    if (type == "KREUZUNG") {
      extractJunctionAndDraw(is, mapApp);
    } else if (type == "STRASSE") {
      extractRoadAndDraw(is, mapApp);
    } else if (type == "PKW") {
      extractCar(is);
    } else if (type == "FAHRRAD") {
      extractBicycle(is);
    }
  }
}

void Map::simulate(const std::chrono::seconds duration,
                   const double speedFactor, const double frequency) {
  const auto count =
      static_cast<size_t>(duration.count() * frequency / speedFactor);
  for (size_t i = 0; i < count; i++) {
    time += static_cast<double>(duration.count()) / 60 / 60 / count;
    for (auto &&junction : junctions) {
      junction->processVehicles(time);
      junction->drawVehicles(mapApp);
    }
    mapApp.setDurationLabel(std::chrono::minutes(static_cast<int>(time * 60)));
    std::this_thread::sleep_for(std::chrono::nanoseconds(
        static_cast<size_t>(std::nano::den / frequency)));
  }
}

std::weak_ptr<Junction> Map::getJunction(const std::string &name) {
  return *std::find_if(
      junctions.begin(), junctions.end(),
      [&name](const auto &junction) { return junction->getName() == name; });
}

55
void Map::extractJunctionAndDraw(std::istream &is, MapAppAPIWrapper &mapApp) {
Martin Kröning's avatar
Martin Kröning committed
56 57 58 59 60 61 62 63 64 65 66
  std::string name;
  double fuelAmount;
  is >> name >> fuelAmount;
  auto junction = std::make_shared<Junction>(name, fuelAmount);
  junctions.push_back(std::move(junction));
  int x;
  int y;
  is >> x >> y;
  mapApp.addJunction(x, y);
}

67
void Map::extractRoadAndDraw(std::istream &is, MapAppAPIWrapper &mapApp) {
Martin Kröning's avatar
Martin Kröning committed
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
  std::string nameJunctionA;
  std::string nameJunctionB;
  std::string nameLaneAToB;
  std::string nameLaneBToA;
  double lengthRoad;
  int speedLimitLevel;
  bool noPassing;
  is >> nameJunctionA >> nameJunctionB >> nameLaneAToB >> nameLaneBToA >>
      lengthRoad >> speedLimitLevel >> noPassing;

  double speedLimit;
  switch (speedLimitLevel) {
  case 1:
    speedLimit = 50;
    break;
  case 2:
    speedLimit = 100;
    break;
  case 3:
    speedLimit = std::numeric_limits<double>::infinity();
    break;
  default:
    throw std::invalid_argument('\'' + std::to_string(speedLimitLevel) +
                                "' does not encode a speed limit.");
  }

  auto junctionA = getJunction(nameJunctionA);
  auto junctionB = getJunction(nameJunctionB);
  Junction::connect(junctionA, junctionB, nameLaneAToB, nameLaneBToA,
                    lengthRoad, speedLimit, noPassing);
  int coordinatePairCount;
  is >> coordinatePairCount;
  std::vector<double> coordinates(coordinatePairCount * 2);
  std::generate(coordinates.begin(), coordinates.end(), [&is]() {
    double x;
    is >> x;
    return x;
  });
  mapApp.addRoad(nameLaneAToB, nameLaneBToA, coordinates);
}

void Map::extractBicycle(std::istream &is) {
  std::string name;
  double maximumVelocity;
  is >> name >> maximumVelocity;
  std::string nameStartJunction;
  double timeOfStart;
  is >> nameStartJunction >> timeOfStart;
  auto &junction = *getJunction(nameStartJunction).lock();
  junction.createBicycle(name, maximumVelocity, timeOfStart, time);
}

void Map::extractCar(std::istream &is) {
  std::string name;
  double maximumVelocity;
  double fuelConsumption;
  double fuelCapacity;
  is >> name >> maximumVelocity >> fuelConsumption >> fuelCapacity;
  std::string nameStartJunction;
  double timeOfStart;
  is >> nameStartJunction >> timeOfStart;
  auto &junction = *getJunction(nameStartJunction).lock();
  junction.createCar(name, maximumVelocity, fuelConsumption, fuelCapacity,
                     timeOfStart, time);
}