convert.h 2.28 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

namespace CGoGN
{
11
namespace DataConversion
Pierre Kraemer's avatar
Pierre Kraemer committed
12
{
Sylvain Thery's avatar
Sylvain Thery committed
13
14
15
16
/**
 * function that convert a Vec of double into a Vec of float
 * template param D: dimension of vector
 */
17
18
19
20
21
22
23
24
template< unsigned int D>
inline Geom::Vector<D,float> funcVecXdToVecXf(const Geom::Vector<D,double>& x)
{
	Geom::Vector<D,float> v;
	for (unsigned int j=0; j<D; ++j)
		v[j] = float(x[j]);
	return v;
}
Sylvain Thery's avatar
Sylvain Thery committed
25

Sylvain Thery's avatar
Sylvain Thery committed
26
27
28
29
/**
 * function that convert a scalar (char/short/int/double) into float
 * template param TYPE_IN: scalar type to convert
 */
30
31
32
33
34
template<typename TYPE_IN>
inline float funcToFloat(const TYPE_IN& x)
{
	return float(x);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
35

Sylvain Thery's avatar
Sylvain Thery committed
36
37
38
39
40
/**
 * functor that convert a scalar (char/short/int/double) into float normalized in [0,1]
 * template param TYPE_IN: scalar type to convert
 * Constructor params: min/max values of data for normalization
 */
41
template<typename TYPE_IN>
Sylvain Thery's avatar
Sylvain Thery committed
42
class functorToFloatNormalized
Pierre Kraemer's avatar
Pierre Kraemer committed
43
44
{
protected:
45
46
	double m_min;
	double m_diff;
Pierre Kraemer's avatar
Pierre Kraemer committed
47
public:
Sylvain Thery's avatar
Sylvain Thery committed
48
	functorToFloatNormalized(TYPE_IN min, TYPE_IN max) :
49
50
		m_min(double(min)),
		m_diff(double(max-min)) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
51

52
	inline float operator() (const TYPE_IN& x)
Pierre Kraemer's avatar
Pierre Kraemer committed
53
	{
54
55
		double v = (double(x) - m_min)/ m_diff;
		return float(v);
Pierre Kraemer's avatar
Pierre Kraemer committed
56
57
58
	}
};

Sylvain Thery's avatar
Sylvain Thery committed
59
60
61
62
63
/**
 * functor that convert a scalar (char/short/int/double) into a RGB color (Vec3f)
 * template param TYPE_IN: scalar type to convert
 * Constructor params: min/max values of data for normalization
 */
64
template<typename TYPE_IN>
Sylvain Thery's avatar
Sylvain Thery committed
65
class functorScalarToRGBf
Pierre Kraemer's avatar
Pierre Kraemer committed
66
67
{
protected:
68
69
	double m_min;
	double m_diff;
Pierre Kraemer's avatar
Pierre Kraemer committed
70
public:
Sylvain Thery's avatar
Sylvain Thery committed
71
	functorScalarToRGBf(TYPE_IN min, TYPE_IN max) :
72
73
		m_min(double(min)),
		m_diff(double(max-min)) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
74

75
	inline Geom::Vec3f operator() (const TYPE_IN& x)
Pierre Kraemer's avatar
Pierre Kraemer committed
76
	{
77
78
79
80
81
		double h = (360.0 /m_diff) * (double(x) - m_min); // normalize in 0-360
		int hi = int(floor(h / 60.0)) % 6;
		float f = float((h / 60.0) - floor(h / 60.0));
		float q = 1.0f - f;
		switch(hi)
Pierre Kraemer's avatar
Pierre Kraemer committed
82
		{
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
			case 0:
			return Geom::Vec3f(0.0f,f,1.0f);
				break;
			case 1:
			return Geom::Vec3f(0.0f,1.0f,q);
				break;
			case 2:
			return Geom::Vec3f(f,1.0f,0.0f);
				break;
			case 3:
			return Geom::Vec3f(1.0f,q,0.0f);
				break;
			case 4:
			return Geom::Vec3f(1.0f,0.0f,f);
				break;
			case 5:
			return Geom::Vec3f(q,0.0f,1.0f);
			default:
				break;
Pierre Kraemer's avatar
Pierre Kraemer committed
102
		}
103
		return Geom::Vec3f(0.0f,0.0f,0.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
104
105
106
	}
};

107
}
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
108

Pierre Kraemer's avatar
Pierre Kraemer committed
109
110
111
}

#endif