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