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