params.h 13.7 KB
Newer Older
1 2
#pragma once

3 4 5 6 7 8 9 10
#include <string>
#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>

11 12 13 14 15 16 17 18 19 20
/*! \brief a static class allowing to change from one parametrization
 *  to another.
 *  \ingroup core
 *
 *  \details
 *  Any \typedef function object or \typedef data object should have
 *  an associated parametrization.
 */
class params
{
21
    public: // data
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		 //! \brief list of all supported parametrization for the input space.
		 //! An unsupported parametrization will go under the name
		 //! <em>unknown</em>.
		 enum input
		 {
			 ROMEIRO_TH_TD,
			 RUSIN_TH_TD,
			 RUSIN_TH_PH_TD,
			 RUSIN_TH_TD_PD,
			 RUSIN_TH_PH_TD_PD,
			 COS_TH,
			 COS_TH_TD,
			 ISOTROPIC_TV_TL_DPHI,
			 ISOTROPIC_TD_PD, // Difference between two directions such as R and H
			 CARTESIAN,
			 SPHERICAL_TL_PL_TV_PV,
			 UNKNOWN_INPUT
		 };

		 //! \brief list of all supported parametrization for the output space.
		 //! An unsupported parametrization will go under the name
		 //! <em>unknown</em>.
		 enum output
		 {
			 INV_STERADIAN,
			 ENERGY,
			 RGB_COLOR,
			 XYZ_COLOR,
			 UNKNOWN_OUTPUT
		 };
53 54 55

    public: // methods

56 57 58
        //! \brief parse a string to provide a parametrization type.
        static params::input parse_input(const std::string& txt)
        {
59 60 61 62 63 64 65 66 67 68 69
				for(std::map<params::input, const params::param_info>::const_iterator it=input_map.begin(); it != input_map.end(); ++it)
				{
					if(txt.compare(it->second.name) == 0)
					{
						return it->first;
					}
				}

				return params::UNKNOWN_INPUT;

			  /*
70 71 72 73 74 75 76 77
            if(txt == std::string("COS_TH"))
            {
                return params::COS_TH;
            }
            else if(txt == std::string("RUSIN_TH_TD"))
            {
                return params::RUSIN_TH_TD;
            }
78 79 80 81
            else if(txt == std::string("RUSIN_TH_PH_TD_PD"))
            {
                return params::RUSIN_TH_PH_TD_PD;
            }
82 83 84 85
            else
            {
                return params::UNKNOWN_INPUT;
            }
86
				*/
87 88
        }

Laurent Belcour's avatar
Laurent Belcour committed
89 90 91 92 93 94 95 96 97 98 99
		  static std::string get_name(const params::input param)
		  {
			  std::map<params::input, const params::param_info>::const_iterator it = input_map.find(param);
			  if(it != input_map.end())
			  {
				  return it->second.name;
			  }

			  return std::string();
		  }

100 101 102 103 104 105 106 107 108 109 110 111 112
        //! \brief parse a string to provide a parametrization type.
        static params::output parse_output(const std::string& txt)
        {
            if(txt == std::string("ENERGY"))
            {
                return params::ENERGY;
            }
            else
            {
                return params::UNKNOWN_OUTPUT;
            }
        }

113 114
        //! \brief static function for input type convertion. This
        //! function allocate the resulting vector.
115 116
        static double* convert(const double* invec, params::input intype,
                               params::input outtype)
117
        {
118 119 120 121 122
            int dim = dimension(outtype); // Get the size of the output vector

            if(dim > 0)
            {
                double* outvec = new double[dim];
123
                double  temvec[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // Temp CARTESIAN vectors
124 125 126 127 128 129 130 131 132 133 134
                to_cartesian(invec, intype, temvec);
                from_cartesian(temvec, outtype, outvec);

                return outvec;
            }
            else
            {
                return NULL;
            }
        }

135 136
        //! \brief static function for input type convertion. The outvec
        //! resulting vector should be allocated with the correct
137
        //! output size.
138 139
        static void convert(const double* invec, params::input intype,
                            params::input outtype, double* outvec)
140
        {
141
            double  temvec[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // Temp CARTESIAN vectors
142
            to_cartesian(invec, intype, temvec);
143 144 145 146 147
#ifdef DEBUG
            std::cout << "<<DEBUG>> temp vec = ["
                      << temvec[0] << ", " << temvec[1] << ", " << temvec[2] << "] => ["
                      << temvec[3] << ", " << temvec[4] << ", " << temvec[5] << "]" << std::endl;
#endif
148 149 150 151 152 153
            from_cartesian(temvec, outtype, outvec);
        }

        //! \brief convert a input vector in a given parametrization to an
        //! output vector in a cartesian parametrization, that is two 3d
        //! vectors concatenated.
154
        static void to_cartesian(const double* invec, params::input intype,
155 156 157 158
                                 double* outvec)
        {
            switch(intype)
            {
159 160
                // 1D Parametrizations
                case params::COS_TH:
161 162 163 164 165 166 167 168 169 170
#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
171 172
                    break;

173 174 175 176 177 178 179 180 181
                // 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:
                    half_to_cartesian(invec[0], 0.0, invec[1], 0.0, outvec);
                    break;

182 183
                // 3D Parametrization
                case params::RUSIN_TH_PH_TD:
184
                    half_to_cartesian(invec[0], invec[1], invec[2], 0.0, outvec);
185
                    break;
Laurent Belcour's avatar
Laurent Belcour committed
186 187 188
                case params::RUSIN_TH_TD_PD:
                    half_to_cartesian(invec[0], 0.0, invec[1], invec[2], outvec);
                    break;
189 190 191

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

195 196 197 198 199 200 201 202
                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;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

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

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

        }

        //! \brief convert a input CARTESIAN vector, that is two 3d vectors
        //! concatenated  to an output vector in a given parametrization.
218
        static void from_cartesian(const double* invec, params::input outtype,
219 220 221 222 223 224 225 226 227 228 229
                                   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;
230 231 232
            
				// Difference vector 
				double diff[3];
233 234 235

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

241 242 243 244 245
                // 2D Parametrizations
                case params::COS_TH_TD:
                    outvec[0] = half[2];
                    outvec[1] = half[0]*outvec[0] + half[1]*outvec[1] + half[2]*outvec[2];
                    break;
Laurent Belcour's avatar
Laurent Belcour committed
246 247 248 249
                case params::RUSIN_TH_TD:
                    outvec[0] = acos(half[2]);
                    outvec[2] = acos(half[0]*outvec[0] + half[1]*outvec[1] + half[2]*outvec[2]);
                    break;
250 251 252 253 254 255

                // 3D Parametrization
                case params::RUSIN_TH_PH_TD:
                    outvec[0] = acos(half[2]);
                    outvec[1] = atan2(half[0], half[1]);
                    outvec[2] = acos(half[0]*outvec[0] + half[1]*outvec[1] + half[2]*outvec[2]);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
                    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]);
272 273
                    break;

274 275 276 277 278 279 280
					 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]);
						  break;

281 282 283 284 285 286
                // 6D Parametrization
                case params::CARTESIAN:
                    memcpy(outvec, invec, 6*sizeof(double));
                    break;

                default:
287
                    assert(false);
288 289 290 291 292
                    break;
            }
        }

        //! \brief provide a dimension associated with a parametrization
293
        static int  dimension(params::input t)
294
        {
295 296 297 298 299 300 301 302 303 304
			  std::map<params::input, const params::param_info>::const_iterator it = params::input_map.find(t);
			  if(it != params::input_map.end())
			  {
				  return it->second.dimension;
			  }
			  else
			  {
				  return -1;
			  }
			  /*
305 306
            switch(t)
            {
307 308 309 310 311
                // 1D Parametrizations
                case params::COS_TH:
                    return 1;
                    break;

312 313
                // 2D Parametrizations
                case params::ISOTROPIC_TD_PD:
314
                case params::RUSIN_TH_TD:
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
                case params::ROMEIRO_TH_TD:
                case params::COS_TH_TD:
                    return 2;
                    break;

                // 3D Parametrization
                case params::RUSIN_TH_PH_TD:
                case params::RUSIN_TH_TD_PD:
                case params::ISOTROPIC_TV_TL_DPHI:
                    return 3;
                    break;

                // 4D Parametrization
                case params::RUSIN_TH_PH_TD_PD:
                case params::SPHERICAL_TL_PL_TV_PV:
                    return 4;
                    break;

                // 6D Parametrization
                case params::CARTESIAN:
                    return 6;
                    break;

                default:
339
                    assert(false);
340 341 342
                    return -1;
                    break;
            }
343
				*/
344 345
        }

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        //! \brief provide a dimension associated with a parametrization
        static int  dimension(params::output t)
        {
            switch(t)
            {
                // 1D Parametrizations
                case params::INV_STERADIAN:
                case params::ENERGY:
                    return 1;
                    break;

                // 3D Parametrization
                case params::RGB_COLOR:
                case params::XYZ_COLOR:
                    return 3;
                    break;

                default:
364
                    assert(false);
365 366 367 368 369
                    return -1;
                    break;
            }
        }

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
        //! \brief from the 4D definition of a half vector parametrization,
        //! export the cartesian coordinates.
        static void half_to_cartesian(double theta_h, double phi_h,
                                      double theta_d, double phi_d, double* out)
        {
            // Calculate the half vector
            double half[3];
            half[0] = sin(theta_h)*cos(phi_h);
            half[1] = sin(theta_h)*sin(phi_h);
            half[2] = cos(theta_h);

            // Compute the light vector using the rotation formula.
            out[0] = sin(theta_d)*cos(phi_d);
            out[1] = sin(theta_d)*sin(phi_d);
            out[2] = cos(theta_d);
385 386 387 388 389

				//! \todo investigate here, the rotation along N should be
				//1 of phi_h not theta_h !
            rotate_normal(out, phi_h);
            rotate_binormal(out, theta_h);
390 391 392 393

            // Compute the out vector from the in vector and the half
            // vector.
            const double dot = out[0]*half[0] + out[1]*half[1] + out[2]*half[2];
394 395 396 397 398
            out[3] = -out[0] + (dot+1.0) * half[0];
            out[4] = -out[1] + (dot+1.0) * half[1];
            out[5] = -out[2] + (dot+1.0) * half[2];

				assert(out[5] >= 0.0);
399 400
        }

401
        //! \brief rotate a cartesian vector with respect to the normal of
402
        //! theta degrees.
403
        static void rotate_normal(double* vec, double theta)
404 405 406
        {
            const double cost = cos(theta);
            const double sint = sin(theta);
407 408 409

            vec[0] = cost * vec[0] - sint * vec[1];
            vec[1] = sint * vec[0] + cost * vec[1];
410 411
        }

412
        //! \brief rotate a cartesian vector with respect to the bi-normal of
413
        //! theta degrees.
414
        static void rotate_binormal(double* vec, double theta)
415 416 417 418
        {
            const double cost = cos(theta);
            const double sint = sin(theta);

419 420
            vec[0] = cost * vec[0] - sint * vec[2];
            vec[2] = sint * vec[0] + cost * vec[2];
421
        }
422 423 424 425 426 427 428 429 430

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

Laurent Belcour's avatar
Laurent Belcour committed
431

432 433 434 435 436 437 438 439 440 441 442 443 444
	 protected:

		  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;
		  };

		  static const std::map<params::input, const params::param_info> input_map;
445
};