Geogram Version 1.9.6-rc
A programming library of geometric algorithms
Loading...
Searching...
No Matches
vector_attribute.h
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40
41#ifndef GEOGRAM_BASIC_VECTOR_ATTRIBUTE
42#define GEOGRAM_BASIC_VECTOR_ATTRIBUTE
43
47
48namespace GEO {
49
54 template <index_t DIM> class Attribute< vecng<DIM,double> > {
55 public:
57 static constexpr index_t vec_dim = DIM;
58
59 Attribute() {
60 manager_ = nullptr;
61 store_ = nullptr;
62 }
63
64 ~Attribute() {
65 if(is_bound()) {
66 unbind();
67 }
68 }
69
70 Attribute(AttributesManager& manager, const std::string& name) {
71 manager_ = nullptr;
72 store_ = nullptr;
73 bind(manager, name);
74 }
75
76 static bool is_defined(
77 AttributesManager& manager, const std::string& name
78 ) {
80 if(store == nullptr) {
81 return false;
82 }
83 if(!store->elements_type_matches(typeid(double).name())) {
84 return false;
85 }
86 if(store->dimension() != vec_dim) {
87 return false;
88 }
89 return true;
90 }
91
92 void bind(AttributesManager& manager, const std::string& name) {
94 manager_ = &manager;
95 store_ = manager.find_attribute_store(name);
96 if(store_ == nullptr) {
97 store_ = new TypedAttributeStore<double>(vec_dim);
98 manager_->bind_attribute_store(name,store_);
99 }
100 geo_assert(store_->elements_type_matches(typeid(double).name()));
101 geo_assert(store_->dimension() == vec_dim);
102 store_observer_.register_me(store_);
103 }
104
105 void unbind() {
107 // If the AttributesManager was destroyed before, do not
108 // do anything. This can occur in Lua scripting when using
109 // Attribute wrapper objects.
110 if(!store_observer_.disconnected()) {
111 store_observer_.unregister_me(store_);
112 }
113 manager_ = nullptr;
114 store_ = nullptr;
115 }
116
118 AttributesManager& manager, const std::string& name
119 ) {
121 if(is_defined(manager, name)) {
122 bind(manager, name);
123 }
124 }
125
126 bool is_bound() const {
127 return (store_ != nullptr && !store_observer_.disconnected());
128 }
129
130 index_t size() const {
131 return store_observer_.size()/vec_dim;
132 }
133
134 index_t dimension() const {
135 return 1;
136 }
137
139 return manager_;
140 }
141
142#ifdef GEO_COMPILER_CLANG
143#pragma clang diagnostic push
144#pragma clang diagnostic ignored "-Wcast-align"
145#endif
147 geo_debug_assert(i < size());
148 return ((vec_type*)store_observer_.base_addr())[i];
149 }
150
151 const vec_type& operator[](index_t i) const {
152 geo_debug_assert(i < size());
153 return ((vec_type*)store_observer_.base_addr())[i];
154 }
155
156#ifdef GEO_COMPILER_CLANG
157#pragma clang diagnostic pop
158#endif
159
160 private:
161 AttributeStoreObserver store_observer_;
162 AttributesManager* manager_;
163 AttributeStore* store_;
164 };
165
166}
167
168#endif
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:149
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition assert.h:196
Generic mechanism for attributes.
AttributesManager * manager() const
Gets the AttributesManager this Attribute is bound to.
index_t size() const
Gets the size.
bool is_bound() const
Tests whether an Attribute is bound.
void unbind()
Unbinds this Attribute.
static bool is_defined(AttributesManager &manager, const std::string &name, index_t dim=0)
Tests whether an attribute with the specified name and with corresponding type exists in an Attribute...
void bind(AttributesManager &manager, const std::string &name)
Binds this Attribute to an AttributesManager.
bool bind_if_is_defined(AttributesManager &manager, const std::string &name)
Binds this Attribute to an AttributesManager if it already exists in the AttributesManager.
Base class for attributes. They are notified whenever the AttributeStore is modified.
Definition attributes.h:69
index_t dimension() const
Gets the dimension.
Definition attributes.h:107
Notifies a set of AttributeStoreObservers each time the stored array changes size and/or base address...
Definition attributes.h:212
index_t dimension() const
Gets the dimension.
Definition attributes.h:302
virtual bool elements_type_matches(const std::string &type_name) const =0
Tests whether this AttributeStore stores elements of a given type.
Manages an attribute attached to a set of object.
Attribute()
Creates an uninitialized (unbound) Attribute.
T & operator[](index_t i)
Gets a modifiable element by index.
Managers a set of attributes attached to an object.
Definition attributes.h:849
AttributeStore * find_attribute_store(const std::string &name)
Finds an AttributeStore by name.
void bind_attribute_store(const std::string &name, AttributeStore *as)
Binds an AttributeStore with the specified name. Ownership of this AttributeStore is transferred to t...
Stores an array of elements of a given type, and notifies a set of AttributeStoreObservers each time ...
Definition attributes.h:624
Generic maths vector.
Definition vecg.h:70
Common include file, providing basic definitions. Should be included before anything else by all head...
Geometric functions in 2d and 3d.
Global Vorpaline namespace.
Definition basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:329