Last active
June 14, 2018 06:54
-
-
Save Unril/03fa353d0461ed6bd41d to your computer and use it in GitHub Desktop.
Using Eigen::Vector3f in OpenMesh mesh
This file contains 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
EIGEN_STRONG_INLINE Matrix &normalize() { | |
*this /= norm(); | |
return *this; | |
} | |
EIGEN_STRONG_INLINE Scalar length() const { return norm(); } | |
EIGEN_STRONG_INLINE Matrix &vectorize(Scalar v) { | |
fill(v); | |
return *this; | |
} |
This file contains 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
#define EIGEN_MATRIX_PLUGIN "EigenOpenMeshPlugin.h" | |
#include <Eigen/Core> | |
#include <Eigen/Geometry> | |
#include <OpenMesh/Core/Mesh/Traits.hh> | |
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh> | |
namespace OpenMesh { | |
template <typename _Scalar, int _Rows, int _Cols, int _Options> | |
struct vector_traits<Eigen::Matrix<_Scalar, _Rows, _Cols, _Options>> { | |
static_assert(_Rows != Eigen::Dynamic && _Cols != Eigen::Dynamic, | |
"Should not use dynamic vectors."); | |
static_assert(_Rows == 1 || _Cols == 1, "Should not use matrices."); | |
using vector_type = Eigen::Matrix<_Scalar, _Rows, _Cols, _Options>; | |
using value_type = _Scalar; | |
static const size_t size_ = _Rows * _Cols; | |
static size_t size() { return size_; } | |
}; | |
template <typename _Scalar, int _Rows, int _Cols, int _Options> | |
struct VectorDimensionsT<Eigen::Matrix<_Scalar, _Rows, _Cols, _Options>> { | |
static_assert(_Rows != Eigen::Dynamic && _Cols != Eigen::Dynamic, | |
"Should not use dynamic vectors."); | |
static_assert(_Rows == 1 || _Cols == 1, "Should not use matrices."); | |
enum { value = _Rows * _Cols }; | |
}; | |
template <typename Derived> | |
typename Derived::Scalar dot(Eigen::MatrixBase<Derived> const &v1, | |
Eigen::MatrixBase<Derived> const &v2) { | |
return v1.dot(v2); | |
} | |
template <typename Derived> | |
typename Derived::template cross_product_return_type<Derived>::type | |
cross(Eigen::MatrixBase<Derived> const &v1, | |
Eigen::MatrixBase<Derived> const &v2) { | |
return v1.cross(v2); | |
} | |
} |
Usage:
struct MyTraitsEigen : OpenMesh::DefaultTraits {
using Point = Eigen::Vector3f;
using Normal = Eigen::Vector3f;
VertexAttributes(OpenMesh::Attributes::Normal);
HalfedgeAttributes(OpenMesh::Attributes::PrevHalfedge);
EdgeAttributes(0);
FaceAttributes(OpenMesh::Attributes::Normal);
};
using MyMeshEigen = OpenMesh::TriMesh_ArrayKernelT<MyTraitsEigen>;
You may also need to force OpenMesh to use right alignment for points and normals:
#include<Eigen/StdVector>
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Vector3f); // Type used in traits.
Though in MSVC it may not be required because it auto aligns in 64 bit builds.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It appears to be ~10% faster then original OpenMesh::Vector.