CARLA
 
载入中...
搜索中...
未找到
Mesh.h
浏览该文件的文档.
1// Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include <vector>
10
11#include <carla/geom/Vector3D.h>
12#include <carla/geom/Vector2D.h>
13
14#ifdef LIBCARLA_INCLUDED_FROM_UE4
18#endif // LIBCARLA_INCLUDED_FROM_UE4
19
20namespace carla {
21namespace geom {
22
23 /// 引用其影响的网格的顶点索引的起点和终点的材质。
24 struct MeshMaterial {
25
27 const std::string &new_name,
28 size_t start = 0u,
29 size_t end = 0u)
30 : name(new_name),
31 index_start(start),
32 index_end(end) {}
33
34 const std::string name;
35
37
38 size_t index_end;
39
40 };
41
42 /// 网格数据容器、验证器和导出器。
43 class Mesh {
44 public:
45
51
52 // =========================================================================
53 // -- 构造函数 -------------------------------------------------------------
54 // =========================================================================
55
56 Mesh(const std::vector<vertex_type> &vertices = {},
57 const std::vector<normal_type> &normals = {},
58 const std::vector<index_type> &indexes = {},
59 const std::vector<uv_type> &uvs = {})
60 : _vertices(vertices),
61 _normals(normals),
62 _indexes(indexes),
63 _uvs(uvs) {}
64
65 // =========================================================================
66 // -- 验证方法 --------------------------------------------------------------
67 // =========================================================================
68
69 /// 检查网格是否有效。
70 bool IsValid() const;
71
72 // =========================================================================
73 // -- 网格构建方法 ----------------------------------------------------------
74 // =========================================================================
75
76 /// 向网格添加三角形带,顶点顺序为逆时针。
77 void AddTriangleStrip(const std::vector<vertex_type> &vertices);
78
79 /// 向网格添加三角形扇形,顶点顺序为逆时针。
80 void AddTriangleFan(const std::vector<vertex_type> &vertices);
81
82 /// 将顶点附加到顶点列表。
83 void AddVertex(vertex_type vertex);
84
85 /// 将顶点附加到顶点列表。
86 void AddVertices(const std::vector<vertex_type> &vertices);
87
88 /// 将法线附加到法线列表。
89 void AddNormal(normal_type normal);
90
91 /// 将索引附加到索引列表。
92 void AddIndex(index_type index);
93
94 /// 将一个顶点附加到顶点列表,它们将被读取 3 个中的 3 个。
95 void AddUV(uv_type uv);
96
97 /// 添加纹理映射坐标(Texture-Mapping Coordinates, UV)
98 void AddUVs(const std::vector<uv_type> & uv);
99
100 /// 开始将新材质应用到新添加的三角形。
101 void AddMaterial(const std::string &material_name);
102
103 /// 停止将材质应用到新添加的三角形。
104 void EndMaterial();
105
106 // =========================================================================
107 // -- 导出方法 --------------------------------------------------------------
108 // =========================================================================
109
110 /// 返回包含 OBJ 中编码的网格的字符串。单位为米。它位于虚幻空间中。
111 std::string GenerateOBJ() const;
112
113 /// 返回包含 OBJ 中编码的网格的字符串。单位为米。
114 /// 此函数导出 OBJ 文件,供 Recast 库专门使用。更改构建面的方向和坐标空间。
115 std::string GenerateOBJForRecast() const;
116
117 /// 返回包含 PLY 中编码的网格的字符串。单位为米。
118 std::string GeneratePLY() const;
119
120 // =========================================================================
121 // -- 其他方法 -------------------------------------------------------------
122 // =========================================================================
123
124 const std::vector<vertex_type> &GetVertices() const;
125
126 std::vector<vertex_type> &GetVertices();
127
128 size_t GetVerticesNum() const;
129
130 const std::vector<normal_type> &GetNormals() const;
131
132 const std::vector<index_type>& GetIndexes() const;
133
134 std::vector<index_type> &GetIndexes();
135
136 size_t GetIndexesNum() const;
137
138 const std::vector<uv_type> &GetUVs() const;
139
140 const std::vector<material_type> &GetMaterials() const;
141
142 /// 返回最后添加的顶点索引(顶点数)。
143 size_t GetLastVertexIndex() const;
144
145 /// 将两个网格合并为一个网格
146 Mesh& ConcatMesh(const Mesh& rhs, int num_vertices_to_link);
147
148 /// 将两个网格合并为一个网格
149 Mesh &operator+=(const Mesh &rhs);
150
151 friend Mesh operator+(const Mesh &lhs, const Mesh &rhs);
152
153 // =========================================================================
154 // -- 转换为 UE4 类型 -------------------------------------------------------
155 // =========================================================================
156
157#ifdef LIBCARLA_INCLUDED_FROM_UE4
158
159 operator FProceduralCustomMesh() const {
161
162 // 构建网格
163 for (const auto Vertex : GetVertices())
164 {
165 // 从米到厘米
166 Mesh.Vertices.Add(FVector{1e2f * Vertex.x, 1e2f * Vertex.y, 1e2f * Vertex.z});
167 }
168
169 const auto Indexes = GetIndexes();
170 TArray<FTriIndices> TriIndices;
171 for (auto i = 0u; i < Indexes.size(); i += 3)
172 {
173 FTriIndices Triangle;
174 // “-1”,因为 Unreal 中的网格索引从索引 0 开始。
175 Mesh.Triangles.Add(Indexes[i] - 1);
176 // 由于虚幻的坐标是左手的,因此将最后两个索引反转。
177 Mesh.Triangles.Add(Indexes[i + 2] - 1);
178 Mesh.Triangles.Add(Indexes[i + 1] - 1);
179
180 Triangle.v0 = Indexes[i] - 1;
181 Triangle.v1 = Indexes[i + 2] - 1;
182 Triangle.v2 = Indexes[i + 1] - 1;
183 TriIndices.Add(Triangle);
184 }
185
186 // 计算法线
187 TArray<FVector> Normals;
188 Mesh.Normals.Init(FVector::UpVector, Mesh.Vertices.Num());
189
190 for (const auto &Triangle : TriIndices) {
191 FVector Normal;
192 const FVector U = Mesh.Vertices[Triangle.v1] - Mesh.Vertices[Triangle.v0];
193 const FVector V = Mesh.Vertices[Triangle.v2] - Mesh.Vertices[Triangle.v0];
194 Normal.X = (U.Y * V.Z) - (U.Z * V.Y);
195 Normal.Y = (U.Z * V.X) - (U.X * V.Z);
196 Normal.Z = (U.X * V.Y) - (U.Y * V.X);
197 Normal = -Normal;
198 Normal = Normal.GetSafeNormal(.0001f);
199 if (Normal != FVector::ZeroVector)
200 {
201 // 修复以防止在曲率非常大的几何形状中出现难看的 x-fighting,确保所有道路几何形状都朝上
202 if (FVector::DotProduct(Normal, FVector(0,0,1)) < 0)
203 {
204 Normal = -Normal;
205 }
206 Mesh.Normals[Triangle.v0] = Normal;
207 Mesh.Normals[Triangle.v1] = Normal;
208 Mesh.Normals[Triangle.v2] = Normal;
209 }
210 }
211
212 for (const auto uv : GetUVs())
213 {
214 // 从米到厘米
215 Mesh.UV0.Add(FVector2D{uv.x, uv.y});
216 }
217
218 return Mesh;
219 }
220
221#endif // LIBCARLA_INCLUDED_FROM_UE4
222
223 private:
224
225 // =========================================================================
226 // -- 私有数据成员 ----------------------------------------------------------
227 // =========================================================================
228
229 std::vector<vertex_type> _vertices;
230
231 std::vector<normal_type> _normals;
232
233 std::vector<index_type> _indexes;
234
235 std::vector<uv_type> _uvs;
236
237 std::vector<material_type> _materials;
238 };
239
240} // namespace geom
241} // namespace carla
auto end() const noexcept
Traits::size_t size_t
网格数据容器、验证器和导出器。
Definition Mesh.h:43
void AddIndex(index_type index)
将索引附加到索引列表。
Definition Mesh.cpp:132
void AddVertex(vertex_type vertex)
将顶点附加到顶点列表。
Definition Mesh.cpp:120
std::vector< vertex_type > _vertices
Definition Mesh.h:229
void AddVertices(const std::vector< vertex_type > &vertices)
将顶点附加到顶点列表。
Definition Mesh.cpp:124
std::string GenerateOBJ() const
返回包含 OBJ 中编码的网格的字符串。单位为米。它位于虚幻空间中。
Definition Mesh.cpp:177
void AddUV(uv_type uv)
将一个顶点附加到顶点列表,它们将被读取 3 个中的 3 个。
Definition Mesh.cpp:136
Mesh & ConcatMesh(const Mesh &rhs, int num_vertices_to_link)
将两个网格合并为一个网格
Definition Mesh.cpp:323
std::vector< index_type > _indexes
Definition Mesh.h:233
const std::vector< uv_type > & GetUVs() const
Definition Mesh.cpp:311
const std::vector< index_type > & GetIndexes() const
Definition Mesh.cpp:300
std::string GeneratePLY() const
返回包含 PLY 中编码的网格的字符串。单位为米。
Definition Mesh.cpp:275
Mesh(const std::vector< vertex_type > &vertices={}, const std::vector< normal_type > &normals={}, const std::vector< index_type > &indexes={}, const std::vector< uv_type > &uvs={})
Definition Mesh.h:56
bool IsValid() const
检查网格是否有效。
Definition Mesh.cpp:20
void AddUVs(const std::vector< uv_type > &uv)
添加纹理映射坐标(Texture-Mapping Coordinates, UV)
Definition Mesh.cpp:140
std::string GenerateOBJForRecast() const
返回包含 OBJ 中编码的网格的字符串。单位为米。 此函数导出 OBJ 文件,供 Recast 库专门使用。更改构建面的方向和坐标空间。
Definition Mesh.cpp:233
size_t GetVerticesNum() const
Definition Mesh.cpp:292
void AddNormal(normal_type normal)
将法线附加到法线列表。
Definition Mesh.cpp:128
std::vector< normal_type > _normals
Definition Mesh.h:231
std::vector< material_type > _materials
Definition Mesh.h:237
void AddMaterial(const std::string &material_name)
开始将新材质应用到新添加的三角形。
Definition Mesh.cpp:144
void EndMaterial()
停止将材质应用到新添加的三角形。
Definition Mesh.cpp:160
size_t index_type
Definition Mesh.h:48
Mesh & operator+=(const Mesh &rhs)
将两个网格合并为一个网格
Definition Mesh.cpp:376
const std::vector< material_type > & GetMaterials() const
Definition Mesh.cpp:315
const std::vector< normal_type > & GetNormals() const
Definition Mesh.cpp:296
Vector3D vertex_type
Definition Mesh.h:46
size_t GetLastVertexIndex() const
返回最后添加的顶点索引(顶点数)。
Definition Mesh.cpp:319
friend Mesh operator+(const Mesh &lhs, const Mesh &rhs)
Definition Mesh.cpp:414
void AddTriangleFan(const std::vector< vertex_type > &vertices)
向网格添加三角形扇形,顶点顺序为逆时针。
Definition Mesh.cpp:107
std::vector< uv_type > _uvs
Definition Mesh.h:235
size_t GetIndexesNum() const
Definition Mesh.cpp:307
Vector3D normal_type
Definition Mesh.h:47
Vector2D uv_type
Definition Mesh.h:49
void AddTriangleStrip(const std::vector< vertex_type > &vertices)
向网格添加三角形带,顶点顺序为逆时针。
Definition Mesh.cpp:52
const std::vector< vertex_type > & GetVertices() const
Definition Mesh.cpp:284
定义两个嵌套的命名空间:carla和geom。
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
A definition of a Carla Mesh.
引用其影响的网格的顶点索引的起点和终点的材质。
Definition Mesh.h:24
const std::string name
Definition Mesh.h:34
MeshMaterial(const std::string &new_name, size_t start=0u, size_t end=0u)
Definition Mesh.h:26