Lagrange
Loading...
Searching...
No Matches
convert_attribute_utils.h
1/*
2 * Copyright 2023 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13#pragma once
14
15#include <lagrange/SurfaceMesh.h>
16#include <lagrange/unify_index_buffer.h>
17
18#include <vector>
19
20namespace lagrange::io {
21
22template <typename Scalar, typename Index>
23bool involve_indexed_attribute(
24 const SurfaceMesh<Scalar, Index>& mesh,
26{
27 if (attr_ids.empty()) {
28 bool has_indexed_attribute = false;
30 if (has_indexed_attribute) return;
31 auto name = mesh.get_attribute_name(id);
32 if (mesh.attr_name_is_reserved(name)) return;
33 if (mesh.is_attribute_indexed(id)) {
34 has_indexed_attribute = true;
35 }
36 });
37 return has_indexed_attribute;
38 } else {
39 for (auto id : attr_ids) {
40 if (mesh.is_attribute_indexed(id)) return true;
41 }
42 return false;
43 }
44}
45
46template <typename Scalar, typename Index>
47std::tuple<SurfaceMesh<Scalar, Index>, std::vector<AttributeId>> remap_indexed_attributes(
48 const SurfaceMesh<Scalar, Index>& in_mesh,
49 span<const AttributeId> in_attr_ids)
50{
51 std::vector<AttributeId> attr_ids(in_attr_ids.begin(), in_attr_ids.end());
52
53 auto mesh = unify_index_buffer(in_mesh);
54 std::transform(
55 attr_ids.begin(),
56 attr_ids.end(),
57 attr_ids.begin(),
58 [&](AttributeId id) -> AttributeId {
59 auto name = in_mesh.get_attribute_name(id);
60 la_runtime_assert(mesh.has_attribute(name));
61 return mesh.get_attribute_id(name);
62 });
63
64 return {mesh, attr_ids};
65}
66
67} // namespace lagrange::io
std::string_view get_attribute_name(AttributeId id) const
Retrieve attribute name from its id.
Definition SurfaceMesh.cpp:355
bool is_attribute_indexed(std::string_view name) const
Determines whether the specified attribute is indexed.
Definition SurfaceMesh.cpp:1229
static bool attr_name_is_reserved(std::string_view name)
Check whether the given name corresponds to a reserved attribute.
Definition SurfaceMesh.cpp:1409
void seq_foreach_attribute_id(function_ref< void(AttributeId)> func) const
Iterates over all attribute ids sequentially.
Definition SurfaceMesh.cpp:1242
SurfaceMesh< Scalar, Index > unify_index_buffer(const SurfaceMesh< Scalar, Index > &mesh, const std::vector< AttributeId > &attribute_ids={})
Unify index buffers of the input mesh for all attributes specified in attribute_ids.
Definition unify_index_buffer.cpp:34
uint32_t AttributeId
Identified to be used to access an attribute.
Definition AttributeFwd.h:73
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition span.h:27
Mesh input/output.
Definition detect_file_format.h:18