1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_basegfx.hxx"
26 #include <basegfx/point/b2dhompoint.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <basegfx/numeric/ftools.hxx>
29 
30 namespace basegfx
31 {
32 	bool B2DHomPoint::implIsHomogenized() const
33 	{
34 		const double fOne(1.0);
35 		return ::basegfx::fTools::equal(fOne, mfW);
36 	}
37 
38 	void B2DHomPoint::implHomogenize()
39 	{
40 		const double fFactor(1.0 / mfW);
41 		maTuple.setX(maTuple.getX() * fFactor);
42 		maTuple.setY(maTuple.getY() * fFactor);
43 		mfW = 1.0;
44 	}
45 
46 	void B2DHomPoint::implTestAndHomogenize() const
47 	{
48 		if(!implIsHomogenized())
49 			((B2DHomPoint*)this)->implHomogenize();
50 	}
51 
52 	B2DPoint B2DHomPoint::getB2DPoint() const
53 	{
54 		implTestAndHomogenize();
55 		return B2DPoint(maTuple.getX(), maTuple.getY());
56 	}
57 
58 	double B2DHomPoint::getX() const
59 	{
60 		implTestAndHomogenize();
61 		return maTuple.getX();
62 	}
63 
64 	double B2DHomPoint::getY() const
65 	{
66 		implTestAndHomogenize();
67 		return maTuple.getY();
68 	}
69 
70 	void B2DHomPoint::setX(double fX)
71 	{
72 		maTuple.setX(implIsHomogenized() ? fX : fX * mfW );
73 	}
74 
75 	void B2DHomPoint::setY(double fY)
76 	{
77 		maTuple.setY(implIsHomogenized() ? fY : fY * mfW );
78 	}
79 
80 	B2DHomPoint& B2DHomPoint::operator+=( const B2DHomPoint& rPnt )
81 	{
82 		maTuple.setX(getX() * rPnt.mfW + rPnt.getX() * mfW);
83 		maTuple.setY(getY() * rPnt.mfW + rPnt.getY() * mfW);
84 		mfW = mfW * rPnt.mfW;
85 
86 		return *this;
87 	}
88 
89 	B2DHomPoint& B2DHomPoint::operator-=( const B2DHomPoint& rPnt )
90 	{
91 		maTuple.setX(getX() * rPnt.mfW - rPnt.getX() * mfW);
92 		maTuple.setY(getY() * rPnt.mfW - rPnt.getY() * mfW);
93 		mfW = mfW * rPnt.mfW;
94 
95 		return *this;
96 	}
97 
98 	B2DHomPoint& B2DHomPoint::operator*=(double t)
99 	{
100 		if(!::basegfx::fTools::equalZero(t))
101 		{
102 			mfW /= t;
103 		}
104 
105 		return *this;
106 	}
107 
108 	B2DHomPoint& B2DHomPoint::operator*=( const B2DHomMatrix& rMat )
109 	{
110 		const double fTempX( rMat.get(0,0)*maTuple.getX() +
111 							rMat.get(0,1)*maTuple.getY() +
112 							rMat.get(0,2)*mfW );
113 
114 		const double fTempY( rMat.get(1,0)*maTuple.getX() +
115 							rMat.get(1,1)*maTuple.getY() +
116 							rMat.get(1,2)*mfW );
117 
118 		const double fTempZ( rMat.get(2,0)*maTuple.getX() +
119 							rMat.get(2,1)*maTuple.getY() +
120 							rMat.get(2,2)*mfW );
121 		maTuple.setX( fTempX );
122 		maTuple.setY( fTempY );
123 		mfW = fTempZ;
124 
125 		return *this;
126 	}
127 
128 	B2DHomPoint& B2DHomPoint::operator/=(double t)
129 	{
130 		mfW *= t;
131 		return *this;
132 	}
133 
134 	B2DHomPoint& B2DHomPoint::operator-(void)
135 	{
136 		mfW = -mfW;
137 		return *this;
138 	}
139 
140 	bool B2DHomPoint::operator==( const B2DHomPoint& rPnt ) const
141 	{
142 		implTestAndHomogenize();
143 		return (maTuple == rPnt.maTuple);
144 	}
145 
146 	bool B2DHomPoint::operator!=( const B2DHomPoint& rPnt ) const
147 	{
148 		implTestAndHomogenize();
149 		return (maTuple != rPnt.maTuple);
150 	}
151 
152 	B2DHomPoint& B2DHomPoint::operator=( const B2DHomPoint& rPnt )
153 	{
154 		maTuple = rPnt.maTuple;
155 		mfW = rPnt.mfW;
156 		return *this;
157 	}
158 
159 	B2DHomPoint minimum(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB)
160 	{
161 		B2DHomPoint aMin(
162 			(rVecB.getX() < rVecA.getX()) ? rVecB.getX() : rVecA.getX(),
163 			(rVecB.getY() < rVecA.getY()) ? rVecB.getY() : rVecA.getY());
164 		return aMin;
165 	}
166 
167 	B2DHomPoint maximum(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB)
168 	{
169 		B2DHomPoint aMax(
170 			(rVecB.getX() > rVecA.getX()) ? rVecB.getX() : rVecA.getX(),
171 			(rVecB.getY() > rVecA.getY()) ? rVecB.getY() : rVecA.getY());
172 		return aMax;
173 	}
174 	B2DHomPoint absolute(const B2DHomPoint& rVec)
175 	{
176 		B2DHomPoint aAbs(
177 			(0.0 > rVec.getX()) ? -rVec.getX() : rVec.getX(),
178 			(0.0 > rVec.getY()) ? -rVec.getY() : rVec.getY());
179 		return aAbs;
180 	}
181 
182 	B2DHomPoint interpolate(B2DHomPoint& rOld1, B2DHomPoint& rOld2, double t)
183 	{
184 		B2DHomPoint aInt(
185 			((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
186 			((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
187 		return aInt;
188 	}
189 
190 	B2DHomPoint average(B2DHomPoint& rOld1, B2DHomPoint& rOld2)
191 	{
192 		B2DHomPoint aAvg(
193 			(rOld1.getX() + rOld2.getX()) * 0.5,
194 			(rOld1.getY() + rOld2.getY()) * 0.5);
195 		return aAvg;
196 	}
197 
198 	B2DHomPoint average(B2DHomPoint& rOld1, B2DHomPoint& rOld2, B2DHomPoint& rOld3)
199 	{
200 		B2DHomPoint aAvg(
201 			(rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0),
202 			(rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0));
203 		return aAvg;
204 	}
205 
206 	B2DHomPoint operator+(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB)
207 	{
208 		B2DHomPoint aSum(rVecA);
209 		aSum += rVecB;
210 		return aSum;
211 	}
212 
213 	B2DHomPoint operator-(const B2DHomPoint& rVecA, const B2DHomPoint& rVecB)
214 	{
215 		B2DHomPoint aSub(rVecA);
216 		aSub -= rVecB;
217 		return aSub;
218 	}
219 
220 	B2DHomPoint operator*(const B2DHomPoint& rVec, double t)
221 	{
222 		B2DHomPoint aNew(rVec);
223 		aNew *= t;
224 		return aNew;
225 	}
226 
227 	B2DHomPoint operator*(double t, const B2DHomPoint& rVec)
228 	{
229 		B2DHomPoint aNew(rVec);
230 		aNew *= t;
231 		return aNew;
232 	}
233 
234 	B2DHomPoint operator*( const B2DHomMatrix& rMat, const B2DHomPoint& rPoint )
235 	{
236 		B2DHomPoint aNew(rPoint);
237 		return aNew*=rMat;
238 	}
239 
240 	B2DHomPoint operator/(const B2DHomPoint& rVec, double t)
241 	{
242 		B2DHomPoint aNew(rVec);
243 		aNew /= t;
244 		return aNew;
245 	}
246 
247 	B2DHomPoint operator/(double t, const B2DHomPoint& rVec)
248 	{
249 		B2DHomPoint aNew(rVec);
250 		aNew /= t;
251 		return aNew;
252 	}
253 } // end of namespace basegfx
254 
255 // eof
256