Dandelion 2.0.0
A light-weight 3D builder for educational usage
载入中...
搜索中...
未找到
mesh.hpp
浏览该文件的文档.
1#pragma once
2
3#include <array>
4#include <vector>
5#include <string>
6
7#include <Eigen/Core>
8
9/*!
10 * \file geometry/mesh.hpp
11 * \~chinese
12 * 提供用于渲染数据同步的内存 Mesh 数据结构。
13 */
14
15/*!
16 * \ingroup geometry
17 * \ingroup rendering
18 * \~chinese
19 * \brief 用于同步渲染数据的三角形 Mesh 。
20 *
21 * 这个类负责表示可以直接同步到 GPU 的三角形 Mesh 数据。
22 *
23 * 外界读取 Mesh 中的顶点坐标、法线,以及边和三角形的索引时一般应该使用
24 * `positions`/`normals` 等存储向量或定长数组的容器;而渲染器需要向 GPU
25 * 同步数据时,则应该直接使用存储 `float` 或 `unsigned int` 的扁平 `vector` 。
26 */
27struct Mesh
28{
29 /*! \~chinese 默认构造一个空的 Mesh 。 */
30 Mesh() = default;
31 /*! \~chinese 复制另一个 Mesh 中所有的数据。 */
32 Mesh(const Mesh& other);
33 /*! \~chinese 调用各成员的移动构造。 */
34 Mesh(Mesh&& other) = default;
35 /*!
36 * \~chinese
37 * \brief 清空 Mesh 中所有的图元数据。
38 *
39 * 该方法只清空直接保存数据的容器(公有成员),不会清空用于向 GPU
40 * 传输数据的 buffer (私有成员)。
41 */
42 void clear() noexcept;
43
44 /*! \~chinese 顶点坐标。 */
45 std::vector<Eigen::Vector3f> positions;
46 /*! \~chinese 顶点法线。 */
47 std::vector<Eigen::Vector3f> normals;
48 /*! \~chinese 边的顶点索引。 */
49 std::vector<std::array<unsigned int, 2>> edges;
50 /*! \~chinese 面片的顶点索引。 */
51 std::vector<std::array<unsigned int, 3>> faces;
52 /*! \~chinese 相较于上次渲染,该 Mesh 是否被修改过。 */
54 /*! \~chinese 名称。 */
55 std::string name;
56};
57
58/*!
59 * \ingroup geometry
60 * \ingroup rendering
61 * \~chinese
62 * \brief 用于同步线条渲染数据的内存对象。
63 *
64 * 这个类负责表示可以直接同步到 GPU 的线条数据。
65 *
66 * 外界读取顶点和线条数据时一般应该选择 `positions`/`lines`
67 * 属性,而渲染器需要向 GPU 同步数据时而渲染器需要向 GPU 同步数据时则应该直接使用扁平的 `vector` 。
68 *
69 * 线条只支持单色渲染,着色效果不受光照的影响。
70 */
71struct LineSet
72{
73 /*! \~chinese 默认构造一个空的 LineSet 。 */
74 LineSet() = default;
75 /*! \~chinese 默认复制另一个 LineSet 中的所有数据。 */
76 LineSet(const LineSet& other) = default;
77 /*! \~chinese 调用各成员的移动构造。 */
78 LineSet(LineSet&& other) = default;
79 /*!
80 * \~chinese
81 * 添加一条从 `from` 到 `to` 的线段。
82 * \param from 起始点。
83 * \param to 终止点。
84 */
85 void add_line(const Eigen::Vector3f& from, const Eigen::Vector3f& to);
86 /*!
87 * \~chinese
88 * 统计当前总共有多少条线段。
89 * \returns 线段总数。
90 */
91 std::size_t n_lines() const noexcept;
92 /*!
93 * \~chinese
94 * \brief 清空 Mesh 中所有的图元数据。
95 *
96 * 该方法只清空直接保存数据的容器(公有成员),不会清空用于向 GPU
97 * 传输数据的 buffer (私有成员)。
98 */
99 void clear() noexcept;
100
101 /*! \~chinese 顶点坐标。 */
102 std::vector<Eigen::Vector3f> positions;
103 /*! \~chinese 线条的顶点索引。 */
104 std::vector<std::array<unsigned int, 2>> lines;
105 /*! \~chinese 线条的颜色,每个分量取值范围在 0 到 1 之间。 */
106 Eigen::Vector3f color;
107 /*! \~chinese 相较于上次渲染,该线条集是否被修改过。 */
109 /*! \~chinese 名称。 */
110 std::string name;
111};
112
113/*!
114 * \~chinese
115 * \brief 用于渲染箭头的特殊线条集。
116 *
117 * `ArrowSet` 本质上还是一个 `LineSet` ,但提供了添加和更新箭头的便捷方法。
118 * 这个类并不强制保证所有的线条能正确地组成箭头,所以使用 `ArrowSet` 时,
119 * 应该尽可能避免直接更新 `positions` 和 `lines` ,只使用添加、更新、清空操作。
120 *
121 * 出于性能考虑,`ArrowSet` **不会强制检查其中的线段是否能组成箭头** 。
122 * 如果随意插入线段,很可能导致更新和统计总数时得不到预期的结果。
123 */
124struct ArrowSet : public LineSet
125{
126 /*!
127 * \~chinese
128 * 添加一个箭头。
129 * \param from 起始点。
130 * \param to 终止点。
131 */
132 void add_arrow(const Eigen::Vector3f& from, const Eigen::Vector3f& to);
133 /*!
134 * \~chinese
135 * \brief 更新指定的箭头。
136 * \param index 线条集中全都是箭头的前提下,要更新的箭头索引。
137 * \param from 起始点。
138 * \param to 终止点。
139 */
140 void update_arrow(std::size_t index, const Eigen::Vector3f& from, const Eigen::Vector3f& to);
141 /*!
142 * \~chinese
143 * 按每个箭头由 5 条线段组成计,统计当前的箭头总数。
144 * \returns 箭头总数。
145 */
146 std::size_t n_arrows() const noexcept;
147};
148
149/*!
150 * \~chinese
151 * \brief 用于渲染轴对齐包围盒 (AABB) 的特殊线条集。
152 *
153 * 类似 `ArrowSet` ,`AABBSet` 也是一个不提供强制数据检查的 `LineSet` 。
154 * 使用约定、注意事项与 `ArrowSet` 相似。
155 */
156struct AABBSet : public LineSet
157{
158 /*!
159 * \~chinese
160 * 添加一个 AABB 。
161 * \param p_min 三轴坐标最小的点。
162 * \param p_max 三轴坐标最大的点。
163 */
164 void add_AABB(const Eigen::Vector3f& p_min, const Eigen::Vector3f& p_max);
165};
用于渲染轴对齐包围盒 (AABB) 的特殊线条集。
定义 mesh.hpp:157
void add_AABB(const Eigen::Vector3f &p_min, const Eigen::Vector3f &p_max)
定义 mesh.cpp:94
用于渲染箭头的特殊线条集。
定义 mesh.hpp:125
void add_arrow(const Eigen::Vector3f &from, const Eigen::Vector3f &to)
定义 mesh.cpp:57
std::size_t n_arrows() const noexcept
定义 mesh.cpp:88
void update_arrow(std::size_t index, const Eigen::Vector3f &from, const Eigen::Vector3f &to)
更新指定的箭头。
定义 mesh.cpp:75
std::string name
定义 mesh.hpp:110
void add_line(const Eigen::Vector3f &from, const Eigen::Vector3f &to)
定义 mesh.cpp:26
std::vector< std::array< unsigned int, 2 > > lines
定义 mesh.hpp:104
LineSet(LineSet &&other)=default
Eigen::Vector3f color
定义 mesh.hpp:106
LineSet()=default
void clear() noexcept
清空 Mesh 中所有的图元数据。
定义 mesh.cpp:40
bool modified
定义 mesh.hpp:108
LineSet(const LineSet &other)=default
std::size_t n_lines() const noexcept
定义 mesh.cpp:35
std::vector< Eigen::Vector3f > positions
定义 mesh.hpp:102
void clear() noexcept
清空 Mesh 中所有的图元数据。
定义 mesh.cpp:17
std::vector< std::array< unsigned int, 2 > > edges
定义 mesh.hpp:49
std::vector< Eigen::Vector3f > normals
定义 mesh.hpp:47
Mesh(Mesh &&other)=default
std::vector< Eigen::Vector3f > positions
定义 mesh.hpp:45
std::vector< std::array< unsigned int, 3 > > faces
定义 mesh.hpp:51
std::string name
定义 mesh.hpp:55
bool modified
定义 mesh.hpp:53
Mesh()=default