|
| 1 | +#include "ASTAR.h" |
| 2 | + |
| 3 | +// Constructor |
| 4 | +ASTAR::ASTAR(sf::RenderWindow *window, |
| 5 | + std::stack<std::unique_ptr<State>> &states) |
| 6 | + : Algorithm(window, states, "A-STAR") {} |
| 7 | + |
| 8 | +// Destructor |
| 9 | +ASTAR::~ASTAR() {} |
| 10 | + |
| 11 | +// override initAlgorithm() function |
| 12 | +void ASTAR::initAlgorithm() { |
| 13 | + // initialize ASTAR by clearing frontier and add start node |
| 14 | + while (!frontier_.empty()) { |
| 15 | + frontier_.pop(); |
| 16 | + } |
| 17 | + nodeStart_->setGDistance(0.0); |
| 18 | + nodeStart_->setFDistance(L1_Distance(nodeStart_, nodeEnd_)); |
| 19 | + frontier_.push(nodeStart_); |
| 20 | +} |
| 21 | + |
| 22 | +// override updateNodes() function |
| 23 | +void ASTAR::updateNodes() { |
| 24 | + if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && getKeyTime()) { |
| 25 | + int localY = ((mousePositionWindow_.x - 300) / gridSize_); |
| 26 | + int localX = ((mousePositionWindow_.y - 60) / gridSize_); |
| 27 | + |
| 28 | + if (localX >= 0 && localX < mapHeight_ / gridSize_) { |
| 29 | + if (localY >= 0 && localY < mapWidth_ / gridSize_) { |
| 30 | + if (!Algorithm_solved_) { |
| 31 | + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))) { |
| 32 | + nodeStart_ = nodes_[(mapWidth_ / gridSize_) * localX + localY]; |
| 33 | + } else if ((sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))) { |
| 34 | + nodeEnd_ = nodes_[(mapWidth_ / gridSize_) * localX + localY]; |
| 35 | + } else { |
| 36 | + nodes_[(mapWidth_ / gridSize_) * localX + localY]->setObstacle( |
| 37 | + !nodes_[(mapWidth_ / gridSize_) * localX + localY] |
| 38 | + ->isObstacle()); |
| 39 | + } |
| 40 | + } else { |
| 41 | + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))) { |
| 42 | + nodeEnd_ = nodes_[(mapWidth_ / gridSize_) * localX + localY]; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// override renderNodes() function |
| 51 | +void ASTAR::renderNodes() { |
| 52 | + for (int x = 0; x < mapHeight_ / gridSize_; x++) { |
| 53 | + for (int y = 0; y < mapWidth_ / gridSize_; y++) { |
| 54 | + float size = static_cast<float>(gridSize_); |
| 55 | + sf::RectangleShape rectangle(sf::Vector2f(size, size)); |
| 56 | + rectangle.setOutlineThickness(2.f); |
| 57 | + rectangle.setOutlineColor(BGN_COL); |
| 58 | + rectangle.setPosition(300 + y * size, 60 + x * size); |
| 59 | + |
| 60 | + if (nodes_[(mapWidth_ / gridSize_) * x + y]->isObstacle()) { |
| 61 | + rectangle.setFillColor(OBST_COL); |
| 62 | + } else { |
| 63 | + rectangle.setFillColor(IDLE_COL); |
| 64 | + } |
| 65 | + |
| 66 | + if (nodes_[(mapWidth_ / gridSize_) * x + y]->isVisited()) { |
| 67 | + rectangle.setFillColor(VISITED_COL); |
| 68 | + } |
| 69 | + |
| 70 | + if (nodes_[(mapWidth_ / gridSize_) * x + y]->isFrontier()) { |
| 71 | + rectangle.setFillColor(FRONTIER_COL); |
| 72 | + } |
| 73 | + |
| 74 | + if (nodes_[(mapWidth_ / gridSize_) * x + y]->isPath()) { |
| 75 | + rectangle.setFillColor(START_COL); |
| 76 | + nodes_[(mapWidth_ / gridSize_) * x + y]->setPath(false); |
| 77 | + } |
| 78 | + |
| 79 | + if (nodes_[(mapWidth_ / gridSize_) * x + y] == nodeStart_) { |
| 80 | + rectangle.setFillColor(START_COL); |
| 81 | + } |
| 82 | + |
| 83 | + if (nodes_[(mapWidth_ / gridSize_) * x + y] == nodeEnd_) { |
| 84 | + rectangle.setFillColor(END_BORDER_COL); |
| 85 | + } |
| 86 | + |
| 87 | + window_->draw(rectangle); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // visualizing path |
| 92 | + if (nodeEnd_ != nullptr) { |
| 93 | + std::shared_ptr<Node> current = nodeEnd_; |
| 94 | + |
| 95 | + while (current->getParentNode() != nullptr && current != nodeStart_) { |
| 96 | + current->setPath(true); |
| 97 | + current = current->getParentNode(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +double ASTAR::L1_Distance(const std::shared_ptr<Node> &n1, |
| 103 | + const std::shared_ptr<Node> &n2) { |
| 104 | + return fabs(n1->getPos().x - n2->getPos().x) + |
| 105 | + fabs(n1->getPos().y - n2->getPos().y); |
| 106 | +} |
| 107 | + |
| 108 | +void ASTAR::solveConcurrently( |
| 109 | + std::shared_ptr<Node> nodeStart, std::shared_ptr<Node> nodeEnd, |
| 110 | + std::shared_ptr<MessageQueue<bool>> message_queue) { |
| 111 | + // copy assignment |
| 112 | + // thread-safe due to shared_ptrs |
| 113 | + std::shared_ptr<Node> s_nodeStart = nodeStart; |
| 114 | + std::shared_ptr<Node> s_nodeEnd = nodeEnd; |
| 115 | + std::shared_ptr<MessageQueue<bool>> s_message_queue = message_queue; |
| 116 | + |
| 117 | + bool solved = false; |
| 118 | + |
| 119 | + double cycleDuration = 1; // duration of a single simulation cycle in ms |
| 120 | + // init stop watch |
| 121 | + auto lastUpdate = std::chrono::system_clock::now(); |
| 122 | + |
| 123 | + while (true) { |
| 124 | + // compute time difference to stop watch |
| 125 | + long timeSinceLastUpdate = |
| 126 | + std::chrono::duration_cast<std::chrono::milliseconds>( |
| 127 | + std::chrono::system_clock::now() - lastUpdate) |
| 128 | + .count(); |
| 129 | + |
| 130 | + if (timeSinceLastUpdate >= cycleDuration) { |
| 131 | + //////////////////////////// |
| 132 | + // run the main algorithm // |
| 133 | + //////////////////////////// |
| 134 | + if (!frontier_.empty()) { |
| 135 | + std::shared_ptr<Node> nodeCurrent = frontier_.top(); |
| 136 | + nodeCurrent->setFrontier(false); |
| 137 | + nodeCurrent->setVisited(true); |
| 138 | + frontier_.pop(); |
| 139 | + |
| 140 | + if (nodeCurrent == s_nodeEnd) { |
| 141 | + solved = true; |
| 142 | + } |
| 143 | + |
| 144 | + for (auto nodeNeighbour : *nodeCurrent->getNeighbours()) { |
| 145 | + if (nodeNeighbour->isVisited() || nodeNeighbour->isObstacle()) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + double dist = nodeCurrent->getGDistance() + |
| 150 | + L1_Distance(nodeCurrent, nodeNeighbour); |
| 151 | + |
| 152 | + if (dist < nodeNeighbour->getGDistance()) { |
| 153 | + nodeNeighbour->setParentNode(nodeCurrent); |
| 154 | + nodeNeighbour->setGDistance(dist); |
| 155 | + |
| 156 | + // f = g + h |
| 157 | + double f_dist = nodeCurrent->getGDistance() + |
| 158 | + L1_Distance(nodeNeighbour, s_nodeEnd); |
| 159 | + nodeNeighbour->setFDistance(f_dist); |
| 160 | + nodeNeighbour->setFrontier(true); |
| 161 | + frontier_.push(nodeNeighbour); |
| 162 | + } |
| 163 | + } |
| 164 | + } else { |
| 165 | + solved = true; |
| 166 | + } |
| 167 | + //////////////////////////// |
| 168 | + |
| 169 | + // reset stop watch for next cycle |
| 170 | + lastUpdate = std::chrono::system_clock::now(); |
| 171 | + } |
| 172 | + |
| 173 | + // sends an update method to the message queue using move semantics |
| 174 | + auto ftr = std::async(std::launch::async, &MessageQueue<bool>::send, |
| 175 | + s_message_queue, std::move(solved)); |
| 176 | + ftr.wait(); |
| 177 | + |
| 178 | + if (solved) return; |
| 179 | + |
| 180 | + // sleep at every iteration to reduce CPU usage |
| 181 | + std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 182 | + } |
| 183 | +} |
0 commit comments