javauis/amms_qt/module/src/ammsutil.cpp
changeset 23 98ccebc37403
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Static general reusable methods.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32math.h>
       
    21 #include "ammsutil.h"
       
    22 #include <cmmaplayer.h>
       
    23 #include <cmmacontrol.h>
       
    24 #include "cammscontrol.h"
       
    25 
       
    26 // CONSTANTS
       
    27 namespace
       
    28 {
       
    29 const TInt KAMMSStraightAngle = 180;
       
    30 
       
    31 // Set a value to 0 if its absolute value is less than this.
       
    32 const TReal KAMMSDiscreteErrorConstant = 0.00000001;
       
    33 }
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CAMMSControlGroup::FindControl
       
    40 // Finds the control of a corresponding name.
       
    41 // (other items were commented in a header).
       
    42 // -----------------------------------------------------------------------------
       
    43 CMMAControl* AMMSUtil::FindControl(CMMAPlayer* aPlayer,
       
    44                                    const TDesC& aControlName,
       
    45                                    TAMMSControlTypes aControlType)
       
    46 {
       
    47     TInt controlCount = aPlayer->ControlCount();
       
    48     TInt index = 0;
       
    49 
       
    50     // if control is not found NULL will be returned
       
    51     CMMAControl* control = NULL;
       
    52 
       
    53     // go through all controls
       
    54     while (index < controlCount)
       
    55     {
       
    56         CMMAControl* tmp = aPlayer->Control(index);
       
    57 
       
    58         // if the name of the Control matches the name used in finding
       
    59         if (tmp->ClassName() == aControlName)
       
    60         {
       
    61 
       
    62             // a base Control can be accepted always, it can not be derived
       
    63             if (aControlType == EAMMSBaseControl)
       
    64             {
       
    65                 // found correct control
       
    66                 control = tmp;
       
    67 
       
    68                 // set index to stop while loop
       
    69                 index = controlCount;
       
    70             }
       
    71 
       
    72             // in case the Control is not a base Control, check AMMS subtype
       
    73             else
       
    74             {
       
    75                 if (aControlType == ((CAMMSControl*)tmp)->iControlType)
       
    76                 {
       
    77                     // found correct (derived) control
       
    78                     control = tmp;
       
    79 
       
    80                     // set index to stop while loop
       
    81                     index = controlCount;
       
    82                 }
       
    83                 else
       
    84                 {
       
    85                     // move to next control
       
    86                     index++;
       
    87                 }
       
    88             }
       
    89         }
       
    90 
       
    91         // Control name was not the one used in finding
       
    92         else
       
    93         {
       
    94             // move to next control
       
    95             index++;
       
    96         }
       
    97     }
       
    98     return control;
       
    99 }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // AMMSUtil::FromSphericalToCartesianL
       
   103 // Converts vector from spherical to cartesian.
       
   104 // (other items were commented in a header).
       
   105 // -----------------------------------------------------------------------------
       
   106 void AMMSUtil::FromSphericalToCartesianL(
       
   107     TInt aSphericalVector[ KAMMSVectorComponents ],
       
   108     TInt aCartesianVector[ KAMMSVectorComponents ])
       
   109 {
       
   110     // convert to radians
       
   111     TReal elevation = aSphericalVector[ EElevation ] *
       
   112                       (KPi / KAMMSStraightAngle);
       
   113     TReal azimuth = aSphericalVector[ EAzimuth ] *
       
   114                     (KPi / KAMMSStraightAngle);
       
   115     TReal radius = aSphericalVector[ ERadius ];
       
   116 
       
   117     TReal elevationSin;
       
   118     TReal elevationCos;
       
   119     User::LeaveIfError(Math::Sin(elevationSin, elevation));
       
   120     User::LeaveIfError(Math::Cos(elevationCos, elevation));
       
   121 
       
   122     TReal cartesian[ KAMMSVectorComponents ];
       
   123 
       
   124     cartesian[ EComponentY ] = elevationSin * radius;
       
   125     TReal distXZ = elevationCos * radius; // distance in x-z plane
       
   126 
       
   127     TReal azimuthSin;
       
   128     TReal azimuthCos;
       
   129     User::LeaveIfError(Math::Sin(azimuthSin, azimuth));
       
   130     User::LeaveIfError(Math::Cos(azimuthCos, azimuth));
       
   131 
       
   132     // azimuth of 0 degrees points to negative z axis
       
   133     cartesian[ EComponentZ ] = -azimuthCos * distXZ;
       
   134     cartesian[ EComponentX ] = azimuthSin * distXZ;
       
   135 
       
   136     // round real values and convert them to integers
       
   137     RoundVectorL(cartesian, aCartesianVector);
       
   138 }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // AMMSUtil::RotateVectorL
       
   142 // Rotates a vector round the given axis.
       
   143 // (other items were commented in a header).
       
   144 // -----------------------------------------------------------------------------
       
   145 void AMMSUtil::RotateVectorL(
       
   146     TReal aVector[ KAMMSVectorComponents ],
       
   147     TReal aAxisVector[ KAMMSVectorComponents ],
       
   148     TInt aAngle,
       
   149     TReal aRotatedVector[ KAMMSVectorComponents ])
       
   150 {
       
   151     // calculate the length of the axis vector
       
   152     TReal lengthSquare = aAxisVector[ EComponentX ] *
       
   153                          aAxisVector[ EComponentX ] + aAxisVector[ EComponentY ] *
       
   154                          aAxisVector[ EComponentY ] + aAxisVector[ EComponentZ ] *
       
   155                          aAxisVector[ EComponentZ ];
       
   156 
       
   157     TReal length;
       
   158     User::LeaveIfError(Math::Sqrt(length, lengthSquare));
       
   159 
       
   160     // check that the vector is long enough
       
   161     __ASSERT_DEBUG(length > 0, User::Invariant());
       
   162 
       
   163     // normalize the axis vector
       
   164     TReal x = aAxisVector[ EComponentX ] / length;
       
   165     TReal y = aAxisVector[ EComponentY ] / length;
       
   166     TReal z = aAxisVector[ EComponentZ ] / length;
       
   167 
       
   168     // calculate sine and cosine values
       
   169     TReal angleRad = aAngle / 180.0 * KPi;
       
   170     TReal c;
       
   171     User::LeaveIfError(Math::Cos(c, angleRad));
       
   172     TReal s;
       
   173     User::LeaveIfError(Math::Sin(s, angleRad));
       
   174 
       
   175     // calculate some help variables
       
   176     TReal t = 1 - c;
       
   177     TReal txy = t * x * y;
       
   178     TReal txz = t * x * z;
       
   179     TReal tyz = t * y * z;
       
   180     TReal sz = s * z;
       
   181     TReal sy = s * y;
       
   182     TReal sx = s * x;
       
   183     TReal x2 = aVector[ EComponentX ];
       
   184     TReal y2 = aVector[ EComponentY ];
       
   185     TReal z2 = aVector[ EComponentZ ];
       
   186 
       
   187     // calculate new x value
       
   188     aRotatedVector[ EComponentX ] = (t * x * x + c) * x2 +
       
   189                                     (txy - sz) * y2 + (txz + sy) * z2;
       
   190 
       
   191     // calculate new y value
       
   192     aRotatedVector[ EComponentY ] = (txy + sz) * x2 +
       
   193                                     (t * y * y + c) * y2 + (tyz - sx) * z2;
       
   194 
       
   195     // calculate new z value
       
   196     aRotatedVector[ EComponentZ ] = (txz - sy) * x2 +
       
   197                                     (tyz + sx) * y2 + (t * z * z + c) * z2;
       
   198 
       
   199     // Remove error of discrete values.
       
   200     for (TInt i = 0; i < KAMMSVectorComponents; i++)
       
   201     {
       
   202         if (Abs(aRotatedVector[ i ]) < KAMMSDiscreteErrorConstant)       // CSI: 2 Wrong index means implementation error #
       
   203         {
       
   204             aRotatedVector[ i ] = 0;  // CSI: 2 Wrong index means implementation error #
       
   205         }
       
   206     }
       
   207 }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // AMMSUtil::RoundVectorL
       
   211 // Rounds vector components.
       
   212 // (other items were commented in a header).
       
   213 // -----------------------------------------------------------------------------
       
   214 void AMMSUtil::RoundVectorL(
       
   215     TReal aVector[ KAMMSVectorComponents ],
       
   216     TInt aRoundedVector[ KAMMSVectorComponents ])
       
   217 {
       
   218     for (TInt i = 0; i < KAMMSVectorComponents; i++)
       
   219     {
       
   220         TReal roundedValue;
       
   221         User::LeaveIfError(Math::Round(roundedValue,
       
   222                                        (TReal)aVector[ i ], 0));      // CSI: 2 Wrong index means implementation error #
       
   223 
       
   224         aRoundedVector[ i ] = (TInt)roundedValue;    // CSI: 2 Wrong index means implementation error #
       
   225     }
       
   226 }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // AMMSUtil::MultiplyVector
       
   230 // Multiplies a vector by a scalar.
       
   231 // (other items were commented in a header).
       
   232 // -----------------------------------------------------------------------------
       
   233 void AMMSUtil::MultiplyVector(
       
   234     TReal aVector[ KAMMSVectorComponents ],
       
   235     TReal aMultiplier)
       
   236 {
       
   237     for (TInt i = 0; i < KAMMSVectorComponents; i++)
       
   238     {
       
   239         aVector[ i ] *= aMultiplier;  // CSI: 2 Wrong index means implementation error #
       
   240     }
       
   241 }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // AMMSUtil::CrossProduct
       
   245 // Calculates vector cross product.
       
   246 // (other items were commented in a header).
       
   247 // -----------------------------------------------------------------------------
       
   248 void AMMSUtil::CrossProduct(
       
   249     TReal aA[ KAMMSVectorComponents ],
       
   250     TReal aB[ KAMMSVectorComponents ],
       
   251     TReal aResultVector[ KAMMSVectorComponents ])
       
   252 {
       
   253     // This function can handle only vectors with 3 components
       
   254 
       
   255     // Calculate the cross product.
       
   256     aResultVector[ EComponentX ] = -aA[ EComponentZ ] * aB[ EComponentY ] +
       
   257                                    aA[ EComponentY ] * aB[ EComponentZ ];
       
   258 
       
   259     aResultVector[ EComponentY ] = aA[ EComponentZ ] * aB[ EComponentX ] -
       
   260                                    aA[ EComponentX ] * aB[ EComponentZ ];
       
   261 
       
   262     aResultVector[ EComponentZ ] = -aA[ EComponentY ] * aB[ EComponentX ] +
       
   263                                    aA[ EComponentX ] * aB[ EComponentY ];
       
   264 }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // AMMSUtil::VectorLengthL
       
   268 // Calculates the length of the given vector.
       
   269 // (other items were commented in a header).
       
   270 // -----------------------------------------------------------------------------
       
   271 TReal AMMSUtil::VectorLengthL(
       
   272     TReal aVector[ KAMMSVectorComponents ])
       
   273 {
       
   274     TReal squareLength = 0;
       
   275 
       
   276     for (TInt i = 0; i < KAMMSVectorComponents; i++)
       
   277     {
       
   278         squareLength += aVector[ i ] * aVector[ i ];
       
   279     }
       
   280 
       
   281     TReal length;
       
   282     User::LeaveIfError(Math::Sqrt(length, squareLength));
       
   283 
       
   284     return length;
       
   285 }
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // AMMSUtil::ConvertToUnitVectorL
       
   289 // Converts the given vector to unit vector.
       
   290 // (other items were commented in a header).
       
   291 // -----------------------------------------------------------------------------
       
   292 void AMMSUtil::ConvertToUnitVectorL(
       
   293     TReal aVector[ KAMMSVectorComponents ])
       
   294 {
       
   295     TReal length = VectorLengthL(aVector);
       
   296 
       
   297     for (TInt i = 0; i < KAMMSVectorComponents; i++)
       
   298     {
       
   299         aVector[ i ] /= length;  // CSI: 2 Wrong index means implementation error #
       
   300     }
       
   301 }
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // AMMSUtil::AreVectorsSimilar
       
   305 // Checks whether two vectors are similar enough.
       
   306 // (other items were commented in a header).
       
   307 // -----------------------------------------------------------------------------
       
   308 TBool AMMSUtil::AreVectorsSimilar(
       
   309     TReal aA[ KAMMSVectorComponents ],
       
   310     TInt aB[ KAMMSVectorComponents ],
       
   311     TInt aMaxComponentErrorPercentage)
       
   312 {
       
   313     TReal maxRelativeError = aMaxComponentErrorPercentage / 100.0;  // CSI: 47 Value 100 means 100% #
       
   314 
       
   315     for (TInt i = 0; i < KAMMSVectorComponents; i++)
       
   316     {
       
   317         TReal maxError =
       
   318             Max(Abs(aA[ i ]), Abs(aB[ i ])) * maxRelativeError;        // CSI: 2 Wrong index means implementation error #
       
   319 
       
   320         if (Abs(aA[ i ] - aB[ i ]) > maxError)     // CSI: 2 Wrong index means implementation error #
       
   321         {
       
   322             return EFalse;
       
   323         }
       
   324     }
       
   325 
       
   326     // Vectors were similar enough.
       
   327     return ETrue;
       
   328 }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // AMMSUtil::MaxVectorComponent
       
   332 // Returns the maximum component in the given vector.
       
   333 // (other items were commented in a header).
       
   334 // -----------------------------------------------------------------------------
       
   335 TReal AMMSUtil::MaxVectorComponent(
       
   336     TReal aVector[ KAMMSVectorComponents ])
       
   337 {
       
   338     TReal maxValue = aVector[ 0 ];
       
   339 
       
   340     for (TInt i = 1; i < KAMMSVectorComponents; i++)
       
   341     {
       
   342         maxValue = Max(maxValue, aVector[ i ]);    // CSI: 2 Wrong index means implementation error #
       
   343     }
       
   344 
       
   345     return maxValue;
       
   346 }
       
   347 
       
   348 // -----------------------------------------------------------------------------
       
   349 // AMMSUtil::MinVectorComponent
       
   350 // Returns the minimum component in the given vector.
       
   351 // (other items were commented in a header).
       
   352 // -----------------------------------------------------------------------------
       
   353 TReal AMMSUtil::MinVectorComponent(
       
   354     TReal aVector[ KAMMSVectorComponents ])
       
   355 {
       
   356     TReal minValue = aVector[ 0 ];
       
   357 
       
   358     for (TInt i = 1; i < KAMMSVectorComponents; i++)
       
   359     {
       
   360         minValue = Min(minValue, aVector[ i ]);    // CSI: 2 Wrong index means implementation error #
       
   361     }
       
   362 
       
   363     return minValue;
       
   364 }
       
   365 
       
   366 //  End of File