Subversion Repositories gelsvn

Rev

Rev 39 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
39 bj 1
#ifndef __IMESH_ATTRVEC_H__
2
#define __IMESH_ATTRVEC_H__
3
 
4
#include <string>
5
#include <vector>
6
 
7
#include "CGLA/Vec4f.h"
8
#include "CGLA/Vec3f.h"
9
#include "CGLA/Vec3i.h"
10
 
11
namespace IMesh
12
{
89 jab 13
		typedef std::vector<CGLA::Vec3i> FaceSet;
39 bj 14
 
89 jab 15
		class AttrVecIf
39 bj 16
		{
89 jab 17
				const std::string name;
39 bj 18
		public:
89 jab 19
				AttrVecIf::AttrVecIf(const std::string& _name): name(_name) {}
20
				virtual ~AttrVecIf() {}
39 bj 21
 
89 jab 22
				const std::string& get_name() const {return name;}
39 bj 23
 
89 jab 24
				virtual int size() const = 0;
39 bj 25
 
89 jab 26
				virtual void resize(int n) = 0;
39 bj 27
 
89 jab 28
				virtual AttrVecIf* clone() const = 0;
39 bj 29
		};
30
 
89 jab 31
		template<class ATTR>
39 bj 32
		class AttrVec: public AttrVecIf
33
		{
89 jab 34
				std::vector<ATTR> attributes;
39 bj 35
		public:
89 jab 36
				AttrVec(const std::string& name): AttrVecIf(name) {}
39 bj 37
 
89 jab 38
				ATTR& get(int i) 
39
						{
40
								assert(i<attributes.size());
41
								return attributes[i];
42
						}
39 bj 43
 
89 jab 44
				const ATTR& get(int i) const 
45
						{
46
								assert(i<attributes.size());
47
								return attributes[i];
48
						}
39 bj 49
 
89 jab 50
				void push_back(const ATTR& attr)
51
						{
52
								attributes.push_back(attr);
53
						}
39 bj 54
 
89 jab 55
				int size() const
56
						{
57
								return attributes.size();
58
						}
39 bj 59
 
89 jab 60
				void resize(int n) 
61
						{
62
								attributes.resize(n);
63
						}
39 bj 64
 
89 jab 65
				virtual AttrVecIf* clone() const
66
						{
67
								AttrVec<ATTR>* twin = new AttrVec<ATTR>(get_name());
68
								twin->attributes = attributes;
69
								return twin;
70
						}
39 bj 71
 
72
		};
73
 
89 jab 74
		template<class ATTR> 
75
		inline ATTR& attr(AttrVecIf& avec, int i) 
76
		{
39 bj 77
//#ifdef NDEBUG 
89 jab 78
				return static_cast<AttrVec<ATTR>&>(avec).get(i);
39 bj 79
//#else
80
//		return dynamic_cast<AttrVec<ATTR>&>(avec).get(i);
81
//#endif
89 jab 82
		}
39 bj 83
 
89 jab 84
		template<class ATTR> 
85
		const ATTR& attr(const AttrVecIf& avec, int i) 
86
		{
39 bj 87
//#ifdef NDEBUG
89 jab 88
				return static_cast<const AttrVec<ATTR>&>(avec).get(i);
39 bj 89
//#else
90
//		return dynamic_cast<const AttrVec<ATTR>&>(avec).get(i);
91
//#endif
89 jab 92
		}
39 bj 93
 
89 jab 94
		template<class ATTR> 
95
		void push_back_attr(AttrVecIf& avec, const ATTR& attr) 
96
		{
39 bj 97
//#ifdef NDEBUG 
89 jab 98
				static_cast<AttrVec<ATTR>&>(avec).push_back(attr);
39 bj 99
//#else
100
//		dynamic_cast<AttrVec<ATTR>&>(avec).push_back(attr);
101
//#endif
89 jab 102
		}
39 bj 103
 
104
 
89 jab 105
		class TriMeshData;
106
		class TriMeshBuilder;
39 bj 107
 
89 jab 108
		/** \brief Attribute vector handle. 
109
 
110
		This is simply an index into a vector
111
		of attribute vectors. The only data in this struct is an integer
112
		contain the index, but the class is a template, hence the handle
113
		also carries the type information. 
39 bj 114
 
89 jab 115
		The idea of using a handle class which carries both the attribute
116
		type and the index of the attribute vector was fostered because
117
		VC 6.0 has a completely brain dead implementation of class member
118
		template functions. It is necessary to let the template argument
119
		be deduced from the normal function arguments, the template argument
120
		cannot be supplied explicitly. However, this solution is actually 
121
		quite nice. */
122
		template<class ATTR>
123
		class AttrHandle
124
		{
125
				friend class TriMeshData;
126
				friend class TriMeshBuilder;
127
				int idx;
39 bj 128
 
89 jab 129
				AttrHandle(int _idx): idx(_idx) {}
39 bj 130
 
89 jab 131
		public:
39 bj 132
 
89 jab 133
				AttrHandle(): idx(-1) {}
134
				AttrHandle(const AttrHandle& a): idx(a.idx) {}
135
				const AttrHandle& operator=(const AttrHandle& ah)
136
						{
137
								idx = ah.idx;
138
						}
39 bj 139
 
89 jab 140
				int get_idx() const {return idx;}
39 bj 141
 
89 jab 142
		};
39 bj 143
 
144
}
145
#endif