params.h 8.12 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32
        //! \brief list of all supported parametrization. An unsupported
        //! parametrization will go under the name <em>unknown</em>.
        enum type
        {
            ROMEIRO_TH_TD,
            RUSIN_TH_PH_TD,
            RUSIN_TH_TD_PD,
            RUSIN_TH_PH_TD_PD,
            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
        };

    public: // methods

33 34 35 36
        //! \brief static function for input type convertion. This
        //! function allocate the resulting vector.
        static double* convert(const double* invec, params::type intype,
                               params::type outtype)
37
        {
38 39 40 41 42 43 44 45 46 47 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
            int dim = dimension(outtype); // Get the size of the output vector

            if(dim > 0)
            {
                double* outvec = new double[dim];
                double  temvec[6]; // Temp CARTESIAN vectors
                to_cartesian(invec, intype, temvec);
                from_cartesian(temvec, outtype, outvec);

                return outvec;
            }
            else
            {
                return NULL;
            }
        }

        //! \brief static function for input type convertion. The \param
        //! outvec resulting vector should be allocated with the correct
        //! output size.
        static void convert(const double* invec, params::type intype,
                            params::type outtype, double* outvec)
        {
            double  temvec[6]; // Temp CARTESIAN vectors
            to_cartesian(invec, intype, temvec);
            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.
        static void to_cartesian(const double* invec, params::type intype,
                                 double* outvec)
        {
            double half[3];

            switch(intype)
            {
                // 3D Parametrization
                case params::RUSIN_TH_PH_TD:
78
                    // Calculate the half vector
79 80 81 82
                    half[0] = sin(invec[0])*cos(invec[1]);
                    half[1] = sin(invec[0])*sin(invec[1]);
                    half[2] = cos(invec[0]);

83 84 85 86 87 88
                    outvec[0] = sin(invec[2]);
                    outvec[1] = 0;
                    outvec[2] = cos(invec[2]);
                    rotate_binormal(outvec, invec[0]);
                    rotate_normal(outvec, invec[1]);

89 90 91 92 93 94 95 96
                    // Compute the out vector from the in vector and the half
                    // vector.
                    {
                    const double dot = outvec[0]*half[0] + outvec[1]*half[1] + outvec[2]*half[2];
                    outvec[3] = -outvec[0] + 2.0*dot * half[0];
                    outvec[4] = -outvec[1] + 2.0*dot * half[1];
                    outvec[5] = -outvec[2] + 2.0*dot * half[2];
                    }
97 98 99 100
                    break;

                // 4D Parametrization
                case params::RUSIN_TH_PH_TD_PD:
101
                    // Calculate the half vector
102 103 104 105
                    half[0] = sin(invec[0])*cos(invec[1]);
                    half[1] = sin(invec[0])*sin(invec[1]);
                    half[2] = cos(invec[0]);

106
                    // Compute the light vector using the rotation formula.
107 108 109 110 111 112
                    outvec[0] = sin(invec[2])*cos(invec[3]);
                    outvec[1] = sin(invec[2])*sin(invec[3]);
                    outvec[2] = cos(invec[2]);
                    rotate_binormal(outvec, invec[0]);
                    rotate_normal(outvec, invec[1]);

113 114 115 116 117 118 119 120
                    // Compute the out vector from the in vector and the half
                    // vector.
                    {
                    const double dot = outvec[0]*half[0] + outvec[1]*half[1] + outvec[2]*half[2];
                    outvec[3] = -outvec[0] + 2.0*dot * half[0];
                    outvec[4] = -outvec[1] + 2.0*dot * half[1];
                    outvec[5] = -outvec[2] + 2.0*dot * half[2];
                    }
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
                    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.
        static void from_cartesian(const double* invec, params::type 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;

            switch(outtype)
            {
                // 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:
                    throw("Transformation not implemented, params::from_cartesian");
                    break;
            }
        }

        //! \brief provide a dimension associated with a parametrization
        static int  dimension(params::type t)
        {
            switch(t)
            {
                // 2D Parametrizations
                case params::ISOTROPIC_TD_PD:
                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:
                    return -1;
                    break;
            }
        }

213
        //! \brief rotate a cartesian vector with respect to the normal of
214
        //! \param theta degrees.
215
        static void rotate_normal(double* vec, double theta)
216 217 218
        {
            const double cost = cos(theta);
            const double sint = sin(theta);
219 220 221

            vec[0] = cost * vec[0] - sint * vec[1];
            vec[1] = sint * vec[0] + cost * vec[1];
222 223
        }

224
        //! \brief rotate a cartesian vector with respect to the bi-normal of
225
        //! \param theta degrees.
226
        static void rotate_binormal(double* vec, double theta)
227 228 229 230
        {
            const double cost = cos(theta);
            const double sint = sin(theta);

231 232
            vec[0] = cost * vec[0] - sint * vec[2];
            vec[2] = sint * vec[0] + cost * vec[2];
233
        }
234
};