Dandelion
2.0.0
A light-weight 3D builder for educational usage
Toggle main menu visibility
载入中...
搜索中...
未找到
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
12
using
nlohmann::json;
13
14
/*!
15
* \file utils/json_serialize.hpp
16
* \ingroup utils
17
* \~chinese
18
* \brief 实现场景保存、读取时,所有场景相关对象的 JSON 序列化和反序列化函数。
19
*/
20
21
// Eigen
22
23
namespace
Eigen {
24
25
using
Eigen::Matrix;
26
27
// implement json (de)serializer for any Eigen matrix
28
template
<
typename
T,
int
Rows,
int
Cols>
29
void
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
35
template
<
typename
T,
int
Rows,
int
Cols>
36
void
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
51
void
to_json(json& j,
const
Object
& o);
52
void
from_json(
const
json& j,
Object
& o);
53
54
// Camera
55
void
to_json(json& j,
const
Camera
& c);
56
void
from_json(
const
json& j,
Camera
& c);
57
58
// Light
59
void
to_json(json& j,
const
Light
& l);
60
void
from_json(
const
json& j,
Light
& l);
61
62
#endif
// DANDELION_UTILS_JSON_SERIALIZE_HPP
camera.h
包含相机的类,用来表示场景中的相机。
Object
表示物体的类。
定义
object.h:39
light.h
包含光源的类,目前只有一个点光源。
object.h
包含物体的类。
Camera
表示观察点的相机,既可以用于预览视角,也可以用于渲染视角。
定义
camera.h:24
Light
表示一个点光源的类。
定义
light.h:19
src
utils
json_serialize.hpp
制作者
1.18.0