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 // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_basegfx.hxx" 30*cdf0e10cSrcweir #include <basegfx/point/b2dhompoint.hxx> 31*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx> 32*cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir namespace basegfx 35*cdf0e10cSrcweir { 36*cdf0e10cSrcweir bool B2DHomPoint::implIsHomogenized() const 37*cdf0e10cSrcweir { 38*cdf0e10cSrcweir const double fOne(1.0); 39*cdf0e10cSrcweir return ::basegfx::fTools::equal(fOne, mfW); 40*cdf0e10cSrcweir } 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir void B2DHomPoint::implHomogenize() 43*cdf0e10cSrcweir { 44*cdf0e10cSrcweir const double fFactor(1.0 / mfW); 45*cdf0e10cSrcweir maTuple.setX(maTuple.getX() * fFactor); 46*cdf0e10cSrcweir maTuple.setY(maTuple.getY() * fFactor); 47*cdf0e10cSrcweir mfW = 1.0; 48*cdf0e10cSrcweir } 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir void B2DHomPoint::implTestAndHomogenize() const 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir if(!implIsHomogenized()) 53*cdf0e10cSrcweir ((B2DHomPoint*)this)->implHomogenize(); 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir B2DPoint B2DHomPoint::getB2DPoint() const 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir implTestAndHomogenize(); 59*cdf0e10cSrcweir return B2DPoint(maTuple.getX(), maTuple.getY()); 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir double B2DHomPoint::getX() const 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir implTestAndHomogenize(); 65*cdf0e10cSrcweir return maTuple.getX(); 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir double B2DHomPoint::getY() const 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir implTestAndHomogenize(); 71*cdf0e10cSrcweir return maTuple.getY(); 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir void B2DHomPoint::setX(double fX) 75*cdf0e10cSrcweir { 76*cdf0e10cSrcweir maTuple.setX(implIsHomogenized() ? fX : fX * mfW ); 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir void B2DHomPoint::setY(double fY) 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir maTuple.setY(implIsHomogenized() ? fY : fY * mfW ); 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir B2DHomPoint& B2DHomPoint::operator+=( const B2DHomPoint& rPnt ) 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir maTuple.setX(getX() * rPnt.mfW + rPnt.getX() * mfW); 87*cdf0e10cSrcweir maTuple.setY(getY() * rPnt.mfW + rPnt.getY() * mfW); 88*cdf0e10cSrcweir mfW = mfW * rPnt.mfW; 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir return *this; 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir B2DHomPoint& B2DHomPoint::operator-=( const B2DHomPoint& rPnt ) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir maTuple.setX(getX() * rPnt.mfW - rPnt.getX() * mfW); 96*cdf0e10cSrcweir maTuple.setY(getY() * rPnt.mfW - rPnt.getY() * mfW); 97*cdf0e10cSrcweir mfW = mfW * rPnt.mfW; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir return *this; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir B2DHomPoint& B2DHomPoint::operator*=(double t) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir if(!::basegfx::fTools::equalZero(t)) 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir mfW /= t; 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir return *this; 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir B2DHomPoint& B2DHomPoint::operator*=( const B2DHomMatrix& rMat ) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir const double fTempX( rMat.get(0,0)*maTuple.getX() + 115*cdf0e10cSrcweir rMat.get(0,1)*maTuple.getY() + 116*cdf0e10cSrcweir rMat.get(0,2)*mfW ); 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir const double fTempY( rMat.get(1,0)*maTuple.getX() + 119*cdf0e10cSrcweir rMat.get(1,1)*maTuple.getY() + 120*cdf0e10cSrcweir rMat.get(1,2)*mfW ); 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir const double fTempZ( rMat.get(2,0)*maTuple.getX() + 123*cdf0e10cSrcweir rMat.get(2,1)*maTuple.getY() + 124*cdf0e10cSrcweir rMat.get(2,2)*mfW ); 125*cdf0e10cSrcweir maTuple.setX( fTempX ); 126*cdf0e10cSrcweir maTuple.setY( fTempY ); 127*cdf0e10cSrcweir mfW = fTempZ; 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir return *this; 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir B2DHomPoint& B2DHomPoint::operator/=(double t) 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir mfW *= t; 135*cdf0e10cSrcweir return *this; 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir B2DHomPoint& B2DHomPoint::operator-(void) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir mfW = -mfW; 141*cdf0e10cSrcweir return *this; 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir bool B2DHomPoint::operator==( const B2DHomPoint& rPnt ) const 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir implTestAndHomogenize(); 147*cdf0e10cSrcweir return (maTuple == rPnt.maTuple); 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir bool B2DHomPoint::operator!=( const B2DHomPoint& rPnt ) const 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir implTestAndHomogenize(); 153*cdf0e10cSrcweir return (maTuple != rPnt.maTuple); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir B2DHomPoint& B2DHomPoint::operator=( const B2DHomPoint& rPnt ) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir maTuple = rPnt.maTuple; 159*cdf0e10cSrcweir mfW = rPnt.mfW; 160*cdf0e10cSrcweir return *this; 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir B2DHomPoint minimum(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir B2DHomPoint aMin( 166*cdf0e10cSrcweir (rVecB.getX() < rVecA.getX()) ? rVecB.getX() : rVecA.getX(), 167*cdf0e10cSrcweir (rVecB.getY() < rVecA.getY()) ? rVecB.getY() : rVecA.getY()); 168*cdf0e10cSrcweir return aMin; 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir B2DHomPoint maximum(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir B2DHomPoint aMax( 174*cdf0e10cSrcweir (rVecB.getX() > rVecA.getX()) ? rVecB.getX() : rVecA.getX(), 175*cdf0e10cSrcweir (rVecB.getY() > rVecA.getY()) ? rVecB.getY() : rVecA.getY()); 176*cdf0e10cSrcweir return aMax; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir B2DHomPoint absolute(const B2DHomPoint& rVec) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir B2DHomPoint aAbs( 181*cdf0e10cSrcweir (0.0 > rVec.getX()) ? -rVec.getX() : rVec.getX(), 182*cdf0e10cSrcweir (0.0 > rVec.getY()) ? -rVec.getY() : rVec.getY()); 183*cdf0e10cSrcweir return aAbs; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir B2DHomPoint interpolate(B2DHomPoint& rOld1, B2DHomPoint& rOld2, double t) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir B2DHomPoint aInt( 189*cdf0e10cSrcweir ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(), 190*cdf0e10cSrcweir ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY()); 191*cdf0e10cSrcweir return aInt; 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir B2DHomPoint average(B2DHomPoint& rOld1, B2DHomPoint& rOld2) 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir B2DHomPoint aAvg( 197*cdf0e10cSrcweir (rOld1.getX() + rOld2.getX()) * 0.5, 198*cdf0e10cSrcweir (rOld1.getY() + rOld2.getY()) * 0.5); 199*cdf0e10cSrcweir return aAvg; 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir B2DHomPoint average(B2DHomPoint& rOld1, B2DHomPoint& rOld2, B2DHomPoint& rOld3) 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir B2DHomPoint aAvg( 205*cdf0e10cSrcweir (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0), 206*cdf0e10cSrcweir (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0)); 207*cdf0e10cSrcweir return aAvg; 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir B2DHomPoint operator+(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB) 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir B2DHomPoint aSum(rVecA); 213*cdf0e10cSrcweir aSum += rVecB; 214*cdf0e10cSrcweir return aSum; 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir B2DHomPoint operator-(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB) 218*cdf0e10cSrcweir { 219*cdf0e10cSrcweir B2DHomPoint aSub(rVecA); 220*cdf0e10cSrcweir aSub -= rVecB; 221*cdf0e10cSrcweir return aSub; 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir B2DHomPoint operator*(const B2DHomPoint& rVec, double t) 225*cdf0e10cSrcweir { 226*cdf0e10cSrcweir B2DHomPoint aNew(rVec); 227*cdf0e10cSrcweir aNew *= t; 228*cdf0e10cSrcweir return aNew; 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir B2DHomPoint operator*(double t, const B2DHomPoint& rVec) 232*cdf0e10cSrcweir { 233*cdf0e10cSrcweir B2DHomPoint aNew(rVec); 234*cdf0e10cSrcweir aNew *= t; 235*cdf0e10cSrcweir return aNew; 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir B2DHomPoint operator*( const B2DHomMatrix& rMat, const B2DHomPoint& rPoint ) 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir B2DHomPoint aNew(rPoint); 241*cdf0e10cSrcweir return aNew*=rMat; 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir B2DHomPoint operator/(const B2DHomPoint& rVec, double t) 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir B2DHomPoint aNew(rVec); 247*cdf0e10cSrcweir aNew /= t; 248*cdf0e10cSrcweir return aNew; 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir B2DHomPoint operator/(double t, const B2DHomPoint& rVec) 252*cdf0e10cSrcweir { 253*cdf0e10cSrcweir B2DHomPoint aNew(rVec); 254*cdf0e10cSrcweir aNew /= t; 255*cdf0e10cSrcweir return aNew; 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir } // end of namespace basegfx 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir // eof 260