xref: /aoo41x/main/svtools/source/dialogs/mcvmath.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _MCVMATH_HXX
29 #define _MCVMATH_HXX
30 
31 #include <tools/solar.h>
32 
33 class FixCpx;
34 class ColWheel;
35 
36 // No of fractal bits
37 // allowed range 0..14, must be even
38 #define FIX_POST 14
39 
40 // scale for ...Big() -Functions
41 #if (FIX_POST>=4)
42 #define FIX_P2  4
43 #define FIX_P3  (FIX_POST-FIX_P2)
44 #else
45 #define FIX_P2  0
46 #define FIX_P3  FIX_POST
47 #endif
48 
49 #if (FIX_POST>=1)
50 #define FIX_ADD (1<<(FIX_POST-1))
51 #else
52 #define FIX_ADD 0
53 #endif
54 
55 #if (FIX_P2>=1)
56 #define FIX_A2 (1<<(FIX_P2-1))
57 #else
58 #define FIX_A2 0
59 #endif
60 
61 #if (FIX_P3>=1)
62 #define FIX_A3 (1<<(FIX_P3-1))
63 #else
64 #define FIX_A3 0
65 #endif
66 
67 // -------
68 // - Fix -
69 // -------
70 
71 class Fix
72 {
73 private:
74 	friend	class FixCpx;
75 	friend	class ColWheel;
76 
77 //	friend	Fix ImpMultBig2( const Fix& a, const Fix& b );
78 
79 public:
80 	long            x;
81 
82 public:
83 					Fix() { x=0; }
84 					Fix( int i ) { x=(long(i)<<FIX_POST); }
85 					Fix( short l ) { x=(long(l)<<FIX_POST); }
86 					Fix( sal_uInt16 l ) { x=(long(l)<<FIX_POST); }
87 					Fix( long l ) { x=(l<<FIX_POST); }
88 					Fix( long Z, long N ) { x=(Z<<FIX_POST)/N; }
89 
90 	void            SetInternVal( long nVal ) { x=nVal; }
91 	long            GetInternVal() const { return x; }
92 
93 	void            operator+= ( const Fix& a ) { x+=a.x; }
94 	void            operator-= ( const Fix& a ) { x-=a.x; }
95 	void            operator*= ( const Fix& a ) { x=(x*a.x+FIX_ADD)>>FIX_POST; }
96 	void            operator/= ( const Fix& a ) { x=(x<<FIX_POST)/a.x; }
97 	friend Fix      operator-  ( const Fix& a );
98 
99 	void            MultBig( const Fix& a )
100 						{ x=((((a.x+FIX_A2)>>FIX_P2)*x+FIX_A3)>>FIX_P3); }
101 	void            DivBig( const Fix& a )
102 						{ x=((x<<FIX_P3)/a.x)<<FIX_P2; }
103 
104 	friend sal_Bool     operator> ( const Fix& a, const Fix& b ) { return a.x > b.x; }
105 	friend sal_Bool     operator< ( const Fix& a, const Fix& b ) { return a.x < b.x; }
106 
107 	operator        long() const    { return (x+FIX_ADD) >> FIX_POST; }
108 	operator        double() const  { return double(x)/(1<<FIX_POST); }
109 
110 	friend Fix      operator+ ( const Fix& a, const Fix& b );
111 	friend Fix      operator- ( const Fix& a, const Fix& b );
112 	friend Fix      operator* ( const Fix& a, const Fix& b );
113 	friend Fix      operator/ ( const Fix& a, const Fix& b );
114 
115 	friend FixCpx   operator-( const FixCpx& a );
116 };
117 
118 // ----------
119 // - FixCpx -
120 // ----------
121 
122 class FixCpx
123 {
124 //	friend	FixCpx ImpMultBig2( const FixCpx& ra, const FixCpx& rb );
125 
126 public:
127 	Fix             r;
128 	Fix             i;
129 
130 public:
131 					FixCpx()               : r(), i() {}
132 					FixCpx( Fix a )        : r( a ), i() {}
133 					FixCpx( Fix a, Fix b ) : r( a ), i( b ) {}
134 
135 	Fix&            GetReal() { return r; }
136 	Fix&            GetImag() { return i; }
137 
138 	void            operator*= ( const FixCpx& ra );
139 	void            MultBig( const FixCpx& ra, const FixCpx& rb );
140 
141 	friend FixCpx   operator+ ( const FixCpx& a, const FixCpx& b );
142 	friend FixCpx   operator- ( const FixCpx& a, const FixCpx& b );
143 	friend FixCpx   operator* ( const FixCpx& a, const FixCpx& b );
144 	friend FixCpx   operator/ ( const FixCpx& a, const FixCpx& b );
145 	friend FixCpx   operator- ( const FixCpx& a );
146 };
147 
148 inline Fix operator- ( const Fix& a )
149 {
150 	Fix f;
151 	f.x = -a.x;
152 	return f;
153 }
154 
155 inline Fix operator+ ( const Fix& a, const Fix& b )
156 {
157 	long l = a.x+b.x;
158 	return *((Fix*)&l);
159 }
160 
161 inline Fix operator- ( const Fix& a, const Fix& b )
162 {
163 	long l = a.x-b.x;
164 	return *((Fix*)&l);
165 }
166 
167 inline Fix operator* ( const Fix& a, const Fix& b )
168 {
169 	long l=(a.x*b.x+FIX_ADD)>>FIX_POST;
170 	return *((Fix*)&l);
171 }
172 
173 inline Fix operator/ ( const Fix& a, const Fix& b )
174 {
175 	long l=(a.x<<FIX_POST)/b.x;
176 	return *((Fix*)&l);
177 }
178 
179 inline FixCpx operator- ( const FixCpx& a )
180 {
181 	FixCpx fc;
182 
183 	fc.r.x = -a.r.x;
184 	fc.i.x = -a.i.x;
185 	return fc;
186 }
187 
188 inline FixCpx operator+ ( const FixCpx& a, const FixCpx& b )
189 {
190 	return FixCpx( a.r+b.r, a.i+b.i );
191 }
192 
193 inline FixCpx operator- ( const FixCpx& a, const FixCpx& b )
194 {
195 	return FixCpx( a.r-b.r, a.i-b.i );
196 }
197 
198 inline void FixCpx::operator*= ( const FixCpx& ra )
199 {
200 	Fix rr = ra.r*r-ra.i*i;
201 	i = ra.r*i+ra.i*r;
202 	r = rr;
203 }
204 
205 inline FixCpx operator* ( const FixCpx& a, const FixCpx& b )
206 {
207 	return FixCpx( a.r*b.r-a.i*b.i, a.r*b.i+a.i*b.r );
208 }
209 
210 inline FixCpx operator/ ( const FixCpx& a, const FixCpx& b )
211 {
212 	return FixCpx( (a.r*b.r+a.i*b.i)/(b.r*b.r+b.i*b.i),
213 				   (b.r*a.r-a.r*b.i)/(b.r*b.r+b.i*b.i) );
214 }
215 
216 // -----------------------------------------------------------------------
217 
218 Fix ImpMultBig2( const Fix& a, const Fix& b );
219 FixCpx ImpMultBig2( const FixCpx& ra, const FixCpx& rb );
220 
221 void ImpCartToPolar( const short x, const short y, Fix& rRad, sal_uInt16& rPhi );
222 void ImpPolarToCart( const Fix& rR, const sal_uInt16 Phi, short& rX, short& rY );
223 
224 sal_uInt16 ImpSqrt( sal_uLong nRadi );
225 sal_uInt16 ImpATan2( const short x, const short y );
226 FixCpx ImpExPI( sal_uInt16 nPhi );
227 
228 #endif // _MCVMATH_HXX
229