1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <basegfx/tuple/b3dtuple.hxx>
23 #include <basegfx/basegfxdllapi.h>
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.
37 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B3DVector
: public ::basegfx::B3DTuple
40 /** Create a 3D Vector
42 The vector is initialized to (0.0, 0.0, 0.0)
47 /** Create a 3D Vector
50 This parameter is used to initialize the X-coordinate
54 This parameter is used to initialize the Y-coordinate
58 This parameter is used to initialize the Z-coordinate
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
)
72 /** *=operator to allow usage from B3DVector, too
74 B3DVector
& operator*=( const B3DVector
& rPnt
)
82 /** *=operator to allow usage from B3DVector, too
84 B3DVector
& operator*=(double t
)
92 /** assignment operator to allow assigning the results
93 of B3DTuple calculations
95 B3DVector
& operator=( const ::basegfx::B3DTuple
& rVec
)
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
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
))
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
167 @param rNormalizedVec
168 A normalized 3D Vector.
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.
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
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
237 The Cross Product of both 3D Vectors
239 inline B3DVector
cross(const B3DVector
& rVecA
, const B3DVector
& rVecB
)
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());
247 } // end of namespace basegfx
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */