Skip to content

Commit

Permalink
add support for Eigen::Transform (#1555)
Browse files Browse the repository at this point in the history
* add support of dynamically sized Eigen types

* ensure buffer bounds safety and add unit test for dynamic Eigen matrix.

* add support for Eigen::Transform

* remove redundant print in unit test

* remove redundant initialization in unit test

* add lower precision unit test

* remove high precision unit test

---------

Co-authored-by: wangzhiqiang <[email protected]>
  • Loading branch information
WanZhiQiu-ac and wangzhiqiang authored Jan 7, 2025
1 parent b716727 commit 0bc0c5f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
27 changes: 27 additions & 0 deletions include/glaze/ext/eigen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ namespace glz
GLZ_MATCH_CLOSE_BRACKET;
}
};

template <typename Scalar, int Dim, int Mode>
struct from<JSON, Eigen::Transform<Scalar, Dim, Mode>>
{
template <auto Opts>
static void op(auto& value, is_context auto&& ctx, auto&& it, auto&& end)
{
constexpr auto size =
Mode == Eigen::TransformTraits::AffineCompact ? (Dim + 1) * Dim : (Dim + 1) * (Dim + 1);
std::span<Scalar, size> view(value.data(), size);
detail::read<JSON>::op<Opts>(view, ctx, it, end);
}
};

template <typename Scalar, int Dim, int Mode>
struct to<JSON, Eigen::Transform<Scalar, Dim, Mode>>
{
template <auto Opts>
static void op(auto&& value, is_context auto&& ctx, auto&& b, auto&& ix)
{
constexpr auto size =
Mode == Eigen::TransformTraits::AffineCompact ? (Dim + 1) * Dim : (Dim + 1) * (Dim + 1);
std::span<Scalar, size> view(value.data(), size);
using Value = std::remove_cvref_t<decltype(value)>;
detail::to<JSON, Value>::template op<Opts>(view, ctx, b, ix);
}
};
} // namespace detail
} // namespace glaze

Expand Down
18 changes: 18 additions & 0 deletions tests/eigen_test/eigen_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "glaze/ext/eigen.hpp"

#include <Eigen/Core>
#include <Eigen/Geometry>
#include <any>
#include <chrono>
#include <iostream>
Expand Down Expand Up @@ -394,4 +395,21 @@ int main()
expect(!glz::read_json(e, b));
expect(bool(m == e));
};

"Eigen::Transform"_test = [] {
Eigen::Isometry3d pose1, pose2;
pose1.setIdentity();
pose1.translation() << 1.111, 2.222, 3.333;
std::string buffer = glz::write_json(pose1).value();
expect(buffer == "[1,0,0,0,0,1,0,0,0,0,1,0,1.111,2.222,3.333,1]");
expect(not glz::read_json(pose2, buffer));
expect(pose1.matrix() == pose2.matrix());

Eigen::AffineCompact2d c1, c2;
c1.setIdentity();
buffer = glz::write_json(c1).value();
expect(buffer == "[1,0,0,1,0,0]");
expect(not glz::read_json(c2, buffer));
expect(c1.matrix() == c2.matrix());
};
}

0 comments on commit 0bc0c5f

Please sign in to comment.