Subversion Repositories gelsvn

Rev

Rev 67 | 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
{
94 bj 13
		typedef std::vector<CGLA::Vec3i> FaceSet;
39 bj 14
 
94 bj 15
		class AttrVecIf
39 bj 16
		{
94 bj 17
				const std::string name;
39 bj 18
		public:
94 bj 19
				AttrVecIf::AttrVecIf(const std::string& _name): name(_name) {}
20
				virtual ~AttrVecIf() {}
39 bj 21
 
94 bj 22
				const std::string& get_name() const {return name;}
39 bj 23
 
94 bj 24
				virtual int size() const = 0;
39 bj 25
 
94 bj 26
				virtual void resize(int n) = 0;
39 bj 27
 
94 bj 28
				virtual AttrVecIf* clone() const = 0;
39 bj 29
		};
30
 
94 bj 31
		template<class ATTR>
39 bj 32
		class AttrVec: public AttrVecIf
33
		{
94 bj 34
				std::vector<ATTR> attributes;
39 bj 35
		public:
94 bj 36
				AttrVec(const std::string& name): AttrVecIf(name) {}
39 bj 37
 
94 bj 38
				ATTR& get(int i) 
39
						{
40
								assert(i<attributes.size());
41
								return attributes[i];
42
						}
39 bj 43
 
94 bj 44
				const ATTR& get(int i) const 
45
						{
46
								assert(i<attributes.size());
47
								return attributes[i];
48
						}
39 bj 49
 
94 bj 50
				void push_back(const ATTR& attr)
51
						{
52
								attributes.push_back(attr);
53
						}
39 bj 54
 
94 bj 55
				int size() const
56
						{
57
								return attributes.size();
58
						}
39 bj 59
 
94 bj 60
				void resize(int n) 
61
						{
62
								attributes.resize(n);
63
						}
39 bj 64
 
94 bj 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
 
94 bj 74
		template<class ATTR> 
75
		inline ATTR& attr(AttrVecIf& avec, int i) 
76
		{
39 bj 77
//#ifdef NDEBUG 
94 bj 78
				return static_cast<AttrVec<ATTR>&>(avec).get(i);
39 bj 79
//#else
80
//		return dynamic_cast<AttrVec<ATTR>&>(avec).get(i);
81
//#endif
94 bj 82
		}
39 bj 83
 
94 bj 84
		template<class ATTR> 
85
		const ATTR& attr(const AttrVecIf& avec, int i) 
86
		{
39 bj 87
//#ifdef NDEBUG
94 bj 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
94 bj 92
		}
39 bj 93
 
94 bj 94
		template<class ATTR> 
95
		void push_back_attr(AttrVecIf& avec, const ATTR& attr) 
96
		{
39 bj 97
//#ifdef NDEBUG 
94 bj 98
				static_cast<AttrVec<ATTR>&>(avec).push_back(attr);
39 bj 99
//#else
100
//		dynamic_cast<AttrVec<ATTR>&>(avec).push_back(attr);
101
//#endif
94 bj 102
		}
39 bj 103
 
104
 
94 bj 105
		class TriMeshData;
106
		class TriMeshBuilder;
39 bj 107
 
94 bj 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
 
94 bj 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
 
94 bj 129
				AttrHandle(int _idx): idx(_idx) {}
39 bj 130
 
94 bj 131
		public:
39 bj 132
 
94 bj 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
 
94 bj 140
				int get_idx() const {return idx;}
39 bj 141
 
94 bj 142
		};
39 bj 143
 
144
}
145
#endif