Lagrange
Attributes.h
1/*
2 * Copyright 2017 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 <Eigen/Core>
15#include <map>
16#include <sstream>
17#include <string>
18#include <vector>
19
20#include <lagrange/common.h>
21#include <lagrange/utils/warning.h>
22
23namespace lagrange {
24
26template <typename _AttributeArray>
28{
29public:
30 using AttributeArray = _AttributeArray;
31 using Scalar = typename AttributeArray::Scalar;
32
33public:
34 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
35
36 std::vector<std::string> get_attribute_names() const
37 {
38 std::vector<std::string> names;
39 for (const auto& itr : m_data) {
40 names.push_back(itr.first);
41 }
42 return names;
43 }
44
45 bool has_attribute(const std::string& name) const { return m_data.find(name) != m_data.end(); }
46
47 void add_attribute(const std::string& name) { m_data.insert({name, AttributeArray()}); }
48
49 template <typename Derived>
50 void add_attribute(const std::string& name, const Eigen::PlainObjectBase<Derived>& value)
51 {
52 m_data.insert({name, value});
53 }
54
55 template <typename Derived>
56 void set_attribute(const std::string& name, const Eigen::PlainObjectBase<Derived>& value)
57 {
58 auto itr = m_data.find(name);
59 if (itr == m_data.end()) {
60 std::stringstream err_msg;
61 err_msg << "Attributes::set_attribute() failed: Attribute \"" << name
62 << "\" does not exists.";
63 throw std::runtime_error(err_msg.str());
64 }
65 itr->second = value;
66 }
67
68 const AttributeArray& get_attribute(const std::string& name) const
69 {
70 auto itr = m_data.find(name);
71 if (itr == m_data.end()) {
72 std::stringstream err_msg;
73 err_msg << "Attributes::get_attribute() failed: Attribute \"" << name
74 << "\" does not exists.";
75 throw std::runtime_error(err_msg.str());
76 }
77 return itr->second;
78 }
79
80 void remove_attribute(const std::string& name)
81 {
82 auto itr = m_data.find(name);
83 if (itr == m_data.end()) {
84 std::stringstream err_msg;
85 err_msg << "Attributes::remove_attribute() failed: Attribute \"" << name
86 << "\" does not exists.";
87 throw std::runtime_error(err_msg.str());
88 }
89 m_data.erase(itr);
90 }
91
92 template <typename Derived>
93 void import_attribute(const std::string& name, Eigen::PlainObjectBase<Derived>& attr)
94 {
95 auto itr = m_data.find(name);
96 if (itr == m_data.end()) {
97 std::stringstream err_msg;
98 err_msg << "Attributes::import_attribute() failed: Attribute \"" << name
99 << "\" does not exists.";
100 throw std::runtime_error(err_msg.str());
101 }
102 move_data(attr, itr->second);
103 }
104
105 template <typename Derived>
106 void export_attribute(const std::string& name, Eigen::PlainObjectBase<Derived>& attr)
107 {
108 auto itr = m_data.find(name);
109 if (itr == m_data.end()) {
110 std::stringstream err_msg;
111 err_msg << "Attributes::export_attribute() failed: Attribute \"" << name
112 << "\" does not exists.";
113 throw std::runtime_error(err_msg.str());
114 }
115 move_data(itr->second, attr);
116 }
117
118 template <typename Archive>
119 void serialize(Archive& ar)
120 {
121 enum { //
122 DATA = 0,
123 };
125 ar.object([&](auto& ar) { //
126 ar("data", DATA) & m_data;
127 });
128 LA_IGNORE_SHADOW_WARNING_END
129 }
130
131protected:
132 std::map<std::string, AttributeArray> m_data;
133};
134
135template <typename AttributeArray, typename Archive>
136void serialize(Attributes<AttributeArray>& attributes, Archive& ar)
137{
138 attributes.serialize(ar);
139} // end of serialize()
140
141} // namespace lagrange
Legacy attribute class.
Definition: Attributes.h:28
#define LA_IGNORE_SHADOW_WARNING_BEGIN
Ignore shadow warnings.
Definition: warning.h:68
Main namespace for Lagrange.
Definition: AABBIGL.h:30
void move_data(Eigen::DenseBase< Derived1 > &from, Eigen::DenseBase< Derived2 > &to)
Move data from one Eigen obj to another.
Definition: common.h:61