Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _BGFX_VECTOR_B3DVECTOR_HXX
30 : : #define _BGFX_VECTOR_B3DVECTOR_HXX
31 : :
32 : : #include <basegfx/tuple/b3dtuple.hxx>
33 : : #include <basegfx/basegfxdllapi.h>
34 : :
35 : : //////////////////////////////////////////////////////////////////////////////
36 : :
37 : : namespace basegfx
38 : : {
39 : : // predeclaration
40 : : class B3DHomMatrix;
41 : :
42 : : /** Base Point class with three double values
43 : :
44 : : This class derives all operators and common handling for
45 : : a 3D data class from B3DTuple. All necessary extensions
46 : : which are special for 3D Vectors are added here.
47 : :
48 : : @see B3DTuple
49 : : */
50 : 31781 : class BASEGFX_DLLPUBLIC B3DVector : public ::basegfx::B3DTuple
51 : : {
52 : : public:
53 : : /** Create a 3D Vector
54 : :
55 : : The vector is initialized to (0.0, 0.0, 0.0)
56 : : */
57 : 14012 : B3DVector()
58 : 14012 : : B3DTuple()
59 : 14012 : {}
60 : :
61 : : /** Create a 3D Vector
62 : :
63 : : @param fX
64 : : This parameter is used to initialize the X-coordinate
65 : : of the 3D Vector.
66 : :
67 : : @param fY
68 : : This parameter is used to initialize the Y-coordinate
69 : : of the 3D Vector.
70 : :
71 : : @param fZ
72 : : This parameter is used to initialize the Z-coordinate
73 : : of the 3D Vector.
74 : : */
75 : 41292 : B3DVector(double fX, double fY, double fZ)
76 : 41292 : : B3DTuple(fX, fY, fZ)
77 : 41292 : {}
78 : :
79 : : /** Create a copy of a 3D Vector
80 : :
81 : : @param rVec
82 : : The 3D Vector which will be copied.
83 : : */
84 : 66523 : B3DVector(const B3DVector& rVec)
85 : 66523 : : B3DTuple(rVec)
86 : 66523 : {}
87 : :
88 : : /** constructor with tuple to allow copy-constructing
89 : : from B3DTuple-based classes
90 : : */
91 : 45276 : B3DVector(const ::basegfx::B3DTuple& rTuple)
92 : 45276 : : B3DTuple(rTuple)
93 : 45276 : {}
94 : :
95 : 189637 : ~B3DVector()
96 : 189637 : {}
97 : :
98 : : /** *=operator to allow usage from B3DVector, too
99 : : */
100 : : B3DVector& operator*=( const B3DVector& rPnt )
101 : : {
102 : : mfX *= rPnt.mfX;
103 : : mfY *= rPnt.mfY;
104 : : mfZ *= rPnt.mfZ;
105 : : return *this;
106 : : }
107 : :
108 : : /** *=operator to allow usage from B3DVector, too
109 : : */
110 : 0 : B3DVector& operator*=(double t)
111 : : {
112 : 0 : mfX *= t;
113 : 0 : mfY *= t;
114 : 0 : mfZ *= t;
115 : 0 : return *this;
116 : : }
117 : :
118 : : /** assignment operator to allow assigning the results
119 : : of B3DTuple calculations
120 : : */
121 : 6609 : B3DVector& operator=( const ::basegfx::B3DTuple& rVec )
122 : : {
123 : 6609 : mfX = rVec.getX();
124 : 6609 : mfY = rVec.getY();
125 : 6609 : mfZ = rVec.getZ();
126 : 6609 : return *this;
127 : : }
128 : :
129 : : /** Calculate the length of this 3D Vector
130 : :
131 : : @return The Length of the 3D Vector
132 : : */
133 : 1486 : double getLength(void) const
134 : : {
135 : 1486 : double fLen(scalar(*this));
136 [ + + ][ + - ]: 1486 : if((0.0 == fLen) || (1.0 == fLen))
137 : 1472 : return fLen;
138 : 1486 : return sqrt(fLen);
139 : : }
140 : :
141 : : /** Calculate the length in the XY-Plane for this 3D Vector
142 : :
143 : : @return The XY-Plane Length of the 3D Vector
144 : : */
145 : : double getXYLength(void) const
146 : : {
147 : : double fLen((mfX * mfX) + (mfY * mfY));
148 : : if((0.0 == fLen) || (1.0 == fLen))
149 : : return fLen;
150 : : return sqrt(fLen);
151 : : }
152 : :
153 : : /** Calculate the length in the XZ-Plane for this 3D Vector
154 : :
155 : : @return The XZ-Plane Length of the 3D Vector
156 : : */
157 : 0 : double getXZLength(void) const
158 : : {
159 : 0 : double fLen((mfX * mfX) + (mfZ * mfZ)); // #i73040#
160 [ # # ][ # # ]: 0 : if((0.0 == fLen) || (1.0 == fLen))
161 : 0 : return fLen;
162 : 0 : return sqrt(fLen);
163 : : }
164 : :
165 : : /** Calculate the length in the YZ-Plane for this 3D Vector
166 : :
167 : : @return The YZ-Plane Length of the 3D Vector
168 : : */
169 : 761 : double getYZLength(void) const
170 : : {
171 : 761 : double fLen((mfY * mfY) + (mfZ * mfZ));
172 [ + - ][ + - ]: 761 : if((0.0 == fLen) || (1.0 == fLen))
173 : 761 : return fLen;
174 : 761 : return sqrt(fLen);
175 : : }
176 : :
177 : : /** Set the length of this 3D Vector
178 : :
179 : : @param fLen
180 : : The to be achieved length of the 3D Vector
181 : : */
182 : 0 : B3DVector& setLength(double fLen)
183 : : {
184 : 0 : double fLenNow(scalar(*this));
185 : :
186 [ # # ]: 0 : if(!::basegfx::fTools::equalZero(fLenNow))
187 : : {
188 : 0 : const double fOne(1.0);
189 : :
190 [ # # ]: 0 : if(!::basegfx::fTools::equal(fOne, fLenNow))
191 : : {
192 : 0 : fLen /= sqrt(fLenNow);
193 : : }
194 : :
195 : 0 : mfX *= fLen;
196 : 0 : mfY *= fLen;
197 : 0 : mfZ *= fLen;
198 : : }
199 : :
200 : 0 : return *this;
201 : : }
202 : :
203 : : /** Normalize this 3D Vector
204 : :
205 : : The length of the 3D Vector is set to 1.0
206 : : */
207 : : B3DVector& normalize();
208 : :
209 : : /** Test if this 3D Vector is normalized
210 : :
211 : : @return
212 : : true if lenth of vector is equal to 1.0
213 : : false else
214 : : */
215 : : bool isNormalized() const
216 : : {
217 : : const double fOne(1.0);
218 : : const double fScalar(scalar(*this));
219 : :
220 : : return (::basegfx::fTools::equal(fOne, fScalar));
221 : : }
222 : :
223 : : /** get a 3D Vector which is perpendicular to this and a given 3D Vector
224 : :
225 : : @attention This only works if this and the given 3D Vector are
226 : : both normalized.
227 : :
228 : : @param rNormalizedVec
229 : : A normalized 3D Vector.
230 : :
231 : : @return
232 : : A 3D Vector perpendicular to this and the given one
233 : : */
234 : : B3DVector getPerpendicular(const B3DVector& rNormalizedVec) const;
235 : :
236 : : /** Calculate the Scalar product
237 : :
238 : : This method calculates the Scalar product between this
239 : : and the given 3D Vector.
240 : :
241 : : @param rVec
242 : : A second 3D Vector.
243 : :
244 : : @return
245 : : The Scalar Product of two 3D Vectors
246 : : */
247 : 2250 : double scalar(const B3DVector& rVec) const
248 : : {
249 : 2250 : return ((mfX * rVec.mfX) + (mfY * rVec.mfY) + (mfZ * rVec.mfZ));
250 : : }
251 : :
252 : : /** Transform vector by given transformation matrix.
253 : :
254 : : Since this is a vector, translational components of the
255 : : matrix are disregarded.
256 : : */
257 : : B3DVector& operator*=( const B3DHomMatrix& rMat );
258 : :
259 : : static const B3DVector& getEmptyVector()
260 : : {
261 : : return (const B3DVector&) ::basegfx::B3DTuple::getEmptyTuple();
262 : : }
263 : : };
264 : :
265 : : // external operators
266 : : //////////////////////////////////////////////////////////////////////////
267 : :
268 : : /** get a 3D Vector which is in 2D (ignoring
269 : : the Z-Coordinate) perpendicular to a given 3D Vector
270 : :
271 : : @attention This only works if the given 3D Vector is normalized.
272 : :
273 : : @param rNormalizedVec
274 : : A normalized 3D Vector.
275 : :
276 : : @return
277 : : A 3D Vector perpendicular to the given one in X,Y (2D).
278 : : */
279 : : inline B3DVector getPerpendicular2D( const B3DVector& rNormalizedVec )
280 : : {
281 : : B3DVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX(), rNormalizedVec.getZ());
282 : : return aPerpendicular;
283 : : }
284 : :
285 : : /** Test two vectors which need not to be normalized for parallelism
286 : :
287 : : @param rVecA
288 : : The first 3D Vector
289 : :
290 : : @param rVecB
291 : : The second 3D Vector
292 : :
293 : : @return
294 : : bool if the two values are parallel. Also true if
295 : : one of the vectors is empty.
296 : : */
297 : : BASEGFX_DLLPUBLIC bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB );
298 : :
299 : : /** Transform vector by given transformation matrix.
300 : :
301 : : Since this is a vector, translational components of the
302 : : matrix are disregarded.
303 : : */
304 : : BASEGFX_DLLPUBLIC B3DVector operator*( const B3DHomMatrix& rMat, const B3DVector& rVec );
305 : :
306 : : /** Calculate the Cross Product of two 3D Vectors
307 : :
308 : : @param rVecA
309 : : A first 3D Vector.
310 : :
311 : : @param rVecB
312 : : A second 3D Vector.
313 : :
314 : : @return
315 : : The Cross Product of both 3D Vectors
316 : : */
317 : 98 : inline B3DVector cross(const B3DVector& rVecA, const B3DVector& rVecB)
318 : : {
319 : : B3DVector aVec(
320 : 98 : rVecA.getY() * rVecB.getZ() - rVecA.getZ() * rVecB.getY(),
321 : 98 : rVecA.getZ() * rVecB.getX() - rVecA.getX() * rVecB.getZ(),
322 : 196 : rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
323 : 98 : return aVec;
324 : : }
325 : : } // end of namespace basegfx
326 : :
327 : : //////////////////////////////////////////////////////////////////////////////
328 : :
329 : : #endif /* _BGFX_VECTOR_B3DVECTOR_HXX */
330 : :
331 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|