Subversion Repositories gelsvn

Rev

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