Open3D (C++ API)  0.11.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  FN_SPECIFIERS T& operator[](size_t i) { return arr[i]; }
57  template <class T2>
60  for (int i = 0; i < N; ++i) a[i] = T2(operator[](i));
61  return a;
62  }
63 
64  FN_SPECIFIERS T dot(const MiniVec<T, N>& a) const {
65  T result = 0;
66  for (int i = 0; i < N; ++i) result += operator[](i) * a[i];
67  return result;
68  }
69 
72  for (int i = 0; i < N; ++i) r[i] = std::abs(operator[](i));
73  return r;
74  }
75 
76  T arr[N];
77 };
78 
79 template <int N>
82  for (int i = 0; i < N; ++i) r[i] = floorf(a[i]);
83  return r;
84 }
85 
86 template <int N>
89  for (int i = 0; i < N; ++i) r[i] = std::floor(a[i]);
90  return r;
91 }
92 
93 template <class T, int N>
95  bool result = true;
96  for (int i = 0; i < N && result; ++i) result = result && a[i] == b[i];
97  return result;
98 }
99 
100 template <class T, int N>
102  return !(a == b);
103 }
104 
105 template <class T, int N>
107  MiniVec<T, N> r;
108  for (int i = 0; i < N; ++i) r[i] = -a[i];
109  return r;
110 }
111 
112 #define DEFINE_OPERATOR(op, opas) \
113  template <class T, int N> \
114  FN_SPECIFIERS MiniVec<T, N> operator op(const MiniVec<T, N>& a, \
115  const MiniVec<T, N>& b) { \
116  MiniVec<T, N> c; \
117  for (int i = 0; i < N; ++i) c[i] = a[i] op b[i]; \
118  return c; \
119  } \
120  \
121  template <class T, int N> \
122  FN_SPECIFIERS void operator opas(MiniVec<T, N>& a, \
123  const MiniVec<T, N>& b) { \
124  for (int i = 0; i < N; ++i) a[i] opas b[i]; \
125  } \
126  \
127  template <class T, int N> \
128  FN_SPECIFIERS MiniVec<T, N> operator op(const MiniVec<T, N>& a, T b) { \
129  MiniVec<T, N> c; \
130  for (int i = 0; i < N; ++i) c[i] = a[i] op b; \
131  return c; \
132  } \
133  \
134  template <class T, int N> \
135  FN_SPECIFIERS MiniVec<T, N> operator op(T a, const MiniVec<T, N>& b) { \
136  MiniVec<T, N> c; \
137  for (int i = 0; i < N; ++i) c[i] = a op b[i]; \
138  return c; \
139  } \
140  \
141  template <class T, int N> \
142  FN_SPECIFIERS void operator opas(MiniVec<T, N>& a, T b) { \
143  for (int i = 0; i < N; ++i) a[i] opas b; \
144  }
145 
146 DEFINE_OPERATOR(+, +=)
147 DEFINE_OPERATOR(-, -=)
148 DEFINE_OPERATOR(*, *=)
149 DEFINE_OPERATOR(/, /=)
150 #undef DEFINE_OPERATOR
151 #undef FN_SPECIFIERS
152 
153 } // namespace utility
154 } // namespace open3d
T Scalar_t
Definition: MiniVec.h:44
FN_SPECIFIERS MiniVec(TInit... as)
Definition: MiniVec.h:49
FN_SPECIFIERS bool operator!=(const MiniVec< T, N > &a, const MiniVec< T, N > &b)
Definition: MiniVec.h:101
FN_SPECIFIERS T & operator[](size_t i)
Definition: MiniVec.h:56
FN_SPECIFIERS const T operator[](size_t i) const
Definition: MiniVec.h:55
#define FN_SPECIFIERS
Definition: MiniVec.h:34
FN_SPECIFIERS MiniVec< double, N > floor(const MiniVec< double, N > &a)
Definition: MiniVec.h:87
FN_SPECIFIERS MiniVec< T, N > operator-(const MiniVec< T, N > &a)
Definition: MiniVec.h:106
FN_SPECIFIERS MiniVec< float, N > floor(const MiniVec< float, N > &a)
Definition: MiniVec.h:80
FN_SPECIFIERS MiniVec< T, N > abs() const
Definition: MiniVec.h:70
FN_SPECIFIERS T dot(const MiniVec< T, N > &a) const
Definition: MiniVec.h:64
Definition: PinholeCameraIntrinsic.cpp:35
#define DEFINE_OPERATOR(op, opas)
Definition: MiniVec.h:112
FN_SPECIFIERS MiniVec()
Definition: MiniVec.h:46
FN_SPECIFIERS MiniVec(const T *const ptr)
Definition: MiniVec.h:51
Definition: MiniVec.h:43
FN_SPECIFIERS bool operator==(const MiniVec< T, N > &a, const MiniVec< T, N > &b)
Definition: MiniVec.h:94
FN_SPECIFIERS MiniVec< T2, N > cast() const
Definition: MiniVec.h:58
T arr[N]
Definition: MiniVec.h:76