Subversion Repositories gelsvn

Rev

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