12#include <lagrange/utils/assert.h>
13#include <lagrange/utils/warning.h>
17#if !defined(__clang__) && defined(__gnuc__)
18#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
22namespace experimental {
28 template <
typename Archive>
29 static void serialize(
ArrayBase* arr, Archive& ar)
31 if (arr->is_row_major()) {
32 using EigenType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
33 auto M = arr->template view<EigenType>();
36 using EigenType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
37 auto M = arr->template view<EigenType>();
42 template <
int StorageOrder,
typename Archive>
43 static std::unique_ptr<ArrayBase> deserialize(Archive& ar)
45 using EigenType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, StorageOrder>;
48 return std::make_unique<EigenArray<EigenType>>(std::move(matrix));
52template <
typename Archive>
53void serialize_array(
ArrayBase* arr, Archive& ar)
55 assert(!ar.is_input());
56 enum { SCALAR_TYPE = 0, IS_ROW_MAJOR = 1, DATA = 2 };
58 ar.object([&](
auto& ar) {
59 auto stype = arr->get_scalar_type();
60 auto is_row_major = arr->is_row_major();
61 ar(
"scalar_type", SCALAR_TYPE) & stype;
62 ar(
"is_row_major", IS_ROW_MAJOR) & is_row_major;
64 auto data_ar = ar(
"data", DATA);
67#define LAGRANGE_SERIALIZATION_ADD_CASE(type) \
69 ArraySerialization<EnumToScalar_t<type>>::serialize(arr, data_ar); \
71 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT8)
72 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT16)
73 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT32)
74 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT64)
75 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT8)
76 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT16)
77 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT32)
78 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT64)
79 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::FLOAT)
80 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::DOUBLE)
81 default:
throw std::runtime_error(
"Unsupported scalar type detected!");
82#undef LAGRANGE_SERIALIZATION_ADD_CASE
87template <
typename Archive>
88std::unique_ptr<ArrayBase> deserialize_array(Archive& ar)
90 enum { SCALAR_TYPE = 0, IS_ROW_MAJOR = 1, DATA = 2 };
91 std::unique_ptr<ArrayBase> ptr;
93 ar.object([&](
auto& ar) {
96 ar(
"scalar_type", SCALAR_TYPE) & stype;
97 ar(
"is_row_major", IS_ROW_MAJOR) & is_row_major;
99 auto data_ar = ar(
"data", DATA);
102#define LAGRANGE_SERIALIZATION_ADD_CASE(type) \
106 ? ArraySerialization<EnumToScalar_t<type>>::deserialize<Eigen::RowMajor>(data_ar) \
107 : ArraySerialization<EnumToScalar_t<type>>::deserialize<Eigen::ColMajor>(data_ar); \
109 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT8)
110 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT16)
111 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT32)
112 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT64)
113 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT8)
114 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT16)
115 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT32)
116 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT64)
117 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::FLOAT)
118 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::DOUBLE)
119 default:
throw std::runtime_error(
"Unsupported scalar type detected!");
121#undef LAGRANGE_SERIALIZATION_ADD_CASE
123 assert(ptr !=
nullptr);
136template <
typename Archive>
137void serialize(std::shared_ptr<ArrayBase>& arr, Archive& ar)
140 arr = internal::deserialize_array(ar);
142 internal::serialize_array(arr.get(), ar);
153template <
typename Archive>
154void serialize(std::unique_ptr<ArrayBase>& arr, Archive& ar)
157 arr = internal::deserialize_array(ar);
159 internal::serialize_array(arr.get(), ar);
166LA_IGNORE_SHADOW_WARNING_END
#define LA_IGNORE_SHADOW_WARNING_BEGIN
Ignore shadow warnings.
Definition: warning.h:68
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: Array.serialization.h:27