Dandelion 1.1.2
A light-weight 3D builder for educational usage
载入中...
搜索中...
未找到
json_serialize.hpp
浏览该文件的文档.
1#ifndef DANDELION_UTILS_JSON_SERIALIZE_HPP
2#define DANDELION_UTILS_JSON_SERIALIZE_HPP
3
4#include <nlohmann/json.hpp>
5#include <spdlog/spdlog.h>
6#include <Eigen/Core>
7
8#include "../scene/object.h"
9#include "../scene/camera.h"
10#include "../scene/light.h"
11
12using nlohmann::json;
13
14/*!
15 * \file utils/json_serialize.hpp
16 * \ingroup utils
17 * \~chinese
18 * \brief 实现场景保存、读取时,所有场景相关对象的 JSON 序列化和反序列化函数。
19 */
20
21// Eigen
22
23namespace Eigen {
24
25using Eigen::Matrix;
26
27// implement json (de)serializer for any Eigen matrix
28template<typename T, int Rows, int Cols>
29void to_json(json& j, const Matrix<T, Rows, Cols>& m)
30{
31 std::vector<T> flat_data(m.data(), m.data() + m.size());
32 j = flat_data;
33}
34
35template<typename T, int Rows, int Cols>
36void from_json(const json& j, Matrix<T, Rows, Cols>& m)
37{
38 if ((size_t)j.size() != (size_t)m.size()) {
39 spdlog::error("JSON array size does not match matrix size.");
40 return;
41 }
42 std::vector<T> flat_data = j.get<std::vector<T>>();
43
44 // use Eigen::Map to create a view and copy data to the Matrix
45 m = Eigen::Map<const Matrix<T, Rows, Cols>>(flat_data.data());
46}
47
48}; // namespace Eigen
49
50// Object
51void to_json(json& j, const Object& o);
52void from_json(const json& j, Object& o);
53
54// Camera
55void to_json(json& j, const Camera& c);
56void from_json(const json& j, Camera& c);
57
58// Light
59void to_json(json& j, const Light& l);
60void from_json(const json& j, Light& l);
61
62#endif // DANDELION_UTILS_JSON_SERIALIZE_HPP
包含相机的类,用来表示场景中的相机。
表示物体的类。
定义 object.h:42
包含光源的类,目前只有一个点光源。
包含物体的类。
表示观察点的相机,既可以用于预览视角,也可以用于渲染视角。
定义 camera.h:24
表示一个点光源的类。
定义 light.h:19