Open3D (C++ API)  0.12.0
MiniVec.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <cmath>
30 
31 #ifdef __CUDACC__
32 #define FN_SPECIFIERS inline __host__ __device__
33 #else
34 #define FN_SPECIFIERS inline
35 #endif
36 
37 namespace open3d {
38 namespace utility {
39 
42 template <class T, int N>
43 struct MiniVec {
44  typedef T Scalar_t;
45 
47 
48  template <class... TInit>
49  FN_SPECIFIERS explicit MiniVec(TInit... as) : arr{as...} {}
50 
51  FN_SPECIFIERS explicit MiniVec(const T* const ptr) {
52  for (int i = 0; i < N; ++i) operator[](i) = ptr[i];
53  }
54 
55  FN_SPECIFIERS const T operator[](size_t i) const { return arr[i]; }
56 
57  FN_SPECIFIERS T& operator[](size_t i) { return arr[i]; }
58 
59  template <class T2>
62  for (int i = 0; i < N; ++i) a[i] = T2(operator[](i));
63  return a;
64  }
65 
66  FN_SPECIFIERS T dot(const MiniVec<T, N>& a) const {
67  T result = 0;
68  for (int i = 0; i < N; ++i) result += operator[](i) * a[i];
69  return result;
70  }
71 
73  MiniVec<T, N> r;
74  for (int i = 0; i < N; ++i) r[i] = std::abs(operator[](i));
75  return r;
76  }
77 
78  FN_SPECIFIERS bool all() const {
79  bool result = true;
80  for (int i = 0; i < N && result; ++i) result = result && operator[](i);
81  return result;
82  }
83 
84  FN_SPECIFIERS bool any() const {
85  for (int i = 0; i < N; ++i)
86  if (operator[](i)) return true;
87  return false;
88  }
89 
90  T arr[N];
91 };
92 
93 template <int N>
96  for (int i = 0; i < N; ++i) r[i] = floorf(a[i]);
97  return r;
98 }
99 
100 template <int N>
103  for (int i = 0; i < N; ++i) r[i] = std::floor(a[i]);
104  return r;
105 }
106 
107 template <int N>
110  for (int i = 0; i < N; ++i) r[i] = ceilf(a[i]);
111  return r;
112 }
113 
114 template <int N>
117  for (int i = 0; i < N; ++i) r[i] = std::ceil(a[i]);
118  return r;
119 }
120 
121 template <class T, int N>
123  MiniVec<T, N> r;
124  for (int i = 0; i < N; ++i) r[i] = -a[i];
125  return r;
126 }
127 
128 template <class T, int N>
130  MiniVec<T, N> r;
131  for (int i = 0; i < N; ++i) r[i] = !a[i];
132  return r;
133 }
134 
135 #define DEFINE_OPERATOR(op, opas) \
136  template <class T, int N> \
137  FN_SPECIFIERS MiniVec<T, N> operator op(const MiniVec<T, N>& a, \
138  const MiniVec<T, N>& b) { \
139  MiniVec<T, N> c; \
140  for (int i = 0; i < N; ++i) c[i] = a[i] op b[i]; \
141  return c; \
142  } \
143  \
144  template <class T, int N> \
145  FN_SPECIFIERS void operator opas(MiniVec<T, N>& a, \
146  const MiniVec<T, N>& b) { \
147  for (int i = 0; i < N; ++i) a[i] opas b[i]; \
148  } \
149  \
150  template <class T, int N> \
151  FN_SPECIFIERS MiniVec<T, N> operator op(const MiniVec<T, N>& a, T b) { \
152  MiniVec<T, N> c; \
153  for (int i = 0; i < N; ++i) c[i] = a[i] op b; \
154  return c; \
155  } \
156  \
157  template <class T, int N> \
158  FN_SPECIFIERS MiniVec<T, N> operator op(T a, const MiniVec<T, N>& b) { \
159  MiniVec<T, N> c; \
160  for (int i = 0; i < N; ++i) c[i] = a op b[i]; \
161  return c; \
162  } \
163  \
164  template <class T, int N> \
165  FN_SPECIFIERS void operator opas(MiniVec<T, N>& a, T b) { \
166  for (int i = 0; i < N; ++i) a[i] opas b; \
167  }
168 
169 DEFINE_OPERATOR(+, +=)
170 DEFINE_OPERATOR(-, -=)
171 DEFINE_OPERATOR(*, *=)
172 DEFINE_OPERATOR(/, /=)
173 #undef DEFINE_OPERATOR
174 
175 #define DEFINE_OPERATOR(op) \
176  template <class T, int N> \
177  FN_SPECIFIERS MiniVec<bool, N> operator op(const MiniVec<T, N>& a, \
178  const MiniVec<T, N>& b) { \
179  MiniVec<bool, N> c; \
180  for (int i = 0; i < N; ++i) c[i] = a[i] op b[i]; \
181  return c; \
182  } \
183  \
184  template <class T, int N> \
185  FN_SPECIFIERS MiniVec<bool, N> operator op(const MiniVec<T, N>& a, T b) { \
186  MiniVec<T, N> c; \
187  for (int i = 0; i < N; ++i) c[i] = a[i] op b; \
188  return c; \
189  } \
190  \
191  template <class T, int N> \
192  FN_SPECIFIERS MiniVec<T, N> operator op(T a, const MiniVec<T, N>& b) { \
193  MiniVec<bool, N> c; \
194  for (int i = 0; i < N; ++i) c[i] = a op b[i]; \
195  return c; \
196  }
197 
199 DEFINE_OPERATOR(<=)
201 DEFINE_OPERATOR(>=)
202 DEFINE_OPERATOR(==)
203 DEFINE_OPERATOR(!=)
204 DEFINE_OPERATOR(&&)
205 DEFINE_OPERATOR(||)
206 #undef DEFINE_OPERATOR
207 #undef FN_SPECIFIERS
208 
209 } // namespace utility
210 } // namespace open3d
T Scalar_t
Definition: MiniVec.h:44
FN_SPECIFIERS MiniVec(TInit... as)
Definition: MiniVec.h:49
FN_SPECIFIERS T & operator[](size_t i)
Definition: MiniVec.h:57
FN_SPECIFIERS const T operator[](size_t i) const
Definition: MiniVec.h:55
#define FN_SPECIFIERS
Definition: MiniVec.h:34
FN_SPECIFIERS MiniVec< T, N > operator!(const MiniVec< T, N > &a)
Definition: MiniVec.h:129
FN_SPECIFIERS MiniVec< double, N > floor(const MiniVec< double, N > &a)
Definition: MiniVec.h:101
FN_SPECIFIERS MiniVec< T, N > operator-(const MiniVec< T, N > &a)
Definition: MiniVec.h:122
FN_SPECIFIERS MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:108
FN_SPECIFIERS MiniVec< float, N > floor(const MiniVec< float, N > &a)
Definition: MiniVec.h:94
FN_SPECIFIERS MiniVec< T, N > abs() const
Definition: MiniVec.h:72
FN_SPECIFIERS bool all() const
Definition: MiniVec.h:78
FN_SPECIFIERS bool any() const
Definition: MiniVec.h:84
FN_SPECIFIERS T dot(const MiniVec< T, N > &a) const
Definition: MiniVec.h:66
Definition: PinholeCameraIntrinsic.cpp:35
#define DEFINE_OPERATOR(op, opas)
Definition: MiniVec.h:175
FN_SPECIFIERS MiniVec()
Definition: MiniVec.h:46
FN_SPECIFIERS MiniVec(const T *const ptr)
Definition: MiniVec.h:51
Definition: MiniVec.h:43
FN_SPECIFIERS MiniVec< double, N > ceil(const MiniVec< double, N > &a)
Definition: MiniVec.h:115
FN_SPECIFIERS MiniVec< T2, N > cast() const
Definition: MiniVec.h:60
T arr[N]
Definition: MiniVec.h:90