Skip to content

Instantly share code, notes, and snippets.

@Unril
Last active June 14, 2018 06:54
Show Gist options
  • Save Unril/03fa353d0461ed6bd41d to your computer and use it in GitHub Desktop.
Save Unril/03fa353d0461ed6bd41d to your computer and use it in GitHub Desktop.
Using Eigen::Vector3f in OpenMesh mesh
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;
}
#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);
}
}
@Unril
Copy link
Author

Unril commented Mar 4, 2016

It appears to be ~10% faster then original OpenMesh::Vector.

@Unril
Copy link
Author

Unril commented Mar 4, 2016

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>;

@Unril
Copy link
Author

Unril commented Mar 4, 2016

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