xref: /AOO41X/main/basegfx/inc/basegfx/tuple/b3ituple.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_B3ITUPLE_HXX
25 #define _BGFX_TUPLE_B3ITUPLE_HXX
26 
27 #include <sal/types.h>
28 #include <basegfx/tuple/b3dtuple.hxx>
29 
30 
31 namespace basegfx
32 {
33     /** Base class for all Points/Vectors with three sal_Int32 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 three sal_Int32 values
40     */
41     class B3ITuple
42     {
43     protected:
44         sal_Int32                                       mnX;
45         sal_Int32                                       mnY;
46         sal_Int32                                       mnZ;
47 
48     public:
49         /** Create a 3D Tuple
50 
51             The tuple is initialized to (0, 0, 0)
52         */
B3ITuple()53         B3ITuple()
54         :   mnX(0),
55             mnY(0),
56             mnZ(0)
57         {}
58 
59         /** Create a 3D Tuple
60 
61             @param nX
62             This parameter is used to initialize the X-coordinate
63             of the 3D Tuple.
64 
65             @param nY
66             This parameter is used to initialize the Y-coordinate
67             of the 3D Tuple.
68 
69             @param nZ
70             This parameter is used to initialize the Z-coordinate
71             of the 3D Tuple.
72         */
B3ITuple(sal_Int32 nX,sal_Int32 nY,sal_Int32 nZ)73         B3ITuple(sal_Int32 nX, sal_Int32 nY, sal_Int32 nZ)
74         :   mnX(nX),
75             mnY(nY),
76             mnZ(nZ)
77         {}
78 
79         /** Create a copy of a 3D Tuple
80 
81             @param rTup
82             The 3D Tuple which will be copied.
83         */
B3ITuple(const B3ITuple & rTup)84         B3ITuple(const B3ITuple& rTup)
85         :   mnX( rTup.mnX ),
86             mnY( rTup.mnY ),
87             mnZ( rTup.mnZ )
88         {}
89 
~B3ITuple()90         ~B3ITuple()
91         {}
92 
93         /// get X-Coordinate of 3D Tuple
getX() const94         sal_Int32 getX() const
95         {
96             return mnX;
97         }
98 
99         /// get Y-Coordinate of 3D Tuple
getY() const100         sal_Int32 getY() const
101         {
102             return mnY;
103         }
104 
105         /// get Z-Coordinate of 3D Tuple
getZ() const106         sal_Int32 getZ() const
107         {
108             return mnZ;
109         }
110 
111         /// set X-Coordinate of 3D Tuple
setX(sal_Int32 nX)112         void setX(sal_Int32 nX)
113         {
114             mnX = nX;
115         }
116 
117         /// set Y-Coordinate of 3D Tuple
setY(sal_Int32 nY)118         void setY(sal_Int32 nY)
119         {
120             mnY = nY;
121         }
122 
123         /// set Z-Coordinate of 3D Tuple
setZ(sal_Int32 nZ)124         void setZ(sal_Int32 nZ)
125         {
126             mnZ = nZ;
127         }
128 
129         /// Array-access to 3D Tuple
operator [](int nPos) const130         const sal_Int32& operator[] (int nPos) const
131         {
132             // Here, normally two if(...)'s should be used. In the assumption that
133             // both sal_Int32 members can be accessed as an array a shortcut is used here.
134             // if(0 == nPos) return mnX; if(1 == nPos) return mnY; return mnZ;
135             return *((&mnX) + nPos);
136         }
137 
138         /// Array-access to 3D Tuple
operator [](int nPos)139         sal_Int32& operator[] (int nPos)
140         {
141             // Here, normally two if(...)'s should be used. In the assumption that
142             // both sal_Int32 members can be accessed as an array a shortcut is used here.
143             // if(0 == nPos) return mnX; if(1 == nPos) return mnY; return mnZ;
144             return *((&mnX) + nPos);
145         }
146 
147         // operators
148         //////////////////////////////////////////////////////////////////////
149 
operator +=(const B3ITuple & rTup)150         B3ITuple& operator+=( const B3ITuple& rTup )
151         {
152             mnX += rTup.mnX;
153             mnY += rTup.mnY;
154             mnZ += rTup.mnZ;
155             return *this;
156         }
157 
operator -=(const B3ITuple & rTup)158         B3ITuple& operator-=( const B3ITuple& rTup )
159         {
160             mnX -= rTup.mnX;
161             mnY -= rTup.mnY;
162             mnZ -= rTup.mnZ;
163             return *this;
164         }
165 
operator /=(const B3ITuple & rTup)166         B3ITuple& operator/=( const B3ITuple& rTup )
167         {
168             mnX /= rTup.mnX;
169             mnY /= rTup.mnY;
170             mnZ /= rTup.mnZ;
171             return *this;
172         }
173 
operator *=(const B3ITuple & rTup)174         B3ITuple& operator*=( const B3ITuple& rTup )
175         {
176             mnX *= rTup.mnX;
177             mnY *= rTup.mnY;
178             mnZ *= rTup.mnZ;
179             return *this;
180         }
181 
operator *=(sal_Int32 t)182         B3ITuple& operator*=(sal_Int32 t)
183         {
184             mnX *= t;
185             mnY *= t;
186             mnZ *= t;
187             return *this;
188         }
189 
operator /=(sal_Int32 t)190         B3ITuple& operator/=(sal_Int32 t)
191         {
192             mnX /= t;
193             mnY /= t;
194             mnZ /= t;
195             return *this;
196         }
197 
operator -(void) const198         B3ITuple operator-(void) const
199         {
200             return B3ITuple(-mnX, -mnY, -mnZ);
201         }
202 
equalZero() const203         bool equalZero() const
204         {
205             return (this == &getEmptyTuple() ||
206                 (mnX == 0 && mnY == 0 && mnZ == 0));
207         }
208 
operator ==(const B3ITuple & rTup) const209         bool operator==( const B3ITuple& rTup ) const
210         {
211             return this == &rTup || (rTup.mnX == mnX && rTup.mnY == mnY && rTup.mnZ == mnZ);
212         }
213 
operator !=(const B3ITuple & rTup) const214         bool operator!=( const B3ITuple& rTup ) const
215         {
216             return !(*this == rTup);
217         }
218 
operator =(const B3ITuple & rTup)219         B3ITuple& operator=( const B3ITuple& rTup )
220         {
221             mnX = rTup.mnX;
222             mnY = rTup.mnY;
223             mnZ = rTup.mnZ;
224             return *this;
225         }
226 
227         static const B3ITuple& getEmptyTuple();
228     };
229 
230     // external operators
231     //////////////////////////////////////////////////////////////////////////
232 
minimum(const B3ITuple & rTupA,const B3ITuple & rTupB)233     inline B3ITuple minimum(const B3ITuple& rTupA, const B3ITuple& rTupB)
234     {
235         return B3ITuple(
236             std::min(rTupB.getX(), rTupA.getX()),
237             std::min(rTupB.getY(), rTupA.getY()),
238             std::min(rTupB.getZ(), rTupA.getZ()));
239     }
240 
maximum(const B3ITuple & rTupA,const B3ITuple & rTupB)241     inline B3ITuple maximum(const B3ITuple& rTupA, const B3ITuple& rTupB)
242     {
243         return B3ITuple(
244             std::max(rTupB.getX(), rTupA.getX()),
245             std::max(rTupB.getY(), rTupA.getY()),
246             std::max(rTupB.getZ(), rTupA.getZ()));
247     }
248 
absolute(const B3ITuple & rTup)249     inline B3ITuple absolute(const B3ITuple& rTup)
250     {
251         B3ITuple aAbs(
252             (0 > rTup.getX()) ? -rTup.getX() : rTup.getX(),
253             (0 > rTup.getY()) ? -rTup.getY() : rTup.getY(),
254             (0 > rTup.getZ()) ? -rTup.getZ() : rTup.getZ());
255         return aAbs;
256     }
257 
interpolate(const B3ITuple & rOld1,const B3ITuple & rOld2,double t)258     inline B3ITuple interpolate(const B3ITuple& rOld1, const B3ITuple& rOld2, double t)
259     {
260         if(rOld1 == rOld2)
261         {
262             return rOld1;
263         }
264         else if(0.0 >= t)
265         {
266             return rOld1;
267         }
268         else if(1.0 <= t)
269         {
270             return rOld2;
271         }
272         else
273         {
274             return B3ITuple(
275                 basegfx::fround(((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX()),
276                 basegfx::fround(((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY()),
277                 basegfx::fround(((rOld2.getZ() - rOld1.getZ()) * t) + rOld1.getZ()));
278         }
279     }
280 
average(const B3ITuple & rOld1,const B3ITuple & rOld2)281     inline B3ITuple average(const B3ITuple& rOld1, const B3ITuple& rOld2)
282     {
283         return B3ITuple(
284             rOld1.getX() == rOld2.getX() ? rOld1.getX() : basegfx::fround((rOld1.getX() + rOld2.getX()) * 0.5),
285             rOld1.getY() == rOld2.getY() ? rOld1.getY() : basegfx::fround((rOld1.getY() + rOld2.getY()) * 0.5),
286             rOld1.getZ() == rOld2.getZ() ? rOld1.getZ() : basegfx::fround((rOld1.getZ() + rOld2.getZ()) * 0.5));
287     }
288 
average(const B3ITuple & rOld1,const B3ITuple & rOld2,const B3ITuple & rOld3)289     inline B3ITuple average(const B3ITuple& rOld1, const B3ITuple& rOld2, const B3ITuple& rOld3)
290     {
291         return B3ITuple(
292             (rOld1.getX() == rOld2.getX() && rOld2.getX() == rOld3.getX()) ? rOld1.getX() : basegfx::fround((rOld1.getX() + rOld2.getX() + rOld3.getX()) * (1.0 / 3.0)),
293             (rOld1.getY() == rOld2.getY() && rOld2.getY() == rOld3.getY()) ? rOld1.getX() : basegfx::fround((rOld1.getY() + rOld2.getY() + rOld3.getY()) * (1.0 / 3.0)),
294             (rOld1.getZ() == rOld2.getZ() && rOld2.getZ() == rOld3.getZ()) ? rOld1.getX() : basegfx::fround((rOld1.getZ() + rOld2.getZ() + rOld3.getZ()) * (1.0 / 3.0)));
295     }
296 
operator +(const B3ITuple & rTupA,const B3ITuple & rTupB)297     inline B3ITuple operator+(const B3ITuple& rTupA, const B3ITuple& rTupB)
298     {
299         B3ITuple aSum(rTupA);
300         aSum += rTupB;
301         return aSum;
302     }
303 
operator -(const B3ITuple & rTupA,const B3ITuple & rTupB)304     inline B3ITuple operator-(const B3ITuple& rTupA, const B3ITuple& rTupB)
305     {
306         B3ITuple aSub(rTupA);
307         aSub -= rTupB;
308         return aSub;
309     }
310 
operator /(const B3ITuple & rTupA,const B3ITuple & rTupB)311     inline B3ITuple operator/(const B3ITuple& rTupA, const B3ITuple& rTupB)
312     {
313         B3ITuple aDiv(rTupA);
314         aDiv /= rTupB;
315         return aDiv;
316     }
317 
operator *(const B3ITuple & rTupA,const B3ITuple & rTupB)318     inline B3ITuple operator*(const B3ITuple& rTupA, const B3ITuple& rTupB)
319     {
320         B3ITuple aMul(rTupA);
321         aMul *= rTupB;
322         return aMul;
323     }
324 
operator *(const B3ITuple & rTup,sal_Int32 t)325     inline B3ITuple operator*(const B3ITuple& rTup, sal_Int32 t)
326     {
327         B3ITuple aNew(rTup);
328         aNew *= t;
329         return aNew;
330     }
331 
operator *(sal_Int32 t,const B3ITuple & rTup)332     inline B3ITuple operator*(sal_Int32 t, const B3ITuple& rTup)
333     {
334         B3ITuple aNew(rTup);
335         aNew *= t;
336         return aNew;
337     }
338 
operator /(const B3ITuple & rTup,sal_Int32 t)339     inline B3ITuple operator/(const B3ITuple& rTup, sal_Int32 t)
340     {
341         B3ITuple aNew(rTup);
342         aNew /= t;
343         return aNew;
344     }
345 
operator /(sal_Int32 t,const B3ITuple & rTup)346     inline B3ITuple operator/(sal_Int32 t, const B3ITuple& rTup)
347     {
348         B3ITuple aNew(t, t, t);
349         B3ITuple aTmp(rTup);
350         aNew /= aTmp;
351         return aNew;
352     }
353 } // end of namespace basegfx
354 
355 #endif /* _BGFX_TUPLE_B3ITUPLE_HXX */
356