Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_BASEGFX_VECTOR_B3DVECTOR_HXX
21 : #define INCLUDED_BASEGFX_VECTOR_B3DVECTOR_HXX
22 :
23 : #include <basegfx/tuple/b3dtuple.hxx>
24 : #include <basegfx/basegfxdllapi.h>
25 :
26 : namespace basegfx
27 : {
28 : class B3DHomMatrix;
29 :
30 : /** Base Point class with three double values
31 :
32 : This class derives all operators and common handling for
33 : a 3D data class from B3DTuple. All necessary extensions
34 : which are special for 3D Vectors are added here.
35 :
36 : @see B3DTuple
37 : */
38 503807 : class BASEGFX_DLLPUBLIC B3DVector : public ::basegfx::B3DTuple
39 : {
40 : public:
41 : /** Create a 3D Vector
42 :
43 : The vector is initialized to (0.0, 0.0, 0.0)
44 : */
45 288125 : B3DVector()
46 288125 : : B3DTuple()
47 288125 : {}
48 :
49 : /** Create a 3D Vector
50 :
51 : @param fX
52 : This parameter is used to initialize the X-coordinate
53 : of the 3D Vector.
54 :
55 : @param fY
56 : This parameter is used to initialize the Y-coordinate
57 : of the 3D Vector.
58 :
59 : @param fZ
60 : This parameter is used to initialize the Z-coordinate
61 : of the 3D Vector.
62 : */
63 248693 : B3DVector(double fX, double fY, double fZ)
64 248693 : : B3DTuple(fX, fY, fZ)
65 248693 : {}
66 :
67 : /** Create a copy of a 3D Vector
68 :
69 : @param rVec
70 : The 3D Vector which will be copied.
71 : */
72 815161 : B3DVector(const B3DVector& rVec)
73 815161 : : B3DTuple(rVec)
74 815161 : {}
75 :
76 : /** constructor with tuple to allow copy-constructing
77 : from B3DTuple-based classes
78 : */
79 506633 : B3DVector(const ::basegfx::B3DTuple& rTuple)
80 506633 : : B3DTuple(rTuple)
81 506633 : {}
82 :
83 1857518 : ~B3DVector()
84 1857518 : {}
85 :
86 : /** *=operator to allow usage from B3DVector, too
87 : */
88 : B3DVector& operator*=( const B3DVector& rPnt )
89 : {
90 : mfX *= rPnt.mfX;
91 : mfY *= rPnt.mfY;
92 : mfZ *= rPnt.mfZ;
93 : return *this;
94 : }
95 :
96 : /** *=operator to allow usage from B3DVector, too
97 : */
98 0 : B3DVector& operator*=(double t)
99 : {
100 0 : mfX *= t;
101 0 : mfY *= t;
102 0 : mfZ *= t;
103 0 : return *this;
104 : }
105 :
106 : /** assignment operator to allow assigning the results
107 : of B3DTuple calculations
108 : */
109 35093 : B3DVector& operator=( const ::basegfx::B3DTuple& rVec )
110 : {
111 35093 : mfX = rVec.getX();
112 35093 : mfY = rVec.getY();
113 35093 : mfZ = rVec.getZ();
114 35093 : return *this;
115 : }
116 :
117 : /** Calculate the length of this 3D Vector
118 :
119 : @return The Length of the 3D Vector
120 : */
121 59718 : double getLength() const
122 : {
123 59718 : double fLen(scalar(*this));
124 59718 : if((0.0 == fLen) || (1.0 == fLen))
125 6268 : return fLen;
126 53450 : return sqrt(fLen);
127 : }
128 :
129 : /** Calculate the length in the XY-Plane for this 3D Vector
130 :
131 : @return The XY-Plane Length of the 3D Vector
132 : */
133 : double getXYLength() const
134 : {
135 : double fLen((mfX * mfX) + (mfY * mfY));
136 : if((0.0 == fLen) || (1.0 == fLen))
137 : return fLen;
138 : return sqrt(fLen);
139 : }
140 :
141 : /** Calculate the length in the XZ-Plane for this 3D Vector
142 :
143 : @return The XZ-Plane Length of the 3D Vector
144 : */
145 4951 : double getXZLength() const
146 : {
147 4951 : double fLen((mfX * mfX) + (mfZ * mfZ)); // #i73040#
148 4951 : if((0.0 == fLen) || (1.0 == fLen))
149 90 : return fLen;
150 4861 : return sqrt(fLen);
151 : }
152 :
153 : /** Calculate the length in the YZ-Plane for this 3D Vector
154 :
155 : @return The YZ-Plane Length of the 3D Vector
156 : */
157 1366 : double getYZLength() const
158 : {
159 1366 : double fLen((mfY * mfY) + (mfZ * mfZ));
160 1366 : if((0.0 == fLen) || (1.0 == fLen))
161 1366 : return fLen;
162 0 : return sqrt(fLen);
163 : }
164 :
165 : /** Set the length of this 3D Vector
166 :
167 : @param fLen
168 : The to be achieved length of the 3D Vector
169 : */
170 36 : B3DVector& setLength(double fLen)
171 : {
172 36 : double fLenNow(scalar(*this));
173 :
174 36 : if(!::basegfx::fTools::equalZero(fLenNow))
175 : {
176 36 : const double fOne(1.0);
177 :
178 36 : if(!::basegfx::fTools::equal(fOne, fLenNow))
179 : {
180 36 : fLen /= sqrt(fLenNow);
181 : }
182 :
183 36 : mfX *= fLen;
184 36 : mfY *= fLen;
185 36 : mfZ *= fLen;
186 : }
187 :
188 36 : return *this;
189 : }
190 :
191 : /** Normalize this 3D Vector
192 :
193 : The length of the 3D Vector is set to 1.0
194 : */
195 : B3DVector& normalize();
196 :
197 : /** Test if this 3D Vector is normalized
198 :
199 : @return
200 : true if lenth of vector is equal to 1.0
201 : false else
202 : */
203 : bool isNormalized() const
204 : {
205 : const double fOne(1.0);
206 : const double fScalar(scalar(*this));
207 :
208 : return (::basegfx::fTools::equal(fOne, fScalar));
209 : }
210 :
211 : /** get a 3D Vector which is perpendicular to this and a given 3D Vector
212 :
213 : @attention This only works if this and the given 3D Vector are
214 : both normalized.
215 :
216 : @param rNormalizedVec
217 : A normalized 3D Vector.
218 :
219 : @return
220 : A 3D Vector perpendicular to this and the given one
221 : */
222 : B3DVector getPerpendicular(const B3DVector& rNormalizedVec) const;
223 :
224 : /** Calculate the Scalar product
225 :
226 : This method calculates the Scalar product between this
227 : and the given 3D Vector.
228 :
229 : @param rVec
230 : A second 3D Vector.
231 :
232 : @return
233 : The Scalar Product of two 3D Vectors
234 : */
235 570462 : double scalar(const B3DVector& rVec) const
236 : {
237 570462 : return ((mfX * rVec.mfX) + (mfY * rVec.mfY) + (mfZ * rVec.mfZ));
238 : }
239 :
240 : /** Transform vector by given transformation matrix.
241 :
242 : Since this is a vector, translational components of the
243 : matrix are disregarded.
244 : */
245 : B3DVector& operator*=( const B3DHomMatrix& rMat );
246 :
247 78124 : static const B3DVector& getEmptyVector()
248 : {
249 78124 : return static_cast<const B3DVector&>( ::basegfx::B3DTuple::getEmptyTuple() );
250 : }
251 : };
252 :
253 : // external operators
254 :
255 :
256 : /** get a 3D Vector which is in 2D (ignoring
257 : the Z-Coordinate) perpendicular to a given 3D Vector
258 :
259 : @attention This only works if the given 3D Vector is normalized.
260 :
261 : @param rNormalizedVec
262 : A normalized 3D Vector.
263 :
264 : @return
265 : A 3D Vector perpendicular to the given one in X,Y (2D).
266 : */
267 : inline B3DVector getPerpendicular2D( const B3DVector& rNormalizedVec )
268 : {
269 : B3DVector aPerpendicular(-rNormalizedVec.getY(), rNormalizedVec.getX(), rNormalizedVec.getZ());
270 : return aPerpendicular;
271 : }
272 :
273 : /** Test two vectors which need not to be normalized for parallelism
274 :
275 : @param rVecA
276 : The first 3D Vector
277 :
278 : @param rVecB
279 : The second 3D Vector
280 :
281 : @return
282 : bool if the two values are parallel. Also true if
283 : one of the vectors is empty.
284 : */
285 : BASEGFX_DLLPUBLIC bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB );
286 :
287 : /** Transform vector by given transformation matrix.
288 :
289 : Since this is a vector, translational components of the
290 : matrix are disregarded.
291 : */
292 : BASEGFX_DLLPUBLIC B3DVector operator*( const B3DHomMatrix& rMat, const B3DVector& rVec );
293 :
294 : /** Calculate the Cross Product of two 3D Vectors
295 :
296 : @param rVecA
297 : A first 3D Vector.
298 :
299 : @param rVecB
300 : A second 3D Vector.
301 :
302 : @return
303 : The Cross Product of both 3D Vectors
304 : */
305 171978 : inline B3DVector cross(const B3DVector& rVecA, const B3DVector& rVecB)
306 : {
307 : B3DVector aVec(
308 171978 : rVecA.getY() * rVecB.getZ() - rVecA.getZ() * rVecB.getY(),
309 171978 : rVecA.getZ() * rVecB.getX() - rVecA.getX() * rVecB.getZ(),
310 515934 : rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
311 171978 : return aVec;
312 : }
313 : } // end of namespace basegfx
314 :
315 : #endif // INCLUDED_BASEGFX_VECTOR_B3DVECTOR_HXX
316 :
317 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|