params.h 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#pragma once

/*! \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
{
13
    public: // data
14

15 16 17 18
        //! \brief list of all supported parametrization for the input space.
        //! An unsupported parametrization will go under the name
        //! <em>unknown</em>.
        enum input
19 20
        {
            ROMEIRO_TH_TD,
21
            RUSIN_TH_TD,
22 23 24
            RUSIN_TH_PH_TD,
            RUSIN_TH_TD_PD,
            RUSIN_TH_PH_TD_PD,
25
            COS_TH,
26 27 28 29 30
            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,
31 32 33 34 35 36 37 38 39 40 41 42 43
            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
44 45 46 47
        };

    public: // methods

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        //! \brief parse a string to provide a parametrization type.
        static params::input parse_input(const std::string& txt)
        {
            if(txt == std::string("COS_TH"))
            {
                return params::COS_TH;
            }
            else if(txt == std::string("RUSIN_TH_TD"))
            {
                return params::RUSIN_TH_TD;
            }
            else
            {
                return params::UNKNOWN_INPUT;
            }
        }

        //! \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;
            }
        }

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

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

                return outvec;
            }
            else
            {
                return NULL;
            }
        }

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

129 130 131 132 133 134 135 136 137
                // 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;

138 139
                // 3D Parametrization
                case params::RUSIN_TH_PH_TD:
140
                    half_to_cartesian(invec[0], invec[1], invec[2], 0.0, outvec);
141 142 143 144
                    break;

                // 4D Parametrization
                case params::RUSIN_TH_PH_TD_PD:
145
                    half_to_cartesian(invec[0], invec[1], invec[2], invec[3], outvec);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                    break;


                // 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.
163
        static void from_cartesian(const double* invec, params::input outtype,
164 165 166 167 168 169 170 171 172 173 174 175 176 177
                                   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;

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

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
                // 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;

                // 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]);
                    break;

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

                default:
202
                    assert(false);
203 204 205 206 207
                    break;
            }
        }

        //! \brief provide a dimension associated with a parametrization
208
        static int  dimension(params::input t)
209 210 211
        {
            switch(t)
            {
212 213 214 215 216
                // 1D Parametrizations
                case params::COS_TH:
                    return 1;
                    break;

217 218
                // 2D Parametrizations
                case params::ISOTROPIC_TD_PD:
219
                case params::RUSIN_TH_TD:
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                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:
244
                    assert(false);
245 246 247 248 249
                    return -1;
                    break;
            }
        }

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

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        //! \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);
289 290
            rotate_binormal(out, phi_h);
            rotate_normal(out, theta_h);
291 292 293 294 295 296 297 298 299

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

300
        //! \brief rotate a cartesian vector with respect to the normal of
301
        //! theta degrees.
302
        static void rotate_normal(double* vec, double theta)
303 304 305
        {
            const double cost = cos(theta);
            const double sint = sin(theta);
306 307 308

            vec[0] = cost * vec[0] - sint * vec[1];
            vec[1] = sint * vec[0] + cost * vec[1];
309 310
        }

311
        //! \brief rotate a cartesian vector with respect to the bi-normal of
312
        //! theta degrees.
313
        static void rotate_binormal(double* vec, double theta)
314 315 316 317
        {
            const double cost = cos(theta);
            const double sint = sin(theta);

318 319
            vec[0] = cost * vec[0] - sint * vec[2];
            vec[2] = sint * vec[0] + cost * vec[2];
320
        }
321
};