Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ConvertToAStarMap.h"
- #include <algorithm>
- #include <utility>
- #include <cmath>
- #include <cstdlib>
- #include <fstream>
- #include <iostream>
- #include <limits>
- #include <sstream>
- std::vector<std::string> ConvertToAStarMap::split(std::string strToSplit, char delimeter)
- {
- std::stringstream ss(strToSplit);
- std::string item;
- std::vector<std::string> splittedStrings;
- while (std::getline(ss, item, delimeter))
- {
- splittedStrings.push_back(item);
- }
- return splittedStrings;
- }
- std::pair<max, max> ConvertToAStarMap::GetLimits(const std::string& robot_map)
- {
- // Init max and min variables.
- int max_x = std::numeric_limits<int>::min();
- int min_x = std::numeric_limits<int>::max();
- int max_y = max_x;
- int min_y = min_x;
- std::vector<std::string> map_cells = ConvertToAStarMap::split(robot_map, ';');
- for (std::vector<std::string>::iterator it = map_cells.begin(); it != map_cells.end(); ++it)
- {
- std::vector<std::string> locations = ConvertToAStarMap::split(*it, ',');
- int aux_x = std::stoi(locations[0]);
- if (aux_x > max_x)
- max_x = aux_x;
- if (aux_x < min_x)
- min_x = aux_x;
- int aux_y = std::stoi(locations[1]);
- if (aux_y > max_y)
- max_y = aux_y;
- if (aux_y < min_y)
- min_y = aux_y;
- }
- // Return max and min values for x and y.
- struct max x = { min_x, max_x };
- struct max y = { min_y, max_y };
- return std::make_pair(x, y);
- }
- std::string ConvertToAStarMap::GenerateAStarMap(
- const std::string& map,
- std::pair<max, max> pairs,
- const std::string& output_file)
- {
- std::string output_line;
- struct max x_boundary = pairs.first;
- struct max y_boundary = pairs.second;
- for (int y = y_boundary.positive; y >= y_boundary.negative; y--)
- {
- std::string line;
- for (int x = x_boundary.negative; x <= x_boundary.positive; x++)
- {
- if (line.empty())
- {
- line += "{";
- }
- else
- {
- line += ",";
- }
- std::string cell = std::to_string(x) + "," + std::to_string(y);
- //std::cout << cell << " ";
- if (map.find(cell) != std::string::npos)
- {
- line += "1";
- }
- else
- {
- line += "0";
- }
- }
- if (y > y_boundary.negative)
- {
- line += "},";
- }
- else if (y == y_boundary.negative)
- {
- line += "}};";
- }
- // Debug
- std::cout << line << std::endl;
- std::cout << std::endl;
- // Concatenate to file output line.
- output_line += line + '\n';
- }
- // Write map to text file
- std::ofstream out_file(output_file);
- out_file << output_line;
- out_file.close();
- return output_file;
- }
- std::string ConvertToAStarMap::TruncateMap(const std::string& robot_map)
- {
- std::string truncatedMap;
- std::vector<std::pair<int, int>> list;
- int current_x = 0;
- int current_y = 0;
- std::vector<std::string> map_cells = ConvertToAStarMap::split(robot_map, ';');
- for (std::vector<std::string>::iterator it = map_cells.begin(); it != map_cells.end(); ++it)
- {
- std::vector<std::string> locations = ConvertToAStarMap::split(*it, ',');
- double x = std::stod(locations[0]);
- double y = std::stod(locations[1]);
- if (x < 0)
- current_x = static_cast<int>(std::trunc(x));
- else
- current_x = static_cast<int>(std::trunc(x + 1));
- if (y < 0)
- current_y = static_cast<int>(std::trunc(y));
- else
- current_y = static_cast<int>(std::trunc(y + 1));
- std::pair<int, int> current = std::make_pair(current_x, current_y);
- if (std::find(list.begin(), list.end(), current) != list.end())
- {
- list.push_back(current);
- truncatedMap += std::to_string(current_x) + ",";
- truncatedMap += std::to_string(current_y) + ";";
- }
- }
- return truncatedMap;
- }
- // PUBLIC
- // *****************************************************************************
- std::string ConvertToAStarMap::Convert(
- const std::string& map,
- const std::string& output_file)
- {
- std::string truncatedMap = ConvertToAStarMap::TruncateMap(map);
- std::pair<max, max> pairs = ConvertToAStarMap::GetLimits(truncatedMap);
- // Debug
- std::cout << "x "
- << "(" << pairs.first.negative << ", "
- << pairs.first.positive << "), "
- << "y "
- << "(" << pairs.second.negative << ", "
- << pairs.second.positive << ")" << std::endl;
- return ConvertToAStarMap::GenerateAStarMap(truncatedMap, pairs, output_file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement