Commit 7ac1b4a8 authored by Patrik Huber's avatar Patrik Huber

Added cereal serialisation to the PCA and Morphable Model classes

Added cereal include directory to CMake
parent 9547afe3
...@@ -42,6 +42,8 @@ else(Boost_FOUND) ...@@ -42,6 +42,8 @@ else(Boost_FOUND)
endif() endif()
set(CEREAL_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/3rdparty/cereal-1.1.1/include")
# Header files: # Header files:
set(HEADERS set(HEADERS
include/eos/core/LandmarkMapper.hpp include/eos/core/LandmarkMapper.hpp
...@@ -57,6 +59,7 @@ set(HEADERS ...@@ -57,6 +59,7 @@ set(HEADERS
# Add header includes: # Add header includes:
include_directories("include") include_directories("include")
include_directories(${CEREAL_INCLUDE_DIR})
include_directories(${Boost_INCLUDE_DIRS}) include_directories(${Boost_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS}) include_directories(${OpenCV_INCLUDE_DIRS})
...@@ -73,6 +76,7 @@ source_group(render\\detail include/eos/render/detail/*) ...@@ -73,6 +76,7 @@ source_group(render\\detail include/eos/render/detail/*)
# The install target: # The install target:
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_PREFIX}/include) install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/share/ DESTINATION ${CMAKE_INSTALL_PREFIX}/share) install(DIRECTORY ${CMAKE_SOURCE_DIR}/share/ DESTINATION ${CMAKE_INSTALL_PREFIX}/share)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/3rdparty/cereal-1.1.1/include/ DESTINATION 3rdparty/cereal-1.1.1/include) # cereal headers
if(BUILD_EXAMPLES) if(BUILD_EXAMPLES)
add_subdirectory(examples) add_subdirectory(examples)
......
...@@ -26,6 +26,11 @@ ...@@ -26,6 +26,11 @@
#include "eos/render/Mesh.hpp" #include "eos/render/Mesh.hpp"
#include "eos/morphablemodel/io/mat_cerealisation.hpp"
#include "cereal/access.hpp"
#include "cereal/types/vector.hpp"
#include "cereal/archives/binary.hpp"
#include <vector> #include <vector>
#include <array> #include <array>
...@@ -49,6 +54,7 @@ namespace eos { ...@@ -49,6 +54,7 @@ namespace eos {
class MorphableModel class MorphableModel
{ {
public: public:
MorphableModel() = default;
/** /**
* Create a Morphable Model from a shape and a color PCA model, and optional * Create a Morphable Model from a shape and a color PCA model, and optional
...@@ -187,6 +193,49 @@ private: ...@@ -187,6 +193,49 @@ private:
return texture_coordinates.size() > 0 ? true : false; return texture_coordinates.size() > 0 ? true : false;
}; };
friend class cereal::access;
/**
* Serialises this class using cereal.
*
* @param[in] ar The archive to serialise to (or to serialise from).
*/
template<class Archive>
void serialize(Archive& archive)
{
archive(shape_model, color_model, texture_coordinates);
};
};
/**
* Helper method to load a Morphable Model from
* a cereal::BinaryInputArchive from the harddisk.
*
* @param[in] filename Filename to a model.
* @return The loaded Morphable Model.
*/
MorphableModel load_model(std::string filename)
{
MorphableModel model;
std::ifstream file(filename, std::ios::binary);
cereal::BinaryInputArchive input_archive(file);
input_archive(model);
return model;
};
/**
* Helper method to save a Morphable Model to the
* harddrive as cereal::BinaryInputArchive.
*
* @param[in] model The model to be saved.
* @param[in] filename Filename for the model.
*/
void save_model(MorphableModel model, std::string filename)
{
std::ofstream file(filename, std::ios::binary);
cereal::BinaryOutputArchive output_archive(file);
output_archive(model);
}; };
namespace detail { /* eos::morphablemodel::detail */ namespace detail { /* eos::morphablemodel::detail */
......
...@@ -22,6 +22,11 @@ ...@@ -22,6 +22,11 @@
#ifndef PCAMODEL_HPP_ #ifndef PCAMODEL_HPP_
#define PCAMODEL_HPP_ #define PCAMODEL_HPP_
#include "eos/morphablemodel/io/mat_cerealisation.hpp"
#include "cereal/access.hpp"
#include "cereal/types/array.hpp"
#include "cereal/types/vector.hpp"
#include "opencv2/core/core.hpp" #include "opencv2/core/core.hpp"
#include <string> #include <string>
...@@ -251,6 +256,20 @@ private: ...@@ -251,6 +256,20 @@ private:
cv::Mat eigenvalues; ///< A col-vector of the eigenvalues (variances in the PCA space). cv::Mat eigenvalues; ///< A col-vector of the eigenvalues (variances in the PCA space).
std::vector<std::array<int, 3>> triangle_list; ///< List of triangles that make up the mesh of the model. std::vector<std::array<int, 3>> triangle_list; ///< List of triangles that make up the mesh of the model.
friend class cereal::access;
/**
* Serialises this class using cereal.
*
* @param[in] ar The archive to serialise to (or to serialise from).
*/
template<class Archive>
void serialize(Archive& archive)
{
archive(mean, normalised_pca_basis, unnormalised_pca_basis, eigenvalues, triangle_list);
// Note: If the files are too big, We could split this in save/load, only
// store one of the bases, and calculate the other one when loading.
};
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment