params.h 9.36 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
/*! \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.
18 19 20 21 22 23
 *
 *  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)
24 25 26
 */
class params
{
27
    public: // data
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 53 54 55 56 57 58
		 //! \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
		 };
59 60 61

    public: // methods

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

65 66 67
		  //! \brief look for the string associated with a parametrization
		  //! type.
		  static std::string get_name(const params::input param);
Laurent Belcour's avatar
Laurent Belcour committed
68

69 70 71 72 73 74 75 76 77 78 79 80 81
        //! \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;
            }
        }

82 83
        //! \brief static function for input type convertion. This
        //! function allocate the resulting vector.
84 85
        static double* convert(const double* invec, params::input intype,
                               params::input outtype)
86
        {
87 88 89 90 91
            int dim = dimension(outtype); // Get the size of the output vector

            if(dim > 0)
            {
                double* outvec = new double[dim];
92
                double  temvec[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // Temp CARTESIAN vectors
93 94 95 96 97 98 99 100 101 102 103
                to_cartesian(invec, intype, temvec);
                from_cartesian(temvec, outtype, outvec);

                return outvec;
            }
            else
            {
                return NULL;
            }
        }

104 105
        //! \brief static function for input type convertion. The outvec
        //! resulting vector should be allocated with the correct
106
        //! output size.
107 108
        static void convert(const double* invec, params::input intype,
                            params::input outtype, double* outvec)
109
        {
110
            double  temvec[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // Temp CARTESIAN vectors
111
            to_cartesian(invec, intype, temvec);
112 113 114 115 116
#ifdef DEBUG
            std::cout << "<<DEBUG>> temp vec = ["
                      << temvec[0] << ", " << temvec[1] << ", " << temvec[2] << "] => ["
                      << temvec[3] << ", " << temvec[4] << ", " << temvec[5] << "]" << std::endl;
#endif
117 118 119 120 121 122
            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.
123
        static void to_cartesian(const double* invec, params::input intype,
124
                                 double* outvec);
125 126 127

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

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

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        //! \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:
152
                    assert(false);
153 154 155 156 157
                    return -1;
                    break;
            }
        }

158 159 160 161 162 163 164
        //! \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];
165 166
            half[0] = sin(theta_h)*sin(phi_h);
            half[1] = sin(theta_h)*cos(phi_h);
167 168 169
            half[2] = cos(theta_h);

            // Compute the light vector using the rotation formula.
170 171
            out[0] = sin(theta_d)*sin(phi_d);
            out[1] = sin(theta_d)*cos(phi_d);
172
            out[2] = cos(theta_d);
173

174
				// Rotate the diff vector to get the output vector
175
            rotate_binormal(out, theta_h);
176
            rotate_normal(out, phi_h);
177 178 179 180

            // 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];
181 182 183
            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];
184

185 186 187
#ifdef DEBUG
				assert(out[2] >= 0.0 && out[5] >= 0.0);
#endif
188
        }
189 190 191 192 193 194 195 196 197 198 199 200 201
			
        //! \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);
		  }
202

203 204 205 206 207 208
		  //! \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);
209

210
			  const double temp = cost * vec[0] + sint * vec[1];
211

212 213 214
			  vec[1] = cost * vec[1] - sint * vec[0];
			  vec[0] = temp;
		  }
215

216 217 218 219 220 221
		  //! \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);
222

223
			  const double temp = cost * vec[0] + sint * vec[2];
224

225 226 227
			  vec[2] = cost * vec[2] - sint * vec[0];
			  vec[0] = temp;
		  }
228

229
		  static void print_input_params();
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

/*! \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:
		parametrized() : _in_param(params::UNKNOWN_INPUT), 
		                 _out_param(params::UNKNOWN_OUTPUT) { }

		//! \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;
				std::cout << "<<ERROR>> trying to change to: " << params::get_name(new_param) << std::endl;
			}
		}
		
		//! \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;
			}
291
            else if(_out_param == params::UNKNOWN_OUTPUT)
292 293 294 295 296 297 298 299 300 301 302 303 304 305
			{
				_out_param = new_param;
			}
			else
			{
				std::cout << "<<ERROR>> an output parametrization is already defined: " << std::endl;
			}
		}

	protected:
		// Input and output parametrization
		params::input  _in_param ;
		params::output _out_param ;
};