Subversion Repositories gelsvn

Rev

Rev 536 | Rev 578 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
512 s042372 1
/* ----------------------------------------------------------------------- *
572 jab 2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
512 s042372 5
 * ----------------------------------------------------------------------- */
511 s042372 6
 
7
#ifndef __HMESH_ATTRIBUTEVECTOR_H__
8
#define __HMESH_ATTRIBUTEVECTOR_H__
9
 
518 s042372 10
#include <cassert>
511 s042372 11
#include <vector>
12
#include <map>
13
 
14
namespace HMesh
15
{
512 s042372 16
    template<typename ITEM, typename ITEMID>
17
    class AttributeVector
18
    {
19
    public:
520 s042372 20
        /// Construct from optional size and item (size should be identical to number of entities in associated container
518 s042372 21
        AttributeVector(size_t _size = 0, ITEM item = ITEM());
511 s042372 22
 
522 s042372 23
        /// Add an item
520 s042372 24
       // ITEMID add(const ITEM& item);
511 s042372 25
 
520 s042372 26
        /// const reference to item given by ID
511 s042372 27
        const ITEM& get(ITEMID id) const;
520 s042372 28
 
29
        /// reference to item given by ID
511 s042372 30
        ITEM& get(ITEMID id);
31
 
520 s042372 32
        /// const reference to item given by ID
512 s042372 33
        const ITEM& operator [](ITEMID id) const;
520 s042372 34
 
35
        /// reference to item given by ID
512 s042372 36
        ITEM& operator [](ITEMID id);
511 s042372 37
 
522 s042372 38
        /// resize the vector (may be necessary if associated container size grows)
518 s042372 39
        void resize(size_t _size, ITEM item = ITEM());
511 s042372 40
 
520 s042372 41
        /// number of attribute items in vector
518 s042372 42
        size_t size() const;
514 s042372 43
 
520 s042372 44
        /// clear the vector
515 s042372 45
        void clear();
46
 
520 s042372 47
        /// clenup unused items from the vector, given by remap from associated container
515 s042372 48
        void cleanup(const std::map<ITEMID, ITEMID>& map);
49
 
512 s042372 50
    private:
51
        std::vector<ITEM> items;
52
    };
511 s042372 53
 
512 s042372 54
    template<typename ITEM>
55
    class VertexAttributeVector : public AttributeVector<ITEM, VertexID>
56
    {
57
    public:
519 s042372 58
        VertexAttributeVector(size_t _size = 0, ITEM item = ITEM());
512 s042372 59
    };
511 s042372 60
 
512 s042372 61
    template<typename ITEM>
62
    class FaceAttributeVector : public AttributeVector<ITEM, FaceID>
63
    {
64
    public:
519 s042372 65
        FaceAttributeVector(size_t _size = 0, ITEM item = ITEM());
512 s042372 66
    };
511 s042372 67
 
512 s042372 68
    template<typename ITEM>
69
    class HalfEdgeAttributeVector : public AttributeVector<ITEM, HalfEdgeID>
70
    {
71
    public:
519 s042372 72
        HalfEdgeAttributeVector(size_t _size = 0, ITEM item = ITEM());
512 s042372 73
    };
511 s042372 74
 
512 s042372 75
    template<typename ITEM>
519 s042372 76
    inline VertexAttributeVector<ITEM>::VertexAttributeVector(size_t _size, ITEM item) : AttributeVector<ITEM, VertexID>(_size, item){}
511 s042372 77
 
512 s042372 78
    template<typename ITEM>
519 s042372 79
    inline FaceAttributeVector<ITEM>::FaceAttributeVector(size_t _size, ITEM item) : AttributeVector<ITEM, FaceID>(_size, item){}
511 s042372 80
 
512 s042372 81
    template<typename ITEM>
519 s042372 82
    inline HalfEdgeAttributeVector<ITEM>::HalfEdgeAttributeVector(size_t _size, ITEM item) : AttributeVector<ITEM, HalfEdgeID>(_size, item){}
511 s042372 83
 
515 s042372 84
 
512 s042372 85
    template<typename ITEM, typename ITEMID>
518 s042372 86
    inline AttributeVector<ITEM, ITEMID>::AttributeVector(size_t _size, ITEM item) : items(_size, item){}
511 s042372 87
 
512 s042372 88
    template<typename ITEM, typename ITEMID>
515 s042372 89
    inline void AttributeVector<ITEM, ITEMID>::clear()
90
    { items.clear(); }
91
 
92
    template<typename ITEM, typename ITEMID>
93
    inline void AttributeVector<ITEM, ITEMID>::cleanup(const std::map<ITEMID, ITEMID>& remap)
94
    {
95
        std::vector<ITEM> new_items(remap.size());
526 jab 96
        for(typename std::map<ITEMID, ITEMID>::const_iterator it = remap.begin(); it != remap.end(); ++it){
518 s042372 97
            assert(it->second.index < remap.size());
515 s042372 98
            new_items[it->second.index] = items[it->first.index];
518 s042372 99
        }
515 s042372 100
        std::swap(items, new_items);
101
    }
102
 
103
 
104
    template<typename ITEM, typename ITEMID>
518 s042372 105
    inline void AttributeVector<ITEM, ITEMID>::resize(size_t _size, ITEM item)
512 s042372 106
    { items.resize(_size, item); }
107
 
108
    template<typename ITEM, typename ITEMID>
518 s042372 109
    inline size_t AttributeVector<ITEM, ITEMID>::size() const
514 s042372 110
    { return items.size(); }
111
 
520 s042372 112
    // just returning should be ok; manifold and attribs should always be in sync.
113
    // const context means manifold and attribs should be const, hence in sync.
512 s042372 114
    template<typename ITEM, typename ITEMID>
115
    inline const ITEM& AttributeVector<ITEM, ITEMID>::get(ITEMID id) const
515 s042372 116
    { 
518 s042372 117
        assert(id.index < items.size());
515 s042372 118
        return items[id.index]; 
119
    }
511 s042372 120
 
512 s042372 121
    template<typename ITEM, typename ITEMID>
511 s042372 122
    inline ITEM& AttributeVector<ITEM, ITEMID>::get(ITEMID id)
123
    {
522 s042372 124
        if(id.index >= items.size())
515 s042372 125
            items.resize(id.index + 1);
126
        return items[id.index]; 
511 s042372 127
    }
512 s042372 128
 
129
    template<typename ITEM, typename ITEMID>
130
    inline const ITEM& AttributeVector<ITEM, ITEMID>::operator [](ITEMID id) const
515 s042372 131
    { 
518 s042372 132
        assert(id.index < items.size());
515 s042372 133
        return items[id.index]; 
134
    }
511 s042372 135
 
512 s042372 136
    template<typename ITEM, typename ITEMID>
511 s042372 137
    inline ITEM& AttributeVector<ITEM, ITEMID>::operator [](ITEMID id)
138
    {
522 s042372 139
        if(id.index >= items.size())
515 s042372 140
            items.resize(id.index + 1);
141
        return items[id.index]; 
511 s042372 142
    }
143
}
144
 
145
#endif