|
1 /* |
|
2 * Copyright (c) 2002 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: Graphics Extension Library source file |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32std.h> |
|
20 #include "GfxFloatFixPt.h" |
|
21 |
|
22 |
|
23 #ifdef SVG_FLOAT_BUILD |
|
24 #include <e32math.h> |
|
25 |
|
26 // -------------------------------------------------------------------------- |
|
27 // |
|
28 // --------------------------------------------------------------------------- |
|
29 void TFloatFixPt::GetString( float aFloat, TDes& aBuf ) |
|
30 { |
|
31 TRealFormat rf( 10, 3 ); |
|
32 rf.iType = KRealFormatFixed | KDoNotUseTriads; |
|
33 aBuf.Num( ( aFloat ), rf ); |
|
34 } |
|
35 |
|
36 // -------------------------------------------------------------------------- |
|
37 // |
|
38 // --------------------------------------------------------------------------- |
|
39 float TFloatFixPt::ConvertString( const TDesC& aVal ) |
|
40 { |
|
41 TLex aString( aVal ); |
|
42 TReal32 val; |
|
43 aString.SkipSpace(); |
|
44 aString.Val( val, '.' ); |
|
45 return float( val ); |
|
46 } |
|
47 |
|
48 // -------------------------------------------------------------------------- |
|
49 // Error code is returned |
|
50 // --------------------------------------------------------------------------- |
|
51 TInt TFloatFixPt::ConvertString( const TDesC& aString, float& aValue ) |
|
52 { |
|
53 TLex lex( aString ); |
|
54 lex.SkipSpace(); |
|
55 return lex.Val( aValue, '.' ); |
|
56 } |
|
57 |
|
58 // -------------------------------------------------------------------------- |
|
59 // Error code is returned |
|
60 // --------------------------------------------------------------------------- |
|
61 TInt TFloatFixPt::ConvertString( const TDesC& aString, TFloatFixPt& aValue ) |
|
62 { |
|
63 TLex lex( aString ); |
|
64 lex.SkipSpace(); |
|
65 return lex.Val( aValue.iValue, '.' ); |
|
66 } |
|
67 |
|
68 // -------------------------------------------------------------------------- |
|
69 // |
|
70 // --------------------------------------------------------------------------- |
|
71 float TFloatFixPt::Sqrt( float number ) |
|
72 { |
|
73 long i; |
|
74 float x, y; |
|
75 const float f = 1.5F; |
|
76 |
|
77 x = number * 0.5F; |
|
78 y = number; |
|
79 i = * ( long * ) &y; |
|
80 i = 0x5f3759df - ( i >> 1 ); |
|
81 y = * ( float * ) &i; |
|
82 y = y * ( f - ( x * y * y ) ); |
|
83 y = y * ( f - ( x * y * y ) ); |
|
84 return number * y; |
|
85 } |
|
86 |
|
87 // -------------------------------------------------------------------------- |
|
88 // |
|
89 // --------------------------------------------------------------------------- |
|
90 float TFloatFixPt::SinFloatDouble(float angle) |
|
91 { |
|
92 double sn = 0; |
|
93 Math::Sin(sn, double(angle)); |
|
94 return float(sn); |
|
95 } |
|
96 |
|
97 // -------------------------------------------------------------------------- |
|
98 // |
|
99 // --------------------------------------------------------------------------- |
|
100 float TFloatFixPt::CosFloatDouble(float angle) |
|
101 { |
|
102 //double cs = 0; |
|
103 float cs = 0; |
|
104 //Math::Cos(cs, double(angle)); |
|
105 //cs = CosineDouble(double(angle)); |
|
106 cs = CosineDouble(angle); |
|
107 return float(cs); |
|
108 } |
|
109 |
|
110 float TFloatFixPt::CosineDouble(float x) |
|
111 { |
|
112 float p0,p1,p2,p3,p4,p5,y,t,absx,frac,quad,pi2; |
|
113 p0= 0.999999999781; |
|
114 p1=-0.499999993585; |
|
115 p2= 0.041666636258; |
|
116 p3=-0.0013888361399; |
|
117 p4= 0.00002476016134; |
|
118 p5=-0.00000026051495; |
|
119 pi2=1.570796326794896; /* pi/2 */ |
|
120 absx=x; |
|
121 if (x<0) absx=-absx; /* absolute value of input */ |
|
122 quad=(int) (absx/pi2); /* quadrant (0 to 3) */ |
|
123 frac= (absx/pi2) - quad; /* fractional part of input */ |
|
124 if(quad==0) t=frac * pi2; |
|
125 if(quad==1) t=(1-frac) * pi2; |
|
126 if(quad==2) t=frac * pi2; |
|
127 if(quad==3) t=(frac-1) * pi2; |
|
128 t=t * t; |
|
129 y=p0 + (p1*t) + (p2*t*t) + (p3*t*t*t) + (p4*t*t*t*t) + (p5*t*t*t*t*t); |
|
130 if(quad==2 | quad==1) y=-y; /* correct sign */ |
|
131 return(y); |
|
132 } |
|
133 |
|
134 // -------------------------------------------------------------------------- |
|
135 // |
|
136 // --------------------------------------------------------------------------- |
|
137 float TFloatFixPt::TanFloatDouble(float angle) |
|
138 { |
|
139 double tn = 0; |
|
140 Math::Tan(tn, double(angle)); |
|
141 return float(tn); |
|
142 } |
|
143 |
|
144 |
|
145 // -------------------------------------------------------------------------- |
|
146 // |
|
147 // --------------------------------------------------------------------------- |
|
148 float TFloatFixPt::SinApprox(float angle) |
|
149 { |
|
150 float c = 0.70710678118654752440f; |
|
151 return ((2 - 4 * c) * angle * angle + c + angle); |
|
152 } |
|
153 |
|
154 // -------------------------------------------------------------------------- |
|
155 // |
|
156 // --------------------------------------------------------------------------- |
|
157 float TFloatFixPt::CosApprox(float angle) |
|
158 { |
|
159 float c = 0.70710678118654752440f; |
|
160 return ((2 - 4 * c) * angle * angle + c - angle); |
|
161 } |
|
162 |
|
163 // -------------------------------------------------------------------------- |
|
164 // |
|
165 // --------------------------------------------------------------------------- |
|
166 float TFloatFixPt::FastSin(const float val) |
|
167 { |
|
168 float fASqr = val*val; |
|
169 float fResult = -2.39e-08f; |
|
170 fResult *= fASqr; |
|
171 fResult += 2.7526e-06f; |
|
172 fResult *= fASqr; |
|
173 fResult -= 1.98409e-04f; |
|
174 fResult *= fASqr; |
|
175 fResult += 8.3333315e-03f; |
|
176 fResult *= fASqr; |
|
177 fResult -= 1.666666664e-01f; |
|
178 fResult *= fASqr; |
|
179 fResult += 1.0f; |
|
180 fResult *= val; |
|
181 |
|
182 return fResult; |
|
183 } |
|
184 |
|
185 // -------------------------------------------------------------------------- |
|
186 // |
|
187 // --------------------------------------------------------------------------- |
|
188 float TFloatFixPt::FastCos(const float val) |
|
189 { |
|
190 float fASqr = val*val; |
|
191 float fResult = -2.605e-07f; |
|
192 fResult *= fASqr; |
|
193 fResult += 2.47609e-05f; |
|
194 fResult *= fASqr; |
|
195 fResult -= 1.3888397e-03f; |
|
196 fResult *= fASqr; |
|
197 fResult += 4.16666418e-02f; |
|
198 fResult *= fASqr; |
|
199 fResult -= 4.999999963e-01f; |
|
200 fResult *= fASqr; |
|
201 fResult += 1.0f; |
|
202 |
|
203 return fResult; |
|
204 } |
|
205 |
|
206 // -------------------------------------------------------------------------- |
|
207 // |
|
208 // --------------------------------------------------------------------------- |
|
209 float TFloatFixPt::FastTan(const float val) |
|
210 { |
|
211 float fASqr = val*val; |
|
212 float fResult = 9.5168091e-03f; |
|
213 fResult *= fASqr; |
|
214 fResult += 2.900525e-03f; |
|
215 fResult *= fASqr; |
|
216 fResult += 2.45650893e-02f; |
|
217 fResult *= fASqr; |
|
218 fResult += 5.33740603e-02f; |
|
219 fResult *= fASqr; |
|
220 fResult += 1.333923995e-01f; |
|
221 fResult *= fASqr; |
|
222 fResult += 3.333314036e-01f; |
|
223 fResult *= fASqr; |
|
224 fResult += 1.0f; |
|
225 fResult *= val; |
|
226 |
|
227 return fResult; |
|
228 |
|
229 } |
|
230 |
|
231 // -------------------------------------------------------------------------- |
|
232 // |
|
233 // --------------------------------------------------------------------------- |
|
234 float TFloatFixPt::FastASin(float val) |
|
235 { |
|
236 float fRoot = Sqrt(1.0f-val); |
|
237 float fResult = -0.0187293f; |
|
238 fResult *= val; |
|
239 fResult += 0.0742610f; |
|
240 fResult *= val; |
|
241 fResult -= 0.2121144f; |
|
242 fResult *= val; |
|
243 fResult += 1.5707288f; |
|
244 fResult = 1.57079632679489661923 - fRoot*fResult; |
|
245 |
|
246 return fResult; |
|
247 } |
|
248 |
|
249 // -------------------------------------------------------------------------- |
|
250 // |
|
251 // --------------------------------------------------------------------------- |
|
252 float TFloatFixPt::FastACos(float val) |
|
253 { |
|
254 float fRoot = Sqrt(1.0f-val); |
|
255 float fResult = -0.0187293f; |
|
256 fResult *= val; |
|
257 fResult += 0.0742610f; |
|
258 fResult *= val; |
|
259 fResult -= 0.2121144f; |
|
260 fResult *= val; |
|
261 fResult += 1.5707288f; |
|
262 fResult *= fRoot; |
|
263 |
|
264 return fResult; |
|
265 } |
|
266 |
|
267 // -------------------------------------------------------------------------- |
|
268 // |
|
269 // --------------------------------------------------------------------------- |
|
270 float TFloatFixPt::FastATan(float val) |
|
271 { |
|
272 float fVSqr = val*val; |
|
273 float fResult = 0.0028662257f; |
|
274 fResult *= fVSqr; |
|
275 fResult -= 0.0161657367f; |
|
276 fResult *= fVSqr; |
|
277 fResult += 0.0429096138f; |
|
278 fResult *= fVSqr; |
|
279 fResult -= 0.0752896400f; |
|
280 fResult *= fVSqr; |
|
281 fResult += 0.1065626393f; |
|
282 fResult *= fVSqr; |
|
283 fResult -= 0.1420889944f; |
|
284 fResult *= fVSqr; |
|
285 fResult += 0.1999355085f; |
|
286 fResult *= fVSqr; |
|
287 fResult -= 0.3333314528f; |
|
288 fResult *= fVSqr; |
|
289 fResult += 1.0f; |
|
290 fResult *= val; |
|
291 |
|
292 return fResult; |
|
293 } |
|
294 |
|
295 // -------------------------------------------------------------------------- |
|
296 // |
|
297 // --------------------------------------------------------------------------- |
|
298 float TFloatFixPt::Cos(float angle) |
|
299 /* computes cos of x (x in radians) by an expansion */ |
|
300 { |
|
301 float result = 1.0; |
|
302 int factor = 1; |
|
303 float power = angle; |
|
304 |
|
305 for ( int i = 2; i <= 10; i++ ) |
|
306 { |
|
307 factor = factor * i; |
|
308 power = power * angle; |
|
309 |
|
310 if ( (i & 1) == 0 ) |
|
311 { |
|
312 if ( (i & 3) == 0 ) |
|
313 { |
|
314 result += power/factor; |
|
315 } |
|
316 else |
|
317 { |
|
318 result -= power/factor; |
|
319 } |
|
320 } |
|
321 } |
|
322 return (result); |
|
323 } |
|
324 |
|
325 // -------------------------------------------------------------------------- |
|
326 // TFloatFixPt TFloatFixPt::Sqrt( TFloatFixPt aA ) |
|
327 // --------------------------------------------------------------------------- |
|
328 TFloatFixPt TFloatFixPt::Sqrt( TFloatFixPt aA ) |
|
329 { |
|
330 TFloatFixPt tmp; |
|
331 tmp.iValue = Sqrt( aA.iValue ); |
|
332 return tmp; |
|
333 } |
|
334 |
|
335 // -------------------------------------------------------------------------- |
|
336 // TFloatFixPt TFloatFixPt::Abs( TFixPt& aA ) |
|
337 // --------------------------------------------------------------------------- |
|
338 TFloatFixPt TFloatFixPt::Abs( TFloatFixPt& aA ) |
|
339 { |
|
340 return ( aA.iValue > 0.0f ) ? TFloatFixPt( aA.iValue ) : ( TFloatFixPt( -aA.iValue ) ); |
|
341 } |
|
342 |
|
343 // -------------------------------------------------------------------------- |
|
344 // -------------------------------------------------------------------------- |
|
345 #else /*FIXED POINT BUILD*/ |
|
346 // -------------------------------------------------------------------------- |
|
347 // -------------------------------------------------------------------------- |
|
348 |
|
349 // -------------------------------------------------------------------------- |
|
350 // void TFixPt::GetString( TDes& aBuf ) const |
|
351 // --------------------------------------------------------------------------- |
|
352 void TFloatFixPt::GetString( TDes& aBuf ) const |
|
353 { |
|
354 TRealFormat rf( 10, 3 ); |
|
355 rf.iType = KRealFormatFixed | KDoNotUseTriads; |
|
356 aBuf.Num( ( ( TReal32 ) iValue ) / KFixPtFracVal, rf ); |
|
357 } |
|
358 |
|
359 // -------------------------------------------------------------------------- |
|
360 // TFloatFixPt TFloatFixPt::ConvertString( const TDesC& aVal ) |
|
361 // --------------------------------------------------------------------------- |
|
362 TFloatFixPt TFloatFixPt::ConvertString( const TDesC& aVal ) |
|
363 { |
|
364 TLex aString( aVal ); |
|
365 TReal32 val; |
|
366 aString.SkipSpace(); |
|
367 aString.Val( val, '.' ); |
|
368 return TFloatFixPt( val ); |
|
369 } |
|
370 |
|
371 // -------------------------------------------------------------------------- |
|
372 // TInt TFloatFixPt::ConvertString( const TDesC& aValueString, TFloatFixPt& aValue ) |
|
373 // --------------------------------------------------------------------------- |
|
374 TInt TFloatFixPt::ConvertString( const TDesC& aValueString, TFloatFixPt& aValue ) |
|
375 { |
|
376 TLex aString( aValueString ); |
|
377 TReal32 value; |
|
378 aString.SkipSpace(); |
|
379 TInt errorCode = aString.Val( value, '.' ); |
|
380 aValue = (float)value; |
|
381 return errorCode; |
|
382 } |
|
383 // -------------------------------------------------------------------------- |
|
384 // TFloatFixPt TFloatFixPt::Abs( TFixPt& aA ) |
|
385 // --------------------------------------------------------------------------- |
|
386 TFloatFixPt TFloatFixPt::Abs( TFloatFixPt& aA ) |
|
387 { |
|
388 TFloatFixPt KZero; |
|
389 return ( aA > KZero ) ? aA : ( KZero - aA ); |
|
390 } |
|
391 |
|
392 // ========================================================================== |
|
393 // fixed point SQRT from Graphics Gems |
|
394 // ========================================================================== |
|
395 // -------------------------------------------------------------------------- |
|
396 // TFloatFixPt TFloatFixPt::Sqrt( TFloatFixPt aA ) |
|
397 // --------------------------------------------------------------------------- |
|
398 TFloatFixPt TFloatFixPt::Sqrt( TFloatFixPt aA ) |
|
399 { |
|
400 TFloatFixPt tmp; |
|
401 tmp.iValue = FixedSqrtGeneral( aA.iValue, KFixPtFrac ); |
|
402 return tmp; |
|
403 } |
|
404 |
|
405 // ========================================================================== |
|
406 // Fixed point sqrt from Graphics Gems |
|
407 // ========================================================================== |
|
408 // -------------------------------------------------------------------------- |
|
409 // TInt32 TFloatFixPt::FixedSqrtGeneral( TInt32 aX, TUint32 aFracbits ) |
|
410 // --------------------------------------------------------------------------- |
|
411 TInt32 TFloatFixPt::FixedSqrtGeneral( TInt32 aX, TUint32 aFracbits ) |
|
412 { |
|
413 TUint32 root, remHi, remLo, testDiv, count; |
|
414 root = 0; |
|
415 remHi = 0; |
|
416 remLo = aX; |
|
417 count = 15 + ( aFracbits >> 1 ); |
|
418 |
|
419 do |
|
420 { |
|
421 remHi = ( remHi << 2 ) | ( remLo >> 30 ); |
|
422 remLo <<= 2; |
|
423 root <<= 1; |
|
424 testDiv = ( root << 1 ) + 1; |
|
425 if ( remHi >= testDiv ) |
|
426 { |
|
427 remHi -= testDiv; |
|
428 root += 1; |
|
429 } |
|
430 } |
|
431 while ( count-- != 0 ); |
|
432 |
|
433 return root; |
|
434 } |
|
435 #endif |