blockmem_gridmap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2017, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef PLANNER_CSPACE_BLOCKMEM_GRIDMAP_H
31 #define PLANNER_CSPACE_BLOCKMEM_GRIDMAP_H
32 
33 #include <limits>
34 
36 
37 template <class T, int DIM, int NONCYCLIC, int BLOCK_WIDTH = 0x20>
39 {
40 protected:
41  std::unique_ptr<T[]> c_;
44  size_t ser_size_;
46  size_t block_num_;
47  T dummy_;
48 
49  void block_addr(
50  const CyclicVecInt<DIM, NONCYCLIC>& pos, size_t& baddr, size_t& addr) const
51  {
52  addr = 0;
53  baddr = 0;
54  for (int i = 0; i < NONCYCLIC; i++)
55  {
56  addr *= BLOCK_WIDTH;
57  addr += pos[i] % BLOCK_WIDTH;
58  baddr *= block_size_[i];
59  baddr += pos[i] / BLOCK_WIDTH;
60  }
61  for (int i = NONCYCLIC; i < DIM; i++)
62  {
63  addr *= size_[i];
64  addr += pos[i];
65  }
66  }
67 
68 public:
69  std::function<void(CyclicVecInt<3, 2>, size_t&, size_t&)> getAddressor() const
70  {
71  return std::bind(
73  this,
74  std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
75  }
77  {
78  return size_;
79  }
80  size_t ser_size() const
81  {
82  return ser_size_;
83  }
84  void clear(const T zero)
85  {
86  for (size_t i = 0; i < ser_size_; i++)
87  {
88  c_[i] = zero;
89  }
90  }
91  void clear_positive(const T zero)
92  {
93  for (size_t i = 0; i < ser_size_; i++)
94  {
95  if (c_[i] >= 0)
96  c_[i] = zero;
97  }
98  }
100  {
101  auto size_tmp = size;
102 
103  for (int i = 0; i < NONCYCLIC; i++)
104  {
105  if (size_tmp[i] < BLOCK_WIDTH)
106  size_tmp[i] = BLOCK_WIDTH;
107  }
108 
109  block_ser_size_ = 1;
110  block_num_ = 1;
111  for (int i = 0; i < DIM; i++)
112  {
113  int width;
114  if (i < NONCYCLIC)
115  {
116  width = BLOCK_WIDTH;
117  block_size_[i] = (size_tmp[i] + width - 1) / width;
118  }
119  else
120  {
121  width = size_tmp[i];
122  block_size_[i] = 1;
123  }
124 
125  block_ser_size_ *= width;
126  block_num_ *= block_size_[i];
127  }
128  ser_size_ = block_ser_size_ * block_num_;
129 
130  c_.reset(new T[ser_size_]);
131  size_ = size;
132  }
134  {
135  reset(size_);
136  }
138  {
139  }
141  {
142  size_t baddr, addr;
143  block_addr(pos, baddr, addr);
144  if (addr >= block_ser_size_ || baddr >= block_num_)
145  {
146  dummy_ = std::numeric_limits<T>::max();
147  return dummy_;
148  }
149  return c_[baddr * block_ser_size_ + addr];
150  }
151  const T operator[](const CyclicVecInt<DIM, NONCYCLIC>& pos) const
152  {
153  size_t baddr, addr;
154  block_addr(pos, baddr, addr);
155  if (addr >= block_ser_size_ || baddr >= block_num_)
156  return std::numeric_limits<T>::max();
157  return c_[baddr * block_ser_size_ + addr];
158  }
159  bool validate(const CyclicVecInt<DIM, NONCYCLIC>& pos, const int tolerance = 0) const
160  {
161  for (int i = 0; i < NONCYCLIC; i++)
162  {
163  if (pos[i] < tolerance || size_[i] - tolerance <= pos[i])
164  return false;
165  }
166  for (int i = NONCYCLIC; i < DIM; i++)
167  {
168  if (pos[i] < 0 || size_[i] <= pos[i])
169  return false;
170  }
171  return true;
172  }
175  {
176  reset(gm.size_);
177  memcpy(c_.get(), gm.c_.get(), ser_size_);
178 
179  return *this;
180  }
181 };
182 
183 #endif // PLANNER_CSPACE_BLOCKMEM_GRIDMAP_H
void reset(const CyclicVecInt< DIM, NONCYCLIC > &size)
BlockMemGridmap(const CyclicVecInt< DIM, NONCYCLIC > &size_)
CyclicVecInt< DIM, NONCYCLIC > block_size_
bool validate(const CyclicVecInt< DIM, NONCYCLIC > &pos, const int tolerance=0) const
void clear(const T zero)
void block_addr(const CyclicVecInt< DIM, NONCYCLIC > &pos, size_t &baddr, size_t &addr) const
size_t ser_size() const
std::unique_ptr< T[]> c_
const CyclicVecInt< DIM, NONCYCLIC > & size() const
CyclicVecInt< DIM, NONCYCLIC > size_
const BlockMemGridmap< T, DIM, NONCYCLIC, BLOCK_WIDTH > & operator=(const BlockMemGridmap< T, DIM, NONCYCLIC, BLOCK_WIDTH > &gm)
T & operator[](const CyclicVecInt< DIM, NONCYCLIC > &pos)
void clear_positive(const T zero)
std::function< void(CyclicVecInt< 3, 2 >, size_t &, size_t &)> getAddressor() const
const T operator[](const CyclicVecInt< DIM, NONCYCLIC > &pos) const


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Tue Jul 9 2019 05:00:13