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