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_VECTOR_B2DVECTOR_HXX 25 #define _BGFX_VECTOR_B2DVECTOR_HXX 26 27 #include <basegfx/tuple/b2dtuple.hxx> 28 #include <basegfx/vector/b2ivector.hxx> 29 #include <basegfx/vector/b2enums.hxx> 30 #include <basegfx/basegfxdllapi.h> 31 32 namespace basegfx 33 { 34 // predeclaration 35 class B2DHomMatrix; 36 37 /** Base Point class with two double values 38 39 This class derives all operators and common handling for 40 a 2D data class from B2DTuple. All necessary extensions 41 which are special for 2D Vectors are added here. 42 43 @see B2DTuple 44 */ 45 class BASEGFX_DLLPUBLIC B2DVector : public ::basegfx::B2DTuple 46 { 47 public: 48 /** Create a 2D Vector 49 50 The vector is initialized to (0.0, 0.0) 51 */ B2DVector()52 B2DVector() 53 : B2DTuple() 54 {} 55 56 /** Create a 2D Vector 57 58 @param fX 59 This parameter is used to initialize the X-coordinate 60 of the 2D Vector. 61 62 @param fY 63 This parameter is used to initialize the Y-coordinate 64 of the 2D Vector. 65 */ B2DVector(double fX,double fY)66 B2DVector(double fX, double fY) 67 : B2DTuple(fX, fY) 68 {} 69 70 /** Create a copy of a 2D Vector 71 72 @param rVec 73 The 2D Vector which will be copied. 74 */ B2DVector(const B2DVector & rVec)75 B2DVector(const B2DVector& rVec) 76 : B2DTuple(rVec) 77 {} 78 79 /** Create a copy of a 2D Vector 80 81 @param rVec 82 The 2D Vector which will be copied. 83 */ B2DVector(const::basegfx::B2IVector & rVec)84 B2DVector(const ::basegfx::B2IVector& rVec) 85 : B2DTuple(rVec) 86 {} 87 88 /** constructor with tuple to allow copy-constructing 89 from B2DTuple-based classes 90 */ B2DVector(const::basegfx::B2DTuple & rTuple)91 B2DVector(const ::basegfx::B2DTuple& rTuple) 92 : B2DTuple(rTuple) 93 {} 94 ~B2DVector()95 ~B2DVector() 96 {} 97 98 /** *=operator to allow usage from B2DVector, too 99 */ operator *=(const B2DVector & rPnt)100 B2DVector& operator*=( const B2DVector& rPnt ) 101 { 102 mfX *= rPnt.mfX; 103 mfY *= rPnt.mfY; 104 return *this; 105 } 106 107 /** *=operator to allow usage from B2DVector, too 108 */ operator *=(double t)109 B2DVector& operator*=(double t) 110 { 111 mfX *= t; 112 mfY *= t; 113 return *this; 114 } 115 116 /** assignment operator to allow assigning the results 117 of B2DTuple calculations 118 */ 119 B2DVector& operator=( const ::basegfx::B2DTuple& rVec ); 120 121 /** Calculate the length of this 2D Vector 122 123 @return The Length of the 2D Vector 124 */ 125 double getLength() const; 126 127 /** Set the length of this 2D Vector 128 129 @param fLen 130 The to be achieved length of the 2D Vector 131 */ 132 B2DVector& setLength(double fLen); 133 134 /** Normalize this 2D Vector 135 136 The length of the 2D Vector is set to 1.0 137 */ 138 B2DVector& normalize(); 139 140 /** Test if this 2D Vector is normalized 141 142 @return 143 true if lenth of vector is equal to 1.0 144 false else 145 */ 146 bool isNormalized() const; 147 148 /** Calculate the Scalar with another 2D Vector 149 150 @param rVec 151 The second 2D Vector 152 153 @return 154 The Scalar value of the two involved 2D Vectors 155 */ 156 double scalar( const B2DVector& rVec ) const; 157 158 /** Calculate the length of the cross product with another 2D Vector 159 160 In 2D, returning an actual vector does not make much 161 sense here. The magnitude, although, can be readily 162 used for tasks such as angle calculations, since for 163 the returned value, the following equation holds: 164 retVal = getLength(this)*getLength(rVec)*sin(theta), 165 with theta being the angle between the two vectors. 166 167 @param rVec 168 The second 2D Vector 169 170 @return 171 The length of the cross product of the two involved 2D Vectors 172 */ 173 double cross( const B2DVector& rVec ) const; 174 175 /** Calculate the Angle with another 2D Vector 176 177 @param rVec 178 The second 2D Vector 179 180 @return 181 The Angle value of the two involved 2D Vectors in -pi/2 < return < pi/2 182 */ 183 double angle( const B2DVector& rVec ) const; 184 185 /** Transform vector by given transformation matrix. 186 187 Since this is a vector, translational components of the 188 matrix are disregarded. 189 */ 190 B2DVector& operator*=( const B2DHomMatrix& rMat ); 191 192 static const B2DVector& getEmptyVector(); 193 }; 194 195 // external operators 196 ////////////////////////////////////////////////////////////////////////// 197 198 /** Calculate the orientation to another 2D Vector 199 200 @param rVecA 201 The first 2D Vector 202 203 @param rVecB 204 The second 2D Vector 205 206 @return 207 The mathematical Orientation of the two involved 2D Vectors 208 */ 209 BASEGFX_DLLPUBLIC B2VectorOrientation getOrientation( const B2DVector& rVecA, const B2DVector& rVecB ); 210 211 /** Calculate a perpendicular 2D Vector to the given one 212 213 @param rVec 214 The source 2D Vector 215 216 @attention This only works if the given 2D Vector is normalized. 217 218 @return 219 A 2D Vector perpendicular to the one given in parameter rVec 220 */ 221 BASEGFX_DLLPUBLIC B2DVector getPerpendicular( const B2DVector& rNormalizedVec ); 222 223 /** Calculate a perpendicular 2D Vector to the given one, 224 normalize the given one as preparation 225 226 @param rVec 227 The source 2D Vector 228 229 @return 230 A normalized 2D Vector perpendicular to the one given in parameter rVec 231 */ 232 BASEGFX_DLLPUBLIC B2DVector getNormalizedPerpendicular( const B2DVector& rVec ); 233 234 /** Test two vectors which need not to be normalized for parallelism 235 236 @param rVecA 237 The first 2D Vector 238 239 @param rVecB 240 The second 2D Vector 241 242 @return 243 bool if the two values are parallel. Also true if 244 one of the vectors is empty. 245 */ 246 BASEGFX_DLLPUBLIC bool areParallel( const B2DVector& rVecA, const B2DVector& rVecB ); 247 248 /** Transform vector by given transformation matrix. 249 250 Since this is a vector, translational components of the 251 matrix are disregarded. 252 */ 253 BASEGFX_DLLPUBLIC B2DVector operator*( const B2DHomMatrix& rMat, const B2DVector& rVec ); 254 255 /** Test continuity between given vectors. 256 257 The two given vectors are assumed to describe control points on a 258 common point. Calculate if there is a continuity between them. 259 */ 260 BASEGFX_DLLPUBLIC B2VectorContinuity getContinuity( const B2DVector& rBackVector, const B2DVector& rForwardVector ); 261 262 } // end of namespace basegfx 263 264 #endif /* _BGFX_VECTOR_B2DVECTOR_HXX */ 265