Open3D (C++ API)  0.18.0+252c867
MiniVec.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cmath>
11 
12 #ifdef __CUDACC__
13 #define FN_SPECIFIERS inline __host__ __device__
14 #else
15 #define FN_SPECIFIERS inline
16 #endif
17 
18 namespace open3d {
19 namespace utility {
20 
23 template <class T, int N>
24 struct MiniVec {
25  typedef T Scalar_t;
26 
28 
29  template <class... TInit>
30  FN_SPECIFIERS explicit MiniVec(TInit... as) : arr{as...} {}
31 
32  FN_SPECIFIERS explicit MiniVec(const T* const ptr) {
33  for (int i = 0; i < N; ++i) operator[](i) = ptr[i];
34  }
35 
36  FN_SPECIFIERS const T operator[](size_t i) const { return arr[i]; }
37 
38  FN_SPECIFIERS T& operator[](size_t i) { return arr[i]; }
39 
40  template <class T2>
43  for (int i = 0; i < N; ++i) a[i] = T2(operator[](i));
44  return a;
45  }
46 
47  FN_SPECIFIERS T dot(const MiniVec<T, N>& a) const {
48  T result = 0;
49  for (int i = 0; i < N; ++i) result += operator[](i) * a[i];
50  return result;
51  }
52 
54  MiniVec<T, N> r;
55  for (int i = 0; i < N; ++i) r[i] = std::abs(operator[](i));
56  return r;
57  }
58 
59  FN_SPECIFIERS bool all() const {
60  bool result = true;
61  for (int i = 0; i < N && result; ++i) result = result && operator[](i);
62  return result;
63  }
64 
65  FN_SPECIFIERS bool any() const {
66  for (int i = 0; i < N; ++i)
67  if (operator[](i)) return true;
68  return false;
69  }
70 
71  T arr[N];
72 };
73 
74 template <int N>
77  for (int i = 0; i < N; ++i) r[i] = floorf(a[i]);
78  return r;
79 }
80 
81 template <int N>
84  for (int i = 0; i < N; ++i) r[i] = std::floor(a[i]);
85  return r;
86 }
87 
88 template <int N>
91  for (int i = 0; i < N; ++i) r[i] = ceilf(a[i]);
92  return r;
93 }
94 
95 template <int N>
98  for (int i = 0; i < N; ++i) r[i] = std::ceil(a[i]);
99  return r;
100 }
101 
102 template <class T, int N>
104  MiniVec<T, N> r;
105  for (int i = 0; i < N; ++i) r[i] = -a[i];
106  return r;
107 }
108 
109 template <class T, int N>
111  MiniVec<T, N> r;
112  for (int i = 0; i < N; ++i) r[i] = !a[i];
113  return r;
114 }
115 
116 #define DEFINE_OPERATOR(op, opas) \
117  template <class T, int N> \
118  FN_SPECIFIERS MiniVec<T, N> operator op(const MiniVec<T, N>& a, \
119  const MiniVec<T, N>& b) { \
120  MiniVec<T, N> c; \
121  for (int i = 0; i < N; ++i) c[i] = a[i] op b[i]; \
122  return c; \
123  } \
124  \
125  template <class T, int N> \
126  FN_SPECIFIERS void operator opas(MiniVec<T, N>& a, \
127  const MiniVec<T, N>& b) { \
128  for (int i = 0; i < N; ++i) a[i] opas b[i]; \
129  } \
130  \
131  template <class T, int N> \
132  FN_SPECIFIERS MiniVec<T, N> operator op(const MiniVec<T, N>& a, T b) { \
133  MiniVec<T, N> c; \
134  for (int i = 0; i < N; ++i) c[i] = a[i] op b; \
135  return c; \
136  } \
137  \
138  template <class T, int N> \
139  FN_SPECIFIERS MiniVec<T, N> operator op(T a, const MiniVec<T, N>& b) { \
140  MiniVec<T, N> c; \
141  for (int i = 0; i < N; ++i) c[i] = a op b[i]; \
142  return c; \
143  } \
144  \
145  template <class T, int N> \
146  FN_SPECIFIERS void operator opas(MiniVec<T, N>& a, T b) { \
147  for (int i = 0; i < N; ++i) a[i] opas b; \
148  }
149 
150 DEFINE_OPERATOR(+, +=)
151 DEFINE_OPERATOR(-, -=)
152 DEFINE_OPERATOR(*, *=)
153 DEFINE_OPERATOR(/, /=)
154 #undef DEFINE_OPERATOR
155 
156 #define DEFINE_OPERATOR(op) \
157  template <class T, int N> \
158  FN_SPECIFIERS MiniVec<bool, N> operator op(const MiniVec<T, N>& a, \
159  const MiniVec<T, N>& b) { \
160  MiniVec<bool, N> c; \
161  for (int i = 0; i < N; ++i) c[i] = a[i] op b[i]; \
162  return c; \
163  } \
164  \
165  template <class T, int N> \
166  FN_SPECIFIERS MiniVec<bool, N> operator op(const MiniVec<T, N>& a, T b) { \
167  MiniVec<T, N> c; \
168  for (int i = 0; i < N; ++i) c[i] = a[i] op b; \
169  return c; \
170  } \
171  \
172  template <class T, int N> \
173  FN_SPECIFIERS MiniVec<T, N> operator op(T a, const MiniVec<T, N>& b) { \
174  MiniVec<bool, N> c; \
175  for (int i = 0; i < N; ++i) c[i] = a op b[i]; \
176  return c; \
177  }
178 
180 DEFINE_OPERATOR(<=)
182 DEFINE_OPERATOR(>=)
183 DEFINE_OPERATOR(==)
184 DEFINE_OPERATOR(!=)
185 DEFINE_OPERATOR(&&)
186 DEFINE_OPERATOR(||)
187 #undef DEFINE_OPERATOR
188 #undef FN_SPECIFIERS
189 
190 } // namespace utility
191 } // namespace open3d
#define DEFINE_OPERATOR(op, opas)
Definition: MiniVec.h:156
#define FN_SPECIFIERS
Definition: MiniVec.h:15
core::Tensor result
Definition: VtkUtils.cpp:75
FN_SPECIFIERS MiniVec< T, N > operator!(const MiniVec< T, N > &a)
Definition: MiniVec.h:110
FN_SPECIFIERS MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
FN_SPECIFIERS MiniVec< double, N > floor(const MiniVec< double, N > &a)
Definition: MiniVec.h:82
FN_SPECIFIERS MiniVec< double, N > ceil(const MiniVec< double, N > &a)
Definition: MiniVec.h:96
FN_SPECIFIERS MiniVec< float, N > floor(const MiniVec< float, N > &a)
Definition: MiniVec.h:75
FN_SPECIFIERS MiniVec< T, N > operator-(const MiniVec< T, N > &a)
Definition: MiniVec.h:103
Definition: PinholeCameraIntrinsic.cpp:16
Definition: MiniVec.h:24
FN_SPECIFIERS MiniVec(const T *const ptr)
Definition: MiniVec.h:32
FN_SPECIFIERS bool all() const
Definition: MiniVec.h:59
T arr[N]
Definition: MiniVec.h:71
FN_SPECIFIERS MiniVec()
Definition: MiniVec.h:27
T Scalar_t
Definition: MiniVec.h:25
FN_SPECIFIERS T dot(const MiniVec< T, N > &a) const
Definition: MiniVec.h:47
FN_SPECIFIERS MiniVec< T2, N > cast() const
Definition: MiniVec.h:41
FN_SPECIFIERS bool any() const
Definition: MiniVec.h:65
FN_SPECIFIERS MiniVec< T, N > abs() const
Definition: MiniVec.h:53
FN_SPECIFIERS MiniVec(TInit... as)
Definition: MiniVec.h:30
FN_SPECIFIERS T & operator[](size_t i)
Definition: MiniVec.h:38
FN_SPECIFIERS const T operator[](size_t i) const
Definition: MiniVec.h:36