params.cpp 7.69 KB
Newer Older
1 2
#include "params.h"

3 4 5 6 7 8 9 10 11 12
struct param_info
{
	param_info(std::string n, int d, std::string i) : 
		name(n), dimension(d), info(i) { };

	std::string name;
	int dimension;
	std::string info;
};

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#ifdef WIN32
std::map<params::input, const param_info> create_map()
{
	std::map<params::input, const param_info> _map;
	/* 1D Params */
	_map.insert(std::make_pair<params::input, const param_info>(params::COS_TH, param_info("COS_TH", 1, "Cosine of the Half angle")));
	
	/* 2D Params */
	_map.insert(std::make_pair<params::input, const param_info>(params::RUSIN_TH_TD, param_info("RUSIN_TH_TD", 2, "Radialy symmetric Half angle parametrization")));
	
	/* 3D Params */
	_map.insert(std::make_pair<params::input, const param_info>(params::RUSIN_TH_TD_PD, param_info("RUSIN_TH_TD_PD", 3, "Isotropic Half angle parametrization")));
	_map.insert(std::make_pair<params::input, const param_info>(params::ISOTROPIC_TV_TL_DPHI, param_info("ISOTROPIC_TV_TL_DPHI", 3, "Isotropic Light/View angle parametrization")));
	
	/* 4D Params */
	_map.insert(std::make_pair<params::input, const param_info>(params::RUSIN_TH_PH_TD_PD, param_info("RUSIN_TH_PH_TD_PD", 4, "Complete Half angle parametrization")));
	_map.insert(std::make_pair<params::input, const param_info>(params::SPHERICAL_TL_PL_TV_PV, param_info("SPHERICAL_TL_PL_TV_PV", 4, "Complete classical parametrization")));

	/* 6D Param */
	_map.insert(std::make_pair<params::input, const param_info>(params::CARTESIAN, param_info("CARTESIAN", 6, "Complete vector parametrization")));

	return _map;
}
static const std::map<params::input, const param_info> input_map = create_map();
#else

39
// Assing the input params map
40
static const std::map<params::input, const param_info> input_map = {
41
	/* 1D Params */
42
	{params::COS_TH,                {"COS_TH",                1, "Cosine of the Half angle"}},
43 44

	/* 2D Params */
45
	{params::RUSIN_TH_TD,           {"RUSIN_TH_TD",           2, "Radialy symmetric Half angle parametrization"}},
46 47

	/* 3D Params */
48
	{params::RUSIN_TH_TD_PD,        {"RUSIN_TH_TD_PD",        3, "Isotropic Half angle parametrization"}},
49 50 51
	{params::ISOTROPIC_TV_TL_DPHI,  {"ISOTROPIC_TV_TL_DPHI",  3, "Isotropic Light/View angle parametrization"}},
	
	/* 4D Params */
52 53
	{params::RUSIN_TH_PH_TD_PD,     {"RUSIN_TH_PH_TD_PD",     4, "Complete Half angle parametrization"}},
	{params::SPHERICAL_TL_PL_TV_PV, {"SPHERICAL_TL_PL_TV_PV", 4, "Complete classical parametrization"}},
54 55

	/* 6D Params */
56
	{params::CARTESIAN,             {"CARTESIAN",             6, "Complete vector parametrization"}}
57
};
58
#endif
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
void params::to_cartesian(const double* invec, params::input intype,
		double* outvec)
{
	switch(intype)
	{
		// 1D Parametrizations
		case params::COS_TH:
#ifndef USE_HALF
			half_to_cartesian(acos(invec[0]), 0.0, 0.0, 0.0, outvec);
#else
			outvec[0] = sqrt(1.0 - invec[0]*invec[0]);
			outvec[1] = 0;
			outvec[2] = invec[0];							
			outvec[3] = sqrt(1.0 - invec[0]*invec[0]);
			outvec[4] = 0;
			outvec[5] = invec[0];							
#endif
			break;

			// 2D Parametrizations
		case params::COS_TH_TD:
			half_to_cartesian(acos(invec[0]), 0.0, acos(invec[1]), 0.0, outvec);
			break;

		case params::RUSIN_TH_TD:
85
            half_to_cartesian(invec[0], 0.0, invec[1], 0.0, outvec);
86 87 88 89 90 91 92 93
			break;

			// 3D Parametrization
		case params::RUSIN_TH_PH_TD:
			half_to_cartesian(invec[0], invec[1], invec[2], 0.0, outvec);
			break;
		case params::RUSIN_TH_TD_PD:
			half_to_cartesian(invec[0], 0.0, invec[1], invec[2], outvec);
Laurent Belcour's avatar
Laurent Belcour committed
94 95 96
#ifdef DEBUG
			std::cout << outvec[2] << std::endl;
#endif
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
			break;
		case params::ISOTROPIC_TV_TL_DPHI:
			classical_to_cartesian(invec[0], 0.0, invec[1], invec[2], outvec);
			break;

			// 4D Parametrization
		case params::RUSIN_TH_PH_TD_PD:
			half_to_cartesian(invec[0], invec[1], invec[2], invec[3], outvec);
			break;

		case params::SPHERICAL_TL_PL_TV_PV:
			outvec[0] = cos(invec[1])*sin(invec[0]);
			outvec[1] = sin(invec[1])*sin(invec[0]);
			outvec[2] = cos(invec[0]);
			outvec[3] = cos(invec[3])*sin(invec[2]);
			outvec[4] = sin(invec[3])*sin(invec[2]);
			outvec[5] = cos(invec[2]);
			break;

			// 6D Parametrization
		case params::CARTESIAN:
			memcpy(outvec, invec, 6*sizeof(double));
			break;

		default:
			throw("Transformation not implemented, params::to_cartesian");
			break;
	}

}

void params::from_cartesian(const double* invec, params::input outtype,
		double* outvec)
{
	// Compute the half vector
	double half[3] ;
	half[0] = invec[0] + invec[3];
	half[1] = invec[1] + invec[4];
	half[2] = invec[2] + invec[5];
	double half_norm = sqrt(half[0]*half[0] + half[1]*half[1] + half[2]*half[2]);
	half[0] /= half_norm;
	half[1] /= half_norm;
	half[2] /= half_norm;

	// Difference vector 
	double diff[3];

	switch(outtype)
	{
		// 1D Parametrizations
		case params::COS_TH:
			outvec[0] = half[2];
			break;

			// 2D Parametrizations
		case params::COS_TH_TD:
			outvec[0] = half[2];
154
			outvec[1] = half[0]*invec[0] + half[1]*invec[1] + half[2]*invec[2];
155 156 157
			break;
		case params::RUSIN_TH_TD:
			outvec[0] = acos(half[2]);
158
			outvec[2] = acos(half[0]*invec[0] + half[1]*invec[1] + half[2]*invec[2]);
159 160 161 162 163 164
			break;

			// 3D Parametrization
		case params::RUSIN_TH_PH_TD:
			outvec[0] = acos(half[2]);
			outvec[1] = atan2(half[0], half[1]);
165
			outvec[2] = acos(half[0]*invec[0] + half[1]*invec[1] + half[2]*invec[2]);
166
			break;
167 168 169 170 171 172 173 174 175 176 177 178 179
        case params::RUSIN_TH_TD_PD:
            outvec[0] = acos(half[2]);

            // Compute the diff vector
            diff[0] = invec[0];
            diff[1] = invec[1];
            diff[2] = invec[2];
            rotate_normal(diff, -outvec[1]);
            rotate_binormal(diff, -outvec[0]);

            outvec[1] = acos(diff[2]);
            outvec[2] = atan2(diff[0], diff[1]);
            break;
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
		case params::ISOTROPIC_TV_TL_DPHI:
			outvec[0] = acos(invec[2]);
			outvec[1] = 0.0;
			outvec[2] = acos(invec[5]);
			outvec[3] = atan2(invec[1], invec[0]) - atan2(invec[4], invec[3]);
			break;

			// 4D Parametrization
		case params::RUSIN_TH_PH_TD_PD:
			outvec[0] = acos(half[2]);
			outvec[1] = atan2(half[0], half[1]);

			// Compute the diff vector
			diff[0] = invec[0];
			diff[1] = invec[1];
			diff[2] = invec[2];
			rotate_normal(diff, -outvec[1]);
			rotate_binormal(diff, -outvec[0]);

			outvec[2] = acos(diff[2]);
			outvec[3] = atan2(diff[0], diff[1]);
			break;

		case params::SPHERICAL_TL_PL_TV_PV:
			outvec[0] = acos(invec[2]);
			outvec[1] = atan2(invec[0], invec[1]);
			outvec[2] = acos(invec[5]);
			outvec[3] = atan2(invec[3], invec[4]);
Laurent Belcour's avatar
Laurent Belcour committed
208 209 210
#ifdef DEBUG
			std::cout << invec[2] << " - acos -> " << outvec[0] << std::endl;
#endif
211 212 213 214 215 216 217 218
			break;

			// 6D Parametrization
		case params::CARTESIAN:
			memcpy(outvec, invec, 6*sizeof(double));
			break;

		default:
219
        std::cout << "<<ERROR>> not defined input param: " << get_name(outtype) << std::endl;
220 221 222 223
			assert(false);
			break;
	}
}
224 225 226 227 228 229 230 231 232 233
params::input params::parse_input(const std::string& txt)
{
	for(std::map<params::input, const param_info>::const_iterator it=input_map.begin(); it != input_map.end(); ++it)
	{
		if(txt.compare(it->second.name) == 0)
		{
			return it->first;
		}
	}

234
	std::cout << "<<INFO>> the input parametrization is UNKNOWN_INPUT" << std::endl;
235 236 237 238 239 240 241 242 243 244 245
	return params::UNKNOWN_INPUT;
}
		  
std::string params::get_name(const params::input param)
{
	std::map<params::input, const param_info>::const_iterator it = input_map.find(param);
	if(it != input_map.end())
	{
		return it->second.name;
	}

246
	return std::string("UNKNOWN_INPUT");
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

int  params::dimension(params::input t)
{
	std::map<params::input, const param_info>::const_iterator it = input_map.find(t);
	if(it != input_map.end())
	{
		return it->second.dimension;
	}
	else
	{
		return -1;
	}
}

void params::print_input_params()
{
	for(std::map<params::input, const param_info>::const_iterator it=input_map.begin(); it != input_map.end(); ++it)
	{
		std::cout << it->second.name << std::endl;
	}
}