Lagrange
Attribute.h
1/*
2 * Copyright 2020 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 <lagrange/Logger.h>
15#include <lagrange/common.h>
16#include <lagrange/experimental/Array.h>
17#include <lagrange/experimental/create_array.h>
18
19namespace lagrange {
20namespace experimental {
21
23{
24public:
25 Attribute() = default;
26
27 Attribute(const Attribute& other) = default;
28 Attribute(Attribute&& other) = default;
29 Attribute& operator=(const Attribute& other) = default;
30
31 Attribute(std::shared_ptr<ArrayBase> values)
32 : m_values(std::move(values))
33 {}
34
35 template <typename Derived>
36 Attribute(const Eigen::MatrixBase<Derived>& values)
37 {
38 this->set(values);
39 }
40
41 template <typename Derived>
42 Attribute(Eigen::MatrixBase<Derived>&& values)
43 {
44 this->set(std::move(values));
45 }
46
47public:
48 std::shared_ptr<const ArrayBase> get() const { return m_values; }
49 std::shared_ptr<ArrayBase> get() { return m_values; }
50
51 template <typename Derived>
52 decltype(auto) get() const
53 {
54 la_runtime_assert(m_values != nullptr);
55 const auto& values = *m_values;
56 return values.get<Derived>();
57 }
58
59 template <typename Derived>
60 decltype(auto) get()
61 {
62 la_runtime_assert(m_values != nullptr);
63 return m_values->get<Derived>();
64 }
65
66 template <typename Derived>
67 decltype(auto) view() const
68 {
69 la_runtime_assert(m_values != nullptr);
70 const auto& values = *m_values;
71 return values.view<Derived>();
72 }
73
74 template <typename Derived>
75 decltype(auto) view()
76 {
77 la_runtime_assert(m_values != nullptr);
78 return m_values->view<Derived>();
79 }
80
81 template <typename Derived>
82 void set(const Eigen::MatrixBase<Derived>& values)
83 {
84 if (m_values == nullptr) {
85 m_values = create_array(values.derived());
86 } else {
87 m_values->set(values.derived());
88 }
89 }
90
91 template <typename Derived>
92 void set(Eigen::MatrixBase<Derived>&& values)
93 {
94#ifndef NDEBUG
95 void* ptr = values.derived().data();
96#endif
97 if (m_values == nullptr) {
98 m_values = create_array(std::move(values.derived()));
99 } else {
100 m_values->set(std::move(values.derived()));
101 }
102#ifndef NDEBUG
103 void* ptr2 = m_values->data();
104 if (ptr != ptr2) {
105 logger().warn("Attribute values are copied when they should have been moved. "
106 "Likely caused by inexact match of `Derived` type.");
107 }
108#endif
109 }
110
111 template <typename Derived>
112 void set(const Eigen::ArrayBase<Derived>& values)
113 {
114 set(values.matrix());
115 }
116
117 template <typename Derived>
118 void set(Eigen::ArrayBase<Derived>&& values)
119 {
120 set(std::move(values.matrix()));
121 }
122
123 void set(std::shared_ptr<ArrayBase> values) { m_values = std::move(values); }
124
125 template <typename Archive>
126 void serialize_impl(Archive& ar)
127 {
128 ar & m_values;
129 }
130
131private:
132 std::shared_ptr<ArrayBase> m_values;
133};
134
135template <typename Archive>
136void serialize(Attribute& attribute, Archive& ar)
137{
138 attribute.serialize_impl(ar);
139}
140
141} // namespace experimental
142} // namespace lagrange
Definition: Attribute.h:23
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30