This commit is contained in:
Ivan
2022-04-05 11:42:28 +03:00
commit 6dc0eb0fcf
5565 changed files with 1200500 additions and 0 deletions

View File

@@ -0,0 +1,211 @@
/**
BSD 3-Clause License
This file is part of the Basalt project.
https://gitlab.com/VladyslavUsenko/basalt-headers.git
Copyright (c) 2019, Vladyslav Usenko and Nikolaus Demmel.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@file
@brief Serialization for Eigen and Sophus types
*/
#pragma once
#include <Eigen/Dense>
#include <sophus/se3.hpp>
#include <sophus/sim3.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/archives/json.hpp>
namespace cereal {
// NOTE: Serialization functions for non-basalt types (for now Eigen and Sophus)
// are provided in a separate header to make them available to other libraries
// depending on basalt-headers. Beware that it is not possible to have different
// and incompatible definitions for these same types in other libraries or
// executables that also include basalt-headers, as this would lead to undefined
// behaviour. See
// https://groups.google.com/d/topic/cerealcpp/WswQi_Sh-bw/discussion for a more
// detailed discussion and possible workarounds.
// For binary-archives, don't save a size tag for compact representation.
template <class Archive, class _Scalar, int _Rows, int _Cols, int _Options,
int _MaxRows, int _MaxCols>
std::enable_if_t<(_Rows > 0) && (_Cols > 0) &&
!traits::is_text_archive<Archive>::value>
serialize(
Archive& archive,
Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& m) {
for (int i = 0; i < _Rows; i++) {
for (int j = 0; j < _Cols; j++) {
archive(m(i, j));
}
}
// Note: if we can break binary compatibility, we might want to consider the
// following. However, this would only work for compact Scalar types that can
// be serialized / deserialized by memcopy, such as double.
// archive(binary_data(m.data(), _Rows * _Cols * sizeof(_Scalar)));
}
// For text-archives, save size-tag even for constant size matrices, to ensure
// that the serialization is more compact (e.g. for JSON with size tag is uses a
// simple array, whereas without, it stores a list of pairs like ("value0", v0).
template <class Archive, class _Scalar, int _Rows, int _Cols, int _Options,
int _MaxRows, int _MaxCols>
std::enable_if_t<(_Rows > 0) && (_Cols > 0) &&
traits::is_text_archive<Archive>::value>
serialize(
Archive& archive,
Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& m) {
size_type s = static_cast<size_type>(_Rows * _Cols);
archive(make_size_tag(s));
if (s != _Rows * _Cols) {
throw std::runtime_error("matrix has incorrect length");
}
for (size_t i = 0; i < _Rows; i++) {
for (size_t j = 0; j < _Cols; j++) {
archive(m(i, j));
}
}
}
template <class Archive, class _Scalar, int _Cols, int _Options, int _MaxRows,
int _MaxCols>
std::enable_if_t<(_Cols > 0), void> save(
Archive& archive, const Eigen::Matrix<_Scalar, Eigen::Dynamic, _Cols,
_Options, _MaxRows, _MaxCols>& m) {
archive(make_size_tag(static_cast<size_type>(m.size())));
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < _Cols; j++) {
archive(m(i, j));
}
}
}
template <class Archive, class _Scalar, int _Cols, int _Options, int _MaxRows,
int _MaxCols>
std::enable_if_t<(_Cols > 0), void> load(
Archive& archive,
Eigen::Matrix<_Scalar, Eigen::Dynamic, _Cols, _Options, _MaxRows, _MaxCols>&
m) {
size_type size;
archive(make_size_tag(size));
m.resize(Eigen::Index(size) / _Cols, _Cols);
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < _Cols; j++) {
archive(m(i, j));
}
}
}
template <class Archive, class _Scalar, int _Rows, int _Options, int _MaxRows,
int _MaxCols>
std::enable_if_t<(_Rows > 0), void> save(
Archive& archive, const Eigen::Matrix<_Scalar, _Rows, Eigen::Dynamic,
_Options, _MaxRows, _MaxCols>& m) {
archive(make_size_tag(static_cast<size_type>(m.size())));
for (int i = 0; i < _Rows; i++) {
for (int j = 0; j < m.cols(); j++) {
archive(m(i, j));
}
}
}
template <class Archive, class _Scalar, int _Rows, int _Options, int _MaxRows,
int _MaxCols>
std::enable_if_t<(_Rows > 0), void> load(
Archive& archive,
Eigen::Matrix<_Scalar, _Rows, Eigen::Dynamic, _Options, _MaxRows, _MaxCols>&
m) {
size_type size;
archive(make_size_tag(size));
m.resize(_Rows, Eigen::Index(size) / _Rows);
for (int i = 0; i < _Rows; i++) {
for (int j = 0; j < m.cols(); j++) {
archive(m(i, j));
}
}
}
template <class Archive, class _Scalar, int _Options, int _MaxRows,
int _MaxCols>
void save(Archive& archive,
const Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic, _Options,
_MaxRows, _MaxCols>& m) {
archive(make_size_tag(static_cast<size_type>(m.rows())));
archive(make_size_tag(static_cast<size_type>(m.cols())));
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < m.cols(); j++) {
archive(m(i, j));
}
}
}
template <class Archive, class _Scalar, int _Options, int _MaxRows,
int _MaxCols>
void load(Archive& archive,
Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic, _Options,
_MaxRows, _MaxCols>& m) {
size_type rows;
size_type cols;
archive(make_size_tag(rows));
archive(make_size_tag(cols));
m.resize(rows, cols);
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < m.cols(); j++) {
archive(m(i, j));
}
}
}
template <class Archive>
void serialize(Archive& ar, Sophus::SE3d& p) {
ar(cereal::make_nvp("px", p.translation()[0]),
cereal::make_nvp("py", p.translation()[1]),
cereal::make_nvp("pz", p.translation()[2]),
cereal::make_nvp("qx", p.so3().data()[0]),
cereal::make_nvp("qy", p.so3().data()[1]),
cereal::make_nvp("qz", p.so3().data()[2]),
cereal::make_nvp("qw", p.so3().data()[3]));
}
template <class Archive>
void serialize(Archive& ar, Sophus::Sim3d& p) {
ar(cereal::make_nvp("px", p.translation()[0]),
cereal::make_nvp("py", p.translation()[1]),
cereal::make_nvp("pz", p.translation()[2]),
cereal::make_nvp("qx", p.rxso3().data()[0]),
cereal::make_nvp("qy", p.rxso3().data()[1]),
cereal::make_nvp("qz", p.rxso3().data()[2]),
cereal::make_nvp("qw", p.rxso3().data()[3]));
}
} // namespace cereal

View File

@@ -0,0 +1,266 @@
/**
BSD 3-Clause License
This file is part of the Basalt project.
https://gitlab.com/VladyslavUsenko/basalt-headers.git
Copyright (c) 2019, Vladyslav Usenko and Nikolaus Demmel.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@file
@brief Serialization for basalt types
*/
#pragma once
#include <basalt/serialization/eigen_io.h>
#include <basalt/calibration/calibration.hpp>
#include <basalt/camera/bal_camera.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/archives/json.hpp>
#include <cereal/types/deque.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/types/string.hpp>
#include <cereal/types/vector.hpp>
namespace cereal {
template <class Archive, class Scalar>
inline void save(Archive& ar, const basalt::GenericCamera<Scalar>& cam) {
std::visit(
[&](const auto& cam) {
ar(cereal::make_nvp("camera_type", cam.getName()));
ar(cereal::make_nvp("intrinsics", cam));
},
cam.variant);
}
template <class Archive, class Scalar>
inline void load(Archive& ar, basalt::GenericCamera<Scalar>& cam) {
std::string cam_type;
ar(cereal::make_nvp("camera_type", cam_type));
cam = basalt::GenericCamera<Scalar>::fromString(cam_type);
std::visit([&](auto& cam) { ar(cereal::make_nvp("intrinsics", cam)); },
cam.variant);
}
template <class Archive, class Scalar>
inline void save(Archive& ar, const basalt::KannalaBrandtCamera4<Scalar>& cam) {
ar(cereal::make_nvp("fx", cam.getParam()[0]),
cereal::make_nvp("fy", cam.getParam()[1]),
cereal::make_nvp("cx", cam.getParam()[2]),
cereal::make_nvp("cy", cam.getParam()[3]),
cereal::make_nvp("k1", cam.getParam()[4]),
cereal::make_nvp("k2", cam.getParam()[5]),
cereal::make_nvp("k3", cam.getParam()[6]),
cereal::make_nvp("k4", cam.getParam()[7]));
}
template <class Archive, class Scalar>
inline void load(Archive& ar, basalt::KannalaBrandtCamera4<Scalar>& cam) {
Eigen::Matrix<Scalar, 8, 1> intr;
ar(cereal::make_nvp("fx", intr[0]), cereal::make_nvp("fy", intr[1]),
cereal::make_nvp("cx", intr[2]), cereal::make_nvp("cy", intr[3]),
cereal::make_nvp("k1", intr[4]), cereal::make_nvp("k2", intr[5]),
cereal::make_nvp("k3", intr[6]), cereal::make_nvp("k4", intr[7]));
cam = basalt::KannalaBrandtCamera4<Scalar>(intr);
}
template <class Archive, class Scalar>
inline void save(Archive& ar,
const basalt::ExtendedUnifiedCamera<Scalar>& cam) {
ar(cereal::make_nvp("fx", cam.getParam()[0]),
cereal::make_nvp("fy", cam.getParam()[1]),
cereal::make_nvp("cx", cam.getParam()[2]),
cereal::make_nvp("cy", cam.getParam()[3]),
cereal::make_nvp("alpha", cam.getParam()[4]),
cereal::make_nvp("beta", cam.getParam()[5]));
}
template <class Archive, class Scalar>
inline void load(Archive& ar, basalt::ExtendedUnifiedCamera<Scalar>& cam) {
Eigen::Matrix<Scalar, 6, 1> intr;
ar(cereal::make_nvp("fx", intr[0]), cereal::make_nvp("fy", intr[1]),
cereal::make_nvp("cx", intr[2]), cereal::make_nvp("cy", intr[3]),
cereal::make_nvp("alpha", intr[4]), cereal::make_nvp("beta", intr[5]));
cam = basalt::ExtendedUnifiedCamera<Scalar>(intr);
}
template <class Archive, class Scalar>
inline void save(Archive& ar, const basalt::UnifiedCamera<Scalar>& cam) {
ar(cereal::make_nvp("fx", cam.getParam()[0]),
cereal::make_nvp("fy", cam.getParam()[1]),
cereal::make_nvp("cx", cam.getParam()[2]),
cereal::make_nvp("cy", cam.getParam()[3]),
cereal::make_nvp("alpha", cam.getParam()[4]));
}
template <class Archive, class Scalar>
inline void load(Archive& ar, basalt::UnifiedCamera<Scalar>& cam) {
Eigen::Matrix<Scalar, 5, 1> intr;
ar(cereal::make_nvp("fx", intr[0]), cereal::make_nvp("fy", intr[1]),
cereal::make_nvp("cx", intr[2]), cereal::make_nvp("cy", intr[3]),
cereal::make_nvp("alpha", intr[4]));
cam = basalt::UnifiedCamera<Scalar>(intr);
}
template <class Archive, class Scalar>
inline void save(Archive& ar, const basalt::PinholeCamera<Scalar>& cam) {
ar(cereal::make_nvp("fx", cam.getParam()[0]),
cereal::make_nvp("fy", cam.getParam()[1]),
cereal::make_nvp("cx", cam.getParam()[2]),
cereal::make_nvp("cy", cam.getParam()[3]));
}
template <class Archive, class Scalar>
inline void load(Archive& ar, basalt::PinholeCamera<Scalar>& cam) {
Eigen::Matrix<Scalar, 4, 1> intr;
ar(cereal::make_nvp("fx", intr[0]), cereal::make_nvp("fy", intr[1]),
cereal::make_nvp("cx", intr[2]), cereal::make_nvp("cy", intr[3]));
cam = basalt::PinholeCamera<Scalar>(intr);
}
template <class Archive, class Scalar>
inline void save(Archive& ar, const basalt::DoubleSphereCamera<Scalar>& cam) {
ar(cereal::make_nvp("fx", cam.getParam()[0]),
cereal::make_nvp("fy", cam.getParam()[1]),
cereal::make_nvp("cx", cam.getParam()[2]),
cereal::make_nvp("cy", cam.getParam()[3]),
cereal::make_nvp("xi", cam.getParam()[4]),
cereal::make_nvp("alpha", cam.getParam()[5]));
}
template <class Archive, class Scalar>
inline void load(Archive& ar, basalt::DoubleSphereCamera<Scalar>& cam) {
Eigen::Matrix<Scalar, 6, 1> intr;
ar(cereal::make_nvp("fx", intr[0]), cereal::make_nvp("fy", intr[1]),
cereal::make_nvp("cx", intr[2]), cereal::make_nvp("cy", intr[3]),
cereal::make_nvp("xi", intr[4]), cereal::make_nvp("alpha", intr[5]));
cam = basalt::DoubleSphereCamera<Scalar>(intr);
}
template <class Archive, class Scalar>
inline void save(Archive& ar, const basalt::FovCamera<Scalar>& cam) {
ar(cereal::make_nvp("fx", cam.getParam()[0]),
cereal::make_nvp("fy", cam.getParam()[1]),
cereal::make_nvp("cx", cam.getParam()[2]),
cereal::make_nvp("cy", cam.getParam()[3]),
cereal::make_nvp("w", cam.getParam()[4]));
}
template <class Archive, class Scalar>
inline void load(Archive& ar, basalt::FovCamera<Scalar>& cam) {
Eigen::Matrix<Scalar, 5, 1> intr;
ar(cereal::make_nvp("fx", intr[0]), cereal::make_nvp("fy", intr[1]),
cereal::make_nvp("cx", intr[2]), cereal::make_nvp("cy", intr[3]),
cereal::make_nvp("w", intr[4]));
cam = basalt::FovCamera<Scalar>(intr);
}
template <class Archive, class Scalar>
inline void save(Archive& ar, const basalt::BalCamera<Scalar>& cam) {
ar(cereal::make_nvp("f", cam.getParam()[0]),
cereal::make_nvp("k1", cam.getParam()[1]),
cereal::make_nvp("k2", cam.getParam()[2]));
}
template <class Archive, class Scalar>
inline void load(Archive& ar, basalt::BalCamera<Scalar>& cam) {
Eigen::Matrix<Scalar, 3, 1> intr;
ar(cereal::make_nvp("f", intr[0]), cereal::make_nvp("k1", intr[1]),
cereal::make_nvp("k2", intr[2]));
cam = basalt::BalCamera<Scalar>(intr);
}
template <class Archive, class Scalar, int DIM, int ORDER>
inline void save(Archive& ar,
const basalt::RdSpline<DIM, ORDER, Scalar>& spline) {
ar(spline.minTimeNs());
ar(spline.getTimeIntervalNs());
ar(spline.getKnots());
}
template <class Archive, class Scalar, int DIM, int ORDER>
inline void load(Archive& ar, basalt::RdSpline<DIM, ORDER, Scalar>& spline) {
int64_t start_t_ns;
int64_t dt_ns;
Eigen::aligned_deque<Eigen::Matrix<Scalar, DIM, 1>> knots;
ar(start_t_ns);
ar(dt_ns);
ar(knots);
basalt::RdSpline<DIM, ORDER, Scalar> new_spline(dt_ns, start_t_ns);
for (const auto& k : knots) {
new_spline.knotsPushBack(k);
}
spline = new_spline;
}
template <class Archive, class Scalar>
inline void serialize(Archive& ar, basalt::Calibration<Scalar>& cam) {
ar(cereal::make_nvp("T_imu_cam", cam.T_i_c),
cereal::make_nvp("intrinsics", cam.intrinsics),
cereal::make_nvp("resolution", cam.resolution),
cereal::make_nvp("calib_accel_bias", cam.calib_accel_bias.getParam()),
cereal::make_nvp("calib_gyro_bias", cam.calib_gyro_bias.getParam()),
cereal::make_nvp("imu_update_rate", cam.imu_update_rate),
cereal::make_nvp("accel_noise_std", cam.accel_noise_std),
cereal::make_nvp("gyro_noise_std", cam.gyro_noise_std),
cereal::make_nvp("accel_bias_std", cam.accel_bias_std),
cereal::make_nvp("gyro_bias_std", cam.gyro_bias_std),
cereal::make_nvp("cam_time_offset_ns", cam.cam_time_offset_ns),
cereal::make_nvp("vignette", cam.vignette));
}
template <class Archive, class Scalar>
inline void serialize(Archive& ar, basalt::MocapCalibration<Scalar>& cam) {
ar(cereal::make_nvp("T_mocap_world", cam.T_moc_w),
cereal::make_nvp("T_imu_marker", cam.T_i_mark),
cereal::make_nvp("mocap_time_offset_ns", cam.mocap_time_offset_ns),
cereal::make_nvp("mocap_to_imu_offset_ns", cam.mocap_to_imu_offset_ns));
}
} // namespace cereal