Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

convert.h 7.06 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
4
5
6
#ifndef _CONVERT_H_
#define _CONVERT_H_

#include <limits>
#include <math.h>
#include <iostream>
7
#include "Geometry/vector_gen.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
8
9
10
11
12
13
14

namespace CGoGN
{

/**
 * Conversion d'un T* en U* en fonction de la surcharge des membres
 * Classe abstraite, a surcharger:
15
 * - reserve(), convert(), release() & vectorSize()
Pierre Kraemer's avatar
Pierre Kraemer committed
16
17
 * 
 */
18
class ConvertBuffer
Pierre Kraemer's avatar
Pierre Kraemer committed
19
20
21
22
23
24
25
26
{
protected:
	/**
	 * size of buffer in bytes
	 */
	unsigned int m_size;
	
	/**
Sylvain Thery's avatar
Sylvain Thery committed
27
	 * size of buffer in number of elements
Pierre Kraemer's avatar
Pierre Kraemer committed
28
29
	 */
	unsigned int m_nb;
Sylvain Thery's avatar
Sylvain Thery committed
30

31
32
33
34
35
	/**
	 * size of element in input
	 */
	unsigned int m_szElt;

Pierre Kraemer's avatar
Pierre Kraemer committed
36
37
38
39
	/**
	 * buffer
	 */
	void* m_buffer;
40

Pierre Kraemer's avatar
Pierre Kraemer committed
41
42
public:
	/**
43
	 *
Pierre Kraemer's avatar
Pierre Kraemer committed
44
	 */
45
46
47
	ConvertBuffer() : m_size(0),m_nb(0),m_szElt(0),m_buffer(NULL) {}

	ConvertBuffer(unsigned int sz) : m_size(0),m_nb(0),m_szElt(sz),m_buffer(NULL) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
48
49

	/**
50
	 *
Pierre Kraemer's avatar
Pierre Kraemer committed
51
	 */
52
53
//	virtual ~ConvertBuffer() = 0;
	 ~ConvertBuffer() {}
Pierre Kraemer's avatar
Pierre Kraemer committed
54
55
56

	/**
	 * set the number of element to convert & reserve memory for buffer
57
	 * @param nb nb elements (usually block size when call from VBO)
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	 */
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
59
	virtual void reserve(unsigned int nb) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
60
61

	/**
62
	 * release memory buffer ( and set ptrs to null)
Pierre Kraemer's avatar
Pierre Kraemer committed
63
	 */
64
	 virtual void release() =  0;
Pierre Kraemer's avatar
Pierre Kraemer committed
65
66

	/**
67
	 * convert the buffer
Pierre Kraemer's avatar
Pierre Kraemer committed
68
	 */
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
69
	virtual void convert(const void* ptrIn) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
70
71

	/**
72
	 * get the data size in elements (ex: 3 for Vec3f)
Pierre Kraemer's avatar
Pierre Kraemer committed
73
	 */
74
	virtual unsigned int vectorSize() = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
75
76

	/**
77
	 * get the buffer (void*)
Pierre Kraemer's avatar
Pierre Kraemer committed
78
	 */
79
	void* buffer() { return m_buffer; }
Sylvain Thery's avatar
Sylvain Thery committed
80
81
82
83

	/**
	 * get the size of buffer in bytes
	 */
84
	unsigned int sizeBuffer() { return m_size; }
Sylvain Thery's avatar
Sylvain Thery committed
85

Pierre Kraemer's avatar
Pierre Kraemer committed
86
87
88
89
90
};


/**
* Convertit simplement en castant chaque element
91
* Exemple double ou int float
Pierre Kraemer's avatar
Pierre Kraemer committed
92
*/
93
94
template<typename TYPE_IN>
class ConvertToFloat : public ConvertBuffer
Pierre Kraemer's avatar
Pierre Kraemer committed
95
96
{
protected:
97
98
	float* m_typedBuffer;
	unsigned int m_szVect;
Pierre Kraemer's avatar
Pierre Kraemer committed
99
100

public:
101
102
103
104
105
	ConvertToFloat():
		ConvertBuffer(sizeof(TYPE_IN)),m_szVect(1)
	{}

	~ConvertToFloat()
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
106
107
108
109
	{
		if (m_typedBuffer)
			delete[] m_typedBuffer;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
110
111
112

	void release()
	{
113
114
115
116
117
		delete[] m_typedBuffer;
		m_typedBuffer = NULL;
		m_buffer = NULL;
		m_nb = 0;
		m_size = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
118
119
120
121
	}
	
	void reserve(unsigned int nb)
	{
122
123
		m_nb = nb*m_szVect;						// store number of elements
		m_typedBuffer = new float[m_nb];	// allocate buffer
Pierre Kraemer's avatar
Pierre Kraemer committed
124
		m_buffer = m_typedBuffer;			// store void* casted ptr
125
		m_size = m_nb*sizeof(float);		// store size of buffer in bytes
Pierre Kraemer's avatar
Pierre Kraemer committed
126
127
128
129
130
131
	}
	
	void convert(const void* ptrIn)
	{
		// cast ptr in & out with right type
		const TYPE_IN* typedIn = reinterpret_cast<const TYPE_IN*>(ptrIn);
132
		float* typedOut = reinterpret_cast<float*>(m_buffer);
Pierre Kraemer's avatar
Pierre Kraemer committed
133
		// compute conversion
134
135
136
137
138
139
140
141
142
143
144
145
		for (unsigned int i = 0; i < m_nb; ++i)
			*typedOut++ = float(*typedIn++);
	}

	unsigned int vectorSize()
	{
		return m_szVect;
	}

	void setPseudoVectorSize(unsigned int sz)
	{
		m_szVect = sz;
Pierre Kraemer's avatar
Pierre Kraemer committed
146
147
148
149
150
151
152
153
154
155
	}
};


/**
* Convertit un type scalaire (char, short, int, ...)
* en un autre (float ou double) en le normalisant
* entre 0 et 1
*/
template<typename TYPE_IN, typename TYPE_OUT>
156
class ConvertNormalized : public ConvertBuffer
Pierre Kraemer's avatar
Pierre Kraemer committed
157
158
159
160
161
{
protected:
	TYPE_OUT* m_typedBuffer;
	
public:
162
163
164
165
	ConvertNormalized():
		ConvertBuffer(sizeof(TYPE_IN))
	{}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
166
167
168
169
170
	~ConvertNormalized()
	{
		if (m_typedBuffer)
			delete[] m_typedBuffer;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
171
172
173
174
175
176

	void release()
	{
		if (m_typedBuffer)
		{
			delete[] m_typedBuffer;
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
177
178
			m_typedBuffer = NULL;
			m_buffer = NULL;
179
180
			m_nb = 0;
			m_size = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
181
182
183
184
185
		}
	}
	
	void reserve(unsigned int nb)
	{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
186
		m_nb = nb;							// store number of elements
187
		m_typedBuffer = new TYPE_OUT[m_nb];	// allocate buffer
Pierre Kraemer's avatar
Pierre Kraemer committed
188
		m_buffer = m_typedBuffer;			// store void* casted ptr
189
		m_size = m_nb*sizeof(TYPE_OUT);		// store size of buffer in bytes
Pierre Kraemer's avatar
Pierre Kraemer committed
190
191
192
193
194
195
196
197
198
199
200
	}
	
	void convert(const void* ptrIn)
	{
		// cast ptr in & out with right type
		const TYPE_IN* typedIn = reinterpret_cast<const TYPE_IN*>(ptrIn);
		TYPE_OUT* typedOut = reinterpret_cast<TYPE_OUT*>(m_buffer);
		
		// compute conversion
		for (int i=0; i <m_nb; ++i)
		{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
201
			TYPE_OUT val = (float(*typedIn++) - TYPE_OUT(std::numeric_limits<TYPE_IN>::min()));
Pierre Kraemer's avatar
Pierre Kraemer committed
202
203
204
205
			val /= TYPE_OUT(std::numeric_limits<TYPE_IN>::max()) - TYPE_OUT(std::numeric_limits<TYPE_IN>::min());
			*typedOut++ = val;
		}
	}
206
207
208
209
210
211

	unsigned int vectorSize()
	{
		return 1;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
212
213
};

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
214

Pierre Kraemer's avatar
Pierre Kraemer committed
215
216
217
218
219
220
/**
* Convertit un type scalaire (char, short, int, float ..)
* en une couleur variant suivant un schema hsv
* Un min et max donne les valeurs servant à l'interpolation
*/
template<typename TYPE_IN>
221
class ConvertToRGBf : public ConvertBuffer
Pierre Kraemer's avatar
Pierre Kraemer committed
222
223
224
225
226
{
protected:
	float* m_typedBuffer;
	TYPE_IN m_min;
	TYPE_IN m_diff;
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
227

Pierre Kraemer's avatar
Pierre Kraemer committed
228
public:
229
230
231
	ConvertToRGBf(TYPE_IN min, TYPE_IN max) : ConvertBuffer(sizeof(TYPE_IN)),m_min(min), m_diff(max-min) {}

	~ConvertToRGBf()
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
232
233
234
235
	{
		if (m_typedBuffer)
			delete[] m_typedBuffer;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
236
237
238
239
240
241

	void release()
	{
		if (m_typedBuffer)
		{
			delete[] m_typedBuffer;
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
242
243
			m_typedBuffer = NULL;
			m_buffer = NULL;
244
245
			m_nb = 0;
			m_size = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
246
247
248
249
250
		}
	}
	
	void reserve(unsigned int nb)
	{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
251
		m_nb = nb;							// store number of elements
Pierre Kraemer's avatar
Pierre Kraemer committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
		m_typedBuffer = new float[3*nb];	// allocate buffer
		m_buffer = m_typedBuffer;			// store void* casted ptr
		m_size = 3*nb*sizeof(float);		// store size of buffer in bytes
	}
	
	void convert(const void* ptrIn)
	{
		// cast ptr in & out with right type
		const TYPE_IN* typedIn = reinterpret_cast<const TYPE_IN*>(ptrIn);
		float* typedOut = reinterpret_cast<float*>(m_buffer);
		
		// compute conversion
		for (int i=0; i <m_nb; ++i)
		{
			float h = (360.0f /(m_diff))*(*typedIn++ - m_min); // normalize in 0-360
			int hi = int(floor(h / 60.0f)) % 6;
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
268
			float f = (h / 60.0) - floor(h / 60.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
			float q = 1.0f - f;
			switch(hi)
			{
				case 0:
					*typedOut++ = 0.0f;
					*typedOut++ = f;
					*typedOut++ = 1.0f;
					break;
				case 1:
					*typedOut++ = 0.0f;
					*typedOut++ = 1.0f;
					*typedOut++ = q;
					break;
				case 2:
					*typedOut++ = f;
					*typedOut++ = 1.0f;
					*typedOut++ = 0.0f;
					break;
				case 3:
					*typedOut++ = 1.0f;
					*typedOut++ = q;
					*typedOut++ = 0.0f;
					break;
				case 4:
					*typedOut++ = 1.0f;
					*typedOut++ = 0.0f;
					*typedOut++ = f;
					break;
				case 5:
					*typedOut++ = q;
					*typedOut++ = 0.0f;
					*typedOut++ = 1.0f;
				default:
					break;
			}
		}
	}
306
307
308
309
310

	unsigned int vectorSize()
	{
		return 1;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
311
312
};

313
314
315
316
317
318
319
320
321


class ConvertVec3dToVec3f : public ConvertBuffer
{
protected:
	Geom::Vec3f* m_typedBuffer;

public:
	ConvertVec3dToVec3f():
Sylvain Thery's avatar
Sylvain Thery committed
322
323
		ConvertBuffer(sizeof(Geom::Vec3d)),
		m_typedBuffer(NULL)
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
	{}

	~ConvertVec3dToVec3f()
	{
		if (m_typedBuffer)
			delete[] m_typedBuffer;
	}

	void release()
	{
		delete[] m_typedBuffer;
		m_typedBuffer = NULL;
		m_buffer = NULL;
		m_nb = 0;
		m_size = 0;
	}

	void reserve(unsigned int nb)
	{
		m_nb = nb;							// store number of elements
		m_typedBuffer = new Geom::Vec3f[nb];	// allocate buffer typed (not possible to delete void*)
		m_buffer = m_typedBuffer;			// store void* casted ptr
		m_size = nb*sizeof(Geom::Vec3f);		// store size of buffer in bytes
	}

	void convert(const void* ptrIn)
	{
		// cast ptr in & out with right type
		const Geom::Vec3d* typedIn = reinterpret_cast<const Geom::Vec3d*>(ptrIn);
Sylvain Thery's avatar
Sylvain Thery committed
353
		Geom::Vec3f* typedOut = m_typedBuffer;
354
355
356
357
		// compute conversion
		for (unsigned int i = 0; i < m_nb; ++i)
		{
			const Geom::Vec3d& vd = *typedIn++;
Sylvain Thery's avatar
Sylvain Thery committed
358
			*typedOut++ = Geom::Vec3f(float(vd[0]),float(vd[1]),float(vd[2]));
359
		}
Sylvain Thery's avatar
Sylvain Thery committed
360

361
362
363
364
365
366
367
368
369
370
371
372
	}

	unsigned int vectorSize()
	{
		return 3;
	}
};



typedef ConvertToFloat<double> ConvertDoubleToFloat;

Pierre Kraemer's avatar
Pierre Kraemer committed
373
374
375
}

#endif