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