Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
SquareGridMapParser.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
7
8#pragma once
9
10#include <fstream>
11#include <stdexcept>
12#include <limits>
13#include <iostream>
14#include <tuple>
15
16namespace graph {
17namespace parser {
18
20// ---------------------------------------------------------------------------------------------------------------------
21 public:
22 template<typename TGrid>
23 static
24 std::tuple<TGrid, typename TGrid::node_type, typename TGrid::node_type>
25 parse(const std::string &filename);
26
27// ---------------------------------------------------------------------------------------------------------------------
28};
29
30// =====================================================================================================================
31
32template<typename TGrid>
33std::tuple<TGrid, typename TGrid::node_type, typename TGrid::node_type>
34SquareGridMapParser::parse(const std::string &filename) {
35 using node_type = typename TGrid::node_type;
36 std::ifstream file(filename);
37
38 if (!file.is_open()) {
39 throw std::runtime_error("Could not open file: '" + filename + "'.");
40 }
41
42 // Read header
43 unsigned long height, width, i = 0, j = 0;
44 file.ignore(std::numeric_limits<std::streamsize>::max(), file.widen('\n')); // skip first line
45 file.ignore(std::numeric_limits<std::streamsize>::max(), file.widen(' ')); // skip height
46 file >> height;
47 file.ignore(std::numeric_limits<std::streamsize>::max(), file.widen(' ')); // skip width
48 file >> width;
49 file.ignore(std::numeric_limits<std::streamsize>::max(), file.widen('\n')); // skip the endline
50 file.ignore(std::numeric_limits<std::streamsize>::max(), file.widen('\n')); // skip 'map'
51
52 // Read grid
53 TGrid grid(height, width);
54 node_type start(0, 0), goal(0, 0);
55 std::string line;
56 while (getline(file, line)) {
57 j = 0;
58 for (const auto &c: line) {
59 if (c == 'S') {
60 start.first = i;
61 start.second = j;
62 } else if (c == 'G') {
63 goal.first = i;
64 goal.second = j;
65 } else if (c != '.') {
66 grid.addObstacle(i, j);
67 }
68
69 ++j; // Increase column
70 }
71 ++i; // Increase row
72 }
73
74 file.close();
75
76 return std::make_tuple(grid, start, goal);
77
78}
79
80// ---------------------------------------------------------------------------------------------------------------------
81
82}
83}
84
Definition: SquareGridMapParser.hpp:19
static std::tuple< TGrid, typename TGrid::node_type, typename TGrid::node_type > parse(const std::string &filename)
Definition: SquareGridMapParser.hpp:34
int i
Definition: AllEpsilonClosure.h:118
constexpr const T & max(const T &a)
Root case of maximum computation. The maximum from one value is the value itself.
Definition: algorithm.hpp:278
constexpr auto make_tuple(Elements &&... args)
Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: tuple.hpp:203
Definition: ReconstructPath.hpp:14
Definition: GridDirection.hpp:12
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14