Created
March 26, 2019 21:38
-
-
Save mtao/250512db2e19366db84ae026613b73a8 to your computer and use it in GitHub Desktop.
using https://github.com/mtao/core for a grid datastructure, this code creates a somewhat-virtual matrix that produces a horizontal stack of grid vertices and use-driven data (extra vertices). based off of https://eigen.tuxfamily.org/dox/TopicCustomizing_NullaryExpr.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Eigen/Dense> | |
#include <iostream> | |
#include <mtao/geometry/grid/grid.h> | |
template<class ArgType, int D> | |
struct col_offset_helper { | |
typedef Eigen::Matrix<typename ArgType::Scalar, | |
D, | |
Eigen::Dynamic, | |
Eigen::ColMajor, | |
D, | |
Eigen::Dynamic | |
> MatrixType; | |
}; | |
template <typename MatrixType> | |
class col_offset { | |
const MatrixType V; | |
mtao::geometry::grid::GridD<typename MatrixType::Scalar,MatrixType::RowsAtCompileTime> grid; | |
int offset_cols = 0; | |
public: | |
template <typename Derived, typename... Args> | |
col_offset(const Eigen::MatrixBase<Derived>& v, Args&&... args): V(v), grid(std::forward<Args>(args)...) {} | |
const typename MatrixType::Scalar operator()(int row, int col) const { | |
if(col >= grid.size()) { | |
return V(row, col-grid.size()); | |
} else { | |
return grid.vertex(col)(row); | |
} | |
} | |
}; | |
template <int D, typename ArgType, typename... Args> | |
Eigen::CwiseNullaryOp<col_offset<ArgType>, typename col_offset_helper<ArgType,D>::MatrixType> | |
make_col_offset(const Eigen::MatrixBase<ArgType>& arg, Args&&... args) { | |
using MatType = typename col_offset_helper<ArgType,D>::MatrixType; | |
mtao::geometry::grid::GridD<typename ArgType::Scalar,D> grid(std::forward<Args>(args)...); | |
return MatType::NullaryExpr(arg.cols(), arg.cols()+grid.size(), col_offset<ArgType>(arg.derived(), grid)); | |
} | |
int main() { | |
auto I = Eigen::Matrix3d::Random().eval(); | |
auto F = make_col_offset<3>(I,std::array<int,3>{{3,3,3}}); | |
std::cout << F << std::endl; | |
std::cout << "Rows: " << std::endl; | |
for(int i = 0; i < F.rows(); ++i) { | |
std::cout << i << "): " << F.row(i) << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output generated: