bounds.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <nav_2d_utils/bounds.h>
37 #include <algorithm>
38 #include <stdexcept>
39 #include <vector>
40 
41 namespace nav_2d_utils
42 {
44 {
45  return nav_core2::Bounds(info.origin_x, info.origin_y,
46  info.origin_x + info.resolution * info.width, info.origin_y + info.resolution * info.height);
47 }
48 
50 {
51  // bounds are inclusive, so we subtract one
52  return nav_core2::UIntBounds(0, 0, info.width - 1, info.height - 1);
53 }
54 
56 {
57  unsigned int g_min_x, g_min_y, g_max_x, g_max_y;
58  worldToGridBounded(info, bounds.getMinX(), bounds.getMinY(), g_min_x, g_min_y);
59  worldToGridBounded(info, bounds.getMaxX(), bounds.getMaxY(), g_max_x, g_max_y);
60  return nav_core2::UIntBounds(g_min_x, g_min_y, g_max_x, g_max_y);
61 }
62 
64 {
65  double min_x, min_y, max_x, max_y;
66  gridToWorld(info, bounds.getMinX(), bounds.getMinY(), min_x, min_y);
67  gridToWorld(info, bounds.getMaxX(), bounds.getMaxY(), max_x, max_y);
68  return nav_core2::Bounds(min_x, min_y, max_x, max_y);
69 }
70 
71 std::vector<nav_core2::UIntBounds> divideBounds(const nav_core2::UIntBounds& original_bounds,
72  unsigned int n_cols, unsigned int n_rows)
73 {
74  if (n_cols * n_rows == 0)
75  {
76  throw std::invalid_argument("Number of rows and columns must be positive (not zero)");
77  }
78  unsigned int full_width = original_bounds.getWidth(),
79  full_height = original_bounds.getHeight();
80 
81  unsigned int small_width = static_cast<unsigned int>(ceil(static_cast<double>(full_width) / n_cols)),
82  small_height = static_cast<unsigned int>(ceil(static_cast<double>(full_height) / n_rows));
83 
84  std::vector<nav_core2::UIntBounds> divided;
85 
86  for (unsigned int row = 0; row < n_rows; row++)
87  {
88  unsigned int min_y = original_bounds.getMinY() + small_height * row;
89  unsigned int max_y = std::min(min_y + small_height - 1, original_bounds.getMaxY());
90 
91  for (unsigned int col = 0; col < n_cols; col++)
92  {
93  unsigned int min_x = original_bounds.getMinX() + small_width * col;
94  unsigned int max_x = std::min(min_x + small_width - 1, original_bounds.getMaxX());
95  nav_core2::UIntBounds sub(min_x, min_y, max_x, max_y);
96  if (!sub.isEmpty())
97  divided.push_back(sub);
98  }
99  }
100  return divided;
101 }
102 } // namespace nav_2d_utils
NumericType getMaxX() const
A set of utility functions for Bounds objects interacting with other messages/types.
Definition: bounds.h:45
NumericType getMinX() const
NumericType getMinY() const
std::vector< nav_core2::UIntBounds > divideBounds(const nav_core2::UIntBounds &original_bounds, unsigned int n_cols, unsigned int n_rows)
divide the given bounds up into sub-bounds of roughly equal size
Definition: bounds.cpp:71
void gridToWorld(const NavGridInfo &info, int mx, int my, double &wx, double &wy)
GenericBounds< double > Bounds
nav_core2::UIntBounds translateBounds(const nav_grid::NavGridInfo &info, const nav_core2::Bounds &bounds)
Translate real-valued bounds to uint coordinates based on nav_grid info.
Definition: bounds.cpp:55
unsigned int getHeight() const
nav_core2::UIntBounds getFullUIntBounds(const nav_grid::NavGridInfo &info)
return an integral bounds that covers the entire NavGrid
Definition: bounds.cpp:49
nav_core2::Bounds getFullBounds(const nav_grid::NavGridInfo &info)
return a floating point bounds that covers the entire NavGrid
Definition: bounds.cpp:43
unsigned int getWidth() const
NumericType getMaxY() const
bool worldToGridBounded(const NavGridInfo &info, double wx, double wy, unsigned int &mx, unsigned int &my)


nav_2d_utils
Author(s):
autogenerated on Sun Jan 10 2021 04:08:32