1*01aa44aaSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*01aa44aaSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*01aa44aaSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*01aa44aaSAndrew Rist * distributed with this work for additional information
6*01aa44aaSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*01aa44aaSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*01aa44aaSAndrew Rist * "License"); you may not use this file except in compliance
9*01aa44aaSAndrew Rist * with the License. You may obtain a copy of the License at
10*01aa44aaSAndrew Rist *
11*01aa44aaSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*01aa44aaSAndrew Rist *
13*01aa44aaSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*01aa44aaSAndrew Rist * software distributed under the License is distributed on an
15*01aa44aaSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*01aa44aaSAndrew Rist * KIND, either express or implied. See the License for the
17*01aa44aaSAndrew Rist * specific language governing permissions and limitations
18*01aa44aaSAndrew Rist * under the License.
19*01aa44aaSAndrew Rist *
20*01aa44aaSAndrew Rist *************************************************************/
21*01aa44aaSAndrew Rist
22*01aa44aaSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef _MCVMATH_HXX
25cdf0e10cSrcweir #define _MCVMATH_HXX
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <tools/solar.h>
28cdf0e10cSrcweir
29cdf0e10cSrcweir class FixCpx;
30cdf0e10cSrcweir class ColWheel;
31cdf0e10cSrcweir
32cdf0e10cSrcweir // No of fractal bits
33cdf0e10cSrcweir // allowed range 0..14, must be even
34cdf0e10cSrcweir #define FIX_POST 14
35cdf0e10cSrcweir
36cdf0e10cSrcweir // scale for ...Big() -Functions
37cdf0e10cSrcweir #if (FIX_POST>=4)
38cdf0e10cSrcweir #define FIX_P2 4
39cdf0e10cSrcweir #define FIX_P3 (FIX_POST-FIX_P2)
40cdf0e10cSrcweir #else
41cdf0e10cSrcweir #define FIX_P2 0
42cdf0e10cSrcweir #define FIX_P3 FIX_POST
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir
45cdf0e10cSrcweir #if (FIX_POST>=1)
46cdf0e10cSrcweir #define FIX_ADD (1<<(FIX_POST-1))
47cdf0e10cSrcweir #else
48cdf0e10cSrcweir #define FIX_ADD 0
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir
51cdf0e10cSrcweir #if (FIX_P2>=1)
52cdf0e10cSrcweir #define FIX_A2 (1<<(FIX_P2-1))
53cdf0e10cSrcweir #else
54cdf0e10cSrcweir #define FIX_A2 0
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir
57cdf0e10cSrcweir #if (FIX_P3>=1)
58cdf0e10cSrcweir #define FIX_A3 (1<<(FIX_P3-1))
59cdf0e10cSrcweir #else
60cdf0e10cSrcweir #define FIX_A3 0
61cdf0e10cSrcweir #endif
62cdf0e10cSrcweir
63cdf0e10cSrcweir // -------
64cdf0e10cSrcweir // - Fix -
65cdf0e10cSrcweir // -------
66cdf0e10cSrcweir
67cdf0e10cSrcweir class Fix
68cdf0e10cSrcweir {
69cdf0e10cSrcweir private:
70cdf0e10cSrcweir friend class FixCpx;
71cdf0e10cSrcweir friend class ColWheel;
72cdf0e10cSrcweir
73cdf0e10cSrcweir // friend Fix ImpMultBig2( const Fix& a, const Fix& b );
74cdf0e10cSrcweir
75cdf0e10cSrcweir public:
76cdf0e10cSrcweir long x;
77cdf0e10cSrcweir
78cdf0e10cSrcweir public:
Fix()79cdf0e10cSrcweir Fix() { x=0; }
Fix(int i)80cdf0e10cSrcweir Fix( int i ) { x=(long(i)<<FIX_POST); }
Fix(short l)81cdf0e10cSrcweir Fix( short l ) { x=(long(l)<<FIX_POST); }
Fix(sal_uInt16 l)82cdf0e10cSrcweir Fix( sal_uInt16 l ) { x=(long(l)<<FIX_POST); }
Fix(long l)83cdf0e10cSrcweir Fix( long l ) { x=(l<<FIX_POST); }
Fix(long Z,long N)84cdf0e10cSrcweir Fix( long Z, long N ) { x=(Z<<FIX_POST)/N; }
85cdf0e10cSrcweir
SetInternVal(long nVal)86cdf0e10cSrcweir void SetInternVal( long nVal ) { x=nVal; }
GetInternVal() const87cdf0e10cSrcweir long GetInternVal() const { return x; }
88cdf0e10cSrcweir
operator +=(const Fix & a)89cdf0e10cSrcweir void operator+= ( const Fix& a ) { x+=a.x; }
operator -=(const Fix & a)90cdf0e10cSrcweir void operator-= ( const Fix& a ) { x-=a.x; }
operator *=(const Fix & a)91cdf0e10cSrcweir void operator*= ( const Fix& a ) { x=(x*a.x+FIX_ADD)>>FIX_POST; }
operator /=(const Fix & a)92cdf0e10cSrcweir void operator/= ( const Fix& a ) { x=(x<<FIX_POST)/a.x; }
93cdf0e10cSrcweir friend Fix operator- ( const Fix& a );
94cdf0e10cSrcweir
MultBig(const Fix & a)95cdf0e10cSrcweir void MultBig( const Fix& a )
96cdf0e10cSrcweir { x=((((a.x+FIX_A2)>>FIX_P2)*x+FIX_A3)>>FIX_P3); }
DivBig(const Fix & a)97cdf0e10cSrcweir void DivBig( const Fix& a )
98cdf0e10cSrcweir { x=((x<<FIX_P3)/a.x)<<FIX_P2; }
99cdf0e10cSrcweir
operator >(const Fix & a,const Fix & b)100cdf0e10cSrcweir friend sal_Bool operator> ( const Fix& a, const Fix& b ) { return a.x > b.x; }
operator <(const Fix & a,const Fix & b)101cdf0e10cSrcweir friend sal_Bool operator< ( const Fix& a, const Fix& b ) { return a.x < b.x; }
102cdf0e10cSrcweir
103cdf0e10cSrcweir operator long() const { return (x+FIX_ADD) >> FIX_POST; }
104cdf0e10cSrcweir operator double() const { return double(x)/(1<<FIX_POST); }
105cdf0e10cSrcweir
106cdf0e10cSrcweir friend Fix operator+ ( const Fix& a, const Fix& b );
107cdf0e10cSrcweir friend Fix operator- ( const Fix& a, const Fix& b );
108cdf0e10cSrcweir friend Fix operator* ( const Fix& a, const Fix& b );
109cdf0e10cSrcweir friend Fix operator/ ( const Fix& a, const Fix& b );
110cdf0e10cSrcweir
111cdf0e10cSrcweir friend FixCpx operator-( const FixCpx& a );
112cdf0e10cSrcweir };
113cdf0e10cSrcweir
114cdf0e10cSrcweir // ----------
115cdf0e10cSrcweir // - FixCpx -
116cdf0e10cSrcweir // ----------
117cdf0e10cSrcweir
118cdf0e10cSrcweir class FixCpx
119cdf0e10cSrcweir {
120cdf0e10cSrcweir // friend FixCpx ImpMultBig2( const FixCpx& ra, const FixCpx& rb );
121cdf0e10cSrcweir
122cdf0e10cSrcweir public:
123cdf0e10cSrcweir Fix r;
124cdf0e10cSrcweir Fix i;
125cdf0e10cSrcweir
126cdf0e10cSrcweir public:
FixCpx()127cdf0e10cSrcweir FixCpx() : r(), i() {}
FixCpx(Fix a)128cdf0e10cSrcweir FixCpx( Fix a ) : r( a ), i() {}
FixCpx(Fix a,Fix b)129cdf0e10cSrcweir FixCpx( Fix a, Fix b ) : r( a ), i( b ) {}
130cdf0e10cSrcweir
GetReal()131cdf0e10cSrcweir Fix& GetReal() { return r; }
GetImag()132cdf0e10cSrcweir Fix& GetImag() { return i; }
133cdf0e10cSrcweir
134cdf0e10cSrcweir void operator*= ( const FixCpx& ra );
135cdf0e10cSrcweir void MultBig( const FixCpx& ra, const FixCpx& rb );
136cdf0e10cSrcweir
137cdf0e10cSrcweir friend FixCpx operator+ ( const FixCpx& a, const FixCpx& b );
138cdf0e10cSrcweir friend FixCpx operator- ( const FixCpx& a, const FixCpx& b );
139cdf0e10cSrcweir friend FixCpx operator* ( const FixCpx& a, const FixCpx& b );
140cdf0e10cSrcweir friend FixCpx operator/ ( const FixCpx& a, const FixCpx& b );
141cdf0e10cSrcweir friend FixCpx operator- ( const FixCpx& a );
142cdf0e10cSrcweir };
143cdf0e10cSrcweir
operator -(const Fix & a)144cdf0e10cSrcweir inline Fix operator- ( const Fix& a )
145cdf0e10cSrcweir {
146cdf0e10cSrcweir Fix f;
147cdf0e10cSrcweir f.x = -a.x;
148cdf0e10cSrcweir return f;
149cdf0e10cSrcweir }
150cdf0e10cSrcweir
operator +(const Fix & a,const Fix & b)151cdf0e10cSrcweir inline Fix operator+ ( const Fix& a, const Fix& b )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir long l = a.x+b.x;
154cdf0e10cSrcweir return *((Fix*)&l);
155cdf0e10cSrcweir }
156cdf0e10cSrcweir
operator -(const Fix & a,const Fix & b)157cdf0e10cSrcweir inline Fix operator- ( const Fix& a, const Fix& b )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir long l = a.x-b.x;
160cdf0e10cSrcweir return *((Fix*)&l);
161cdf0e10cSrcweir }
162cdf0e10cSrcweir
operator *(const Fix & a,const Fix & b)163cdf0e10cSrcweir inline Fix operator* ( const Fix& a, const Fix& b )
164cdf0e10cSrcweir {
165cdf0e10cSrcweir long l=(a.x*b.x+FIX_ADD)>>FIX_POST;
166cdf0e10cSrcweir return *((Fix*)&l);
167cdf0e10cSrcweir }
168cdf0e10cSrcweir
operator /(const Fix & a,const Fix & b)169cdf0e10cSrcweir inline Fix operator/ ( const Fix& a, const Fix& b )
170cdf0e10cSrcweir {
171cdf0e10cSrcweir long l=(a.x<<FIX_POST)/b.x;
172cdf0e10cSrcweir return *((Fix*)&l);
173cdf0e10cSrcweir }
174cdf0e10cSrcweir
operator -(const FixCpx & a)175cdf0e10cSrcweir inline FixCpx operator- ( const FixCpx& a )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir FixCpx fc;
178cdf0e10cSrcweir
179cdf0e10cSrcweir fc.r.x = -a.r.x;
180cdf0e10cSrcweir fc.i.x = -a.i.x;
181cdf0e10cSrcweir return fc;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir
operator +(const FixCpx & a,const FixCpx & b)184cdf0e10cSrcweir inline FixCpx operator+ ( const FixCpx& a, const FixCpx& b )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir return FixCpx( a.r+b.r, a.i+b.i );
187cdf0e10cSrcweir }
188cdf0e10cSrcweir
operator -(const FixCpx & a,const FixCpx & b)189cdf0e10cSrcweir inline FixCpx operator- ( const FixCpx& a, const FixCpx& b )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir return FixCpx( a.r-b.r, a.i-b.i );
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
operator *=(const FixCpx & ra)194cdf0e10cSrcweir inline void FixCpx::operator*= ( const FixCpx& ra )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir Fix rr = ra.r*r-ra.i*i;
197cdf0e10cSrcweir i = ra.r*i+ra.i*r;
198cdf0e10cSrcweir r = rr;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir
operator *(const FixCpx & a,const FixCpx & b)201cdf0e10cSrcweir inline FixCpx operator* ( const FixCpx& a, const FixCpx& b )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir return FixCpx( a.r*b.r-a.i*b.i, a.r*b.i+a.i*b.r );
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
operator /(const FixCpx & a,const FixCpx & b)206cdf0e10cSrcweir inline FixCpx operator/ ( const FixCpx& a, const FixCpx& b )
207cdf0e10cSrcweir {
208cdf0e10cSrcweir return FixCpx( (a.r*b.r+a.i*b.i)/(b.r*b.r+b.i*b.i),
209cdf0e10cSrcweir (b.r*a.r-a.r*b.i)/(b.r*b.r+b.i*b.i) );
210cdf0e10cSrcweir }
211cdf0e10cSrcweir
212cdf0e10cSrcweir // -----------------------------------------------------------------------
213cdf0e10cSrcweir
214cdf0e10cSrcweir Fix ImpMultBig2( const Fix& a, const Fix& b );
215cdf0e10cSrcweir FixCpx ImpMultBig2( const FixCpx& ra, const FixCpx& rb );
216cdf0e10cSrcweir
217cdf0e10cSrcweir void ImpCartToPolar( const short x, const short y, Fix& rRad, sal_uInt16& rPhi );
218cdf0e10cSrcweir void ImpPolarToCart( const Fix& rR, const sal_uInt16 Phi, short& rX, short& rY );
219cdf0e10cSrcweir
220cdf0e10cSrcweir sal_uInt16 ImpSqrt( sal_uLong nRadi );
221cdf0e10cSrcweir sal_uInt16 ImpATan2( const short x, const short y );
222cdf0e10cSrcweir FixCpx ImpExPI( sal_uInt16 nPhi );
223cdf0e10cSrcweir
224cdf0e10cSrcweir #endif // _MCVMATH_HXX
225