Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / basegfx / vector / b3dvector.hxx
blob399bba3edf7c9ca6a9806aa09fa590fc0863ec1e
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 #pragma once
22 #include <basegfx/tuple/b3dtuple.hxx>
23 #include <basegfx/basegfxdllapi.h>
25 namespace basegfx
27 class B3DHomMatrix;
29 /** Base Point class with three double values
31 This class derives all operators and common handling for
32 a 3D data class from B3DTuple. All necessary extensions
33 which are special for 3D Vectors are added here.
35 @see B3DTuple
37 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B3DVector : public ::basegfx::B3DTuple
39 public:
40 /** Create a 3D Vector
42 The vector is initialized to (0.0, 0.0, 0.0)
44 B3DVector()
47 /** Create a 3D Vector
49 @param fX
50 This parameter is used to initialize the X-coordinate
51 of the 3D Vector.
53 @param fY
54 This parameter is used to initialize the Y-coordinate
55 of the 3D Vector.
57 @param fZ
58 This parameter is used to initialize the Z-coordinate
59 of the 3D Vector.
61 B3DVector(double fX, double fY, double fZ)
62 : B3DTuple(fX, fY, fZ)
65 /** constructor with tuple to allow copy-constructing
66 from B3DTuple-based classes
68 B3DVector(const ::basegfx::B3DTuple& rTuple)
69 : B3DTuple(rTuple)
72 /** *=operator to allow usage from B3DVector, too
74 B3DVector& operator*=( const B3DVector& rPnt )
76 mfX *= rPnt.mfX;
77 mfY *= rPnt.mfY;
78 mfZ *= rPnt.mfZ;
79 return *this;
82 /** *=operator to allow usage from B3DVector, too
84 B3DVector& operator*=(double t)
86 mfX *= t;
87 mfY *= t;
88 mfZ *= t;
89 return *this;
92 /** assignment operator to allow assigning the results
93 of B3DTuple calculations
95 B3DVector& operator=( const ::basegfx::B3DTuple& rVec )
97 mfX = rVec.getX();
98 mfY = rVec.getY();
99 mfZ = rVec.getZ();
100 return *this;
103 /** Calculate the length of this 3D Vector
105 @return The Length of the 3D Vector
107 double getLength() const
109 double fLen(scalar(*this));
110 if((0.0 == fLen) || (1.0 == fLen))
111 return fLen;
112 return sqrt(fLen);
115 /** Calculate the length in the XZ-Plane for this 3D Vector
117 @return The XZ-Plane Length of the 3D Vector
119 double getXZLength() const
121 double fLen((mfX * mfX) + (mfZ * mfZ)); // #i73040#
122 if((0.0 == fLen) || (1.0 == fLen))
123 return fLen;
124 return sqrt(fLen);
127 /** Calculate the length in the YZ-Plane for this 3D Vector
129 @return The YZ-Plane Length of the 3D Vector
131 double getYZLength() const
133 double fLen((mfY * mfY) + (mfZ * mfZ));
134 if((0.0 == fLen) || (1.0 == fLen))
135 return fLen;
136 return sqrt(fLen);
139 /** Set the length of this 3D Vector
141 @param fLen
142 The to be achieved length of the 3D Vector
144 B3DVector& setLength(double fLen)
146 double fLenNow(scalar(*this));
148 if(!::basegfx::fTools::equalZero(fLenNow))
150 const double fOne(1.0);
152 if(!::basegfx::fTools::equal(fOne, fLenNow))
154 fLen /= sqrt(fLenNow);
157 mfX *= fLen;
158 mfY *= fLen;
159 mfZ *= fLen;
162 return *this;
165 /** Normalize this 3D Vector
167 The length of the 3D Vector is set to 1.0
169 B3DVector& normalize();
171 /** get a 3D Vector which is perpendicular to this and a given 3D Vector
173 @attention This only works if this and the given 3D Vector are
174 both normalized.
176 @param rNormalizedVec
177 A normalized 3D Vector.
179 @return
180 A 3D Vector perpendicular to this and the given one
182 B3DVector getPerpendicular(const B3DVector& rNormalizedVec) const;
184 /** Calculate the Scalar product
186 This method calculates the Scalar product between this
187 and the given 3D Vector.
189 @param rVec
190 A second 3D Vector.
192 @return
193 The Scalar Product of two 3D Vectors
195 double scalar(const B3DVector& rVec) const
197 return ((mfX * rVec.mfX) + (mfY * rVec.mfY) + (mfZ * rVec.mfZ));
200 /** Transform vector by given transformation matrix.
202 Since this is a vector, translational components of the
203 matrix are disregarded.
205 B3DVector& operator*=( const B3DHomMatrix& rMat );
207 static const B3DVector& getEmptyVector()
209 return static_cast<const B3DVector&>( ::basegfx::B3DTuple::getEmptyTuple() );
213 // external operators
216 /** Test two vectors which need not to be normalized for parallelism
218 @param rVecA
219 The first 3D Vector
221 @param rVecB
222 The second 3D Vector
224 @return
225 bool if the two values are parallel. Also true if
226 one of the vectors is empty.
228 BASEGFX_DLLPUBLIC bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB );
230 /** Transform vector by given transformation matrix.
232 Since this is a vector, translational components of the
233 matrix are disregarded.
235 BASEGFX_DLLPUBLIC B3DVector operator*( const B3DHomMatrix& rMat, const B3DVector& rVec );
237 /** Calculate the Cross Product of two 3D Vectors
239 @param rVecA
240 A first 3D Vector.
242 @param rVecB
243 A second 3D Vector.
245 @return
246 The Cross Product of both 3D Vectors
248 inline B3DVector cross(const B3DVector& rVecA, const B3DVector& rVecB)
250 B3DVector aVec(
251 rVecA.getY() * rVecB.getZ() - rVecA.getZ() * rVecB.getY(),
252 rVecA.getZ() * rVecB.getX() - rVecA.getX() * rVecB.getZ(),
253 rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
254 return aVec;
256 } // end of namespace basegfx
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */