merge the formfield patch from ooo-build
[ooovba.git] / basegfx / source / point / b2dhompoint.cxx
blob1f172f9b8a7dcee93f817548a44bc0a1b1df681b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: b2dhompoint.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
33 #include <basegfx/point/b2dhompoint.hxx>
34 #include <basegfx/matrix/b2dhommatrix.hxx>
35 #include <basegfx/numeric/ftools.hxx>
37 namespace basegfx
39 bool B2DHomPoint::implIsHomogenized() const
41 const double fOne(1.0);
42 return ::basegfx::fTools::equal(fOne, mfW);
45 void B2DHomPoint::implHomogenize()
47 const double fFactor(1.0 / mfW);
48 maTuple.setX(maTuple.getX() * fFactor);
49 maTuple.setY(maTuple.getY() * fFactor);
50 mfW = 1.0;
53 void B2DHomPoint::implTestAndHomogenize() const
55 if(!implIsHomogenized())
56 ((B2DHomPoint*)this)->implHomogenize();
59 B2DPoint B2DHomPoint::getB2DPoint() const
61 implTestAndHomogenize();
62 return B2DPoint(maTuple.getX(), maTuple.getY());
65 double B2DHomPoint::getX() const
67 implTestAndHomogenize();
68 return maTuple.getX();
71 double B2DHomPoint::getY() const
73 implTestAndHomogenize();
74 return maTuple.getY();
77 void B2DHomPoint::setX(double fX)
79 maTuple.setX(implIsHomogenized() ? fX : fX * mfW );
82 void B2DHomPoint::setY(double fY)
84 maTuple.setY(implIsHomogenized() ? fY : fY * mfW );
87 B2DHomPoint& B2DHomPoint::operator+=( const B2DHomPoint& rPnt )
89 maTuple.setX(getX() * rPnt.mfW + rPnt.getX() * mfW);
90 maTuple.setY(getY() * rPnt.mfW + rPnt.getY() * mfW);
91 mfW = mfW * rPnt.mfW;
93 return *this;
96 B2DHomPoint& B2DHomPoint::operator-=( const B2DHomPoint& rPnt )
98 maTuple.setX(getX() * rPnt.mfW - rPnt.getX() * mfW);
99 maTuple.setY(getY() * rPnt.mfW - rPnt.getY() * mfW);
100 mfW = mfW * rPnt.mfW;
102 return *this;
105 B2DHomPoint& B2DHomPoint::operator*=(double t)
107 if(!::basegfx::fTools::equalZero(t))
109 mfW /= t;
112 return *this;
115 B2DHomPoint& B2DHomPoint::operator*=( const B2DHomMatrix& rMat )
117 const double fTempX( rMat.get(0,0)*maTuple.getX() +
118 rMat.get(0,1)*maTuple.getY() +
119 rMat.get(0,2)*mfW );
121 const double fTempY( rMat.get(1,0)*maTuple.getX() +
122 rMat.get(1,1)*maTuple.getY() +
123 rMat.get(1,2)*mfW );
125 const double fTempZ( rMat.get(2,0)*maTuple.getX() +
126 rMat.get(2,1)*maTuple.getY() +
127 rMat.get(2,2)*mfW );
128 maTuple.setX( fTempX );
129 maTuple.setY( fTempY );
130 mfW = fTempZ;
132 return *this;
135 B2DHomPoint& B2DHomPoint::operator/=(double t)
137 mfW *= t;
138 return *this;
141 B2DHomPoint& B2DHomPoint::operator-(void)
143 mfW = -mfW;
144 return *this;
147 bool B2DHomPoint::operator==( const B2DHomPoint& rPnt ) const
149 implTestAndHomogenize();
150 return (maTuple == rPnt.maTuple);
153 bool B2DHomPoint::operator!=( const B2DHomPoint& rPnt ) const
155 implTestAndHomogenize();
156 return (maTuple != rPnt.maTuple);
159 B2DHomPoint& B2DHomPoint::operator=( const B2DHomPoint& rPnt )
161 maTuple = rPnt.maTuple;
162 mfW = rPnt.mfW;
163 return *this;
166 B2DHomPoint minimum(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB)
168 B2DHomPoint aMin(
169 (rVecB.getX() < rVecA.getX()) ? rVecB.getX() : rVecA.getX(),
170 (rVecB.getY() < rVecA.getY()) ? rVecB.getY() : rVecA.getY());
171 return aMin;
174 B2DHomPoint maximum(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB)
176 B2DHomPoint aMax(
177 (rVecB.getX() > rVecA.getX()) ? rVecB.getX() : rVecA.getX(),
178 (rVecB.getY() > rVecA.getY()) ? rVecB.getY() : rVecA.getY());
179 return aMax;
181 B2DHomPoint absolute(const B2DHomPoint& rVec)
183 B2DHomPoint aAbs(
184 (0.0 > rVec.getX()) ? -rVec.getX() : rVec.getX(),
185 (0.0 > rVec.getY()) ? -rVec.getY() : rVec.getY());
186 return aAbs;
189 B2DHomPoint interpolate(B2DHomPoint& rOld1, B2DHomPoint& rOld2, double t)
191 B2DHomPoint aInt(
192 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
193 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
194 return aInt;
197 B2DHomPoint average(B2DHomPoint& rOld1, B2DHomPoint& rOld2)
199 B2DHomPoint aAvg(
200 (rOld1.getX() + rOld2.getX()) * 0.5,
201 (rOld1.getY() + rOld2.getY()) * 0.5);
202 return aAvg;
205 B2DHomPoint average(B2DHomPoint& rOld1, B2DHomPoint& rOld2, B2DHomPoint& rOld3)
207 B2DHomPoint aAvg(
208 (rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
209 (rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
210 return aAvg;
213 B2DHomPoint operator+(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB)
215 B2DHomPoint aSum(rVecA);
216 aSum += rVecB;
217 return aSum;
220 B2DHomPoint operator-(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB)
222 B2DHomPoint aSub(rVecA);
223 aSub -= rVecB;
224 return aSub;
227 B2DHomPoint operator*(const B2DHomPoint& rVec, double t)
229 B2DHomPoint aNew(rVec);
230 aNew *= t;
231 return aNew;
234 B2DHomPoint operator*(double t, const B2DHomPoint& rVec)
236 B2DHomPoint aNew(rVec);
237 aNew *= t;
238 return aNew;
241 B2DHomPoint operator*( const B2DHomMatrix& rMat, const B2DHomPoint& rPoint )
243 B2DHomPoint aNew(rPoint);
244 return aNew*=rMat;
247 B2DHomPoint operator/(const B2DHomPoint& rVec, double t)
249 B2DHomPoint aNew(rVec);
250 aNew /= t;
251 return aNew;
254 B2DHomPoint operator/(double t, const B2DHomPoint& rVec)
256 B2DHomPoint aNew(rVec);
257 aNew /= t;
258 return aNew;
260 } // end of namespace basegfx
262 // eof