Advertisement
Guest User

ConvertToAStarMap.cpp

a guest
Jul 29th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include "ConvertToAStarMap.h"
  2.  
  3. #include <algorithm>
  4. #include <utility>
  5. #include <cmath>
  6. #include <cstdlib>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <limits>
  10. #include <sstream>
  11.  
  12. std::vector<std::string> ConvertToAStarMap::split(std::string strToSplit, char delimeter)
  13. {
  14.     std::stringstream ss(strToSplit);
  15.     std::string item;
  16.     std::vector<std::string> splittedStrings;
  17.     while (std::getline(ss, item, delimeter))
  18.     {
  19.         splittedStrings.push_back(item);
  20.     }
  21.     return splittedStrings;
  22. }
  23.  
  24. std::pair<max, max> ConvertToAStarMap::GetLimits(const std::string& robot_map)
  25. {
  26.     // Init max and min variables.
  27.     int max_x = std::numeric_limits<int>::min();
  28.     int min_x = std::numeric_limits<int>::max();
  29.  
  30.     int max_y = max_x;
  31.     int min_y = min_x;
  32.  
  33.     std::vector<std::string> map_cells = ConvertToAStarMap::split(robot_map, ';');
  34.  
  35.     for (std::vector<std::string>::iterator it = map_cells.begin(); it != map_cells.end(); ++it)
  36.     {
  37.         std::vector<std::string> locations = ConvertToAStarMap::split(*it, ',');
  38.  
  39.         int aux_x = std::stoi(locations[0]);
  40.         if (aux_x > max_x)
  41.             max_x = aux_x;
  42.         if (aux_x < min_x)
  43.             min_x = aux_x;
  44.  
  45.         int aux_y = std::stoi(locations[1]);
  46.         if (aux_y > max_y)
  47.             max_y = aux_y;
  48.         if (aux_y < min_y)
  49.             min_y = aux_y;
  50.     }
  51.  
  52.     // Return max and min values for x and y.
  53.     struct max x = { min_x, max_x };
  54.     struct max y = { min_y, max_y };
  55.  
  56.     return std::make_pair(x, y);
  57. }
  58.  
  59. std::string ConvertToAStarMap::GenerateAStarMap(
  60.     const std::string& map,
  61.     std::pair<max, max> pairs,
  62.     const std::string& output_file)
  63. {
  64.     std::string output_line;
  65.  
  66.     struct max x_boundary = pairs.first;
  67.     struct max y_boundary = pairs.second;
  68.  
  69.     for (int y = y_boundary.positive; y >= y_boundary.negative; y--)
  70.     {
  71.         std::string line;
  72.  
  73.         for (int x = x_boundary.negative; x <= x_boundary.positive; x++)
  74.         {
  75.             if (line.empty())
  76.             {
  77.                 line += "{";
  78.             }
  79.             else
  80.             {
  81.                 line += ",";
  82.             }
  83.  
  84.             std::string cell = std::to_string(x) + "," + std::to_string(y);
  85.  
  86.             //std::cout << cell << " ";
  87.  
  88.             if (map.find(cell) != std::string::npos)
  89.             {
  90.                 line += "1";
  91.             }
  92.             else
  93.             {
  94.                 line += "0";
  95.             }
  96.         }
  97.  
  98.         if (y > y_boundary.negative)
  99.         {
  100.             line += "},";
  101.         }
  102.         else if (y == y_boundary.negative)
  103.         {
  104.             line += "}};";
  105.         }
  106.  
  107.         // Debug
  108.         std::cout << line << std::endl;
  109.         std::cout << std::endl;
  110.  
  111.         // Concatenate to file output line.
  112.         output_line += line + '\n';
  113.     }
  114.  
  115.  
  116.     // Write map to text file
  117.     std::ofstream out_file(output_file);
  118.     out_file << output_line;
  119.     out_file.close();
  120.  
  121.     return output_file;
  122. }
  123.  
  124. std::string ConvertToAStarMap::TruncateMap(const std::string& robot_map)
  125. {
  126.     std::string truncatedMap;
  127.  
  128.     std::vector<std::pair<int, int>> list;
  129.  
  130.     int current_x = 0;
  131.     int current_y = 0;
  132.  
  133.     std::vector<std::string> map_cells = ConvertToAStarMap::split(robot_map, ';');
  134.  
  135.     for (std::vector<std::string>::iterator it = map_cells.begin(); it != map_cells.end(); ++it)
  136.     {
  137.         std::vector<std::string> locations = ConvertToAStarMap::split(*it, ',');
  138.  
  139.         double x = std::stod(locations[0]);
  140.         double y = std::stod(locations[1]);
  141.  
  142.         if (x < 0)
  143.             current_x = static_cast<int>(std::trunc(x));
  144.         else
  145.             current_x = static_cast<int>(std::trunc(x + 1));
  146.  
  147.         if (y < 0)
  148.             current_y = static_cast<int>(std::trunc(y));
  149.         else
  150.             current_y = static_cast<int>(std::trunc(y + 1));
  151.  
  152.         std::pair<int, int> current = std::make_pair(current_x, current_y);
  153.  
  154.         if (std::find(list.begin(), list.end(), current) != list.end())
  155.         {
  156.             list.push_back(current);
  157.  
  158.             truncatedMap += std::to_string(current_x) + ",";
  159.             truncatedMap += std::to_string(current_y) + ";";
  160.         }
  161.     }
  162.  
  163.     return truncatedMap;
  164. }
  165.  
  166. // PUBLIC
  167. // *****************************************************************************
  168. std::string ConvertToAStarMap::Convert(
  169.     const std::string& map,
  170.     const std::string& output_file)
  171. {
  172.     std::string truncatedMap = ConvertToAStarMap::TruncateMap(map);
  173.     std::pair<max, max> pairs = ConvertToAStarMap::GetLimits(truncatedMap);
  174.  
  175.     // Debug
  176.     std::cout << "x "
  177.         << "(" << pairs.first.negative << ", "
  178.         << pairs.first.positive << "), "
  179.         << "y "
  180.         << "(" << pairs.second.negative << ", "
  181.         << pairs.second.positive << ")" << std::endl;
  182.  
  183.     return ConvertToAStarMap::GenerateAStarMap(truncatedMap, pairs, output_file);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement