|
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 Location 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 <LocationBase.h> |
|
28 #include <e32math.h> |
|
29 #include <math.h> |
|
30 |
|
31 |
|
32 //360 degrees: |
|
33 #define TWO_PI 6283 |
|
34 //180 degrees: |
|
35 #define PI 3142 |
|
36 //90 degrees: |
|
37 #define QUARTER_PI 1570 |
|
38 // ============================ MEMBER FUNCTIONS =============================== |
|
39 |
|
40 // ----------------------------------------------------------------------------- |
|
41 // CLocation::CLocation |
|
42 // C++ default constructor can NOT contain any code, that |
|
43 // might leave. |
|
44 // ----------------------------------------------------------------------------- |
|
45 // |
|
46 EXPORT_C CLocation::CLocation() |
|
47 : iLocationData(0,0,0,0,0,0), |
|
48 iDataPckgTo(iLocationData), |
|
49 iDataPckgFrom(iLocationData) |
|
50 { |
|
51 } |
|
52 |
|
53 // Destructor |
|
54 EXPORT_C CLocation::~CLocation() |
|
55 { |
|
56 } |
|
57 |
|
58 // ----------------------------------------------------------------------------- |
|
59 // CLocation::LocationCartesian |
|
60 // ----------------------------------------------------------------------------- |
|
61 // |
|
62 EXPORT_C void CLocation::LocationCartesian( |
|
63 TInt32& aX, TInt32& aY, TInt32& aZ ) |
|
64 { |
|
65 aX = iLocationData.iXCoordinate; |
|
66 aY = iLocationData.iYCoordinate; |
|
67 aZ = iLocationData.iZCoordinate; |
|
68 } |
|
69 |
|
70 // ----------------------------------------------------------------------------- |
|
71 // CLocation::LocationSpherical |
|
72 // ----------------------------------------------------------------------------- |
|
73 // |
|
74 EXPORT_C void CLocation::LocationSpherical( |
|
75 TInt32& aAzimuth, TInt32& aElevation, TInt32& aRadius ) |
|
76 { |
|
77 aAzimuth = iLocationData.iAzimuth; |
|
78 aElevation = iLocationData.iElevation; |
|
79 aRadius = iLocationData.iRadius; |
|
80 } |
|
81 |
|
82 // ----------------------------------------------------------------------------- |
|
83 // CLocation::SetLocationCartesianL |
|
84 // ----------------------------------------------------------------------------- |
|
85 // |
|
86 EXPORT_C void CLocation::SetLocationCartesianL( |
|
87 TInt32& aX, TInt32& aY, TInt32& aZ ) |
|
88 { |
|
89 iLocationData.iXCoordinate = aX; |
|
90 iLocationData.iYCoordinate = aY; |
|
91 iLocationData.iZCoordinate = aZ; |
|
92 |
|
93 |
|
94 TReal SqrtXYZ = 0, squareX = 0, squareY = 0, squareZ = 0; |
|
95 Math::Pow(squareX, aX, 2); |
|
96 Math::Pow(squareY, aY, 2); |
|
97 Math::Pow(squareZ, aZ, 2); |
|
98 TReal sum = squareX + squareY + squareZ; |
|
99 Math::Sqrt(SqrtXYZ, sum); |
|
100 |
|
101 |
|
102 //Singularity region |
|
103 |
|
104 if(!((aX==0) && (aZ==0))) //was aY |
|
105 { |
|
106 |
|
107 TReal zDividedByXAtan = atan2 ((TReal)-aX, (TReal)-aZ); |
|
108 |
|
109 if (zDividedByXAtan > 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...) |
|
111 else |
|
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...) |
|
113 |
|
114 } |
|
115 // else { we are exactly on Y-axis and therefore azimuth is undefined; let's use the previous azimuth value instead } |
|
116 |
|
117 |
|
118 if (!((aX ==0) && (aY == 0) && (aZ == 0))) |
|
119 { |
|
120 |
|
121 TReal result; |
|
122 TReal yDividedBySqrtXYZ = aY/SqrtXYZ; |
|
123 User::LeaveIfError(Math::ASin(result, yDividedBySqrtXYZ)); //was ACos |
|
124 |
|
125 if (result > 0) |
|
126 iLocationData.iElevation = (TInt32) (result * 1000 + 0.5); |
|
127 else |
|
128 iLocationData.iElevation = (TInt32) (result * 1000 - 0.5); |
|
129 |
|
130 } |
|
131 // else { we are exactly in origin and therefore elevation is undefined; let's use the previous elevation value instead } |
|
132 |
|
133 iLocationData.iRadius= (TInt32) (SqrtXYZ + 0.5); |
|
134 |
|
135 |
|
136 while(iLocationData.iElevation > PI) |
|
137 { |
|
138 iLocationData.iElevation = iLocationData.iElevation - TWO_PI; |
|
139 } |
|
140 |
|
141 if(iLocationData.iElevation > QUARTER_PI) |
|
142 { |
|
143 iLocationData.iElevation = iLocationData.iElevation - (iLocationData.iElevation - QUARTER_PI) * 2; |
|
144 iLocationData.iAzimuth = iLocationData.iAzimuth + PI; |
|
145 } |
|
146 |
|
147 while(iLocationData.iElevation < -PI) |
|
148 { |
|
149 iLocationData.iElevation = iLocationData.iElevation + TWO_PI; |
|
150 } |
|
151 if(iLocationData.iElevation < -QUARTER_PI) |
|
152 { |
|
153 iLocationData.iElevation = iLocationData.iElevation + (QUARTER_PI - iLocationData.iElevation) * 2; |
|
154 iLocationData.iAzimuth = iLocationData.iAzimuth + PI; |
|
155 } |
|
156 |
|
157 while (iLocationData.iAzimuth < 0) |
|
158 iLocationData.iAzimuth = iLocationData.iAzimuth + TWO_PI; |
|
159 while (iLocationData.iAzimuth > TWO_PI) |
|
160 iLocationData.iAzimuth = iLocationData.iAzimuth - TWO_PI; |
|
161 |
|
162 } |
|
163 |
|
164 // ----------------------------------------------------------------------------- |
|
165 // CLocation::SetLocationSphericalL |
|
166 // ----------------------------------------------------------------------------- |
|
167 // |
|
168 EXPORT_C void CLocation::SetLocationSphericalL( |
|
169 TInt32& aAzimuth, TInt32& aElevation, TInt32& aRadius ) |
|
170 { |
|
171 |
|
172 while(aElevation > PI) |
|
173 { |
|
174 aElevation = aElevation - TWO_PI; |
|
175 } |
|
176 |
|
177 if(aElevation > QUARTER_PI) |
|
178 { |
|
179 aElevation = aElevation - (aElevation - QUARTER_PI) * 2; |
|
180 aAzimuth = aAzimuth + PI; |
|
181 } |
|
182 |
|
183 while(aElevation < -PI) |
|
184 { |
|
185 aElevation = aElevation + TWO_PI; |
|
186 } |
|
187 if(aElevation < -QUARTER_PI) |
|
188 { |
|
189 aElevation = aElevation + (QUARTER_PI - aElevation) * 2; |
|
190 aAzimuth = aAzimuth + PI; |
|
191 } |
|
192 |
|
193 while (aAzimuth < 0) |
|
194 aAzimuth = aAzimuth + TWO_PI; |
|
195 while (aAzimuth > TWO_PI) |
|
196 aAzimuth = aAzimuth - TWO_PI; |
|
197 |
|
198 |
|
199 iLocationData.iAzimuth = aAzimuth; |
|
200 iLocationData.iElevation = aElevation; |
|
201 iLocationData.iRadius = aRadius; |
|
202 |
|
203 |
|
204 TReal elevation = aElevation / 1000.0; // conversion from milliradians to radians because Sin and Cos functions eat radians |
|
205 |
|
206 TReal elevationSin; |
|
207 TReal elevationCos; |
|
208 User::LeaveIfError( Math::Sin( elevationSin, elevation ) ); |
|
209 User::LeaveIfError( Math::Cos( elevationCos, elevation ) ); |
|
210 |
|
211 TReal azimuthSin; |
|
212 TReal azimuthCos; |
|
213 User::LeaveIfError( Math::Sin( azimuthSin, aAzimuth / 1000.0) ); |
|
214 User::LeaveIfError( Math::Cos( azimuthCos, aAzimuth / 1000.0) ); |
|
215 |
|
216 |
|
217 iLocationData.iXCoordinate = (TInt32)(0.5 + aRadius * elevationCos * azimuthSin); |
|
218 iLocationData.iYCoordinate = (TInt32)(0.5 + aRadius * elevationSin); |
|
219 iLocationData.iZCoordinate = (TInt32)(0.5 - aRadius * elevationCos * azimuthCos); |
|
220 |
|
221 } |
|
222 |
|
223 // ----------------------------------------------------------------------------- |
|
224 // CLocation::DoEffectData |
|
225 // ----------------------------------------------------------------------------- |
|
226 // |
|
227 EXPORT_C const TDesC8& CLocation::DoEffectData() |
|
228 { |
|
229 #ifdef _DEBUG |
|
230 RDebug::Print(_L("CLocation::DoEffectData")); |
|
231 #endif |
|
232 iDataPckgTo = iLocationData; |
|
233 return iDataPckgTo; |
|
234 } |
|
235 |
|
236 // ----------------------------------------------------------------------------- |
|
237 // CLocation::SetEffectData |
|
238 // ----------------------------------------------------------------------------- |
|
239 // |
|
240 EXPORT_C void CLocation::SetEffectData( |
|
241 const TDesC8& aEffectDataBuffer ) |
|
242 { |
|
243 #ifdef _DEBUG |
|
244 RDebug::Print(_L("CLocation::SetEffectData")); |
|
245 #endif |
|
246 TEfLocationDataPckg dataPckg; |
|
247 dataPckg.Copy(aEffectDataBuffer); |
|
248 iLocationData = dataPckg(); |
|
249 iEnabled = iLocationData.iEnabled; |
|
250 iEnforced = iLocationData.iEnforced; |
|
251 iHaveUpdateRights = iLocationData.iHaveUpdateRights; |
|
252 |
|
253 } |
|
254 |
|
255 // ========================== OTHER EXPORTED FUNCTIONS ========================= |
|
256 |
|
257 // End of File |