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