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