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