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
{
515 s042372 21
    class Manifold;
511 s042372 22
 
512 s042372 23
    template<typename ITEM, typename ITEMID>
24
    class AttributeVector
25
    {
26
    public:
518 s042372 27
        AttributeVector(size_t _size = 0, ITEM item = ITEM());
511 s042372 28
 
512 s042372 29
        ITEMID add(const ITEM& item);
511 s042372 30
 
31
        // just returning should be ok; manifold and attribs should always be in sync.
32
        // const context means manifold and attribs should be const, hence in sync.
33
        const ITEM& get(ITEMID id) const;
34
        ITEM& get(ITEMID id);
35
 
512 s042372 36
        const ITEM& operator [](ITEMID id) const;
37
        ITEM& operator [](ITEMID id);
511 s042372 38
 
518 s042372 39
        void resize(size_t _size, ITEM item = ITEM());
511 s042372 40
 
518 s042372 41
        size_t size() const;
514 s042372 42
 
515 s042372 43
        void clear();
44
 
45
        void cleanup(const std::map<ITEMID, ITEMID>& map);
46
 
512 s042372 47
    private:
48
        std::vector<ITEM> items;
49
    };
511 s042372 50
 
512 s042372 51
    template<typename ITEM>
52
    class VertexAttributeVector : public AttributeVector<ITEM, VertexID>
53
    {
54
    public:
55
        VertexAttributeVector(const Manifold& m, ITEM item = ITEM());
56
    };
511 s042372 57
 
512 s042372 58
    template<typename ITEM>
59
    class FaceAttributeVector : public AttributeVector<ITEM, FaceID>
60
    {
61
    public:
62
        FaceAttributeVector(const Manifold& m, ITEM item = ITEM());
63
    };
511 s042372 64
 
512 s042372 65
    template<typename ITEM>
66
    class HalfEdgeAttributeVector : public AttributeVector<ITEM, HalfEdgeID>
67
    {
68
    public:
69
        HalfEdgeAttributeVector(const Manifold& m, ITEM item = ITEM());
70
    };
511 s042372 71
 
512 s042372 72
    template<typename ITEM>
73
    inline VertexAttributeVector<ITEM>::VertexAttributeVector(const Manifold& m, ITEM item) : AttributeVector<ITEM, VertexID>(m.no_vertices(false), item){}
511 s042372 74
 
512 s042372 75
    template<typename ITEM>
76
    inline FaceAttributeVector<ITEM>::FaceAttributeVector(const Manifold& m, ITEM item) : AttributeVector<ITEM, FaceID>(m.no_faces(false), item){}
511 s042372 77
 
512 s042372 78
    template<typename ITEM>
79
    inline HalfEdgeAttributeVector<ITEM>::HalfEdgeAttributeVector(const Manifold& m, ITEM item) : AttributeVector<ITEM, HalfEdgeID>(m.no_halfedges(false), item){}
511 s042372 80
 
515 s042372 81
 
512 s042372 82
    template<typename ITEM, typename ITEMID>
518 s042372 83
    inline AttributeVector<ITEM, ITEMID>::AttributeVector(size_t _size, ITEM item) : items(_size, item){}
511 s042372 84
 
512 s042372 85
    template<typename ITEM, typename ITEMID>
515 s042372 86
    inline void AttributeVector<ITEM, ITEMID>::clear()
87
    { items.clear(); }
88
 
89
    template<typename ITEM, typename ITEMID>
90
    inline void AttributeVector<ITEM, ITEMID>::cleanup(const std::map<ITEMID, ITEMID>& remap)
91
    {
92
        std::vector<ITEM> new_items(remap.size());
518 s042372 93
        for(std::map<ITEMID, ITEMID>::const_iterator it = remap.begin(); it != remap.end(); ++it){
94
            assert(it->second.index < remap.size());
515 s042372 95
            new_items[it->second.index] = items[it->first.index];
518 s042372 96
        }
515 s042372 97
        std::swap(items, new_items);
98
    }
99
 
100
 
101
    template<typename ITEM, typename ITEMID>
518 s042372 102
    inline void AttributeVector<ITEM, ITEMID>::resize(size_t _size, ITEM item)
512 s042372 103
    { items.resize(_size, item); }
104
 
105
    template<typename ITEM, typename ITEMID>
518 s042372 106
    inline size_t AttributeVector<ITEM, ITEMID>::size() const
514 s042372 107
    { return items.size(); }
108
 
109
    template<typename ITEM, typename ITEMID>
512 s042372 110
    inline ITEMID AttributeVector<ITEM, ITEMID>::add(const ITEM& item)
111
    { items.push_back(item); return items.size() - 1; }
112
 
113
    template<typename ITEM, typename ITEMID>
114
    inline const ITEM& AttributeVector<ITEM, ITEMID>::get(ITEMID id) const
515 s042372 115
    { 
518 s042372 116
        assert(id.index < items.size());
515 s042372 117
        return items[id.index]; 
118
    }
511 s042372 119
 
512 s042372 120
    template<typename ITEM, typename ITEMID>
511 s042372 121
    inline ITEM& AttributeVector<ITEM, ITEMID>::get(ITEMID id)
122
    {
512 s042372 123
        if(items.size() < id.idx())
515 s042372 124
            items.resize(id.index + 1);
125
        return items[id.index]; 
511 s042372 126
    }
512 s042372 127
 
128
    template<typename ITEM, typename ITEMID>
129
    inline const ITEM& AttributeVector<ITEM, ITEMID>::operator [](ITEMID id) const
515 s042372 130
    { 
518 s042372 131
        assert(id.index < items.size());
515 s042372 132
        return items[id.index]; 
133
    }
511 s042372 134
 
512 s042372 135
    template<typename ITEM, typename ITEMID>
511 s042372 136
    inline ITEM& AttributeVector<ITEM, ITEMID>::operator [](ITEMID id)
137
    {
515 s042372 138
        if(items.size() < id.index)
139
            items.resize(id.index + 1);
140
        return items[id.index]; 
511 s042372 141
    }
142
}
143
 
144
#endif