Skip to content

Instantly share code, notes, and snippets.

@mtao
Created March 26, 2019 21:38
Show Gist options
  • Save mtao/250512db2e19366db84ae026613b73a8 to your computer and use it in GitHub Desktop.
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
#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;
}
}
@mtao
Copy link
Author

mtao commented Mar 26, 2019

Output generated:

        0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1  0.680375   0.59688 -0.329554
        0         0         0       0.5       0.5       0.5         1         1         1         0         0         0       0.5       0.5       0.5         1         1         1         0         0         0       0.5       0.5       0.5         1         1         1 -0.211234  0.823295  0.536459
        0         0         0         0         0         0         0         0         0       0.5       0.5       0.5       0.5       0.5       0.5       0.5       0.5       0.5         1         1         1         1         1         1         1         1         1  0.566198 -0.604897 -0.444451
Rows: 
0):         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1         0       0.5         1  0.680375   0.59688 -0.329554
1):         0         0         0       0.5       0.5       0.5         1         1         1         0         0         0       0.5       0.5       0.5         1         1         1         0         0         0       0.5       0.5       0.5         1         1         1 -0.211234  0.823295  0.536459
2):         0         0         0         0         0         0         0         0         0       0.5       0.5       0.5       0.5       0.5       0.5       0.5       0.5       0.5         1         1         1         1         1         1         1         1         1  0.566198 -0.604897 -0.444451

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment