GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
mesh.h
Go to the documentation of this file.
1// Copyright 2016 The Draco Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15#ifndef DRACO_MESH_MESH_H_
16#define DRACO_MESH_MESH_H_
17
18#include <memory>
19#include <unordered_map>
20
23#include "draco/core/macros.h"
24#include "draco/core/status.h"
26#ifdef DRACO_TRANSCODER_SUPPORTED
28#include "draco/material/material_library.h"
32#endif
34
35namespace draco {
36
37// List of different variants of mesh attributes.
39 // All corners attached to a vertex share the same attribute value. A typical
40 // example are the vertex positions and often vertex colors.
42 // The most general attribute where every corner of the mesh can have a
43 // different attribute value. Often used for texture coordinates or normals.
45 // All corners of a single face share the same value.
47};
48
49// Mesh class can be used to represent general triangular meshes. Internally,
50// Mesh is just an extended PointCloud with extra connectivity data that defines
51// what points are connected together in triangles.
52class Mesh : public PointCloud {
53 public:
54 typedef std::array<PointIndex, 3> Face;
55
56 Mesh();
57
58#ifdef DRACO_TRANSCODER_SUPPORTED
59 // Copies all data from the |src| mesh.
60 void Copy(const Mesh &src);
61#endif
62
63 void AddFace(const Face &face) { faces_.push_back(face); }
64
65 void SetFace(FaceIndex face_id, const Face &face) {
66 if (face_id >= static_cast<uint32_t>(faces_.size())) {
67 faces_.resize(face_id.value() + 1, Face());
68 }
69 faces_[face_id] = face;
70 }
71
72 // Sets the total number of faces. Creates new empty faces or deletes
73 // existing ones if necessary.
74 void SetNumFaces(size_t num_faces) { faces_.resize(num_faces, Face()); }
75
76 FaceIndex::ValueType num_faces() const {
77 return static_cast<uint32_t>(faces_.size());
78 }
79 const Face &face(FaceIndex face_id) const {
80 DRACO_DCHECK_LE(0, face_id.value());
81 DRACO_DCHECK_LT(face_id.value(), static_cast<int>(faces_.size()));
82 return faces_[face_id];
83 }
84
85 void SetAttribute(int att_id, std::unique_ptr<PointAttribute> pa) override {
86 PointCloud::SetAttribute(att_id, std::move(pa));
87 if (static_cast<int>(attribute_data_.size()) <= att_id) {
88 attribute_data_.resize(att_id + 1);
89 }
90 }
91
92 void DeleteAttribute(int att_id) override {
94 if (att_id >= 0 && att_id < static_cast<int>(attribute_data_.size())) {
95 attribute_data_.erase(attribute_data_.begin() + att_id);
96 }
97 }
98
99#ifdef DRACO_TRANSCODER_SUPPORTED
100 // Adds a point attribute |att| to the mesh and returns the index of the
101 // newly inserted attribute. Attribute connectivity data is specified in
102 // |corner_to_value| array that contains mapping between face corners and
103 // attribute value indices.
104 // The purpose of this function is to allow users to add attributes with
105 // arbitrary connectivity to an existing mesh. New points will be
106 // automatically created if needed.
107 int32_t AddAttributeWithConnectivity(
108 std::unique_ptr<PointAttribute> att,
110
111 // Adds a point attribute |att| to the mesh and returns the index of the
112 // newly inserted attribute. The inserted attribute must have the same
113 // connectivity as the position attribute of the mesh (that is, the attribute
114 // values are defined per-vertex). Each attribute value entry in |att|
115 // corresponds to the corresponding attribute value entry in the position
116 // attribute (AttributeValueIndex in both attributes refer to the same
117 // spatial vertex).
118 // Returns -1 in case of error.
119 int32_t AddPerVertexAttribute(std::unique_ptr<PointAttribute> att);
120
121 // Removes points that are not mapped to any face of the mesh. All attribute
122 // values are going to be removed as well.
123 void RemoveIsolatedPoints();
124
125 // Adds a point attribute |att| to the mesh and returns the index of the
126 // newly inserted attribute. Attribute values are mapped 1:1 to face indices.
127 // Returns -1 in case of error.
128 int32_t AddPerFaceAttribute(std::unique_ptr<PointAttribute> att);
129#endif // DRACO_TRANSCODER_SUPPORTED
130
132 return attribute_data_[att_id].element_type;
133 }
134
136 attribute_data_[att_id].element_type = et;
137 }
138
139 // Returns the point id of for a corner |ci|.
140 inline PointIndex CornerToPointId(int ci) const {
141 if (ci < 0 || static_cast<uint32_t>(ci) == kInvalidCornerIndex.value()) {
142 return kInvalidPointIndex;
143 }
144 return this->face(FaceIndex(ci / 3))[ci % 3];
145 }
146
147 // Returns the point id of a corner |ci|.
149 return this->CornerToPointId(ci.value());
150 }
151
156
157#ifdef DRACO_TRANSCODER_SUPPORTED
158 void SetName(const std::string &name) { name_ = name; }
159 const std::string &GetName() const { return name_; }
160 const MaterialLibrary &GetMaterialLibrary() const {
161 return material_library_;
162 }
163 MaterialLibrary &GetMaterialLibrary() { return material_library_; }
164
165 // Removes all materials that are not referenced by any face of the mesh.
166 // Optional argument |remove_unused_material_indices| can be used to control
167 // whether unusued material indices are removed as well (default = true).
168 // If material indices are not removed, the unused material indices will
169 // point to empty (default) materials.
170 void RemoveUnusedMaterials();
171 void RemoveUnusedMaterials(bool remove_unused_material_indices);
172
173 // Enables or disables Draco geometry compression for this mesh.
174 void SetCompressionEnabled(bool enabled) { compression_enabled_ = enabled; }
175 bool IsCompressionEnabled() const { return compression_enabled_; }
176
177 // Sets |options| that configure Draco geometry compression. This does not
178 // enable or disable compression.
179 void SetCompressionOptions(const DracoCompressionOptions &options) {
180 compression_options_ = options;
181 }
182 const DracoCompressionOptions &GetCompressionOptions() const {
183 return compression_options_;
184 }
185 DracoCompressionOptions &GetCompressionOptions() {
186 return compression_options_;
187 }
188
189 // Library that contains non-material textures.
190 const TextureLibrary &GetNonMaterialTextureLibrary() const {
191 return non_material_texture_library_;
192 }
193 TextureLibrary &GetNonMaterialTextureLibrary() {
194 return non_material_texture_library_;
195 }
196
197 // Mesh feature ID sets as defined by EXT_mesh_features glTF extension.
198 MeshFeaturesIndex AddMeshFeatures(
199 std::unique_ptr<MeshFeatures> mesh_features) {
200 mesh_features_.push_back(std::move(mesh_features));
201 mesh_features_material_mask_.push_back({});
202 return MeshFeaturesIndex(mesh_features_.size() - 1);
203 }
204 int NumMeshFeatures() const { return mesh_features_.size(); }
205 const MeshFeatures &GetMeshFeatures(MeshFeaturesIndex index) const {
206 return *mesh_features_[index];
207 }
208 MeshFeatures &GetMeshFeatures(MeshFeaturesIndex index) {
209 return *mesh_features_[index];
210 }
211 void RemoveMeshFeatures(MeshFeaturesIndex index) {
212 mesh_features_.erase(mesh_features_.begin() + index.value());
213 mesh_features_material_mask_.erase(mesh_features_material_mask_.begin() +
214 index.value());
215 }
216
217 // Restricts given mesh features to faces mapped to a material with
218 // |material_index|. Note that single mesh features can be restricted to
219 // multiple materials.
220 void AddMeshFeaturesMaterialMask(MeshFeaturesIndex index,
221 int material_index) {
222 mesh_features_material_mask_[index].push_back(material_index);
223 }
224
225 size_t NumMeshFeaturesMaterialMasks(MeshFeaturesIndex index) const {
226 return mesh_features_material_mask_[index].size();
227 }
228 int GetMeshFeaturesMaterialMask(MeshFeaturesIndex index,
229 int mask_index) const {
230 return mesh_features_material_mask_[index][mask_index];
231 }
232
233 // Updates mesh features texture pointer to point to a new |texture_library|.
234 // The current texture pointer is used to determine the texture index in the
235 // new texture library via a given |texture_to_index_map|.
236 static void UpdateMeshFeaturesTexturePointer(
237 const std::unordered_map<const Texture *, int> &texture_to_index_map,
238 TextureLibrary *texture_library, MeshFeatures *mesh_features);
239
240 // Copies over mesh features from |source_mesh| and stores them in
241 // |target_mesh| as long as the mesh features material mask is valid for
242 // given |material_index|.
243 static void CopyMeshFeaturesForMaterial(const Mesh &source_mesh,
244 Mesh *target_mesh,
245 int material_index);
246
247 // Structural metadata.
248 const StructuralMetadata &GetStructuralMetadata() const {
249 return structural_metadata_;
250 }
251 StructuralMetadata &GetStructuralMetadata() { return structural_metadata_; }
252#endif // DRACO_TRANSCODER_SUPPORTED
253
254 protected:
255#ifdef DRACO_ATTRIBUTE_INDICES_DEDUPLICATION_SUPPORTED
256 // Extends the point deduplication to face corners. This method is called from
257 // the PointCloud::DeduplicatePointIds() and it remaps all point ids stored in
258 // |faces_| to the new deduplicated point ids using the map |id_map|.
259 void ApplyPointIdDeduplication(
260 const IndexTypeVector<PointIndex, PointIndex> &id_map,
261 const std::vector<PointIndex> &unique_point_ids) override;
262#endif
263
264 // Exposes |faces_|. Use |faces_| at your own risk. DO NOT store the
265 // reference: the |faces_| object is destroyed with the mesh.
267
268 private:
269 // Mesh specific per-attribute data.
270 std::vector<AttributeData> attribute_data_;
271
272 // Vertex indices valid for all attributes. Each attribute has its own map
273 // that converts vertex indices into attribute indices.
275
276#ifdef DRACO_TRANSCODER_SUPPORTED
277 // Mesh name.
278 std::string name_;
279
280 // Materials applied to to this mesh.
281 MaterialLibrary material_library_;
282
283 // Compression options for this mesh.
284 // TODO(vytyaz): Store encoded bitstream that this mesh compresses into.
285 bool compression_enabled_;
286 DracoCompressionOptions compression_options_;
287
288 // Sets of feature IDs as defined by EXT_mesh_features glTF extension.
290 mesh_features_;
291
292 // When the Mesh contains multiple materials, |mesh_features_material_mask_|
293 // can be used to limit specific MeshFeaturesIndex to a vector of material
294 // indices. If for a given mesh feature index, the material indices are empty,
295 // the corresponding mesh features are applied to the entire mesh.
297 mesh_features_material_mask_;
298
299 // Texture library for storing non-material textures used by this mesh, e.g.,
300 // textures containing mesh feature IDs of EXT_mesh_features glTF extension.
301 // If the mesh is part of the scene then the textures are stored in the scene.
302 // Note that mesh features contain pointers to non-material textures. It is
303 // responsibility of class user to update these pointers when updating the
304 // textures. See Mesh::Copy() for example.
305 TextureLibrary non_material_texture_library_;
306
307 // Structural metadata defined by the EXT_structural_metadata glTF extension.
308 StructuralMetadata structural_metadata_;
309#endif // DRACO_TRANSCODER_SUPPORTED
310 friend struct MeshHasher;
311};
312
313// Functor for computing a hash from data stored within a mesh.
314// Note that this can be quite slow. Two meshes will have the same hash only
315// when they have exactly the same connectivity and attribute values.
317 size_t operator()(const Mesh &mesh) const {
318 PointCloudHasher pc_hasher;
319 size_t hash = pc_hasher(mesh);
320 // Hash faces.
321 for (FaceIndex i(0); i < static_cast<uint32_t>(mesh.faces_.size()); ++i) {
322 for (int j = 0; j < 3; ++j) {
323 hash = HashCombine(mesh.faces_[i][j].value(), hash);
324 }
325 }
326 return hash;
327 }
328};
329
330} // namespace draco
331
332#endif // DRACO_MESH_MESH_H_
Definition draco_index_type_vector.h:32
size_t size() const
Definition draco_index_type_vector.h:59
Definition mesh.h:52
MeshAttributeElementType GetAttributeElementType(int att_id) const
Definition mesh.h:131
friend struct MeshHasher
Definition mesh.h:310
Mesh()
Definition mesh.cc:32
FaceIndex::ValueType num_faces() const
Definition mesh.h:76
PointIndex CornerToPointId(CornerIndex ci) const
Definition mesh.h:148
PointIndex CornerToPointId(int ci) const
Definition mesh.h:140
void SetAttribute(int att_id, std::unique_ptr< PointAttribute > pa) override
Definition mesh.h:85
const Face & face(FaceIndex face_id) const
Definition mesh.h:79
IndexTypeVector< FaceIndex, Face > & faces()
Definition mesh.h:266
void AddFace(const Face &face)
Definition mesh.h:63
void SetFace(FaceIndex face_id, const Face &face)
Definition mesh.h:65
std::array< PointIndex, 3 > Face
Definition mesh.h:54
void SetAttributeElementType(int att_id, MeshAttributeElementType et)
Definition mesh.h:135
void SetNumFaces(size_t num_faces)
Definition mesh.h:74
void DeleteAttribute(int att_id) override
Definition mesh.h:92
virtual void SetAttribute(int att_id, std::unique_ptr< PointAttribute > pa)
Definition point_cloud.cc:156
virtual void DeleteAttribute(int att_id)
Definition point_cloud.cc:168
PointCloud()
Definition point_cloud.cc:27
#define DRACO_DCHECK_LT(a, b)
Definition macros.h:86
#define DRACO_DCHECK_LE(a, b)
Definition macros.h:85
iVBORw0KGgoAAAANSUhEUgAAANcAAAB5CAMAAACdtUQZAAAA51BMVEX</div ></div >< div style="padding: 0px 0px; width: 1324px;">< div style="height: 400px; width: 1324px;"></div >< div itemscope itemtype="https: </div><div itemscope itemtype="https:</div >< div itemscope itemtype="https: </div></div><p><div style="padding:0px 0px;width:1324px;"><div></div><div itemscope itemtype="https:< ul >< li >< b > Application name
8AkrcAgqgAs9wqQ0ey2tkAo8gAuuQfeI4hPUH09fXHy8z4+fng4uNKXF/Lz9BzfoAcgpkQMTUAKjAbOT4Qr9AXhJ8Rl7ktMCwTosA...
Definition meshpage.php:1610
tag src
Definition meshpage.php:1786
< h2 > I want smooth transition from jpg png webp to the engine</h2 > There s few options
Definition meshpage.php:1181
Definition attribute_octahedron_transform.cc:21
MeshAttributeElementType
Definition mesh.h:38
@ MESH_CORNER_ATTRIBUTE
Definition mesh.h:44
@ MESH_FACE_ATTRIBUTE
Definition mesh.h:46
@ MESH_VERTEX_ATTRIBUTE
Definition mesh.h:41
size_t HashCombine(T1 a, T2 b)
Definition hash_utils.h:26
CornerIndex CornerIndex
Definition mesh_edgebreaker_encoder_impl.cc:35
FaceIndex FaceIndex
Definition mesh_edgebreaker_encoder_impl.cc:36
Definition GameApi_pl.cc:29382
Definition mesh.h:316
size_t operator()(const Mesh &mesh) const
Definition mesh.h:317
AttributeData()
Definition mesh.h:153
MeshAttributeElementType element_type
Definition mesh.h:154
Definition point_cloud.h:227