| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 44 | #include <geogram/basic/common.h> | ||
| 45 | #include <geogram/basic/attributes.h> | ||
| 46 | #include <geogram/basic/geometry.h> | ||
| 47 | |||
| 48 | namespace GEO { | ||
| 49 | |||
| 50 | /** | ||
| 51 | * \brief An attribute class that can access a vector attribute | ||
| 52 | * as a scalar attribute with vec2,vec3 or vec4 element types | ||
| 53 | */ | ||
| 54 | template <index_t DIM> class Attribute< vecng<DIM,double> > { | ||
| 55 | public: | ||
| 56 | typedef vecng<DIM,double> vec_type; | ||
| 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 | ) { | ||
| 79 | ✗ | AttributeStore* store = manager.find_attribute_store(name); | |
| 80 | ✗ | if(store == nullptr) { | |
| 81 | ✗ | return false; | |
| 82 | } | ||
| 83 | ✗ | if(store->elements_type_matches(typeid(double).name())) { | |
| 84 | ✗ | return (store->dimension() == vec_dim); | |
| 85 | } | ||
| 86 | ✗ | if(store->elements_type_matches(typeid(vec_type).name())) { | |
| 87 | ✗ | return (store->dimension() == 1); | |
| 88 | } | ||
| 89 | ✗ | return false; | |
| 90 | } | ||
| 91 | |||
| 92 | ✗ | void bind(AttributesManager& manager, const std::string& name) { | |
| 93 | ✗ | geo_assert(!is_bound()); | |
| 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(is_defined(manager, name)); | |
| 101 | ✗ | store_observer_.register_me(store_); | |
| 102 | ✗ | } | |
| 103 | |||
| 104 | ✗ | void unbind() { | |
| 105 | ✗ | geo_assert(is_bound()); | |
| 106 | // If the AttributesManager was destroyed before, do not | ||
| 107 | // do anything. This can occur in Lua scripting when using | ||
| 108 | // Attribute wrapper objects. | ||
| 109 | ✗ | if(!store_observer_.disconnected()) { | |
| 110 | ✗ | store_observer_.unregister_me(store_); | |
| 111 | } | ||
| 112 | ✗ | manager_ = nullptr; | |
| 113 | ✗ | store_ = nullptr; | |
| 114 | ✗ | } | |
| 115 | |||
| 116 | bool bind_if_is_defined( | ||
| 117 | AttributesManager& manager, const std::string& name | ||
| 118 | ) { | ||
| 119 | geo_assert(!is_bound()); | ||
| 120 | if(is_defined(manager, name)) { | ||
| 121 | bind(manager, name); | ||
| 122 | return true; | ||
| 123 | } | ||
| 124 | return false; | ||
| 125 | } | ||
| 126 | |||
| 127 | ✗ | bool is_bound() const { | |
| 128 | ✗ | return (store_ != nullptr && !store_observer_.disconnected()); | |
| 129 | } | ||
| 130 | |||
| 131 | ✗ | index_t size() const { | |
| 132 | ✗ | return store_observer_.size(); | |
| 133 | } | ||
| 134 | |||
| 135 | index_t dimension() const { | ||
| 136 | return 1; | ||
| 137 | } | ||
| 138 | |||
| 139 | index_t nb_elements() const { | ||
| 140 | return size() * dimension(); | ||
| 141 | } | ||
| 142 | |||
| 143 | ✗ | AttributesManager* manager() { | |
| 144 | ✗ | return manager_; | |
| 145 | } | ||
| 146 | |||
| 147 | /** | ||
| 148 | * \brief Destroys this attribute in the AttributesManager. | ||
| 149 | * \details On exit, the attribute is no-longer accessible in | ||
| 150 | * the AttributesManager, its name is available again, and | ||
| 151 | * this attribute is in the unbound state. | ||
| 152 | */ | ||
| 153 | void destroy() { | ||
| 154 | geo_assert(is_bound()); | ||
| 155 | store_observer_.unregister_me(store_); | ||
| 156 | manager_->delete_attribute_store(store_); | ||
| 157 | store_ = nullptr; | ||
| 158 | manager_ = nullptr; | ||
| 159 | } | ||
| 160 | |||
| 161 | /** | ||
| 162 | * \brief Binds this Attribute to an AttributesManager if it | ||
| 163 | * already exists in the AttributesManager and types are compatible. | ||
| 164 | * \param[in] manager a reference to the AttributesManager | ||
| 165 | * \param[in] name name of the attribute | ||
| 166 | * \pre !is_bound() | ||
| 167 | * \return \c true if this Attribute was successfully bound | ||
| 168 | */ | ||
| 169 | bool bind_if_is_compatible( | ||
| 170 | AttributesManager& manager, const std::string& name | ||
| 171 | ) { | ||
| 172 | if( is_bound() ) { | ||
| 173 | unbind(); | ||
| 174 | } | ||
| 175 | store_ = manager.find_attribute_store(name); | ||
| 176 | if(store_ != nullptr) { | ||
| 177 | if( !store_->elements_type_matches(typeid(vec_type).name()) ) { | ||
| 178 | store_ = nullptr; | ||
| 179 | return false; | ||
| 180 | } | ||
| 181 | manager_ = &manager; | ||
| 182 | store_observer_.register_me(store_); | ||
| 183 | return true; | ||
| 184 | } | ||
| 185 | return false; | ||
| 186 | } | ||
| 187 | |||
| 188 | /** | ||
| 189 | * \brief Copies all the values from another attribute. | ||
| 190 | * \param[in] rhs the attribute to be copied. | ||
| 191 | * \details rhs needs to have the same size and dimension | ||
| 192 | * as this Attribute. | ||
| 193 | */ | ||
| 194 | void copy(const Attribute<vec_type>& rhs) { | ||
| 195 | geo_assert(rhs.size() == size()); | ||
| 196 | geo_assert(rhs.dimension() == dimension()); | ||
| 197 | for(index_t i=0; i<nb_elements(); ++i) { | ||
| 198 | (*this)[i] = rhs[i]; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | /** | ||
| 204 | * \brief Sets all the elements in this attribute | ||
| 205 | * to a specified value. | ||
| 206 | * \param[in] val the value | ||
| 207 | */ | ||
| 208 | void fill(const vec_type& val) { | ||
| 209 | geo_debug_assert(is_bound()); | ||
| 210 | for(index_t i=0; i<nb_elements(); ++i) { | ||
| 211 | (*this)[i] = val; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | #ifdef GEO_COMPILER_CLANG | ||
| 216 | #pragma clang diagnostic push | ||
| 217 | #pragma clang diagnostic ignored "-Wcast-align" | ||
| 218 | #endif | ||
| 219 | ✗ | vec_type& operator[](index_t i) { | |
| 220 | ✗ | geo_debug_assert(i < size()); | |
| 221 | ✗ | return ((vec_type*)store_observer_.base_addr())[i]; | |
| 222 | } | ||
| 223 | |||
| 224 | const vec_type& operator[](index_t i) const { | ||
| 225 | geo_debug_assert(i < size()); | ||
| 226 | return ((vec_type*)store_observer_.base_addr())[i]; | ||
| 227 | } | ||
| 228 | |||
| 229 | #ifdef GEO_COMPILER_CLANG | ||
| 230 | #pragma clang diagnostic pop | ||
| 231 | #endif | ||
| 232 | |||
| 233 | private: | ||
| 234 | AttributeStoreObserver store_observer_; | ||
| 235 | AttributesManager* manager_; | ||
| 236 | AttributeStore* store_; | ||
| 237 | }; | ||
| 238 | |||
| 239 | } | ||
| 240 | |||
| 241 | #endif | ||
| 242 |