Lagrange
Loading...
Searching...
No Matches
PyIndexedAttribute.h
1/*
2 * Copyright 2022 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#pragma once
13
14#include "PyAttribute.h"
15
16#include <lagrange/Attribute.h>
17#include <lagrange/IndexedAttribute.h>
18#include <lagrange/SurfaceMeshTypes.h>
19#include <lagrange/internal/weak_ptr.h>
20#include <lagrange/utils/Error.h>
21
22#include <memory>
23
24namespace lagrange::python {
25
26class PyIndexedAttribute
27{
28public:
29 PyIndexedAttribute(internal::weak_ptr<AttributeBase> ptr)
30 : m_attr(ptr)
31 {}
32
33public:
34 internal::shared_ptr<AttributeBase> operator->() { return ptr(); }
35
37 {
38 auto attr = m_attr.lock();
39 if (attr == nullptr) throw Error("Indexed attribute is no longer valid!");
40 return attr;
41 }
42
43 template <typename CallBack>
44 auto process(CallBack&& cb)
45 {
46 auto attr = m_attr.lock();
47 if (attr == nullptr) throw Error("Indexed attribute is no longer valid!");
48 if (attr->get_element_type() != Indexed)
49 throw Error("Attribute is not an indexed attribute");
50
51#define LA_X_process(Index, ValueType) \
52 { \
53 auto indexed_attr = dynamic_cast<IndexedAttribute<ValueType, Index>*>(attr.get()); \
54 if (indexed_attr != nullptr) { \
55 return cb(*indexed_attr); \
56 } \
57 }
58#define LA_X_process_index(_, Index) LA_ATTRIBUTE_X(process, Index)
59 LA_SURFACE_MESH_INDEX_X(process_index, 0)
60#undef LA_X_process_index
61#undef LA_X_process
62 throw Error("Cannot process indexed attribute with unsupported types!");
63 }
64
65 PyAttribute values()
66 {
67 return process([&](auto& indexed_attr) {
68 auto attr = m_attr.lock();
69 la_debug_assert(attr != nullptr);
70 auto& value_attr = indexed_attr.values();
71 internal::shared_ptr<AttributeBase> alias_ptr(attr, &value_attr);
72 return PyAttribute(alias_ptr);
73 });
74 }
75
76 PyAttribute indices()
77 {
78 return process([&](auto& indexed_attr) {
79 auto attr = m_attr.lock();
80 la_debug_assert(attr != nullptr);
81 auto& index_attr = indexed_attr.indices();
82 internal::shared_ptr<AttributeBase> alias_ptr(attr, &index_attr);
83 return PyAttribute(alias_ptr);
84 });
85 }
86
87private:
89};
90
91} // namespace lagrange::python
NOT implemented: custom allocator support.
Definition shared_ptr.h:111
Definition weak_ptr.h:26
Definition PyAttribute.h:24
@ Indexed
Indexed mesh attributes.
Definition AttributeFwd.h:45
#define LA_SURFACE_MESH_INDEX_X(mode, data)
X Macro arguments to iterate over index types available for the SurfaceMesh<> class.
Definition SurfaceMeshTypes.h:71
#define la_debug_assert(...)
Debug assertion check.
Definition assert.h:194
@ Error
Throw an error if collision is detected.
Definition MappingPolicy.h:24