Subversion Repositories gelsvn

Rev

Rev 39 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 39 Rev 67
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
	/** Attribute vector handle. This is simply an index into a vector
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
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
110
			contain the index, but the class is a template, hence the handle
111
			also carries the type information. 
111
			also carries the type information. 
112
			
112
			
113
			The idea of using a handle class which carries both the attribute
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
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
115
			VC 6.0 has a completely brain dead implementation of class member
116
			template functions. It is necessary to let the template argument
116
			template functions. It is necessary to let the template argument
117
			be deduced from the normal function arguments, the template argument
117
			be deduced from the normal function arguments, the template argument
118
			cannot be supplied explicitly. However, this solution is actually 
118
			cannot be supplied explicitly. However, this solution is actually 
119
			quite nice. */
119
			quite nice. */
120
	template<class ATTR>
120
	template<class ATTR>
121
	class AttrHandle
121
	class AttrHandle
122
	{
122
	{
123
		friend class TriMeshData;
123
		friend class TriMeshData;
124
		friend class TriMeshBuilder;
124
		friend class TriMeshBuilder;
125
		int idx;
125
		int idx;
126
 
126
 
127
		AttrHandle(int _idx): idx(_idx) {}
127
		AttrHandle(int _idx): idx(_idx) {}
128
 
128
 
129
	public:
129
	public:
130
 
130
 
131
		AttrHandle(): idx(-1) {}
131
		AttrHandle(): idx(-1) {}
132
		AttrHandle(const AttrHandle& a): idx(a.idx) {}
132
		AttrHandle(const AttrHandle& a): idx(a.idx) {}
133
		const AttrHandle& operator=(const AttrHandle& ah)
133
		const AttrHandle& operator=(const AttrHandle& ah)
134
		{
134
		{
135
			idx = ah.idx;
135
			idx = ah.idx;
136
		}
136
		}
137
 
137
 
138
		int get_idx() const {return idx;}
138
		int get_idx() const {return idx;}
139
				 
139
				 
140
	};
140
	};
141
	
141
	
142
}
142
}
143
#endif
143
#endif
144
 
144