Lagrange
stl_eigen.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
15#include <Eigen/Dense>
17
18namespace lagrange {
19
20template <typename Scalar, size_t N, typename Derived>
21void vector_to_eigen(
22 const std::vector<std::array<Scalar, N>> &from,
23 Eigen::PlainObjectBase<Derived> &to)
24{
25 if (from.size() == 0) {
26 to.resize(0, N);
27 return;
28 }
29 to.resize(from.size(), N);
30 for (size_t i = 0; i < from.size(); ++i) {
31 for (size_t j = 0; j < N; ++j) {
32 to(i, j) = from[i][j];
33 }
34 }
35}
36
37template <typename Scalar, typename Derived>
38void vector_to_eigen(
39 const std::vector<std::pair<Scalar, Scalar>> &from,
40 Eigen::PlainObjectBase<Derived> &to)
41{
42 if (from.size() == 0) {
43 to.resize(0, 2);
44 return;
45 }
46 to.resize(from.size(), 2);
47 for (size_t i = 0; i < from.size(); ++i) {
48 to.row(i) << from[i].first, from[i].second;
49 }
50}
51
52template <typename Scalar, int N, typename Derived>
53void vector_to_eigen(
54 const std::vector<Eigen::Matrix<Scalar, N, 1>> &from,
55 Eigen::PlainObjectBase<Derived> &to)
56{
57 if (from.size() == 0) {
58 to.resize(0, N);
59 return;
60 }
61 to.resize(from.size(), N);
62 for (size_t i = 0; i < from.size(); ++i) {
63 for (size_t j = 0; j < N; ++j) {
64 to(i, j) = from[i][j];
65 }
66 }
67}
68
69template <typename Scalar, int N, typename Derived>
70void vector_to_eigen(
71 const std::vector<Eigen::Matrix<Scalar, 1, N>> &from,
72 Eigen::PlainObjectBase<Derived> &to)
73{
74 if (from.size() == 0) {
75 to.resize(0, N);
76 return;
77 }
78 to.resize(from.size(), N);
79 for (size_t i = 0; i < from.size(); ++i) {
80 for (size_t j = 0; j < N; ++j) {
81 to(i, j) = from[i][j];
82 }
83 }
84}
85
86template <typename Scalar>
87void vector_to_eigen(const std::vector<Scalar> &from, Eigen::Matrix<Scalar, Eigen::Dynamic, 1> &to)
88{
89 if (from.size() == 0) {
90 to.resize(0);
91 return;
92 }
93 to.resize(from.size(), 1);
94 for (size_t i = 0; i < from.size(); ++i) {
95 to(i) = from[i];
96 }
97}
98
99template <typename Scalar, typename Derived>
100void flat_vector_to_eigen(
101 const std::vector<Scalar> &from,
102 Eigen::PlainObjectBase<Derived> &to,
103 size_t rows,
104 size_t cols,
105 int row_major_flag = Eigen::RowMajor)
106{
107 la_runtime_assert(rows * cols == from.size(), "Invalid vector size");
108 to.resize(rows, cols);
109 const bool is_row_major = (row_major_flag & Eigen::RowMajor);
110 for (Eigen::Index i = 0; i < to.size(); ++i) {
111 size_t r = (is_row_major ? i / cols : i % rows);
112 size_t c = (is_row_major ? i % cols : i / rows);
113 to(r, c) = from[i];
114 }
115}
116
117template <typename Derived, typename Scalar, size_t N>
118void eigen_to_vector(const Eigen::MatrixBase<Derived> &from, std::vector<std::array<Scalar, N>> &to)
119{
120 if (from.cols() != N) {
121 throw std::invalid_argument("Wrong number of columns");
122 }
123 to.resize(from.rows());
124 for (size_t i = 0; i < to.size(); ++i) {
125 for (size_t j = 0; j < N; ++j) {
126 to[i][j] = from(i, j);
127 }
128 }
129}
130
131template <typename Derived, typename Scalar>
132void eigen_to_vector(
133 const Eigen::MatrixBase<Derived> &from,
134 std::vector<std::pair<Scalar, Scalar>> &to)
135{
136 if (from.cols() != 2) {
137 throw std::invalid_argument("Wrong number of columns");
138 }
139 to.resize(from.rows());
140 for (size_t i = 0; i < to.size(); ++i) {
141 to[i] = std::make_pair(from[i][0], from[i][1]);
142 }
143}
144
145template <typename Derived, typename Scalar>
146void eigen_to_flat_vector(
147 const Eigen::MatrixBase<Derived> &from,
148 std::vector<Scalar> &to,
149 int row_major_flag = Eigen::RowMajor)
150{
151 to.resize(from.size());
152 const bool is_row_major = (row_major_flag & Eigen::RowMajor);
153 for (Eigen::Index i = 0; i < static_cast<Eigen::Index>(to.size()); ++i) {
154 Eigen::Index r = (is_row_major ? i / from.cols() : i % from.rows());
155 Eigen::Index c = (is_row_major ? i % from.cols() : i / from.rows());
156 to[i] = from(r, c);
157 }
158}
159
160} // namespace lagrange
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30