CARLA
Mesh.h
Go to the documentation of this file.
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
16 #endif // LIBCARLA_INCLUDED_FROM_UE4
17 
18 namespace carla {
19 namespace geom {
20 
21  /// Material that references the vertex index start and end of
22  /// a mesh where it is affecting.
23  struct MeshMaterial {
24 
26  const std::string &new_name,
27  size_t start = 0u,
28  size_t end = 0u)
29  : name(new_name),
30  index_start(start),
31  index_end(end) {}
32 
33  const std::string name;
34 
35  size_t index_start;
36 
37  size_t index_end;
38 
39  };
40 
41  /// Mesh data container, validator and exporter.
42  class Mesh {
43  public:
44 
47  using index_type = size_t;
48  using uv_type = Vector2D;
50 
51  // =========================================================================
52  // -- Constructor ----------------------------------------------------------
53  // =========================================================================
54 
55  Mesh(const std::vector<vertex_type> &vertices = {},
56  const std::vector<normal_type> &normals = {},
57  const std::vector<index_type> &indexes = {},
58  const std::vector<uv_type> &uvs = {})
59  : _vertices(vertices),
60  _normals(normals),
61  _indexes(indexes),
62  _uvs(uvs) {}
63 
64  // =========================================================================
65  // -- Validate methods -----------------------------------------------------
66  // =========================================================================
67 
68  /// Check if the mesh can be valid or not.
69  bool IsValid() const;
70 
71  // =========================================================================
72  // -- Mesh build methods ---------------------------------------------------
73  // =========================================================================
74 
75  /// Adds a triangle strip to the mesh, vertex order is counterclockwise.
76  void AddTriangleStrip(const std::vector<vertex_type> &vertices);
77 
78  /// Adds a triangle fan to the mesh, vertex order is counterclockwise.
79  void AddTriangleFan(const std::vector<vertex_type> &vertices);
80 
81  /// Appends a vertex to the vertices list.
82  void AddVertex(vertex_type vertex);
83 
84  /// Appends a vertex to the vertices list.
85  void AddVertices(const std::vector<vertex_type> &vertices);
86 
87  /// Appends a normal to the normal list.
88  void AddNormal(normal_type normal);
89 
90  /// Appends a index to the indexes list.
91  void AddIndex(index_type index);
92 
93  /// Appends a vertex to the vertices list, they will be read 3 in 3.
94  void AddUV(uv_type uv);
95 
96  /// Starts applying a new material to the new added triangles.
97  void AddMaterial(const std::string &material_name);
98 
99  /// Stops applying the material to the new added triangles.
100  void EndMaterial();
101 
102  // =========================================================================
103  // -- Export methods -------------------------------------------------------
104  // =========================================================================
105 
106  /// Returns a string containing the mesh encoded in OBJ.
107  /// Units are in meters. It is in Unreal space.
108  std::string GenerateOBJ() const;
109 
110  /// Returns a string containing the mesh encoded in OBJ.
111  /// Units are in meters. This function exports the OBJ file
112  /// specifically to be consumed by Recast library.
113  /// Changes the build face direction and the coordinate space.
114  std::string GenerateOBJForRecast() const;
115 
116  /// Returns a string containing the mesh encoded in PLY.
117  /// Units are in meters.
118  std::string GeneratePLY() const;
119 
120  // =========================================================================
121  // -- Other methods --------------------------------------------------------
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  size_t GetIndexesNum() const;
135 
136  const std::vector<uv_type> &GetUVs() const;
137 
138  const std::vector<material_type> &GetMaterials() const;
139 
140  /// Returns the index of the last added vertex (number of vertices).
141  size_t GetLastVertexIndex() const;
142 
143  /// Merges two meshes into a single mesh
144  Mesh &operator+=(const Mesh &rhs);
145 
146  friend Mesh operator+(const Mesh &lhs, const Mesh &rhs);
147 
148  // =========================================================================
149  // -- Conversions to UE4 types ---------------------------------------------
150  // =========================================================================
151 
152 #ifdef LIBCARLA_INCLUDED_FROM_UE4
153 
154  operator FProceduralCustomMesh() const {
156 
157  // Build the mesh
158  for (const auto Vertex : GetVertices())
159  {
160  // From meters to centimeters
161  Mesh.Vertices.Add(FVector{1e2f * Vertex.x, 1e2f * Vertex.y, 1e2f * Vertex.z});
162  }
163 
164  const auto Indexes = GetIndexes();
165  TArray<FTriIndices> TriIndices;
166  for (auto i = 0u; i < Indexes.size(); i += 3)
167  {
168  FTriIndices Triangle;
169  // "-1" since mesh indexes in Unreal starts from index 0.
170  Mesh.Triangles.Add(Indexes[i] - 1);
171  // Since Unreal's coords are left handed, invert the last 2 indices.
172  Mesh.Triangles.Add(Indexes[i + 2] - 1);
173  Mesh.Triangles.Add(Indexes[i + 1] - 1);
174 
175  Triangle.v0 = Indexes[i] - 1;
176  Triangle.v1 = Indexes[i + 2] - 1;
177  Triangle.v2 = Indexes[i + 1] - 1;
178  TriIndices.Add(Triangle);
179  }
180 
181  // Compute the normals
182  TArray<FVector> Normals;
183  Mesh.Normals.Init(FVector::UpVector, Mesh.Vertices.Num());
184 
185  for (const auto &Triangle : TriIndices) {
186  FVector Normal;
187  const FVector U = Mesh.Vertices[Triangle.v1] - Mesh.Vertices[Triangle.v0];
188  const FVector V = Mesh.Vertices[Triangle.v2] - Mesh.Vertices[Triangle.v0];
189  Normal.X = (U.Y * V.Z) - (U.Z * V.Y);
190  Normal.Y = (U.Z * V.X) - (U.X * V.Z);
191  Normal.Z = (U.X * V.Y) - (U.Y * V.X);
192  Normal = -Normal;
193  Normal = Normal.GetSafeNormal(.0001f);
194  if (Normal != FVector::ZeroVector)
195  {
196  // fix to prevent ugly x-fighting in geometries with very large curvatures,
197  // ensures that all road geometry is facing upwards
198  if (FVector::DotProduct(Normal, FVector(0,0,1)) < 0)
199  {
200  Normal = -Normal;
201  }
202  Mesh.Normals[Triangle.v0] = Normal;
203  Mesh.Normals[Triangle.v1] = Normal;
204  Mesh.Normals[Triangle.v2] = Normal;
205  }
206  }
207 
208  return Mesh;
209  }
210 
211 #endif // LIBCARLA_INCLUDED_FROM_UE4
212 
213  private:
214 
215  // =========================================================================
216  // -- Private data members -------------------------------------------------
217  // =========================================================================
218 
219  std::vector<vertex_type> _vertices;
220 
221  std::vector<normal_type> _normals;
222 
223  std::vector<index_type> _indexes;
224 
225  std::vector<uv_type> _uvs;
226 
227  std::vector<material_type> _materials;
228  };
229 
230 } // namespace geom
231 } // namespace carla
size_t index_type
Definition: Mesh.h:47
A definition of a Carla Mesh.
const std::string name
Definition: Mesh.h:33
Mesh operator+(const Mesh &lhs, const Mesh &rhs)
Definition: Mesh.cpp:323
MeshMaterial(const std::string &new_name, size_t start=0u, size_t end=0u)
Definition: Mesh.h:25
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:55
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
TArray< FVector > Normals
geom::Vector3D Vector3D
Definition: rpc/Vector3D.h:14
Material that references the vertex index start and end of a mesh where it is affecting.
Definition: Mesh.h:23
TArray< FVector > Vertices
std::vector< material_type > _materials
Definition: Mesh.h:227
Mesh data container, validator and exporter.
Definition: Mesh.h:42
std::vector< index_type > _indexes
Definition: Mesh.h:223
std::vector< uv_type > _uvs
Definition: Mesh.h:225
geom::Vector2D Vector2D
Definition: rpc/Vector2D.h:14
std::vector< normal_type > _normals
Definition: Mesh.h:221
std::vector< vertex_type > _vertices
Definition: Mesh.h:219