os/mm/devsoundextensions/effects/LocationBase/src/LocationBase.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). 
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:   Implementation of the Location effect base class
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
sl@0
    20
sl@0
    21
// INCLUDE FILES
sl@0
    22
sl@0
    23
#ifdef _DEBUG
sl@0
    24
#include <e32svr.h>
sl@0
    25
#endif
sl@0
    26
sl@0
    27
#include <LocationBase.h>
sl@0
    28
#include <e32math.h>
sl@0
    29
#include <math.h>
sl@0
    30
sl@0
    31
sl@0
    32
//360 degrees:
sl@0
    33
#define TWO_PI 6283
sl@0
    34
//180 degrees:
sl@0
    35
#define PI 3142
sl@0
    36
//90 degrees:
sl@0
    37
#define QUARTER_PI 1570
sl@0
    38
// ============================ MEMBER FUNCTIONS ===============================
sl@0
    39
sl@0
    40
// -----------------------------------------------------------------------------
sl@0
    41
// CLocation::CLocation
sl@0
    42
// C++ default constructor can NOT contain any code, that
sl@0
    43
// might leave.
sl@0
    44
// -----------------------------------------------------------------------------
sl@0
    45
//
sl@0
    46
EXPORT_C CLocation::CLocation()
sl@0
    47
    : 	iLocationData(0,0,0,0,0,0),
sl@0
    48
    	iDataPckgTo(iLocationData),
sl@0
    49
    	iDataPckgFrom(iLocationData)
sl@0
    50
    {
sl@0
    51
    }
sl@0
    52
sl@0
    53
// Destructor
sl@0
    54
EXPORT_C CLocation::~CLocation()
sl@0
    55
    {
sl@0
    56
    }
sl@0
    57
sl@0
    58
// -----------------------------------------------------------------------------
sl@0
    59
// CLocation::LocationCartesian
sl@0
    60
// -----------------------------------------------------------------------------
sl@0
    61
//
sl@0
    62
EXPORT_C void CLocation::LocationCartesian(
sl@0
    63
	TInt32& aX, TInt32& aY, TInt32& aZ )
sl@0
    64
	{
sl@0
    65
	aX = iLocationData.iXCoordinate;
sl@0
    66
	aY = iLocationData.iYCoordinate;
sl@0
    67
	aZ = iLocationData.iZCoordinate;
sl@0
    68
	}
sl@0
    69
sl@0
    70
// -----------------------------------------------------------------------------
sl@0
    71
// CLocation::LocationSpherical
sl@0
    72
// -----------------------------------------------------------------------------
sl@0
    73
//
sl@0
    74
EXPORT_C void CLocation::LocationSpherical(
sl@0
    75
	TInt32& aAzimuth, TInt32& aElevation, TInt32& aRadius  )
sl@0
    76
	{
sl@0
    77
	aAzimuth = iLocationData.iAzimuth;
sl@0
    78
	aElevation = iLocationData.iElevation;
sl@0
    79
	aRadius = iLocationData.iRadius;
sl@0
    80
	}
sl@0
    81
sl@0
    82
// -----------------------------------------------------------------------------
sl@0
    83
// CLocation::SetLocationCartesianL
sl@0
    84
// -----------------------------------------------------------------------------
sl@0
    85
//
sl@0
    86
EXPORT_C void CLocation::SetLocationCartesianL(
sl@0
    87
	TInt32& aX, TInt32& aY, TInt32& aZ )
sl@0
    88
	{
sl@0
    89
	iLocationData.iXCoordinate = aX;
sl@0
    90
	iLocationData.iYCoordinate = aY;
sl@0
    91
	iLocationData.iZCoordinate = aZ;
sl@0
    92
sl@0
    93
sl@0
    94
    TReal SqrtXYZ = 0, squareX = 0, squareY = 0, squareZ = 0;
sl@0
    95
    Math::Pow(squareX, aX, 2);
sl@0
    96
    Math::Pow(squareY, aY, 2);
sl@0
    97
    Math::Pow(squareZ, aZ, 2);
sl@0
    98
    TReal sum = squareX + squareY + squareZ; 
sl@0
    99
    Math::Sqrt(SqrtXYZ, sum);
sl@0
   100
        
sl@0
   101
  
sl@0
   102
    //Singularity region 
sl@0
   103
sl@0
   104
       if(!((aX==0) && (aZ==0))) //was aY
sl@0
   105
       {
sl@0
   106
sl@0
   107
            TReal zDividedByXAtan = atan2 ((TReal)-aX, (TReal)-aZ);
sl@0
   108
            
sl@0
   109
            if (zDividedByXAtan > 0)
sl@0
   110
	          iLocationData.iAzimuth = - (TInt32)(zDividedByXAtan * 1000 + 0.5); // minus sign added (shouldn't affect the result since it is compensated three rows above, but to be sure...)
sl@0
   111
            else
sl@0
   112
 	          iLocationData.iAzimuth = - (TInt32)(zDividedByXAtan * 1000 - 0.5); // minus sign added (shouldn't affect the result since it is compensated three rows above, but to be sure...)
sl@0
   113
             
sl@0
   114
       }
sl@0
   115
	// else { we are exactly on Y-axis and therefore azimuth is undefined; let's use the previous azimuth value instead } 
sl@0
   116
sl@0
   117
 
sl@0
   118
       if (!((aX ==0) && (aY == 0) && (aZ == 0))) 
sl@0
   119
       {
sl@0
   120
sl@0
   121
         TReal result;
sl@0
   122
         TReal yDividedBySqrtXYZ = aY/SqrtXYZ; 
sl@0
   123
         User::LeaveIfError(Math::ASin(result, yDividedBySqrtXYZ)); //was ACos
sl@0
   124
sl@0
   125
         if (result > 0)
sl@0
   126
           iLocationData.iElevation = (TInt32) (result * 1000 + 0.5);
sl@0
   127
         else
sl@0
   128
           iLocationData.iElevation = (TInt32) (result * 1000 - 0.5);
sl@0
   129
           
sl@0
   130
       }
sl@0
   131
	   // else { we are exactly in origin and therefore elevation is undefined; let's use the previous elevation value instead } 
sl@0
   132
      
sl@0
   133
       iLocationData.iRadius= (TInt32) (SqrtXYZ + 0.5);
sl@0
   134
  
sl@0
   135
  
sl@0
   136
      while(iLocationData.iElevation > PI) 
sl@0
   137
       { 
sl@0
   138
	   iLocationData.iElevation = iLocationData.iElevation - TWO_PI; 
sl@0
   139
       }
sl@0
   140
    
sl@0
   141
      if(iLocationData.iElevation > QUARTER_PI) 
sl@0
   142
       {
sl@0
   143
	   iLocationData.iElevation = iLocationData.iElevation - (iLocationData.iElevation - QUARTER_PI) * 2;
sl@0
   144
	   iLocationData.iAzimuth = iLocationData.iAzimuth + PI;
sl@0
   145
       }
sl@0
   146
sl@0
   147
      while(iLocationData.iElevation < -PI) 
sl@0
   148
       {
sl@0
   149
	   iLocationData.iElevation = iLocationData.iElevation + TWO_PI;
sl@0
   150
       }
sl@0
   151
      if(iLocationData.iElevation < -QUARTER_PI) 
sl@0
   152
       {
sl@0
   153
	   iLocationData.iElevation = iLocationData.iElevation + (QUARTER_PI - iLocationData.iElevation) * 2;
sl@0
   154
	   iLocationData.iAzimuth = iLocationData.iAzimuth + PI;
sl@0
   155
       }
sl@0
   156
    
sl@0
   157
      while (iLocationData.iAzimuth < 0)
sl@0
   158
	    iLocationData.iAzimuth = iLocationData.iAzimuth + TWO_PI;
sl@0
   159
      while (iLocationData.iAzimuth > TWO_PI)
sl@0
   160
	   iLocationData.iAzimuth = iLocationData.iAzimuth - TWO_PI;
sl@0
   161
	   
sl@0
   162
	}
sl@0
   163
sl@0
   164
// -----------------------------------------------------------------------------
sl@0
   165
// CLocation::SetLocationSphericalL
sl@0
   166
// -----------------------------------------------------------------------------
sl@0
   167
//
sl@0
   168
EXPORT_C void CLocation::SetLocationSphericalL(
sl@0
   169
	TInt32& aAzimuth, TInt32& aElevation, TInt32& aRadius  )
sl@0
   170
	{
sl@0
   171
	
sl@0
   172
     while(aElevation > PI) 
sl@0
   173
      { 
sl@0
   174
         aElevation = aElevation - TWO_PI; 
sl@0
   175
      }
sl@0
   176
    
sl@0
   177
     if(aElevation > QUARTER_PI) 
sl@0
   178
       {
sl@0
   179
	   aElevation = aElevation - (aElevation - QUARTER_PI) * 2;
sl@0
   180
	   aAzimuth = aAzimuth + PI;
sl@0
   181
       }
sl@0
   182
sl@0
   183
     while(aElevation < -PI) 
sl@0
   184
       {
sl@0
   185
	   aElevation = aElevation + TWO_PI;
sl@0
   186
       }
sl@0
   187
     if(aElevation < -QUARTER_PI) 
sl@0
   188
       {
sl@0
   189
	   aElevation = aElevation + (QUARTER_PI - aElevation) * 2;
sl@0
   190
	   aAzimuth = aAzimuth + PI;
sl@0
   191
       }
sl@0
   192
   
sl@0
   193
     while (aAzimuth < 0)
sl@0
   194
       aAzimuth = aAzimuth + TWO_PI;
sl@0
   195
     while (aAzimuth > TWO_PI)
sl@0
   196
	   aAzimuth = aAzimuth - TWO_PI;
sl@0
   197
sl@0
   198
	
sl@0
   199
   	iLocationData.iAzimuth = aAzimuth;
sl@0
   200
	iLocationData.iElevation = aElevation;
sl@0
   201
	iLocationData.iRadius = aRadius;
sl@0
   202
sl@0
   203
sl@0
   204
    TReal elevation = aElevation / 1000.0; // conversion from milliradians to radians because Sin and Cos functions eat radians
sl@0
   205
    
sl@0
   206
    TReal elevationSin;
sl@0
   207
    TReal elevationCos;
sl@0
   208
    User::LeaveIfError( Math::Sin( elevationSin, elevation ) );
sl@0
   209
    User::LeaveIfError( Math::Cos( elevationCos, elevation ) );
sl@0
   210
    
sl@0
   211
    TReal azimuthSin;
sl@0
   212
    TReal azimuthCos;
sl@0
   213
    User::LeaveIfError( Math::Sin( azimuthSin, aAzimuth / 1000.0) );
sl@0
   214
    User::LeaveIfError( Math::Cos( azimuthCos, aAzimuth / 1000.0) );
sl@0
   215
sl@0
   216
sl@0
   217
	iLocationData.iXCoordinate = (TInt32)(0.5 + aRadius * elevationCos * azimuthSin);
sl@0
   218
    iLocationData.iYCoordinate = (TInt32)(0.5 + aRadius * elevationSin);
sl@0
   219
    iLocationData.iZCoordinate = (TInt32)(0.5 - aRadius * elevationCos * azimuthCos);		
sl@0
   220
	
sl@0
   221
	}
sl@0
   222
sl@0
   223
// -----------------------------------------------------------------------------
sl@0
   224
// CLocation::DoEffectData
sl@0
   225
// -----------------------------------------------------------------------------
sl@0
   226
//
sl@0
   227
EXPORT_C const TDesC8& CLocation::DoEffectData()
sl@0
   228
	{
sl@0
   229
#ifdef _DEBUG
sl@0
   230
    RDebug::Print(_L("CLocation::DoEffectData"));
sl@0
   231
#endif
sl@0
   232
	iDataPckgTo = iLocationData;
sl@0
   233
	return iDataPckgTo;
sl@0
   234
	}
sl@0
   235
sl@0
   236
// -----------------------------------------------------------------------------
sl@0
   237
// CLocation::SetEffectData
sl@0
   238
// -----------------------------------------------------------------------------
sl@0
   239
//
sl@0
   240
EXPORT_C void CLocation::SetEffectData(
sl@0
   241
	const TDesC8& aEffectDataBuffer )
sl@0
   242
	{
sl@0
   243
#ifdef _DEBUG
sl@0
   244
    RDebug::Print(_L("CLocation::SetEffectData"));
sl@0
   245
#endif
sl@0
   246
	TEfLocationDataPckg dataPckg;
sl@0
   247
	dataPckg.Copy(aEffectDataBuffer);
sl@0
   248
	iLocationData = dataPckg();
sl@0
   249
	iEnabled = iLocationData.iEnabled;
sl@0
   250
	iEnforced = iLocationData.iEnforced;
sl@0
   251
	iHaveUpdateRights = iLocationData.iHaveUpdateRights;
sl@0
   252
sl@0
   253
	}
sl@0
   254
sl@0
   255
// ========================== OTHER EXPORTED FUNCTIONS =========================
sl@0
   256
sl@0
   257
// End of File