Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
resizeable_matrix.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
11
12#include <Eigen/Core>
13
14#include <vector>
15
16namespace momentum {
17
39template <typename T>
41 public:
44 static constexpr std::size_t kAlignment = 128;
45
46 using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
47
48 ResizeableMatrix() = default;
49
53 explicit ResizeableMatrix(Eigen::Index rows, Eigen::Index cols = 1) {
55 }
56
67 void resizeAndSetZero(Eigen::Index rows, Eigen::Index cols = 1) {
68 rows_ = rows;
69 cols_ = cols;
70
71 // clear() + resize() relies on std::vector preserving capacity across clear(); the
72 // resize() therefore only reallocates when rows*cols exceeds the previous capacity.
73 // TODO: this default-inserts (zero-initializes) `rows*cols` elements every call, which
74 // is wasted work when the caller is about to overwrite all of them. Consider adding a
75 // separate `resizeWithoutZero()` for that case.
76 data_.clear();
77 data_.resize(rows * cols);
78 }
79
80 [[nodiscard]] Eigen::Index rows() const {
81 return rows_;
82 }
83 [[nodiscard]] Eigen::Index cols() const {
84 return cols_;
85 }
86
89 Eigen::Map<MatrixType, Eigen::Aligned128> mat() {
90 return Eigen::Map<MatrixType, Eigen::Aligned128>(data_.data(), rows_, cols_);
91 }
92
94 Eigen::Map<const MatrixType, Eigen::Aligned128> mat() const {
95 return Eigen::Map<const MatrixType, Eigen::Aligned128>(data_.data(), rows_, cols_);
96 }
97
103 Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1>, Eigen::Aligned128> vec() {
104 return Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1>, Eigen::Aligned128>(data_.data(), rows_);
105 }
106
108 Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>, Eigen::Aligned128> vec() const {
109 return Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1>, Eigen::Aligned128>(
110 data_.data(), rows_);
111 }
112
113 private:
114 Eigen::Index rows_ = 0;
115 Eigen::Index cols_ = 0;
116 std::vector<T, AlignedAllocator<T, kAlignment>> data_;
117};
118
119} // namespace momentum
Eigen::Map< const Eigen::Matrix< T, Eigen::Dynamic, 1 >, Eigen::Aligned128 > vec() const
Const overload of vec().
Eigen::Map< MatrixType, Eigen::Aligned128 > mat()
Returns an aligned, mutable Eigen::Map view of the logical rows() x cols() matrix.
void resizeAndSetZero(Eigen::Index rows, Eigen::Index cols=1)
Sets the logical size to rows x cols and zeroes every element.
Eigen::Map< const MatrixType, Eigen::Aligned128 > mat() const
Const overload of mat().
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixType
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, 1 >, Eigen::Aligned128 > vec()
Returns an aligned, mutable Eigen::Map view interpreting the storage as a length-rows() column vector...
Eigen::Index cols() const
static constexpr std::size_t kAlignment
Alignment in bytes for the underlying storage.
Eigen::Index rows() const
ResizeableMatrix(Eigen::Index rows, Eigen::Index cols=1)
Constructs a matrix of the given logical size and zero-initializes all entries.