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 5.24 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef _CONVERT_H_
#define _CONVERT_H_

#include <limits>
#include <math.h>
#include <iostream>

namespace CGoGN
{

/**
 * Conversion d'un T* en U* en fonction de la surcharge des membres
 * Classe abstraite, a surcharger:
 * - reserve(), convert() & destructeur (exemple dans ConvertSimpleCast)
 * 
 */
class ConvertAttrib
{
protected:
	/**
	 * size of buffer in bytes
	 */
	unsigned int m_size;
	
	/**
Sylvain Thery's avatar
Sylvain Thery committed
26
	 * size of buffer in number of elements
Pierre Kraemer's avatar
Pierre Kraemer committed
27
28
29
	 */
	unsigned int m_nb;
	
Sylvain Thery's avatar
Sylvain Thery committed
30

Pierre Kraemer's avatar
Pierre Kraemer committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	/**
	 * buffer
	 */
	void* m_buffer;
	
public:
	/**
	 * size of buffer in
	 */
	 ConvertAttrib():m_size(0),m_nb(0),m_buffer(NULL) {}

	/**
	 * size of buffer in
	 */
	virtual ~ConvertAttrib() {}

	/**
	 * set the number of element to convert & reserve memory for buffer
	 */
	virtual void reserve(unsigned int nb)=0;

	/**
	 * release memory buffer ( and set ptr to null)
	 */
	virtual void release();

	/**
	 * convert a table to tbe buffer
	 */
	virtual void convert(const void* ptrIn)=0;

	/**
	 * get the buffer (void*)
	 */
	void* buffer() { return m_buffer;}

	/**
	 * get the size of buffer in bytes
	 */
	unsigned int sizeBuffer() { return m_size;}
Sylvain Thery's avatar
Sylvain Thery committed
71
72
73
74
75
76
77

	/**
	 * get the size of buffer in bytes
	 */
	unsigned int sizeElt() { return m_size/m_nb;}


Pierre Kraemer's avatar
Pierre Kraemer committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
};




/**
* Convertit simplement en castant chaque element
*/
template<typename TYPE_IN, typename TYPE_OUT>
class ConvertSimpleCast : public ConvertAttrib
{
protected:
	TYPE_OUT* m_typedBuffer; 

public:
	~ConvertSimpleCast(){ if (m_typedBuffer) delete[] m_typedBuffer;}

	void release()
	{
		if (m_typedBuffer)
		{
			delete[] m_typedBuffer;
			m_typedBuffer=NULL;
			m_buffer=NULL;
		}
	}
	
	void reserve(unsigned int nb)
	{
		m_nb=nb;							// store number of elements
		m_typedBuffer = new TYPE_OUT[nb];	// allocate buffer typed (not possible to delete void*)
		m_buffer = m_typedBuffer;			// store void* casted ptr
		m_size = nb*sizeof(TYPE_OUT);		// 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);
		TYPE_OUT* typedOut = reinterpret_cast<TYPE_OUT*>(m_buffer);
		// compute conversion
		for (int i=0; i <m_nb; ++i)
			*typedOut++ = TYPE_OUT(*typedIn++);
	}
};




/**
* 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>
class ConvertNormalized : public ConvertAttrib
{
protected:
	TYPE_OUT* m_typedBuffer;
	
public:
	~ConvertNormalized(){ if (m_typedBuffer) delete[] m_typedBuffer;}

	void release()
	{
		if (m_typedBuffer)
		{
			delete[] m_typedBuffer;
			m_typedBuffer=NULL;
			m_buffer=NULL;
		}
	}
	
	void reserve(unsigned int nb)
	{
		m_nb=nb;							// store number of elements
		m_typedBuffer = new TYPE_OUT[nb];	// allocate buffer
		m_buffer = m_typedBuffer;			// store void* casted ptr
		m_size = nb*sizeof(TYPE_OUT);		// 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);
		TYPE_OUT* typedOut = reinterpret_cast<TYPE_OUT*>(m_buffer);
		
		// compute conversion
		for (int i=0; i <m_nb; ++i)
		{
			TYPE_OUT val =(float(*typedIn++) - TYPE_OUT(std::numeric_limits<TYPE_IN>::min()));
			val /= TYPE_OUT(std::numeric_limits<TYPE_IN>::max()) - TYPE_OUT(std::numeric_limits<TYPE_IN>::min());
			*typedOut++ = val;
		}
	}
};

/**
* 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>
class ConvertFloatToRGBf : public ConvertAttrib
{
protected:
	float* m_typedBuffer;
	TYPE_IN m_min;
	TYPE_IN m_diff;
public:
	ConvertFloatToRGBf(TYPE_IN min, TYPE_IN max): m_min(min), m_diff(max-min) {}
	
	~ConvertFloatToRGBf(){ if (m_typedBuffer) delete[] m_typedBuffer;}

	void release()
	{
		if (m_typedBuffer)
		{
			delete[] m_typedBuffer;
			m_typedBuffer=NULL;
			m_buffer=NULL;
		}
	}
	
	void reserve(unsigned int nb)
	{
		m_nb=nb;							// store number of elements
		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;
			float f =  (h / 60.0) - floor(h / 60.0);
			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;
			}
		}
	}
};

}

#endif