Subversion Repositories gelsvn

Rev

Rev 89 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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