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