Subversion Repositories gelsvn

Rev

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