params.h 17.5 KB
Newer Older
1 2 3
/* ALTA --- Analysis of Bidirectional Reflectance Distribution Functions

   Copyright (C) 2014 CNRS
4
   Copyright (C) 2013, 2014, 2015 Inria
5 6 7 8 9 10 11

   This file is part of ALTA.

   This Source Code Form is subject to the terms of the Mozilla Public
   License, v. 2.0.  If a copy of the MPL was not distributed with this
   file, You can obtain one at http://mozilla.org/MPL/2.0/.  */

12 13
#pragma once

14 15 16 17 18 19 20 21
#include <string>
#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>

22 23
#include "common.h"

24
/*! \class params
25
 *  \ingroup core
26 27
 *  \brief a static class allowing to change from one parametrization
 *  to another.
28
 *
29 30
 *  Any function object or data object should have an associated
 *  parametrization.
31 32 33 34 35 36
 *
 *  We use the following convention to defined the tangent, normal and
 *  bi-normal of the surface:
 *   * The normal is the upper vector (0, 0, 1)
 *   * The tangent direction is along x direction (1, 0, 0)
 *   * The bi-normal is along the y direction (0, 1, 0)
37 38 39
 */
class params
{
40
    public: // data
41

42 43
		 //! \brief list of all supported parametrization for the input space.
		 //! An unsupported parametrization will go under the name
44 45 46 47 48
		 //! <em>unknown</em>. We use the following notations:
		 //!   * The view vector is \f$\vec{v}\f$
		 //!   * The light vector is \f$\vec{l}\f$
		 //!   * The normal vector is \f$\vec{n}\f$
		 //!   * The reflected vector is \f$\vec{r} = 2\mbox{dot}(\vec{v}, \vec{n})\vec{n} - \vec{v}\f$
49 50
		 enum input
		 {
51 52 53 54 55 56 57 58 59 60 61 62 63
       RUSIN_TH_PH_TD_PD,     /*!< Half-angle parametrization as described in [Rusinkiewicz'98] */
       RUSIN_TH_PH_TD,
       RUSIN_TH_TD_PD,
       RUSIN_TH_TD,           /*!< Half-angle parametrization with no azimutal information */
       RUSIN_VH_VD,           /*!< Half-angle parametrization in vector format. Coordinates are:
                               [\f$\vec{h}_x, \vec{h}_y, \vec{h}_z, \vec{d}_x, \vec{d}_y, 
  									  \vec{d}_z \f$].*/
       RUSIN_VH,              /*!< Half-angle parametrization with no difference direction in 
  								     vector format. Coordinates are: [\f$\vec{h}_x, \vec{h}_y, 
  									  \vec{h}_z\f$]. */
       COS_TH_TD,      /*!< Cosine of the RUSIN_TH_TD parametrization: Coordinates are in 
                         $[\cos_\theta_h,\cos_\theta_d]$. */
       COS_TH,
64

65 66 67 68 69 70
       SCHLICK_TK_PK,         /*!< Schlick's back vector parametrization */
       SCHLICK_VK,            /*!< Schlick's back vector */
       SCHLICK_TL_TK_PROJ_DPHI,/*!< 3D Parametrization where the phi component is projected and
                              the parametrization is centered around the back direction.
  									 \f$[\theta_L, x, y] = [\theta_L, \theta_K \cos(\phi_K), \theta_K \sin(\phi_K)]\f$*/
       COS_TK,                /*!< Schlick's back vector dot product with the normal */
71

72

73 74 75 76
       RETRO_TL_TVL_PROJ_DPHI,/*!< 2D Parametrization where the phi component is projected and
                               the parametrization is centered around the retro direction
  									  \f$[x, y] = [\theta_{VL} \cos(\Delta\phi), \theta_{VL} 
  									  \sin(\Delta\phi)]\f$.*/
77

78
       STEREOGRAPHIC,         /*!< Stereographic projection of the Light and View vectors */
79

80

81 82 83 84 85 86 87 88 89 90 91 92 93
       SPHERICAL_TL_PL_TV_PV, /*!< Light and View vectors represented in spherical coordinates */
       COS_TLV,               /*!< Dot product between the Light and View vector */
       COS_TLR,               /*!< Dot product between the Light and Reflected vector */
       ISOTROPIC_TV_TL,       /*!< Light and View vectors represented in spherical coordinates, */
       ISOTROPIC_TV_TL_DPHI,  /*!< Light and View vectors represented in spherical coordinates,
                                   with the difference of azimutal coordinates in the last component  */
       ISOTROPIC_TV_PROJ_DPHI,/*!< 2D Parametrization where the phi component is projected.
                               Coordinates are: [\f$\theta_v \cos(\Delta\phi), \theta_v 
  									  \sin(\Delta\phi).\f$]*/
       ISOTROPIC_TL_TV_PROJ_DPHI,/*!< 3D Parametrization where the phi component is projected.
                                  Coordinates are: [\f$\theta_l, \theta_v \cos(\Delta\phi), 
  										  \theta_v \sin(\Delta\phi).\f$]*/
       ISOTROPIC_TD_PD,       /*!< Difference between two directions such as R and H */
94

95
       BARYCENTRIC_ALPHA_SIGMA, /*!< Barycentric parametrization defined input Stark et al. [2004].
96 97
                                 Coordinates are: \f$[\alpha, \sigma] = [{1\over 2}(1 - \vec{l}\vec{v}), 
  										 (1-(\vec{h}.\vec{n})^2)(1 - \alpha)]\f$ */
98

99 100 101
       CARTESIAN,             /*!< View and Light vectors represented in cartesian coordinates.
                               We always pack the view vector first: \f$\vec{c} = [v.x, v.y, 
  									  v.z, l.x, l.y, l.z] \f$*/
102

103
       UNKNOWN_INPUT          /*!< Default behaviour. Only use this is you do not fit BRDF data */
104 105 106 107 108 109 110
		 };

		 //! \brief list of all supported parametrization for the output space.
		 //! An unsupported parametrization will go under the name
		 //! <em>unknown</em>.
		 enum output
		 {
111 112 113 114 115
			 INV_STERADIAN,                /*!< Output values in inverse steradian (sr-1). 
														   This is the standard definition for a BRDF. */
			 INV_STERADIAN_COSINE_FACTOR,  /*!< Output values in inverse steradian (sr-1)
			                                    weighted by the cosine factor of the output
															direction. */
116 117 118 119 120
			 ENERGY,
			 RGB_COLOR,
			 XYZ_COLOR,
			 UNKNOWN_OUTPUT
		 };
121 122 123

    public: // methods

124
        //! \brief parse a string to provide a parametrization type.
125
        static params::input parse_input(const std::string& txt);
Laurent Belcour's avatar
Laurent Belcour committed
126

127 128 129
        //! \brief parse a string to provide a parametrization type.
        static params::output parse_output(const std::string& txt);

130 131 132
		  //! \brief look for the string associated with a parametrization
		  //! type.
		  static std::string get_name(const params::input param);
133 134 135 136 137
		  
		  //! \brief look for the string associated with a parametrization
		  //! type.
		  //! \todo Finish this implementation. It requires another static
		  //! object.
138
		  static std::string get_name(const params::output);
Laurent Belcour's avatar
Laurent Belcour committed
139

140 141
        //! \brief static function for input type convertion. This
        //! function allocate the resulting vector.
142 143
        static double* convert(const double* invec, params::input intype,
                               params::input outtype)
144
        {
145 146 147 148 149
            int dim = dimension(outtype); // Get the size of the output vector

            if(dim > 0)
            {
                double* outvec = new double[dim];
150
                double  temvec[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // Temp CARTESIAN vectors
151 152 153 154 155 156 157 158 159 160 161
                to_cartesian(invec, intype, temvec);
                from_cartesian(temvec, outtype, outvec);

                return outvec;
            }
            else
            {
                return NULL;
            }
        }

162 163 164
        //! \brief static function for input type convertion. The outvec
        //! resulting vector should be allocated with the correct
        //! output size.
165
        //! TODO:  RP this function is weird the outtype and intype ARE NEVER USED
166 167 168 169
        static void convert(const double* invec, params::output intype, int indim,
                            params::output outtype, int outdim, double* outvec)
        {
        	// The convertion is done using the cartesian parametrization as
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
    			// an intermediate one. If the two parametrizations are equals
    			// there is no need to perform the conversion.
    			if(outdim == indim)
    			{
    				for(int i=0; i<outdim; ++i) { outvec[i] = invec[i]; }
    			}
    			// If the dimension of the output data is bigger than the 
    			// dimensions of the input domain, and the input domain is of
    			// dimension one, spread the data over all dimensions.
    			else if(indim == 1)
    			{
    				for(int i=0; i<outdim; ++i) { outvec[i] = invec[0]; }
    			}
    			// If the output dimension is one, compute the average of the
    			// input vector values.
    			else if(outdim == 1)
    			{
    				for(int i=0; i<indim; ++i) 
            { 
                outvec[0] += invec[i]; 
            }	
            outvec[0] /= static_cast<double>(indim);
    			}
    			else
    			{
    				NOT_IMPLEMENTED();
    			}
197 198
        }

199 200
        //! \brief static function for input type convertion. The outvec
        //! resulting vector should be allocated with the correct
201
        //! output size.
202 203
        static void convert(const double* invec, params::input intype,
                            params::input outtype, double* outvec)
204
        {
205 206 207 208 209 210 211 212 213 214
			  // The convertion is done using the cartesian parametrization as
			  // an intermediate one. If the two parametrizations are equals
			  // there is no need to perform the conversion.
			  if(intype == outtype)
			  {
				  int dim = dimension(outtype);
				  for(int i=0; i<dim; ++i) { outvec[i] = invec[i]; }
			  }
			  // If the input parametrization is the CARTESIAN param, then 
			  // there is no need to transform the input data.
215
			  else if(intype == params::CARTESIAN)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
			  {
				  from_cartesian(invec, outtype, outvec);
			  }
			  // If the output parametrization is the CARTESIAN param, then
			  // there is no need to convert back to another param.
			  else if(outtype == params::CARTESIAN)
			  {
				  to_cartesian(invec, intype, outvec);
			  }
			  else
			  {
				  // temporary CARTESIAN vector
				  double  temvec[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

				  to_cartesian(invec, intype, temvec);
				  from_cartesian(temvec, outtype, outvec);
			  }
233 234 235 236 237
        }

        //! \brief convert a input vector in a given parametrization to an
        //! output vector in a cartesian parametrization, that is two 3d
        //! vectors concatenated.
238
        static void to_cartesian(const double* invec, params::input intype,
239
                                 double* outvec);
240 241 242

        //! \brief convert a input CARTESIAN vector, that is two 3d vectors
        //! concatenated  to an output vector in a given parametrization.
243
        static void from_cartesian(const double* invec, params::input outtype,
244
                                   double* outvec);
245 246

        //! \brief provide a dimension associated with a parametrization
247
        static int  dimension(params::input t);
248

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        //! \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:
267
                    assert(false);
268 269 270 271 272
                    return -1;
                    break;
            }
        }

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
		  //! \brief Is the value stored weighted by a cosine factor
		  static bool is_cosine_weighted(params::output t)
		  {
			  switch(t)
			  {
				  case params::INV_STERADIAN_COSINE_FACTOR:
					  return true;
					  break;

				  case params::INV_STERADIAN:
				  case params::ENERGY:
				  case params::RGB_COLOR:
				  case params::XYZ_COLOR:
				  default:
					  return false;
					  break;
			  }
		  }

292 293 294 295 296 297 298
        //! \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];
299 300
            half[0] = sin(theta_h)*cos(phi_h);
            half[1] = sin(theta_h)*sin(phi_h);
301 302 303
            half[2] = cos(theta_h);

            // Compute the light vector using the rotation formula.
304 305
            out[0] = sin(theta_d)*cos(phi_d);
            out[1] = sin(theta_d)*sin(phi_d);
306
            out[2] = cos(theta_d);
307

308
				// Rotate the diff vector to get the output vector
309
            rotate_binormal(out, theta_h);
310
            rotate_normal(out, phi_h);
311 312 313 314

            // 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];
315 316 317
            out[3] = -out[0] + 2.0*dot * half[0];
            out[4] = -out[1] + 2.0*dot * half[1];
            out[5] = -out[2] + 2.0*dot * half[2];
318

319 320 321
#ifdef DEBUG
				assert(out[2] >= 0.0 && out[5] >= 0.0);
#endif
322
        }
323 324 325 326 327 328 329 330 331 332 333 334 335
			
        //! \brief from the 4D definition of a classical vector parametrization,
        //! export the cartesian coordinates.
		  static void classical_to_cartesian(double theta_l, double phi_l, 
		                                     double theta_v, double phi_v, double* out)
		  {
			  out[0] = cos(phi_l)*sin(theta_l);
			  out[1] = sin(phi_l)*sin(theta_l);
			  out[2] = cos(theta_l);
			  out[3] = cos(phi_v)*sin(theta_v);
			  out[4] = sin(phi_v)*sin(theta_v);
			  out[5] = cos(theta_v);
		  }
336

337 338 339 340 341 342
		  //! \brief rotate a cartesian vector with respect to the normal of
		  //! theta degrees.
		  static void rotate_normal(double* vec, double theta)
		  {
			  const double cost = cos(theta);
			  const double sint = sin(theta);
343

344
			  const double temp = cost * vec[0] + sint * vec[1];
345

346 347 348
			  vec[1] = cost * vec[1] - sint * vec[0];
			  vec[0] = temp;
		  }
349

350 351 352 353 354 355
		  //! \brief rotate a cartesian vector with respect to the bi-normal of
		  //! theta degrees.
		  static void rotate_binormal(double* vec, double theta)
		  {
			  const double cost = cos(theta);
			  const double sint = sin(theta);
356

357
			  const double temp = cost * vec[0] + sint * vec[2];
358

359 360 361
			  vec[2] = cost * vec[2] - sint * vec[0];
			  vec[0] = temp;
		  }
362

363
		  static void print_input_params();
364

365
};
366 367 368 369 370 371 372 373

/*! \brief A parametrized object. Allow to define function object (either data
 *  or functions that are defined over an input space and output space. This
 *  Object allowas to change the parametrization of the input or output space.
 */
class parametrized
{
	public:
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    parametrized() 
      : _in_param(params::UNKNOWN_INPUT), 
        _out_param(params::UNKNOWN_OUTPUT),
        _nX( 0 ),
        _nY( 0 )
    { 
    }

    parametrized( unsigned int dim_X, unsigned int dim_Y)
    : _in_param(params::UNKNOWN_INPUT), 
      _out_param(params::UNKNOWN_OUTPUT),
      _nX( dim_X ),
      _nY( dim_Y ),
      _min( vec::Zero( _nX+_nY) ),
      _max( vec::Zero( _nX+_nY) )
    {}

		parametrized(params::input in_param, params::output out_param) 
    : _in_param( in_param ),
      _out_param( out_param ),
      _nX( params::dimension(_in_param) ),
      _nY( params::dimension(_out_param) ) ,
      _min( vec::Zero( _nX+_nY) ),
      _max( vec::Zero( _nX+_nY) )
    {
399
		}
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

		//! \brief provide the input parametrization of the object.
		virtual params::input parametrization() const
		{
			return _in_param;
		}
		
		//! \brief provide the input parametrization of the object.
		virtual params::input input_parametrization() const
		{
			return _in_param;
		}
		
		//! \brief provide the outout parametrization of the object.
		virtual params::output output_parametrization() const
		{
			return _out_param;
		}

		//! \brief can set the input parametrization of a non-parametrized
		//! object. Print an error if it is already defined.
		virtual void setParametrization(params::input new_param)
		{
			//! \todo Here is something strange happening. The equality between
			//! those enums is not correct for UNKNOWN_INPUT
			if(_in_param == new_param)
			{
				return;
			}
			else if(_in_param == params::UNKNOWN_INPUT)
			{
				_in_param = new_param;
			}
			else
			{
				std::cout << "<<ERROR>> an input parametrization is already defined: " << params::get_name(_in_param) << std::endl;
436 437
				std::cout << "<<ERROR>> changing to: " << params::get_name(new_param) << std::endl;
				_in_param = new_param;
438 439 440 441 442 443 444 445 446 447 448 449
			}
		}
		
		//! \brief can set the output parametrization of a non-parametrized
		//! function. Throw an exception if it tries to erase a previously
		//! defined one.
		virtual void setParametrization(params::output new_param)
		{
			if(_out_param == new_param)
			{
				return;
			}
450
            else if(_out_param == params::UNKNOWN_OUTPUT)
451 452 453 454 455 456
			{
				_out_param = new_param;
			}
			else
			{
				std::cout << "<<ERROR>> an output parametrization is already defined: " << std::endl;
457
			}      
458 459
		}

460 461 462 463 464 465
    //! \brief Set the input and output parametrizations directly
    virtual void setParametrizations(params::input new_in_param, params::output new_out_param)
    {
      setParametrization( new_in_param);
      setParametrization( new_out_param );
    }
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

		/* DIMENSION OF THE INPUT AND OUTPUT DOMAIN */

		//! Provide the dimension of the input space of the function
		virtual int dimX() const { return _nX ; }
		//! Provide the dimension of the output space of the function
		virtual int dimY() const { return _nY ; }

		//! Set the dimension of the input space of the function
		virtual void setDimX(int nX) { _nX = nX ; }
		//! Set the dimension of the output space of the function
		virtual void setDimY(int nY) { _nY = nY ; }


		/* DEFINITION DOMAIN OF THE FUNCTION */

		//! \brief Set the minimum value the input can take
		virtual void setMin(const vec& min) ;

		//! \brief Set the maximum value the input can take
		virtual void setMax(const vec& max) ;

		//! \brief Get the minimum value the input can take
489
		virtual vec min() const ;
490 491

		//! \brief Get the maximum value the input can take
492
		virtual vec max() const ;
493 494


495 496 497 498
	protected:
		// Input and output parametrization
		params::input  _in_param ;
		params::output _out_param ;
499 500 501 502

		// Dimension of the function & domain of definition.
		int _nX, _nY ;
		vec _min, _max ;
503
};