PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Eigen.cuh
1#ifndef PBAT_GPU_IMPL_COMMON_EIGEN_H
2#define PBAT_GPU_IMPL_COMMON_EIGEN_H
3
4#include "Buffer.cuh"
5#include "pbat/common/Eigen.h"
6
7#include <Eigen/Core>
8
9namespace pbat {
10namespace gpu {
11namespace impl {
12namespace common {
13
14template <class ScalarType, auto D, class TDerived>
15void ToBuffer(Eigen::PlainObjectBase<TDerived> const& A, Buffer<ScalarType, D>& buf)
16{
17 if constexpr (not std::is_same_v<typename TDerived::Scalar, ScalarType>)
18 {
19 auto A2 = A.template cast<ScalarType>().eval();
20 ToBuffer(A2, buf);
21 }
22
23 using SizeType = decltype(buf.Size());
24 auto ncols = static_cast<SizeType>(A.cols());
25 auto nrows = static_cast<SizeType>(A.rows());
26 if constexpr (D > 1)
27 {
28 if (nrows != buf.Dimensions())
29 {
30 std::ostringstream ss{};
31 ss << "Expected input dimensions " << buf.Dimensions() << "x" << buf.Size()
32 << ", but got " << A.rows() << "x" << A.cols() << "\n";
33 throw std::invalid_argument(ss.str());
34 }
35 if (buf.Size() != ncols)
36 buf.Resize(ncols);
37 for (auto d = 0; d < buf.Dimensions(); ++d)
38 {
39 thrust::copy(A.row(d).begin(), A.row(d).end(), buf[d].begin());
40 }
41 }
42 else
43 {
44 auto nsize = static_cast<SizeType>(A.size());
45 if (buf.Size() != nsize)
46 buf.Resize(nsize);
47 thrust::copy(A.data(), A.data() + A.size(), buf.Data());
48 }
49}
50
51template <
52 class ScalarType,
53 auto D,
54 auto StorageOrder = Eigen::ColMajor,
55 auto EigenRows = Eigen::Dynamic,
56 auto EigenCols = Eigen::Dynamic>
57void ToBuffer(
58 Eigen::Ref<Eigen::Matrix<ScalarType, EigenRows, EigenCols, StorageOrder> const> const& A,
60{
61 using SizeType = decltype(buf.Size());
62 if constexpr (D > 1)
63 {
64 auto nrows = static_cast<SizeType>(A.rows());
65 auto ncols = static_cast<SizeType>(A.cols());
66 if (nrows != buf.Dimensions())
67 {
68 std::ostringstream ss{};
69 ss << "Expected input dimensions " << buf.Dimensions() << "x" << buf.Size()
70 << ", but got " << A.rows() << "x" << A.cols() << "\n";
71 throw std::invalid_argument(ss.str());
72 }
73 if (buf.Size() != ncols)
74 buf.Resize(ncols);
75 for (auto d = 0; d < buf.Dimensions(); ++d)
76 {
77 thrust::copy(A.row(d).begin(), A.row(d).end(), buf[d].begin());
78 }
79 }
80 else
81 {
82 auto nsize = static_cast<SizeType>(A.size());
83 if (buf.Size() != nsize)
84 buf.Resize(nsize);
85 thrust::copy(A.data(), A.data() + A.size(), buf.Data());
86 }
87}
88
89template <class ScalarType, auto D, auto StorageOrder = (D > 1) ? Eigen::RowMajor : Eigen::ColMajor>
90Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic, StorageOrder>
91ToEigen(Buffer<ScalarType, D> const& buf)
92{
93 if constexpr (D > 1)
94 {
95 return pbat::common::ToEigen(buf.Get()).reshaped(buf.Size(), buf.Dimensions()).transpose();
96 }
97 else
98 {
99 return pbat::common::ToEigen(buf.Get());
100 }
101}
102
103template <class ScalarType, auto D, class TDerived>
104Buffer<ScalarType, D> EigenToBuffer(Eigen::PlainObjectBase<TDerived> const& A)
105{
107 ToBuffer(A, buf);
108 return buf;
109}
110
111template <
112 class ScalarType,
113 auto D,
114 auto StorageOrder = Eigen::ColMajor,
115 auto EigenRows = Eigen::Dynamic,
116 auto EigenCols = Eigen::Dynamic>
117Buffer<ScalarType, D> EigenToBuffer(
118 Eigen::Ref<Eigen::Matrix<ScalarType, EigenRows, EigenCols, StorageOrder> const> const& A)
119{
121 ToBuffer(A, buf);
122 return buf;
123}
124
125} // namespace common
126} // namespace impl
127} // namespace gpu
128} // namespace pbat
129
130#endif // PBAT_GPU_IMPL_COMMON_EIGEN_H
Definition Buffer.cuh:21
Eigen adaptors for ranges.
auto ToEigen(R &&r) -> Eigen::Map< Eigen::Vector< std::ranges::range_value_t< R >, Eigen::Dynamic > const >
Map a range of scalars to an eigen vector of such scalars.
Definition Eigen.h:28
GPU algorithm implementations.
Definition VertexTriangleMixedCcdDcd.h:21
GPU related public functionality.
Definition Buffer.cu:16
The main namespace of the library.
Definition Aliases.h:15
Eigen::Matrix< Scalar, Rows, Cols > Matrix
Fixed-size matrix type.
Definition Aliases.h:31