cyclic_vec.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_CYCLIC_VEC_H
31 #define PLANNER_CSPACE_CYCLIC_VEC_H
32 
33 #include <memory>
34 #define _USE_MATH_DEFINES
35 #include <cmath>
36 #include <cfloat>
37 #include <initializer_list>
38 #include <list>
39 #include <map>
40 #include <unordered_map>
41 #include <vector>
42 
43 #include <boost/chrono.hpp>
44 
46 
48 {
49 template <typename T>
50 void convert(const T val, float& ret)
51 {
52  ret = val;
53 }
54 inline void convert(const int val, float& ret)
55 {
56  ret = val;
57 }
58 inline void convert(const float val, int& ret)
59 {
60  ret = lroundf(val);
61 }
62 
63 inline void normalizeFloatAngle(float& val)
64 {
65  if (val > M_PI)
66  val -= 2 * M_PI;
67  else if (val < -M_PI)
68  val += 2 * M_PI;
69 }
70 inline void normalizeFloatAngle(int&)
71 {
72 }
73 } // namespace cyclic_vec_type_conversion_rule
74 
75 template <int DIM, int NONCYCLIC, typename T>
77 {
78 protected:
79  static_assert(
80  std::is_same<float, T>() || std::is_same<int, T>(), "T must be float or int");
81  T e_[DIM];
82 
83  template <typename T2, typename... ArgList>
84  void setElements(const int i, const T2& first, const ArgList&... rest) noexcept
85  {
86  assert(i < DIM);
87 
89  setElements(i + 1, rest...);
90  }
91  void setElements(const int i) noexcept
92  {
93  assert(i == DIM);
94  }
95 
96  template <typename... ArgList>
98  const int i,
99  const int res, const ArgList&... rest)
100  {
101  assert(i < DIM);
102 
103  e_[i] = e_[i] % res;
104  if (e_[i] < res / 2 - res)
105  e_[i] += res;
106  else if (e_[i] >= res / 2)
107  e_[i] -= res;
108 
109  cycleElements(i + 1, rest...);
110  }
111  void cycleElements(const int i)
112  {
113  assert(i == DIM);
114  }
115  template <typename... ArgList>
117  const int i,
118  const int res, const ArgList&... rest)
119  {
120  assert(i < DIM);
121 
122  e_[i] = e_[i] % res;
123  if (e_[i] < 0)
124  e_[i] += res;
125 
126  cycleUnsignedElements(i + 1, rest...);
127  }
128  void cycleUnsignedElements(const int i)
129  {
130  assert(i == DIM);
131  }
132 
133 public:
134  template <typename T2>
136  {
137  for (int i = 0; i < DIM; i++)
139  }
140  template <typename... ArgList>
141  explicit CyclicVecBase(const float& v, const ArgList&... args) noexcept
142  {
143  setElements(0, v, args...);
144  }
145  template <typename... ArgList>
146  explicit CyclicVecBase(const int& v, const ArgList&... args) noexcept
147  {
148  setElements(0, v, args...);
149  }
150  CyclicVecBase() noexcept
151  {
152  }
153  template <typename T2>
155  {
156  for (int i = 0; i < DIM; i++)
157  if (v.e_[i] != e_[i])
158  return false;
159  return true;
160  }
161  template <typename T2>
163  {
164  return !(*this == v);
165  }
166  template <typename T2>
168  {
170  for (int i = 0; i < DIM; i++)
171  {
172  out[i] += v[i];
173  }
174  return out;
175  }
176  template <typename T2>
178  {
180  for (int i = 0; i < DIM; i++)
181  {
182  out[i] -= v[i];
183  }
184  return out;
185  }
186  template <typename T2>
188  {
190  for (int i = 0; i < DIM; i++)
191  {
192  out[i] = e_[i] * v[i];
193  }
194  return out;
195  }
197  {
198  return (*this)[0] * a[1] - (*this)[1] * a[0];
199  }
201  {
202  return (*this)[0] * a[0] + (*this)[1] * a[1];
203  }
204  float distLine2d(
206  const CyclicVecBase<DIM, NONCYCLIC, T>& b) const
207  {
208  return (b - a).cross2d((*this) - a) / (b - a).len();
209  }
212  const CyclicVecBase<DIM, NONCYCLIC, T>& b) const
213  {
214  const auto to_a = (*this) - a;
215  if ((b - a).dot2d(to_a) <= 0)
216  return to_a.len();
217  const auto to_b = (*this) - b;
218  if ((a - b).dot2d(to_b) <= 0)
219  return to_b.len();
220  return fabs(distLine2d(a, b));
221  }
222  T& operator[](const int& x)
223  {
224  return e_[x];
225  }
226  const T& operator[](const int& x) const
227  {
228  return e_[x];
229  }
230  T sqlen() const
231  {
232  T out = 0;
233  for (int i = 0; i < NONCYCLIC; i++)
234  {
235  out += e_[i] * e_[i];
236  }
237  return out;
238  }
239  float len() const
240  {
241  return sqrtf(sqlen());
242  }
243  float gridToLenFactor() const
244  {
245  const auto l = len();
246  return l / static_cast<int>(l);
247  }
248  float norm() const
249  {
250  float out = 0;
251  for (int i = 0; i < DIM; i++)
252  {
253  out += powf(e_[i], 2.0);
254  }
255  return sqrtf(out);
256  }
257 
258  void rotate(const float ang)
259  {
260  const auto tmp = *this;
261  const float cos_v = cosf(ang);
262  const float sin_v = sinf(ang);
263 
264  cyclic_vec_type_conversion_rule::convert(cos_v * tmp[0] - sin_v * tmp[1], e_[0]);
265  cyclic_vec_type_conversion_rule::convert(sin_v * tmp[0] + cos_v * tmp[1], e_[1]);
266  this->e_[2] = tmp[2] + ang;
268  }
269 
270  // Cyclic operations
271  template <typename... ArgList>
272  void cycle(const int res, const ArgList&... rest)
273  {
274  static_assert(
275  std::is_same<int, T>(), "cycle is provided only for int");
276  cycleElements(NONCYCLIC, res, rest...);
277  }
278  template <typename... ArgList>
279  void cycleUnsigned(const int res, const ArgList&... rest)
280  {
281  static_assert(
282  std::is_same<int, T>(), "cycle is provided only for int");
283  cycleUnsignedElements(NONCYCLIC, res, rest...);
284  }
286  {
287  static_assert(
288  std::is_same<int, T>(), "cycle is provided only for int");
289  for (int i = NONCYCLIC; i < DIM; ++i)
290  cycleElements(i, res[i]);
291  }
293  {
294  static_assert(
295  std::is_same<int, T>(), "cycle is provided only for int");
296  for (int i = NONCYCLIC; i < DIM; ++i)
297  cycleUnsignedElements(i, res[i]);
298  }
299 
300  // Hash
301  size_t operator()(const CyclicVecBase& key) const
302  {
303  size_t hash = static_cast<size_t>(key.e_[0]);
304  for (int i = 1; i < DIM; i++)
305  {
306  const size_t n = 8 * sizeof(std::size_t) * i / DIM;
307  const size_t x = static_cast<size_t>(key.e_[i]);
308  hash ^= (x << n) | (x >> ((8 * sizeof(size_t)) - n));
309  }
310  return hash;
311  }
312 
314  {
315  static_assert(
316  std::is_same<int, T>(), "isExceeded is provided only for T=int");
317  for (int i = 0; i < NONCYCLIC; ++i)
318  {
319  if (static_cast<unsigned int>((*this)[i]) >= static_cast<unsigned int>(v[i]))
320  return true;
321  }
322  return false;
323  }
324 };
325 
326 template <int DIM, int NONCYCLIC>
328 template <int DIM, int NONCYCLIC>
330 
331 #endif // PLANNER_CSPACE_CYCLIC_VEC_H
size_t operator()(const CyclicVecBase &key) const
Definition: cyclic_vec.h:301
const T & operator[](const int &x) const
Definition: cyclic_vec.h:226
void cycleUnsigned(const int res, const ArgList &...rest)
Definition: cyclic_vec.h:279
CyclicVecBase< DIM, NONCYCLIC, T2 > operator+(const CyclicVecBase< DIM, NONCYCLIC, T2 > &v) const
Definition: cyclic_vec.h:167
void cycleElements(const int i, const int res, const ArgList &...rest)
Definition: cyclic_vec.h:97
void rotate(const float ang)
Definition: cyclic_vec.h:258
bool operator==(const CyclicVecBase< DIM, NONCYCLIC, T2 > &v) const
Definition: cyclic_vec.h:154
T sqlen() const
Definition: cyclic_vec.h:230
float len() const
Definition: cyclic_vec.h:239
float distLinestrip2d(const CyclicVecBase< DIM, NONCYCLIC, T > &a, const CyclicVecBase< DIM, NONCYCLIC, T > &b) const
Definition: cyclic_vec.h:210
CyclicVecBase(const CyclicVecBase< DIM, NONCYCLIC, T2 > &c) noexcept
Definition: cyclic_vec.h:135
void cycleUnsigned(const CyclicVecBase< DIM, NONCYCLIC, T > &res)
Definition: cyclic_vec.h:292
T & operator[](const int &x)
Definition: cyclic_vec.h:222
void cycleElements(const int i)
Definition: cyclic_vec.h:111
float norm() const
Definition: cyclic_vec.h:248
bool operator!=(const CyclicVecBase< DIM, NONCYCLIC, T2 > &v) const
Definition: cyclic_vec.h:162
CyclicVecBase< DIM, NONCYCLIC, T2 > operator*(const CyclicVecBase< DIM, NONCYCLIC, T2 > &v) const
Definition: cyclic_vec.h:187
float distLine2d(const CyclicVecBase< DIM, NONCYCLIC, T > &a, const CyclicVecBase< DIM, NONCYCLIC, T > &b) const
Definition: cyclic_vec.h:204
TFSIMD_FORCE_INLINE const tfScalar & x() const
bool isExceeded(const CyclicVecBase< DIM, NONCYCLIC, int > &v) const
Definition: cyclic_vec.h:313
void cycle(const CyclicVecBase< DIM, NONCYCLIC, T > &res)
Definition: cyclic_vec.h:285
void setElements(const int i, const T2 &first, const ArgList &...rest) noexcept
Definition: cyclic_vec.h:84
float dot2d(const CyclicVecBase< DIM, NONCYCLIC, T > &a) const
Definition: cyclic_vec.h:200
float gridToLenFactor() const
Definition: cyclic_vec.h:243
CyclicVecBase< DIM, NONCYCLIC, T2 > operator-(const CyclicVecBase< DIM, NONCYCLIC, T2 > &v) const
Definition: cyclic_vec.h:177
void cycleUnsignedElements(const int i, const int res, const ArgList &...rest)
Definition: cyclic_vec.h:116
void normalizeFloatAngle(float &val)
Definition: cyclic_vec.h:63
float cross2d(const CyclicVecBase< DIM, NONCYCLIC, T > &a) const
Definition: cyclic_vec.h:196
void convert(const T val, float &ret)
Definition: cyclic_vec.h:50
void setElements(const int i) noexcept
Definition: cyclic_vec.h:91
void cycleUnsignedElements(const int i)
Definition: cyclic_vec.h:128
CyclicVecBase() noexcept
Definition: cyclic_vec.h:150
void cycle(const int res, const ArgList &...rest)
Definition: cyclic_vec.h:272
CyclicVecBase(const float &v, const ArgList &...args) noexcept
Definition: cyclic_vec.h:141
CyclicVecBase(const int &v, const ArgList &...args) noexcept
Definition: cyclic_vec.h:146


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