Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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