ResourceManager.cc 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*===========================================================================*\
 *                                                                           *
 *                            OpenVolumeMesh                                 *
 *        Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen         *
 *                        www.openvolumemesh.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenVolumeMesh.                                     *
 *                                                                           *
 *  OpenVolumeMesh is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenVolumeMesh is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenVolumeMesh.  If not,                              *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

#include "ResourceManager.hh"

namespace OpenVolumeMesh {

39 40 41 42 43 44 45 46
ResourceManager::ResourceManager(const ResourceManager &other)
{
   *this = other;
}

ResourceManager::ResourceManager(ResourceManager &&other)
{
   *this = std::move(other);
47 48
}

49 50 51 52 53
ResourceManager& ResourceManager::operator=(const ResourceManager &other)
{
    if (this == &other)
        return *this;

54 55
    assignAllPropertiesFrom<false>(&other);

56 57 58 59 60 61 62 63
    return *this;
}

ResourceManager& ResourceManager::operator=(ResourceManager &&other)
{
    if (this == &other)
        return *this;

64 65
    assignAllPropertiesFrom<true>(&other);

66
    return *this;
67 68
}

69 70
ResourceManager::~ResourceManager() {

71 72 73 74 75 76 77 78
    // Delete persistent props
    clearVec(vertex_props_);
    clearVec(edge_props_);
    clearVec(halfedge_props_);
    clearVec(face_props_);
    clearVec(halfface_props_);
    clearVec(cell_props_);
    clearVec(mesh_props_);
79 80
}

81
void ResourceManager::resize_vprops(size_t _nv) {
82

83
    resize_props(vertex_props_, _nv);
84 85
}

86
void ResourceManager::resize_eprops(size_t _ne) {
87

88 89
    resize_props(edge_props_, _ne);
    resize_props(halfedge_props_, _ne*2u);
90 91
}

92
void ResourceManager::resize_fprops(size_t _nf) {
93

94 95
    resize_props(face_props_, _nf);
    resize_props(halfface_props_, _nf*2u);
96 97
}

98
void ResourceManager::resize_cprops(size_t _nc) {
99

100
    resize_props(cell_props_, _nc);
101 102 103 104
}

void ResourceManager::vertex_deleted(const VertexHandle& _h) {

105
    entity_deleted(vertex_props_, _h);
106 107 108 109
}

void ResourceManager::edge_deleted(const EdgeHandle& _h) {

110 111 112
    entity_deleted(edge_props_, _h);
    entity_deleted(halfedge_props_, OpenVolumeMeshHandle(_h.idx()*2 + 1));
    entity_deleted(halfedge_props_, OpenVolumeMeshHandle(_h.idx()*2));
113 114 115 116
}

void ResourceManager::face_deleted(const FaceHandle& _h) {

117 118 119
    entity_deleted(face_props_, _h);
    entity_deleted(halfface_props_, OpenVolumeMeshHandle(_h.idx()*2 + 1));
    entity_deleted(halfface_props_, OpenVolumeMeshHandle(_h.idx()*2));
120 121 122 123
}

void ResourceManager::cell_deleted(const CellHandle& _h) {

124
    entity_deleted(cell_props_, _h);
125 126
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
void ResourceManager::swap_cell_properties(CellHandle _h1, CellHandle _h2){

    swap_property_elements(cell_props_begin(), cell_props_end(), _h1, _h2);
}

void ResourceManager::swap_face_properties(FaceHandle _h1, FaceHandle _h2){

    swap_property_elements(face_props_begin(), face_props_end(), _h1, _h2);
}

void ResourceManager::swap_halfface_properties(HalfFaceHandle _h1, HalfFaceHandle _h2){

    swap_property_elements(halfface_props_begin(), halfface_props_end(), _h1, _h2);
}

void ResourceManager::swap_edge_properties(EdgeHandle _h1, EdgeHandle _h2){

    swap_property_elements(edge_props_begin(), edge_props_end(), _h1, _h2);
}

void ResourceManager::swap_halfedge_properties(HalfEdgeHandle _h1, HalfEdgeHandle _h2){

    swap_property_elements(halfedge_props_begin(), halfedge_props_end(), _h1, _h2);
}

void ResourceManager::swap_vertex_properties(VertexHandle _h1, VertexHandle _h2){

    swap_property_elements(vertex_props_begin(), vertex_props_end(), _h1, _h2);
}

157
void ResourceManager::release_property(VertexPropHandle _handle) {
158

159
    remove_property(vertex_props_, _handle.uidx());
160 161
}

162
void ResourceManager::release_property(EdgePropHandle _handle) {
163

164
    remove_property(edge_props_, _handle.uidx());
165 166
}

167
void ResourceManager::release_property(HalfEdgePropHandle _handle) {
168

169
    remove_property(halfedge_props_, _handle.uidx());
170 171
}

172
void ResourceManager::release_property(FacePropHandle _handle) {
173

174
    remove_property(face_props_, _handle.uidx());
175 176
}

177
void ResourceManager::release_property(HalfFacePropHandle _handle) {
178

179
    remove_property(halfface_props_, _handle.uidx());
180 181
}

182
void ResourceManager::release_property(CellPropHandle _handle) {
183

184
    remove_property(cell_props_, _handle.uidx());
185 186
}

187
void ResourceManager::release_property(MeshPropHandle _handle) {
188

189
    remove_property(mesh_props_, _handle.idx());
190 191
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
void ResourceManager::delete_multiple_vertex_props(const std::vector<bool>& _tags) {

    Properties::iterator vp_it = vertex_props_.begin();
    Properties::iterator vp_end = vertex_props_.end();
    for(; vp_it != vp_end; ++vp_it) {
        (*vp_it)->delete_multiple_entries(_tags);
    }
}

void ResourceManager::delete_multiple_edge_props(const std::vector<bool>& _tags) {

    Properties::iterator ep_it = edge_props_.begin();
    Properties::iterator ep_end = edge_props_.end();
    for(; ep_it != ep_end; ++ep_it) {
        (*ep_it)->delete_multiple_entries(_tags);
    }
    // Create tags vector for halfedges
    std::vector<bool> hetags;
    for(std::vector<bool>::const_iterator t_it = _tags.begin(),
            t_end = _tags.end(); t_it != t_end; ++t_it) {
        hetags.push_back(*t_it);
        hetags.push_back(*t_it);
    }
    Properties::iterator hep_it = halfedge_props_.begin();
    Properties::iterator hep_end = halfedge_props_.end();
    for(; hep_it != hep_end; ++hep_it) {
        (*hep_it)->delete_multiple_entries(hetags);
    }
}

void ResourceManager::delete_multiple_face_props(const std::vector<bool>& _tags) {

    Properties::iterator fp_it = face_props_.begin();
    Properties::iterator fp_end = face_props_.end();
    for(; fp_it != fp_end; ++fp_it) {
        (*fp_it)->delete_multiple_entries(_tags);
    }
    // Create tags vector for halffaces
    std::vector<bool> hftags;
    for(std::vector<bool>::const_iterator t_it = _tags.begin(),
            t_end = _tags.end(); t_it != t_end; ++t_it) {
        hftags.push_back(*t_it);
        hftags.push_back(*t_it);
    }
    Properties::iterator hfp_it = halfface_props_.begin();
    Properties::iterator hfp_end = halfface_props_.end();
    for(; hfp_it != hfp_end; ++hfp_it) {
        (*hfp_it)->delete_multiple_entries(hftags);
    }
}

void ResourceManager::delete_multiple_cell_props(const std::vector<bool>& _tags) {

    Properties::iterator cp_it = cell_props_.begin();
    Properties::iterator cp_end = cell_props_.end();
    for(; cp_it != cp_end; ++cp_it) {
        (*cp_it)->delete_multiple_entries(_tags);
    }
}

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
template<bool Move>
void ResourceManager::assignProperties(typename std::conditional<Move, Properties&, const Properties&>::type src,
                      Properties &dest)
{
    // If possible, re-use existing properties instead of copying
    // everything blindly.
    Properties out;
    out.reserve(src.size());
    for (BaseProperty *srcprop: src) {
        bool found = false;
        for (auto it = dest.begin(); it != dest.end(); ++it) {
            auto dstprop = *it;
            if (dstprop->name() == srcprop->name())
            {
                // TODO: type check
                out.push_back(dstprop);
                dest.erase(it);
                if (Move) {
                    dstprop->move_values_from(srcprop);
                } else {
                    dstprop->assign_values_from(srcprop);
                }
                found = true;
                break;
            }
        }
        if (!found) {
            if (Move) {
                out.push_back(srcprop);
            } else {
                out.push_back(srcprop->clone(*this, OpenVolumeMeshHandle(-1)));
            }
        }
    }
    updatePropHandles(out);
    dest = std::move(out);
}

template<bool Move>
void ResourceManager::assignAllPropertiesFrom(typename std::conditional<Move, ResourceManager*, const ResourceManager*>::type other)
{
    assignProperties<Move>(other->vertex_props_,   vertex_props_);
    assignProperties<Move>(other->edge_props_,     edge_props_);
    assignProperties<Move>(other->halfedge_props_, halfedge_props_);
    assignProperties<Move>(other->face_props_,     face_props_);
    assignProperties<Move>(other->halfface_props_, halfface_props_);
    assignProperties<Move>(other->cell_props_,     cell_props_);
    assignProperties<Move>(other->mesh_props_,     mesh_props_);
}

302
} // Namespace OpenVolumeMesh