NuriKit v0.1.0b2
Loading...
Searching...
No Matches
eigen_config.h
Go to the documentation of this file.
1//
2// Project NuriKit - Copyright 2023 SNU Compbio Lab.
3// SPDX-License-Identifier: Apache-2.0
4//
5#ifndef NURI_EIGEN_CONFIG_H_
6#define NURI_EIGEN_CONFIG_H_
7
9#include <cstdint>
10#include <type_traits> // IWYU pragma: keep, required for is_class_v
11
12#include <absl/base/optimization.h>
13#include <absl/log/absl_check.h>
14#include <Eigen/Dense>
16
17#include "nuri/meta.h"
18
20
21#ifdef NURI_DEBUG
22#define NURI_EIGEN_TMP(type) Eigen::type
23#else
24#define NURI_EIGEN_TMP(type) \
25 { static_assert(std::is_class_v<Eigen::type>); } \
26 auto
27#endif
28
29#ifdef EIGEN_RUNTIME_NO_MALLOC
30#define NURI_EIGEN_ALLOW_MALLOC(flag) \
31 Eigen::internal::set_is_malloc_allowed(flag)
32#define NURI_EIGEN_MALLOC_ALLOWED() Eigen::internal::is_malloc_allowed()
33#else
34#define NURI_EIGEN_ALLOW_MALLOC(flag) static_cast<void>(flag)
35#define NURI_EIGEN_MALLOC_ALLOWED() true
36#endif
37
39
40namespace nuri {
42
43// NOLINTNEXTLINE(*-naming)
44namespace E = Eigen;
45
46using E::Array;
47using E::Array3d;
48using E::Array3i;
49using E::Array4d;
50using E::Array4i;
51using E::ArrayX;
52using ArrayXb = E::ArrayX<bool>;
53using ArrayXc = ArrayX<std::int8_t>;
54using E::Array2Xd;
55using E::Array2Xi;
56using E::Array33d;
57using E::Array3Xd;
58using E::Array4Xd;
59using E::ArrayX2d;
60using E::ArrayX2i;
61using E::ArrayX3d;
62using E::ArrayXd;
63using E::ArrayXi;
64using E::ArrayXX;
65using E::ArrayXXd;
66using E::ArrayXXi;
67using ArrayXXc = ArrayXX<std::int8_t>;
68
69using E::Matrix;
70using E::Matrix3;
71using E::Matrix3d;
72using E::Matrix3Xd;
73using E::Matrix4d;
74using E::Matrix4Xd;
75using E::MatrixX;
76using E::MatrixX3d;
77using E::MatrixXd;
78
79using E::Vector;
80using E::Vector3;
81using E::Vector3d;
82using E::Vector4d;
83using E::VectorXd;
84
85using E::AngleAxisd;
86using E::Isometry3d;
87using E::Quaterniond;
88using E::Translation3d;
89
90template <class DT, int Dim>
91using IsometryT = E::Transform<DT, Dim, E::Isometry>;
92
93template <class Raw, int Options = 0,
94 class StrideType =
95 std::conditional_t<Raw::IsVectorAtCompileTime != 0,
96 E::InnerStride<1>, E::OuterStride<>>>
97using MutRef = E::Ref<internal::remove_cvref_t<Raw>, Options, StrideType>;
98
99template <class Raw, int Options = 0,
100 class StrideType =
101 std::conditional_t<Raw::IsVectorAtCompileTime != 0,
102 E::InnerStride<1>, E::OuterStride<>>>
103using ConstRef =
104 const E::Ref<const internal::remove_cvref_t<Raw>, Options, StrideType> &;
105
106template <class Raw, int BlockRows = E::Dynamic, int BlockCols = E::Dynamic,
107 bool InnerPanel = false>
108using MutBlock =
109 E::Block<internal::remove_cvref_t<Raw>, BlockRows, BlockCols, InnerPanel>;
110
111template <class Raw, int BlockRows = E::Dynamic, int BlockCols = E::Dynamic,
112 bool InnerPanel = false>
113using ConstBlock = const E::Block<const internal::remove_cvref_t<Raw>,
114 BlockRows, BlockCols, InnerPanel> &;
115
116template <class Raw, int Size = E::Dynamic>
117using MutVecBlock = E::VectorBlock<internal::remove_cvref_t<Raw>, Size>;
118
119template <class Raw, int Size = E::Dynamic>
120using ConstVecBlock =
121 const E::VectorBlock<const internal::remove_cvref_t<Raw>, Size> &;
122
124
133template <int O = 0, int N = 0>
135public:
136 static_assert(N > 0, "N must be positive");
137
138 constexpr static int size() { return N; }
139
140 constexpr int operator[](int i) const {
141 int before = O + i, after = i - kZeroAt;
142 return i < kZeroAt ? before : after;
143 }
144
145private:
146 constexpr static int kZeroAt = (N - O) % N;
147 static_assert(kZeroAt >= 0, "Offset must be smaller than size");
148};
149
150template <int O>
151class CyclicIndex<O, 0> {
152public:
153 constexpr CyclicIndex(int n): n_(n), zero_at_((n - O) % n) {
154 ABSL_DCHECK(zero_at_ >= 0);
155 }
156
157 constexpr int size() const { return n_; }
158
159 constexpr int operator[](int i) const {
160 int before = O + i, after = i - zero_at_;
161 return i < zero_at_ ? before : after;
162 }
163
164private:
165 int n_;
166 int zero_at_;
167};
168
169template <>
170class CyclicIndex<0, 0> {
171public:
172 constexpr CyclicIndex(int n, int offset)
173 : n_(n), offset_(offset), zero_at_((n - offset) % n) {
174 ABSL_DCHECK(zero_at_ >= 0);
175 }
176
177 constexpr int size() const { return n_; }
178
179 constexpr int operator[](int i) const {
180 int before = offset_ + i, after = i - zero_at_;
181 return i < zero_at_ ? before : after;
182 }
183
184private:
185 int n_;
186 int offset_;
187 int zero_at_;
188};
189
191
192template <class ML1, class ML2, class TransformLike>
193// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
194void inplace_transform(ML1 &&m_out, const TransformLike &xform, const ML2 &m) {
195 const E::Index ri = m.rows(), ci = m.cols();
196 const E::Index ro = m_out.rows(), co = m_out.cols();
197 ABSL_ASSUME(ri == ro && ci == co);
198
199 for (E::Index i = 0; i < m.cols(); ++i)
200 m_out.col(i) = xform * m.col(i);
201}
202
203namespace internal {
204 template <bool Allowed>
205 class AllowEigenMallocScoped {
206 public:
207 AllowEigenMallocScoped(const AllowEigenMallocScoped &) = delete;
208 AllowEigenMallocScoped(AllowEigenMallocScoped &&) = delete;
209 AllowEigenMallocScoped &operator=(const AllowEigenMallocScoped &) = delete;
210 AllowEigenMallocScoped &operator=(AllowEigenMallocScoped &&) = delete;
211
212#ifdef EIGEN_RUNTIME_NO_MALLOC
213 AllowEigenMallocScoped(): state_(E::internal::is_malloc_allowed()) {
214 E::internal::set_is_malloc_allowed(Allowed);
215 }
216
217 ~AllowEigenMallocScoped() { E::internal::set_is_malloc_allowed(state_); }
218
219 private:
220 bool state_;
221#else
222 AllowEigenMallocScoped() = default;
223 ~AllowEigenMallocScoped() { }
224#endif
225 };
226} // namespace internal
227} // namespace nuri
228
229#endif /* NURI_EIGEN_CONFIG_H_ */
constexpr CyclicIndex(int n, int offset)
Definition eigen_config.h:172
constexpr int operator[](int i) const
Definition eigen_config.h:179
constexpr int size() const
Definition eigen_config.h:177
constexpr int size() const
Definition eigen_config.h:157
constexpr CyclicIndex(int n)
Definition eigen_config.h:153
constexpr int operator[](int i) const
Definition eigen_config.h:159
Cyclic indexer for Eigen types with a given offset.
Definition eigen_config.h:134
static constexpr int size()
Definition eigen_config.h:138
constexpr int operator[](int i) const
Definition eigen_config.h:140
Definition crdgen.h:16