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