Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / basegfx / vector / b3dvector.hxx
blobefb37aa3f15d832483a7baae818d81ebb15203cd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BASEGFX_VECTOR_B3DVECTOR_HXX
21 #define INCLUDED_BASEGFX_VECTOR_B3DVECTOR_HXX
23 #include <basegfx/tuple/b3dtuple.hxx>
24 #include <basegfx/basegfxdllapi.h>
26 namespace basegfx
28 class B3DHomMatrix;
30 /** Base Point class with three double values
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.
36 @see B3DTuple
38 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B3DVector : public ::basegfx::B3DTuple
40 public:
41 /** Create a 3D Vector
43 The vector is initialized to (0.0, 0.0, 0.0)
45 B3DVector()
46 : B3DTuple()
49 /** Create a 3D Vector
51 @param fX
52 This parameter is used to initialize the X-coordinate
53 of the 3D Vector.
55 @param fY
56 This parameter is used to initialize the Y-coordinate
57 of the 3D Vector.
59 @param fZ
60 This parameter is used to initialize the Z-coordinate
61 of the 3D Vector.
63 B3DVector(double fX, double fY, double fZ)
64 : B3DTuple(fX, fY, fZ)
67 /** Create a copy of a 3D Vector
69 @param rVec
70 The 3D Vector which will be copied.
72 B3DVector(const B3DVector& rVec)
73 : B3DTuple(rVec)
76 /** constructor with tuple to allow copy-constructing
77 from B3DTuple-based classes
79 B3DVector(const ::basegfx::B3DTuple& rTuple)
80 : B3DTuple(rTuple)
83 /** *=operator to allow usage from B3DVector, too
85 B3DVector& operator*=( const B3DVector& rPnt )
87 mfX *= rPnt.mfX;
88 mfY *= rPnt.mfY;
89 mfZ *= rPnt.mfZ;
90 return *this;
93 /** *=operator to allow usage from B3DVector, too
95 B3DVector& operator*=(double t)
97 mfX *= t;
98 mfY *= t;
99 mfZ *= t;
100 return *this;
103 /** assignment operator to allow assigning the results
104 of B3DTuple calculations
106 B3DVector& operator=( const ::basegfx::B3DTuple& rVec )
108 mfX = rVec.getX();
109 mfY = rVec.getY();
110 mfZ = rVec.getZ();
111 return *this;
114 /** Calculate the length of this 3D Vector
116 @return The Length of the 3D Vector
118 double getLength() const
120 double fLen(scalar(*this));
121 if((0.0 == fLen) || (1.0 == fLen))
122 return fLen;
123 return sqrt(fLen);
126 /** Calculate the length in the XZ-Plane for this 3D Vector
128 @return The XZ-Plane Length of the 3D Vector
130 double getXZLength() const
132 double fLen((mfX * mfX) + (mfZ * mfZ)); // #i73040#
133 if((0.0 == fLen) || (1.0 == fLen))
134 return fLen;
135 return sqrt(fLen);
138 /** Calculate the length in the YZ-Plane for this 3D Vector
140 @return The YZ-Plane Length of the 3D Vector
142 double getYZLength() const
144 double fLen((mfY * mfY) + (mfZ * mfZ));
145 if((0.0 == fLen) || (1.0 == fLen))
146 return fLen;
147 return sqrt(fLen);
150 /** Set the length of this 3D Vector
152 @param fLen
153 The to be achieved length of the 3D Vector
155 B3DVector& setLength(double fLen)
157 double fLenNow(scalar(*this));
159 if(!::basegfx::fTools::equalZero(fLenNow))
161 const double fOne(1.0);
163 if(!::basegfx::fTools::equal(fOne, fLenNow))
165 fLen /= sqrt(fLenNow);
168 mfX *= fLen;
169 mfY *= fLen;
170 mfZ *= fLen;
173 return *this;
176 /** Normalize this 3D Vector
178 The length of the 3D Vector is set to 1.0
180 B3DVector& normalize();
182 /** get a 3D Vector which is perpendicular to this and a given 3D Vector
184 @attention This only works if this and the given 3D Vector are
185 both normalized.
187 @param rNormalizedVec
188 A normalized 3D Vector.
190 @return
191 A 3D Vector perpendicular to this and the given one
193 B3DVector getPerpendicular(const B3DVector& rNormalizedVec) const;
195 /** Calculate the Scalar product
197 This method calculates the Scalar product between this
198 and the given 3D Vector.
200 @param rVec
201 A second 3D Vector.
203 @return
204 The Scalar Product of two 3D Vectors
206 double scalar(const B3DVector& rVec) const
208 return ((mfX * rVec.mfX) + (mfY * rVec.mfY) + (mfZ * rVec.mfZ));
211 /** Transform vector by given transformation matrix.
213 Since this is a vector, translational components of the
214 matrix are disregarded.
216 B3DVector& operator*=( const B3DHomMatrix& rMat );
218 static const B3DVector& getEmptyVector()
220 return static_cast<const B3DVector&>( ::basegfx::B3DTuple::getEmptyTuple() );
224 // external operators
227 /** Test two vectors which need not to be normalized for parallelism
229 @param rVecA
230 The first 3D Vector
232 @param rVecB
233 The second 3D Vector
235 @return
236 bool if the two values are parallel. Also true if
237 one of the vectors is empty.
239 BASEGFX_DLLPUBLIC bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB );
241 /** Transform vector by given transformation matrix.
243 Since this is a vector, translational components of the
244 matrix are disregarded.
246 BASEGFX_DLLPUBLIC B3DVector operator*( const B3DHomMatrix& rMat, const B3DVector& rVec );
248 /** Calculate the Cross Product of two 3D Vectors
250 @param rVecA
251 A first 3D Vector.
253 @param rVecB
254 A second 3D Vector.
256 @return
257 The Cross Product of both 3D Vectors
259 inline B3DVector cross(const B3DVector& rVecA, const B3DVector& rVecB)
261 B3DVector aVec(
262 rVecA.getY() * rVecB.getZ() - rVecA.getZ() * rVecB.getY(),
263 rVecA.getZ() * rVecB.getX() - rVecA.getX() * rVecB.getZ(),
264 rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
265 return aVec;
267 } // end of namespace basegfx
269 #endif // INCLUDED_BASEGFX_VECTOR_B3DVECTOR_HXX
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */