Coverage Control Library
Loading...
Searching...
No Matches
map_utils.h
Go to the documentation of this file.
1/*
2 * This file is part of the CoverageControl library
3 *
4 * Author: Saurav Agarwal
5 * Contact: sauravag@seas.upenn.edu, agr.saurav1@gmail.com
6 * Repository: https://github.com/KumarRobotics/CoverageControl
7 *
8 * Copyright (c) 2024, Saurav Agarwal
9 *
10 * The CoverageControl library is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at your
13 * option) any later version.
14 *
15 * The CoverageControl library is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * CoverageControl library. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29#ifndef CPPSRC_CORE_INCLUDE_COVERAGECONTROL_MAP_UTILS_H_
30#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_MAP_UTILS_H_
31
32#include <cmath>
33#include <fstream>
34#include <iostream>
35#include <string>
36
38
39namespace CoverageControl {
43namespace MapUtils {
44struct MapBounds {
45 int left = 0, right = 0, bottom = 0, top = 0;
46 int width = 0, height = 0;
47 void SetZero() { left = 0, right = 0, bottom = 0, top = 0; }
48};
49
51inline void GetClosestGridCoordinate(double const resolution, Point2 const &pt,
52 int &idx, int &idy) {
53 idx = std::round(pt.x() / resolution);
54 idy = std::round(pt.y() / resolution);
55}
56
59inline void ComputeOffsets(double const resolution, Point2 const &pos,
60 int const submap_size, int const map_size,
61 MapBounds &index, MapBounds &offset) {
62 int pos_idx = 0, pos_idy = 0;
63 GetClosestGridCoordinate(resolution, pos, pos_idx, pos_idy);
64 index.left = pos_idx - submap_size / 2;
65 index.right = pos_idx + submap_size / 2;
66 index.bottom = pos_idy - submap_size / 2;
67 index.top = pos_idy + submap_size / 2;
68
69 offset.SetZero();
70 if (index.left < 0) {
71 offset.left = -index.left;
72 }
73 if (index.bottom < 0) {
74 offset.bottom = -index.bottom;
75 }
76
77 if (index.right > map_size) {
78 offset.right = map_size - index.right;
79 }
80 if (index.top > map_size) {
81 offset.top = map_size - index.top;
82 }
83
84 offset.width = index.right + offset.right - (index.left + offset.left);
85 offset.height = index.top + offset.top - (index.bottom + offset.bottom);
86}
87
88template <typename T = MapType>
89inline void GetSubMap(double const resolution, Point2 const &pos,
90 int const map_size, T const &map, int const submap_size,
91 T &submap) {
92 MapBounds index, offset;
93 ComputeOffsets(resolution, pos, submap_size, map_size, index, offset);
94 submap.block(offset.left, offset.bottom, offset.width, offset.height) =
95 map.block(index.left + offset.left, index.bottom + offset.bottom,
96 offset.width, offset.height);
97}
98
99template <typename T = MapType>
100inline auto GetSubMap(double const resolution, Point2 const &pos,
101 int const map_size, T const &map, int const submap_size) {
102 MapBounds index, offset;
103 ComputeOffsets(resolution, pos, submap_size, map_size, index, offset);
104 return map.block(index.left + offset.left, index.bottom + offset.bottom,
105 offset.width, offset.height);
106}
107
109inline int WriteMap(MapType const &map, std::string const &file_name) {
110 std::ofstream file_obj(file_name);
111 if (!file_obj) {
112 std::cerr << "[Error] Could not open " << file_name << " for writing."
113 << std::endl;
114 return 1;
115 }
116 file_obj << map;
117 file_obj.close();
118 return 0;
119}
120
122inline int WriteMapSparse(MapType const &map, std::string const &file_name) {
123 std::ofstream file_obj(file_name);
124 if (!file_obj) {
125 std::cerr << "[Error] Could not open " << file_name << " for writing."
126 << std::endl;
127 return 1;
128 }
129 for (int i = 0; i < map.rows(); ++i) {
130 for (int j = 0; j < map.cols(); ++j) {
131 if (map(i, j) >= 0) {
132 file_obj << i << " " << j << " " << map(i, j) << std::endl;
133 }
134 }
135 }
136 file_obj.close();
137 return 0;
138}
139
140inline int IsPointOutsideBoundary(double const resolution, Point2 const &pos,
141 int const sensor_size, int const boundary) {
142 if (pos.x() <= -sensor_size * resolution / 2.) {
143 return 1;
144 }
145 if (pos.y() <= -sensor_size * resolution / 2.) {
146 return 1;
147 }
148 if (pos.x() >= boundary * resolution + sensor_size * resolution / 2.) {
149 return 1;
150 }
151 if (pos.y() >= boundary * resolution + sensor_size * resolution / 2.) {
152 return 1;
153 }
154 return 0;
155}
156
157} /* namespace MapUtils */
158} /* namespace CoverageControl */
159#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_MAP_UTILS_H_
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MapType
Definition typedefs.h:48
Eigen::Vector2d Point2
Definition typedefs.h:44
int IsPointOutsideBoundary(double const resolution, Point2 const &pos, int const sensor_size, int const boundary)
Definition map_utils.h:140
void GetSubMap(double const resolution, Point2 const &pos, int const map_size, T const &map, int const submap_size, T &submap)
Definition map_utils.h:89
void ComputeOffsets(double const resolution, Point2 const &pos, int const submap_size, int const map_size, MapBounds &index, MapBounds &offset)
Definition map_utils.h:59
int WriteMap(MapType const &map, std::string const &file_name)
Write the world map to a file.
Definition map_utils.h:109
void GetClosestGridCoordinate(double const resolution, Point2 const &pt, int &idx, int &idy)
Gets the closest point on the grid.
Definition map_utils.h:51
int WriteMapSparse(MapType const &map, std::string const &file_name)
Write the world map to a file.
Definition map_utils.h:122
Namespace for the CoverageControl library.
Contains typedefs for the library.