sl@0: /* sl@0: * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: Implementation of the Location effect base class sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: sl@0: // INCLUDE FILES sl@0: sl@0: #ifdef _DEBUG sl@0: #include sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: //360 degrees: sl@0: #define TWO_PI 6283 sl@0: //180 degrees: sl@0: #define PI 3142 sl@0: //90 degrees: sl@0: #define QUARTER_PI 1570 sl@0: // ============================ MEMBER FUNCTIONS =============================== sl@0: sl@0: // ----------------------------------------------------------------------------- sl@0: // CLocation::CLocation sl@0: // C++ default constructor can NOT contain any code, that sl@0: // might leave. sl@0: // ----------------------------------------------------------------------------- sl@0: // sl@0: EXPORT_C CLocation::CLocation() sl@0: : iLocationData(0,0,0,0,0,0), sl@0: iDataPckgTo(iLocationData), sl@0: iDataPckgFrom(iLocationData) sl@0: { sl@0: } sl@0: sl@0: // Destructor sl@0: EXPORT_C CLocation::~CLocation() sl@0: { sl@0: } sl@0: sl@0: // ----------------------------------------------------------------------------- sl@0: // CLocation::LocationCartesian sl@0: // ----------------------------------------------------------------------------- sl@0: // sl@0: EXPORT_C void CLocation::LocationCartesian( sl@0: TInt32& aX, TInt32& aY, TInt32& aZ ) sl@0: { sl@0: aX = iLocationData.iXCoordinate; sl@0: aY = iLocationData.iYCoordinate; sl@0: aZ = iLocationData.iZCoordinate; sl@0: } sl@0: sl@0: // ----------------------------------------------------------------------------- sl@0: // CLocation::LocationSpherical sl@0: // ----------------------------------------------------------------------------- sl@0: // sl@0: EXPORT_C void CLocation::LocationSpherical( sl@0: TInt32& aAzimuth, TInt32& aElevation, TInt32& aRadius ) sl@0: { sl@0: aAzimuth = iLocationData.iAzimuth; sl@0: aElevation = iLocationData.iElevation; sl@0: aRadius = iLocationData.iRadius; sl@0: } sl@0: sl@0: // ----------------------------------------------------------------------------- sl@0: // CLocation::SetLocationCartesianL sl@0: // ----------------------------------------------------------------------------- sl@0: // sl@0: EXPORT_C void CLocation::SetLocationCartesianL( sl@0: TInt32& aX, TInt32& aY, TInt32& aZ ) sl@0: { sl@0: iLocationData.iXCoordinate = aX; sl@0: iLocationData.iYCoordinate = aY; sl@0: iLocationData.iZCoordinate = aZ; sl@0: sl@0: sl@0: TReal SqrtXYZ = 0, squareX = 0, squareY = 0, squareZ = 0; sl@0: Math::Pow(squareX, aX, 2); sl@0: Math::Pow(squareY, aY, 2); sl@0: Math::Pow(squareZ, aZ, 2); sl@0: TReal sum = squareX + squareY + squareZ; sl@0: Math::Sqrt(SqrtXYZ, sum); sl@0: sl@0: sl@0: //Singularity region sl@0: sl@0: if(!((aX==0) && (aZ==0))) //was aY sl@0: { sl@0: sl@0: TReal zDividedByXAtan = atan2 ((TReal)-aX, (TReal)-aZ); sl@0: sl@0: if (zDividedByXAtan > 0) sl@0: 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: else sl@0: 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: sl@0: } sl@0: // else { we are exactly on Y-axis and therefore azimuth is undefined; let's use the previous azimuth value instead } sl@0: sl@0: sl@0: if (!((aX ==0) && (aY == 0) && (aZ == 0))) sl@0: { sl@0: sl@0: TReal result; sl@0: TReal yDividedBySqrtXYZ = aY/SqrtXYZ; sl@0: User::LeaveIfError(Math::ASin(result, yDividedBySqrtXYZ)); //was ACos sl@0: sl@0: if (result > 0) sl@0: iLocationData.iElevation = (TInt32) (result * 1000 + 0.5); sl@0: else sl@0: iLocationData.iElevation = (TInt32) (result * 1000 - 0.5); sl@0: sl@0: } sl@0: // else { we are exactly in origin and therefore elevation is undefined; let's use the previous elevation value instead } sl@0: sl@0: iLocationData.iRadius= (TInt32) (SqrtXYZ + 0.5); sl@0: sl@0: sl@0: while(iLocationData.iElevation > PI) sl@0: { sl@0: iLocationData.iElevation = iLocationData.iElevation - TWO_PI; sl@0: } sl@0: sl@0: if(iLocationData.iElevation > QUARTER_PI) sl@0: { sl@0: iLocationData.iElevation = iLocationData.iElevation - (iLocationData.iElevation - QUARTER_PI) * 2; sl@0: iLocationData.iAzimuth = iLocationData.iAzimuth + PI; sl@0: } sl@0: sl@0: while(iLocationData.iElevation < -PI) sl@0: { sl@0: iLocationData.iElevation = iLocationData.iElevation + TWO_PI; sl@0: } sl@0: if(iLocationData.iElevation < -QUARTER_PI) sl@0: { sl@0: iLocationData.iElevation = iLocationData.iElevation + (QUARTER_PI - iLocationData.iElevation) * 2; sl@0: iLocationData.iAzimuth = iLocationData.iAzimuth + PI; sl@0: } sl@0: sl@0: while (iLocationData.iAzimuth < 0) sl@0: iLocationData.iAzimuth = iLocationData.iAzimuth + TWO_PI; sl@0: while (iLocationData.iAzimuth > TWO_PI) sl@0: iLocationData.iAzimuth = iLocationData.iAzimuth - TWO_PI; sl@0: sl@0: } sl@0: sl@0: // ----------------------------------------------------------------------------- sl@0: // CLocation::SetLocationSphericalL sl@0: // ----------------------------------------------------------------------------- sl@0: // sl@0: EXPORT_C void CLocation::SetLocationSphericalL( sl@0: TInt32& aAzimuth, TInt32& aElevation, TInt32& aRadius ) sl@0: { sl@0: sl@0: while(aElevation > PI) sl@0: { sl@0: aElevation = aElevation - TWO_PI; sl@0: } sl@0: sl@0: if(aElevation > QUARTER_PI) sl@0: { sl@0: aElevation = aElevation - (aElevation - QUARTER_PI) * 2; sl@0: aAzimuth = aAzimuth + PI; sl@0: } sl@0: sl@0: while(aElevation < -PI) sl@0: { sl@0: aElevation = aElevation + TWO_PI; sl@0: } sl@0: if(aElevation < -QUARTER_PI) sl@0: { sl@0: aElevation = aElevation + (QUARTER_PI - aElevation) * 2; sl@0: aAzimuth = aAzimuth + PI; sl@0: } sl@0: sl@0: while (aAzimuth < 0) sl@0: aAzimuth = aAzimuth + TWO_PI; sl@0: while (aAzimuth > TWO_PI) sl@0: aAzimuth = aAzimuth - TWO_PI; sl@0: sl@0: sl@0: iLocationData.iAzimuth = aAzimuth; sl@0: iLocationData.iElevation = aElevation; sl@0: iLocationData.iRadius = aRadius; sl@0: sl@0: sl@0: TReal elevation = aElevation / 1000.0; // conversion from milliradians to radians because Sin and Cos functions eat radians sl@0: sl@0: TReal elevationSin; sl@0: TReal elevationCos; sl@0: User::LeaveIfError( Math::Sin( elevationSin, elevation ) ); sl@0: User::LeaveIfError( Math::Cos( elevationCos, elevation ) ); sl@0: sl@0: TReal azimuthSin; sl@0: TReal azimuthCos; sl@0: User::LeaveIfError( Math::Sin( azimuthSin, aAzimuth / 1000.0) ); sl@0: User::LeaveIfError( Math::Cos( azimuthCos, aAzimuth / 1000.0) ); sl@0: sl@0: sl@0: iLocationData.iXCoordinate = (TInt32)(0.5 + aRadius * elevationCos * azimuthSin); sl@0: iLocationData.iYCoordinate = (TInt32)(0.5 + aRadius * elevationSin); sl@0: iLocationData.iZCoordinate = (TInt32)(0.5 - aRadius * elevationCos * azimuthCos); sl@0: sl@0: } sl@0: sl@0: // ----------------------------------------------------------------------------- sl@0: // CLocation::DoEffectData sl@0: // ----------------------------------------------------------------------------- sl@0: // sl@0: EXPORT_C const TDesC8& CLocation::DoEffectData() sl@0: { sl@0: #ifdef _DEBUG sl@0: RDebug::Print(_L("CLocation::DoEffectData")); sl@0: #endif sl@0: iDataPckgTo = iLocationData; sl@0: return iDataPckgTo; sl@0: } sl@0: sl@0: // ----------------------------------------------------------------------------- sl@0: // CLocation::SetEffectData sl@0: // ----------------------------------------------------------------------------- sl@0: // sl@0: EXPORT_C void CLocation::SetEffectData( sl@0: const TDesC8& aEffectDataBuffer ) sl@0: { sl@0: #ifdef _DEBUG sl@0: RDebug::Print(_L("CLocation::SetEffectData")); sl@0: #endif sl@0: TEfLocationDataPckg dataPckg; sl@0: dataPckg.Copy(aEffectDataBuffer); sl@0: iLocationData = dataPckg(); sl@0: iEnabled = iLocationData.iEnabled; sl@0: iEnforced = iLocationData.iEnforced; sl@0: iHaveUpdateRights = iLocationData.iHaveUpdateRights; sl@0: sl@0: } sl@0: sl@0: // ========================== OTHER EXPORTED FUNCTIONS ========================= sl@0: sl@0: // End of File