GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/HMesh/AttributeVector.h
00001 /* ----------------------------------------------------------------------- *
00002  * This file is part of GEL, www.imm.dtu.dk/GEL
00003  * Copyright (C) the authors (see AUTHORS.txt) and DTU Informatics
00004  *
00005  * Principal authors:
00006  *  Christian Thode Larsen (thode2d@gmail.com)
00007  *  J. Andreas Baerentzen (jab@imm.dtu.dk)
00008  *
00009  * See LICENSE.txt for licensing information
00010  * ----------------------------------------------------------------------- */
00011 
00012 #ifndef __HMESH_ATTRIBUTEVECTOR_H__
00013 #define __HMESH_ATTRIBUTEVECTOR_H__
00014 
00015 #include <cassert>
00016 #include <vector>
00017 #include <map>
00018 
00019 namespace HMesh
00020 {
00021     template<typename ITEM, typename ITEMID>
00022     class AttributeVector
00023     {
00024     public:
00026         AttributeVector(size_t _size = 0, ITEM item = ITEM());
00027 
00029        // ITEMID add(const ITEM& item);
00030 
00032         const ITEM& get(ITEMID id) const;
00033 
00035         ITEM& get(ITEMID id);
00036 
00038         const ITEM& operator [](ITEMID id) const;
00039 
00041         ITEM& operator [](ITEMID id);
00042 
00044         void resize(size_t _size, ITEM item = ITEM());
00045 
00047         size_t size() const;
00048 
00050         void clear();
00051 
00053         void cleanup(const std::map<ITEMID, ITEMID>& map);
00054 
00055     private:
00056         std::vector<ITEM> items;
00057     };
00058 
00059     template<typename ITEM>
00060     class VertexAttributeVector : public AttributeVector<ITEM, VertexID>
00061     {
00062     public:
00063         VertexAttributeVector(size_t _size = 0, ITEM item = ITEM());
00064     };
00065 
00066     template<typename ITEM>
00067     class FaceAttributeVector : public AttributeVector<ITEM, FaceID>
00068     {
00069     public:
00070         FaceAttributeVector(size_t _size = 0, ITEM item = ITEM());
00071     };
00072 
00073     template<typename ITEM>
00074     class HalfEdgeAttributeVector : public AttributeVector<ITEM, HalfEdgeID>
00075     {
00076     public:
00077         HalfEdgeAttributeVector(size_t _size = 0, ITEM item = ITEM());
00078     };
00079 
00080     template<typename ITEM>
00081     inline VertexAttributeVector<ITEM>::VertexAttributeVector(size_t _size, ITEM item) : AttributeVector<ITEM, VertexID>(_size, item){}
00082 
00083     template<typename ITEM>
00084     inline FaceAttributeVector<ITEM>::FaceAttributeVector(size_t _size, ITEM item) : AttributeVector<ITEM, FaceID>(_size, item){}
00085 
00086     template<typename ITEM>
00087     inline HalfEdgeAttributeVector<ITEM>::HalfEdgeAttributeVector(size_t _size, ITEM item) : AttributeVector<ITEM, HalfEdgeID>(_size, item){}
00088 
00089 
00090     template<typename ITEM, typename ITEMID>
00091     inline AttributeVector<ITEM, ITEMID>::AttributeVector(size_t _size, ITEM item) : items(_size, item){}
00092 
00093     template<typename ITEM, typename ITEMID>
00094     inline void AttributeVector<ITEM, ITEMID>::clear()
00095     { items.clear(); }
00096 
00097     template<typename ITEM, typename ITEMID>
00098     inline void AttributeVector<ITEM, ITEMID>::cleanup(const std::map<ITEMID, ITEMID>& remap)
00099     {
00100         std::vector<ITEM> new_items(remap.size());
00101         for(typename std::map<ITEMID, ITEMID>::const_iterator it = remap.begin(); it != remap.end(); ++it){
00102             assert(it->second.index < remap.size());
00103             new_items[it->second.index] = items[it->first.index];
00104         }
00105         std::swap(items, new_items);
00106     }
00107 
00108 
00109     template<typename ITEM, typename ITEMID>
00110     inline void AttributeVector<ITEM, ITEMID>::resize(size_t _size, ITEM item)
00111     { items.resize(_size, item); }
00112 
00113     template<typename ITEM, typename ITEMID>
00114     inline size_t AttributeVector<ITEM, ITEMID>::size() const
00115     { return items.size(); }
00116 
00117     // just returning should be ok; manifold and attribs should always be in sync.
00118     // const context means manifold and attribs should be const, hence in sync.
00119     template<typename ITEM, typename ITEMID>
00120     inline const ITEM& AttributeVector<ITEM, ITEMID>::get(ITEMID id) const
00121     { 
00122         assert(id.index < items.size());
00123         return items[id.index]; 
00124     }
00125 
00126     template<typename ITEM, typename ITEMID>
00127     inline ITEM& AttributeVector<ITEM, ITEMID>::get(ITEMID id)
00128     {
00129         if(id.index >= items.size())
00130             items.resize(id.index + 1);
00131         return items[id.index]; 
00132     }
00133 
00134     template<typename ITEM, typename ITEMID>
00135     inline const ITEM& AttributeVector<ITEM, ITEMID>::operator [](ITEMID id) const
00136     { 
00137         assert(id.index < items.size());
00138         return items[id.index]; 
00139     }
00140 
00141     template<typename ITEM, typename ITEMID>
00142     inline ITEM& AttributeVector<ITEM, ITEMID>::operator [](ITEMID id)
00143     {
00144         if(id.index >= items.size())
00145             items.resize(id.index + 1);
00146         return items[id.index]; 
00147     }
00148 }
00149 
00150 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations