1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtPositioning module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qdoublevector3d_p.h"
41 #include <QtCore/qdatastream.h>
42 #include <QtCore/qmath.h>
43 #include <QtCore/qdebug.h>
44 
45 QT_BEGIN_NAMESPACE
46 
normalized() const47 QDoubleVector3D QDoubleVector3D::normalized() const
48 {
49     // Need some extra precision if the length is very small.
50     double len = double(xp) * double(xp) +
51                  double(yp) * double(yp) +
52                  double(zp) * double(zp);
53     if (qFuzzyIsNull(len - 1.0))
54         return *this;
55     else if (!qFuzzyIsNull(len))
56         return *this / (double)qSqrt(len);
57     else
58         return QDoubleVector3D();
59 }
60 
normalize()61 void QDoubleVector3D::normalize()
62 {
63     // Need some extra precision if the length is very small.
64     double len = double(xp) * double(xp) +
65                  double(yp) * double(yp) +
66                  double(zp) * double(zp);
67     if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len))
68         return;
69 
70     len = qSqrt(len);
71 
72     xp /= len;
73     yp /= len;
74     zp /= len;
75 }
76 
normal(const QDoubleVector3D & v1,const QDoubleVector3D & v2)77 QDoubleVector3D QDoubleVector3D::normal(const QDoubleVector3D &v1, const QDoubleVector3D &v2)
78 {
79     return crossProduct(v1, v2).normalized();
80 }
81 
normal(const QDoubleVector3D & v1,const QDoubleVector3D & v2,const QDoubleVector3D & v3)82 QDoubleVector3D QDoubleVector3D::normal
83         (const QDoubleVector3D &v1, const QDoubleVector3D &v2, const QDoubleVector3D &v3)
84 {
85     return crossProduct((v2 - v1), (v3 - v1)).normalized();
86 }
87 
distanceToPlane(const QDoubleVector3D & plane1,const QDoubleVector3D & plane2,const QDoubleVector3D & plane3) const88 double QDoubleVector3D::distanceToPlane
89     (const QDoubleVector3D &plane1, const QDoubleVector3D &plane2, const QDoubleVector3D &plane3) const
90 {
91     QDoubleVector3D n = normal(plane2 - plane1, plane3 - plane1);
92     return dotProduct(*this - plane1, n);
93 }
94 
distanceToLine(const QDoubleVector3D & point,const QDoubleVector3D & direction) const95 double QDoubleVector3D::distanceToLine
96         (const QDoubleVector3D &point, const QDoubleVector3D &direction) const
97 {
98     if (direction.isNull())
99         return (*this - point).length();
100     QDoubleVector3D p = point + dotProduct(*this - point, direction) * direction;
101     return (*this - p).length();
102 }
103 
length() const104 double QDoubleVector3D::length() const
105 {
106     return qSqrt(xp * xp + yp * yp + zp * zp);
107 }
108 
109 #ifndef QT_NO_DEBUG_STREAM
110 
operator <<(QDebug dbg,const QDoubleVector3D & vector)111 QDebug operator<<(QDebug dbg, const QDoubleVector3D &vector)
112 {
113     QDebugStateSaver saver(dbg);
114     dbg.nospace() << "QDoubleVector3D("
115         << vector.x() << ", " << vector.y() << ", " << vector.z() << ')';
116     return dbg;
117 }
118 
119 #endif
120 
121 #ifndef QT_NO_DATASTREAM
122 
operator <<(QDataStream & stream,const QDoubleVector3D & vector)123 QDataStream &operator<<(QDataStream &stream, const QDoubleVector3D &vector)
124 {
125     stream << double(vector.x()) << double(vector.y())
126            << double(vector.z());
127     return stream;
128 }
129 
operator >>(QDataStream & stream,QDoubleVector3D & vector)130 QDataStream &operator>>(QDataStream &stream, QDoubleVector3D &vector)
131 {
132     double x, y, z;
133     stream >> x;
134     stream >> y;
135     stream >> z;
136     vector.setX(double(x));
137     vector.setY(double(y));
138     vector.setZ(double(z));
139     return stream;
140 }
141 
142 #endif // QT_NO_DATASTREAM
143 
144 QT_END_NAMESPACE
145