xref: /trunk/main/basegfx/inc/basegfx/tuple/b2i64tuple.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
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 #ifndef _BGFX_TUPLE_B2I64TUPLE_HXX
25 #define _BGFX_TUPLE_B2I64TUPLE_HXX
26 
27 #include <sal/types.h>
28 #include <basegfx/tuple/b2dtuple.hxx>
29 #include <basegfx/basegfxdllapi.h>
30 
31 
32 namespace basegfx
33 {
34     /** Base class for all Points/Vectors with two sal_Int64 values
35 
36         This class provides all methods common to Point
37         avd Vector classes which are derived from here.
38 
39         @derive Use this class to implement Points or Vectors
40         which are based on two sal_Int64 values
41     */
42     class BASEGFX_DLLPUBLIC B2I64Tuple
43     {
44     protected:
45         sal_Int64                                       mnX;
46         sal_Int64                                       mnY;
47 
48     public:
49         /** Create a 2D Tuple
50 
51             The tuple is initialized to (0, 0)
52         */
B2I64Tuple()53         B2I64Tuple()
54         :   mnX(0),
55             mnY(0)
56         {}
57 
58         /** Create a 2D Tuple
59 
60             @param fX
61             This parameter is used to initialize the X-coordinate
62             of the 2D Tuple.
63 
64             @param fY
65             This parameter is used to initialize the Y-coordinate
66             of the 2D Tuple.
67         */
B2I64Tuple(sal_Int64 fX,sal_Int64 fY)68         B2I64Tuple(sal_Int64 fX, sal_Int64 fY)
69         :   mnX( fX ),
70             mnY( fY )
71         {}
72 
73         /** Create a copy of a 2D Tuple
74 
75             @param rTup
76             The 2D Tuple which will be copied.
77         */
B2I64Tuple(const B2I64Tuple & rTup)78         B2I64Tuple(const B2I64Tuple& rTup)
79         :   mnX( rTup.mnX ),
80             mnY( rTup.mnY )
81         {}
82 
~B2I64Tuple()83         ~B2I64Tuple()
84         {}
85 
86         /// Get X-Coordinate of 2D Tuple
getX() const87         sal_Int64 getX() const
88         {
89             return mnX;
90         }
91 
92         /// Get Y-Coordinate of 2D Tuple
getY() const93         sal_Int64 getY() const
94         {
95             return mnY;
96         }
97 
98         /// Set X-Coordinate of 2D Tuple
setX(sal_Int64 fX)99         void setX(sal_Int64 fX)
100         {
101             mnX = fX;
102         }
103 
104         /// Set Y-Coordinate of 2D Tuple
setY(sal_Int64 fY)105         void setY(sal_Int64 fY)
106         {
107             mnY = fY;
108         }
109 
110         /// Array-access to 2D Tuple
operator [](int nPos) const111         const sal_Int64& operator[] (int nPos) const
112         {
113             // Here, normally one if(...) should be used. In the assumption that
114             // both sal_Int64 members can be accessed as an array a shortcut is used here.
115             // if(0 == nPos) return mnX; return mnY;
116             return *((&mnX) + nPos);
117         }
118 
119         /// Array-access to 2D Tuple
operator [](int nPos)120         sal_Int64& operator[] (int nPos)
121         {
122             // Here, normally one if(...) should be used. In the assumption that
123             // both sal_Int64 members can be accessed as an array a shortcut is used here.
124             // if(0 == nPos) return mnX; return mnY;
125             return *((&mnX) + nPos);
126         }
127 
128         // operators
129         //////////////////////////////////////////////////////////////////////
130 
operator +=(const B2I64Tuple & rTup)131         B2I64Tuple& operator+=( const B2I64Tuple& rTup )
132         {
133             mnX += rTup.mnX;
134             mnY += rTup.mnY;
135             return *this;
136         }
137 
operator -=(const B2I64Tuple & rTup)138         B2I64Tuple& operator-=( const B2I64Tuple& rTup )
139         {
140             mnX -= rTup.mnX;
141             mnY -= rTup.mnY;
142             return *this;
143         }
144 
operator /=(const B2I64Tuple & rTup)145         B2I64Tuple& operator/=( const B2I64Tuple& rTup )
146         {
147             mnX /= rTup.mnX;
148             mnY /= rTup.mnY;
149             return *this;
150         }
151 
operator *=(const B2I64Tuple & rTup)152         B2I64Tuple& operator*=( const B2I64Tuple& rTup )
153         {
154             mnX *= rTup.mnX;
155             mnY *= rTup.mnY;
156             return *this;
157         }
158 
operator *=(sal_Int64 t)159         B2I64Tuple& operator*=(sal_Int64 t)
160         {
161             mnX *= t;
162             mnY *= t;
163             return *this;
164         }
165 
operator /=(sal_Int64 t)166         B2I64Tuple& operator/=(sal_Int64 t)
167         {
168             mnX /= t;
169             mnY /= t;
170             return *this;
171         }
172 
operator -(void) const173         B2I64Tuple operator-(void) const
174         {
175             return B2I64Tuple(-mnX, -mnY);
176         }
177 
equalZero() const178         bool equalZero() const { return mnX == 0 && mnY == 0; }
179 
operator ==(const B2I64Tuple & rTup) const180         bool operator==( const B2I64Tuple& rTup ) const
181         {
182             return this == &rTup || (rTup.mnX == mnX && rTup.mnY == mnY);
183         }
184 
operator !=(const B2I64Tuple & rTup) const185         bool operator!=( const B2I64Tuple& rTup ) const
186         {
187             return !(*this == rTup);
188         }
189 
operator =(const B2I64Tuple & rTup)190         B2I64Tuple& operator=( const B2I64Tuple& rTup )
191         {
192             mnX = rTup.mnX;
193             mnY = rTup.mnY;
194             return *this;
195         }
196 
197         static const B2I64Tuple& getEmptyTuple();
198     };
199 
200     // external operators
201     //////////////////////////////////////////////////////////////////////////
202 
minimum(const B2I64Tuple & rTupA,const B2I64Tuple & rTupB)203     inline B2I64Tuple minimum(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
204     {
205         return B2I64Tuple(
206             std::min(rTupB.getX(), rTupA.getX()),
207             std::min(rTupB.getY(), rTupA.getY()));
208     }
209 
maximum(const B2I64Tuple & rTupA,const B2I64Tuple & rTupB)210     inline B2I64Tuple maximum(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
211     {
212         return B2I64Tuple(
213             std::max(rTupB.getX(), rTupA.getX()),
214             std::max(rTupB.getY(), rTupA.getY()));
215     }
216 
absolute(const B2I64Tuple & rTup)217     inline B2I64Tuple absolute(const B2I64Tuple& rTup)
218     {
219         B2I64Tuple aAbs(
220             (0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
221             (0 > rTup.getY()) ? -rTup.getY() : rTup.getY());
222         return aAbs;
223     }
224 
interpolate(const B2I64Tuple & rOld1,const B2I64Tuple & rOld2,double t)225     inline B2I64Tuple interpolate(const B2I64Tuple& rOld1, const B2I64Tuple& rOld2, double t)
226     {
227         if(rOld1 == rOld2)
228         {
229             return rOld1;
230         }
231         else if(0.0 >= t)
232         {
233             return rOld1;
234         }
235         else if(1.0 <= t)
236         {
237             return rOld2;
238         }
239         else
240         {
241             return B2I64Tuple(
242                 basegfx::fround64(((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX()),
243                 basegfx::fround64(((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY()));
244         }
245     }
246 
average(const B2I64Tuple & rOld1,const B2I64Tuple & rOld2)247     inline B2I64Tuple average(const B2I64Tuple& rOld1, const B2I64Tuple& rOld2)
248     {
249         return B2I64Tuple(
250             rOld1.getX() == rOld2.getX() ? rOld1.getX() : basegfx::fround64((rOld1.getX() + rOld2.getX()) * 0.5),
251             rOld1.getY() == rOld2.getY() ? rOld1.getY() : basegfx::fround64((rOld1.getY() + rOld2.getY()) * 0.5));
252     }
253 
average(const B2I64Tuple & rOld1,const B2I64Tuple & rOld2,const B2I64Tuple & rOld3)254     inline B2I64Tuple average(const B2I64Tuple& rOld1, const B2I64Tuple& rOld2, const B2I64Tuple& rOld3)
255     {
256         return B2I64Tuple(
257             (rOld1.getX() == rOld2.getX() && rOld2.getX() == rOld3.getX()) ? rOld1.getX() : basegfx::fround64((rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0)),
258             (rOld1.getY() == rOld2.getY() && rOld2.getY() == rOld3.getY()) ? rOld1.getY() : basegfx::fround64((rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0)));
259     }
260 
operator +(const B2I64Tuple & rTupA,const B2I64Tuple & rTupB)261     inline B2I64Tuple operator+(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
262     {
263         B2I64Tuple aSum(rTupA);
264         aSum += rTupB;
265         return aSum;
266     }
267 
operator -(const B2I64Tuple & rTupA,const B2I64Tuple & rTupB)268     inline B2I64Tuple operator-(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
269     {
270         B2I64Tuple aSub(rTupA);
271         aSub -= rTupB;
272         return aSub;
273     }
274 
operator /(const B2I64Tuple & rTupA,const B2I64Tuple & rTupB)275     inline B2I64Tuple operator/(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
276     {
277         B2I64Tuple aDiv(rTupA);
278         aDiv /= rTupB;
279         return aDiv;
280     }
281 
operator *(const B2I64Tuple & rTupA,const B2I64Tuple & rTupB)282     inline B2I64Tuple operator*(const B2I64Tuple& rTupA, const B2I64Tuple& rTupB)
283     {
284         B2I64Tuple aMul(rTupA);
285         aMul *= rTupB;
286         return aMul;
287     }
288 
operator *(const B2I64Tuple & rTup,sal_Int64 t)289     inline B2I64Tuple operator*(const B2I64Tuple& rTup, sal_Int64 t)
290     {
291         B2I64Tuple aNew(rTup);
292         aNew *= t;
293         return aNew;
294     }
295 
operator *(sal_Int64 t,const B2I64Tuple & rTup)296     inline B2I64Tuple operator*(sal_Int64 t, const B2I64Tuple& rTup)
297     {
298         B2I64Tuple aNew(rTup);
299         aNew *= t;
300         return aNew;
301     }
302 
operator /(const B2I64Tuple & rTup,sal_Int64 t)303     inline B2I64Tuple operator/(const B2I64Tuple& rTup, sal_Int64 t)
304     {
305         B2I64Tuple aNew(rTup);
306         aNew /= t;
307         return aNew;
308     }
309 
operator /(sal_Int64 t,const B2I64Tuple & rTup)310     inline B2I64Tuple operator/(sal_Int64 t, const B2I64Tuple& rTup)
311     {
312         B2I64Tuple aNew(t, t);
313         B2I64Tuple aTmp(rTup);
314         aNew /= aTmp;
315         return aNew;
316     }
317 } // end of namespace basegfx
318 
319 #endif /* _BGFX_TUPLE_B2I64TUPLE_HXX */
320