#pragma once #include #include #include #include #include template Eigen::Matrix mirror(const tc::Group &group) { Eigen::Matrix res; res.setZero(); for (int c = 0; c < group.rank(); ++c) { for (int r = 0; r < c; ++r) { auto angle = M_PI / group.get(c, r); auto dot = res.col(c).dot(res.col(r)); res(r, c) = (dot - cos(angle)) / res(r, r); } res(c, c) = sqrt(1 - res.col(c).squaredNorm()); } return res; } struct Stereo { template auto operator()(U &&mat) const { const auto Rows = std::remove_reference::type::RowsAtCompileTime; return std::forward(mat).template topRows().rowwise() / (1 - mat.template bottomRows<1>()); } }; struct Ortho { template auto operator()(U &&mat) const { const auto Rows = std::remove_reference::type::RowsAtCompileTime; return std::forward(mat).template topRows(); } }; struct Project { template auto operator()(U &&point, V &&axis) const { return point.dot(axis) / axis.dot(axis) * std::forward(axis); } }; struct Reflect { template auto operator()(U &&point, V &&axis) const { return std::forward(point) - 2 * Project()(point, axis); } }; template Mat gram_schmidt(Mat mat) { for (int i = 0; i < mat.cols(); ++i) { for (int j = i + 1; j < mat.cols(); ++j) { mat.col(j) -= Project()(mat.col(j), mat.col(i)); } } return mat; } template Mat plane_intersections(Mat normals) { auto last = normals.cols() - 1; Mat results(normals.rows(), normals.cols()); results.setZero(); Eigen::Matrix indices(normals.cols()); std::iota(indices.begin(), indices.end(), 0); for (int i = 0; i < normals.cols(); ++i) { std::rotate(indices.begin(), indices.begin() + 1, indices.end()); Mat cur = normals * Eigen::PermutationMatrix(indices); Mat res = gram_schmidt(cur); results.col(i) = res.col(last); } results.colwise().normalize(); return results; } template mat rot(int u, int v, float theta) { mat res = mat::Identity(); res(u, u) = std::cos(theta); res(u, v) = std::sin(theta); res(v, u) = -std::sin(theta); res(v, v) = std::cos(theta); return res; } template auto rotor(U const &u, V const &v) { using Rotor = Eigen::Matrix< typename U::Scalar, std::min(U::RowsAtCompileTime, V::RowsAtCompileTime), std::min(U::RowsAtCompileTime, V::RowsAtCompileTime), U::Options, std::max(U::MaxRowsAtCompileTime, V::MaxRowsAtCompileTime), std::max(U::MaxRowsAtCompileTime, V::MaxRowsAtCompileTime) >; const auto &ident = Rotor::Identity(u.rows(), v.rows()); const auto &inner = (u + v) / (1 + u.dot(v)) * (u + v).transpose(); const auto &outer = v * u.transpose(); return ident - inner + 2 * outer; }