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