workaround segfault in compiler on macos-clang-intel
[LibreOffice.git] / include / basegfx / vector / b3dvector.hxx
blobdf7638f7326834cc66f13ae79b8267fa73a75742
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 mnX *= rPnt.mnX;
77 mnY *= rPnt.mnY;
78 mnZ *= rPnt.mnZ;
79 return *this;
82 /** *=operator to allow usage from B3DVector, too
84 B3DVector& operator*=(double t)
86 mnX *= t;
87 mnY *= t;
88 mnZ *= t;
89 return *this;
92 /** assignment operator to allow assigning the results
93 of B3DTuple calculations
95 B3DVector& operator=( const ::basegfx::B3DTuple& rVec )
97 mnX = rVec.getX();
98 mnY = rVec.getY();
99 mnZ = 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 return std::hypot(mnX, mnY, mnZ);
112 /** Calculate the length in the XZ-Plane for this 3D Vector
114 @return The XZ-Plane Length of the 3D Vector
116 double getXZLength() const
118 return std::hypot(mnX, mnZ);
121 /** Calculate the length in the YZ-Plane for this 3D Vector
123 @return The YZ-Plane Length of the 3D Vector
125 double getYZLength() const
127 return std::hypot(mnY, mnZ);
130 /** Set the length of this 3D Vector
132 @param fLen
133 The to be achieved length of the 3D Vector
135 B3DVector& setLength(double fLen)
137 double fLenNow(std::hypot(mnX, mnY, mnZ));
139 if(!::basegfx::fTools::equalZero(fLenNow))
141 const double fOne(1.0);
143 if(!::basegfx::fTools::equal(fOne, fLenNow))
145 fLen /= fLenNow;
148 mnX *= fLen;
149 mnY *= fLen;
150 mnZ *= fLen;
153 return *this;
156 /** Normalize this 3D Vector
158 The length of the 3D Vector is set to 1.0
160 B3DVector& normalize();
162 /** get a 3D Vector which is perpendicular to this and a given 3D Vector
164 @attention This only works if this and the given 3D Vector are
165 both normalized.
167 @param rNormalizedVec
168 A normalized 3D Vector.
170 @return
171 A 3D Vector perpendicular to this and the given one
173 B3DVector getPerpendicular(const B3DVector& rNormalizedVec) const;
175 /** Calculate the Scalar product
177 This method calculates the Scalar product between this
178 and the given 3D Vector.
180 @param rVec
181 A second 3D Vector.
183 @return
184 The Scalar Product of two 3D Vectors
186 double scalar(const B3DVector& rVec) const
188 return ((mnX * rVec.mnX) + (mnY * rVec.mnY) + (mnZ * rVec.mnZ));
191 /** Transform vector by given transformation matrix.
193 Since this is a vector, translational components of the
194 matrix are disregarded.
196 B3DVector& operator*=( const B3DHomMatrix& rMat );
198 static const B3DVector& getEmptyVector()
200 return static_cast<const B3DVector&>( ::basegfx::B3DTuple::getEmptyTuple() );
204 // external operators
207 /** Test two vectors which need not to be normalized for parallelism
209 @param rVecA
210 The first 3D Vector
212 @param rVecB
213 The second 3D Vector
215 @return
216 bool if the two values are parallel. Also true if
217 one of the vectors is empty.
219 BASEGFX_DLLPUBLIC bool areParallel( const B3DVector& rVecA, const B3DVector& rVecB );
221 /** Transform vector by given transformation matrix.
223 Since this is a vector, translational components of the
224 matrix are disregarded.
226 BASEGFX_DLLPUBLIC B3DVector operator*( const B3DHomMatrix& rMat, const B3DVector& rVec );
228 /** Calculate the Cross Product of two 3D Vectors
230 @param rVecA
231 A first 3D Vector.
233 @param rVecB
234 A second 3D Vector.
236 @return
237 The Cross Product of both 3D Vectors
239 inline B3DVector cross(const B3DVector& rVecA, const B3DVector& rVecB)
241 B3DVector aVec(
242 rVecA.getY() * rVecB.getZ() - rVecA.getZ() * rVecB.getY(),
243 rVecA.getZ() * rVecB.getX() - rVecA.getX() * rVecB.getZ(),
244 rVecA.getX() * rVecB.getY() - rVecA.getY() * rVecB.getX());
245 return aVec;
247 } // end of namespace basegfx
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */