devsoundextensions/effects/OrientationBase/src/OrientationBase.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 /*
       
     2 * Copyright (c) 2004 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:   Implementation of the Orientation effect base class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #ifdef _DEBUG
       
    24 #include <e32svr.h>
       
    25 #endif
       
    26 
       
    27 #include <OrientationBase.h>
       
    28 #include <e32math.h>
       
    29 #include <math.h>
       
    30 
       
    31 
       
    32 
       
    33 //360 degrees:
       
    34 #define TWO_PI 6283
       
    35 //180 degrees:
       
    36 #define PI 3142
       
    37 //90 degrees:
       
    38 #define QUARTER_PI 1570
       
    39 
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ===============================
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // COrientation::COrientation
       
    45 // C++ default constructor can NOT contain any code, that
       
    46 // might leave.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 EXPORT_C COrientation::COrientation()
       
    50     : 	iOrientationData(0,0,0,0,0,0,0,0,0),
       
    51     	iDataPckgTo(iOrientationData),
       
    52     	iDataPckgFrom(iOrientationData)
       
    53     {
       
    54     }
       
    55 
       
    56 // Destructor
       
    57 EXPORT_C COrientation::~COrientation()
       
    58     {
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // COrientation::Orientation
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 EXPORT_C void COrientation::Orientation(
       
    66 	TInt32& aHeading, TInt32& aPitch, TInt32& aRoll )
       
    67 	{
       
    68 	aHeading = iOrientationData.iHeading;
       
    69 	aPitch = iOrientationData.iPitch;
       
    70 	aRoll = iOrientationData.iRoll;
       
    71 	}
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // COrientation::OrientationVectors
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 EXPORT_C void COrientation::OrientationVectors(
       
    78 	TInt32& aFrontX, TInt32& aFrontY, TInt32& aFrontZ,
       
    79 	TInt32& aAboveX, TInt32& aAboveY, TInt32& aAboveZ )
       
    80 	{
       
    81 	aFrontX = iOrientationData.iFrontX;
       
    82 	aFrontY = iOrientationData.iFrontY;
       
    83 	aFrontZ = iOrientationData.iFrontZ;
       
    84 	aAboveX = iOrientationData.iAboveX;
       
    85 	aAboveY = iOrientationData.iAboveY;
       
    86 	aAboveZ = iOrientationData.iAboveZ;
       
    87 	
       
    88 	
       
    89 	
       
    90 	
       
    91 	}
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // COrientation::SetOrientationL
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C void COrientation::SetOrientationL(
       
    98 	TInt32 aHeading, TInt32 aPitch, TInt32 aRoll )
       
    99 	{
       
   100 
       
   101     while(aPitch > PI) 
       
   102       {
       
   103 	  aPitch = aPitch - TWO_PI;
       
   104       }
       
   105     if(aPitch > QUARTER_PI) 
       
   106       {
       
   107 	  //pitch is here between 90 and 180degs. We swap it back to between 0 and 90degs. Heading needs swaping then too.
       
   108 	  aPitch = aPitch - (aPitch-QUARTER_PI)*2;
       
   109 	  aHeading = aHeading + PI;
       
   110 	  aRoll = aRoll + PI; //I'm not sure if this line is needed. Remove it if you don't get correct results for roll or upVector
       
   111       }
       
   112 
       
   113     while(aPitch < -PI) 
       
   114       {
       
   115    	 aPitch = aPitch + TWO_PI;
       
   116       }
       
   117      
       
   118      if(aPitch < -QUARTER_PI) 
       
   119      {
       
   120 	  //pitch is here between -90 and -180degs. We swap it back to between 0 and -90degs. Heading needs swaping then too.
       
   121 	  aPitch = aPitch + (QUARTER_PI-aPitch)*2;
       
   122 	  aHeading = aHeading + PI;
       
   123 	  aRoll = aRoll + PI; //I'm not sure if this line is needed. Remove it if you don't get correct results for roll or upVector
       
   124      }
       
   125 
       
   126      while (aHeading < 0)
       
   127      {
       
   128 	    aHeading = aHeading + TWO_PI;
       
   129 	 }
       
   130      
       
   131      while (aHeading > TWO_PI)
       
   132      {
       
   133 	    aHeading = aHeading - TWO_PI;
       
   134      }
       
   135      
       
   136      while (aRoll < 0)
       
   137      {
       
   138         aRoll = aRoll + TWO_PI;
       
   139      }
       
   140      while (aRoll > TWO_PI)
       
   141      {
       
   142 	    aRoll = aRoll - TWO_PI;
       
   143      }
       
   144 
       
   145     iOrientationData.iHeading = aHeading;
       
   146 	iOrientationData.iPitch = aPitch;
       
   147 	iOrientationData.iRoll = aRoll;
       
   148 
       
   149 
       
   150     TReal headingSin, headingCos, pitchSin, pitchCos, rollSin, rollCos;
       
   151     
       
   152     User::LeaveIfError( Math::Sin( headingSin, (TReal)aHeading / 1000 ) );
       
   153     User::LeaveIfError( Math::Cos( headingCos, (TReal)aHeading / 1000) );
       
   154     User::LeaveIfError( Math::Sin( pitchSin, (TReal)aPitch / 1000) );
       
   155     User::LeaveIfError( Math::Cos( pitchCos, (TReal)aPitch / 1000) );
       
   156     User::LeaveIfError( Math::Sin( rollSin, (TReal)aRoll / 1000) );
       
   157     User::LeaveIfError( Math::Cos( rollCos, (TReal)aRoll / 1000) );   
       
   158         		
       
   159     iOrientationData.iFrontX = -headingSin * pitchCos * 1000;
       
   160     iOrientationData.iFrontY = pitchSin * 1000;
       
   161     iOrientationData.iFrontZ = -headingCos * pitchCos * 1000;
       
   162     iOrientationData.iAboveX = (-rollSin * headingCos + rollCos * pitchSin * headingSin) * 1000;
       
   163     iOrientationData.iAboveY = pitchCos * rollCos * 1000;
       
   164     iOrientationData.iAboveZ = (rollSin * headingSin + rollCos * headingCos * pitchSin) * 1000;
       
   165 	}
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // COrientation::SetOrientationVectorsL
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 EXPORT_C void COrientation::SetOrientationVectorsL(
       
   172 	TInt32 aFrontX, TInt32 aFrontY, TInt32 aFrontZ,
       
   173 	TInt32 aAboveX, TInt32 aAboveY, TInt32 aAboveZ )
       
   174 	{
       
   175 
       
   176     iOrientationData.iFrontX = aFrontX;
       
   177 	iOrientationData.iFrontY = aFrontY;
       
   178 	iOrientationData.iFrontZ = aFrontZ;
       
   179 	iOrientationData.iAboveX = aAboveX;
       
   180 	iOrientationData.iAboveY = aAboveY;
       
   181 	iOrientationData.iAboveZ = aAboveZ;
       
   182 	
       
   183 	TInt32 previous_valid_value_of_pitch = iOrientationData.iPitch;
       
   184 	
       
   185 	
       
   186 	if(!((aFrontX==0) && (aFrontZ==0))) 
       
   187 	{
       
   188 		TReal xDividedByZAtan;
       
   189         xDividedByZAtan = atan2 (-aFrontX, -aFrontZ);
       
   190         
       
   191         if(xDividedByZAtan > 0)
       
   192 	      iOrientationData.iHeading = (TInt32) (xDividedByZAtan * 1000 + 0.5);
       
   193         else
       
   194  	      iOrientationData.iHeading = (TInt32) (xDividedByZAtan * 1000 - 0.5);
       
   195         
       
   196 	}
       
   197   
       
   198     if(aFrontX == 0 && aFrontZ == 0) 
       
   199     {
       
   200 	  if (aFrontY > 0)
       
   201 	     iOrientationData.iPitch = KPi/2 * 1000 + 0.5; //verify the units!
       
   202 	  else if (aFrontY == 0)
       
   203 	     iOrientationData.iPitch = previous_valid_value_of_pitch;
       
   204 	  else if (aFrontY < 0)
       
   205 	     iOrientationData.iPitch = -KPi/2 * 1000 - 0.5; //verify the units!
       
   206     } 
       
   207     else 
       
   208     {
       
   209 
       
   210       TReal sqrtXZ = 0, squareFx = 0, squareFz = 0;
       
   211       Math::Pow(squareFx, aFrontX, 2);
       
   212       Math::Pow(squareFz, aFrontZ, 2);
       
   213       TReal total = squareFx + squareFz;
       
   214 
       
   215       Math::Sqrt (sqrtXZ, total);
       
   216       
       
   217       TReal yTotalAtan =  atan2 ((TReal)aFrontY, sqrtXZ);
       
   218       
       
   219       if (yTotalAtan > 0)
       
   220    	    iOrientationData.iPitch = (TInt32) (yTotalAtan * 1000 + 0.5);
       
   221       else
       
   222     	 iOrientationData.iPitch = (TInt32) (yTotalAtan * 1000 - 0.5);
       
   223      
       
   224      }  
       
   225 		
       
   226     TReal rot_aboveX = 0;
       
   227     
       
   228     rot_aboveX = cos(-((TReal)iOrientationData.iHeading)/1000) * (((TReal)aAboveX)/1000) + sin(-((TReal)iOrientationData.iHeading)/1000) * (((TReal)aAboveZ)/1000);
       
   229 
       
   230     TReal rot_aboveY = 0;
       
   231     
       
   232     rot_aboveY = sin(-((TReal)iOrientationData.iHeading)/1000) * sin(-((TReal)iOrientationData.iPitch)/1000) * (((TReal)aAboveX)/1000) + cos(-((TReal)iOrientationData.iPitch)/1000) * (((TReal)aAboveY)/1000) - sin(-(TReal)iOrientationData.iPitch/1000) * cos(-((TReal)iOrientationData.iHeading)/1000)* (((TReal)aAboveZ)/1000);
       
   233 
       
   234     TReal roll = atan2(-rot_aboveX, rot_aboveY);
       
   235    	
       
   236    	if (roll > 0)
       
   237    	  iOrientationData.iRoll = (TInt32) (roll * 1000 + 0.5);  
       
   238 	else
       
   239    	  iOrientationData.iRoll = (TInt32) (roll * 1000 - 0.5);  
       
   240 	
       
   241 
       
   242      while (iOrientationData.iHeading < 0)
       
   243 	    iOrientationData.iHeading = iOrientationData.iHeading + TWO_PI;
       
   244 	    
       
   245      while (iOrientationData.iHeading > TWO_PI)
       
   246 	    iOrientationData.iHeading = iOrientationData.iHeading - TWO_PI;
       
   247 
       
   248      while (iOrientationData.iPitch < 0)
       
   249 	    iOrientationData.iPitch = iOrientationData.iPitch + TWO_PI;
       
   250 	    
       
   251      while (iOrientationData.iPitch > TWO_PI)
       
   252 	    iOrientationData.iPitch = iOrientationData.iPitch - TWO_PI;
       
   253 	    
       
   254      while (iOrientationData.iRoll < 0)
       
   255 	    iOrientationData.iRoll = iOrientationData.iRoll + TWO_PI;
       
   256      
       
   257      while (iOrientationData.iRoll > TWO_PI)
       
   258 	    iOrientationData.iRoll = iOrientationData.iRoll - TWO_PI;
       
   259 
       
   260 	}
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // COrientation::DoEffectData
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 EXPORT_C const TDesC8& COrientation::DoEffectData()
       
   267 	{
       
   268 #ifdef _DEBUG
       
   269     RDebug::Print(_L("COrientation::DoEffectData"));
       
   270 #endif
       
   271 	iDataPckgTo = iOrientationData;
       
   272 	return iDataPckgTo;
       
   273 	}
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // COrientation::SetEffectData
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 EXPORT_C void COrientation::SetEffectData(
       
   280 	const TDesC8& aEffectDataBuffer )
       
   281 	{
       
   282 #ifdef _DEBUG
       
   283     RDebug::Print(_L("COrientation::SetEffectData"));
       
   284 #endif
       
   285 	TEfOrientationDataPckg dataPckg;
       
   286 	dataPckg.Copy(aEffectDataBuffer);
       
   287 	iOrientationData = dataPckg();
       
   288 	iEnabled = iOrientationData.iEnabled;
       
   289 	iEnforced = iOrientationData.iEnforced;
       
   290 	iHaveUpdateRights = iOrientationData.iHaveUpdateRights;
       
   291 
       
   292 	}
       
   293 
       
   294 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   295 
       
   296 // End of File