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 double fLen(scalar(*this));
110 if((0.0 == fLen
) || (1.0 == 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
))
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
))
139 /** Set the length of this 3D Vector
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
);
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
176 @param rNormalizedVec
177 A normalized 3D Vector.
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.
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
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
246 The Cross Product of both 3D Vectors
248 inline B3DVector
cross(const B3DVector
& rVecA
, const B3DVector
& rVecB
)
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());
256 } // end of namespace basegfx
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */