Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdoublevector3d.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtCore/qdatastream.h>
6#include <QtCore/qmath.h>
7#include <QtCore/qdebug.h>
8
10
11QDoubleVector3D QDoubleVector3D::normalized() const
12{
13 // Need some extra precision if the length is very small.
14 double len = double(xp) * double(xp) +
15 double(yp) * double(yp) +
16 double(zp) * double(zp);
17 if (qFuzzyIsNull(len - 1.0))
18 return *this;
19 else if (!qFuzzyIsNull(len))
20 return *this / (double)qSqrt(len);
21 else
22 return QDoubleVector3D();
23}
24
25void QDoubleVector3D::normalize()
26{
27 // Need some extra precision if the length is very small.
28 double len = double(xp) * double(xp) +
29 double(yp) * double(yp) +
30 double(zp) * double(zp);
31 if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len))
32 return;
33
34 len = qSqrt(len);
35
36 xp /= len;
37 yp /= len;
38 zp /= len;
39}
40
41QDoubleVector3D QDoubleVector3D::normal(const QDoubleVector3D &v1, const QDoubleVector3D &v2)
42{
43 return crossProduct(v1, v2).normalized();
44}
45
46QDoubleVector3D QDoubleVector3D::normal
47 (const QDoubleVector3D &v1, const QDoubleVector3D &v2, const QDoubleVector3D &v3)
48{
49 return crossProduct((v2 - v1), (v3 - v1)).normalized();
50}
51
52double QDoubleVector3D::distanceToPlane
53 (const QDoubleVector3D &plane1, const QDoubleVector3D &plane2, const QDoubleVector3D &plane3) const
54{
55 QDoubleVector3D n = normal(plane2 - plane1, plane3 - plane1);
56 return dotProduct(*this - plane1, n);
57}
58
59double QDoubleVector3D::distanceToLine
60 (const QDoubleVector3D &point, const QDoubleVector3D &direction) const
61{
62 if (direction.isNull())
63 return (*this - point).length();
64 QDoubleVector3D p = point + dotProduct(*this - point, direction) * direction;
65 return (*this - p).length();
66}
67
68double QDoubleVector3D::length() const
69{
70 return qSqrt(xp * xp + yp * yp + zp * zp);
71}
72
73#ifndef QT_NO_DEBUG_STREAM
74
75QDebug operator<<(QDebug dbg, const QDoubleVector3D &vector)
76{
77 QDebugStateSaver saver(dbg);
78 dbg.nospace() << "QDoubleVector3D("
79 << vector.x() << ", " << vector.y() << ", " << vector.z() << ')';
80 return dbg;
81}
82
83#endif
84
85#ifndef QT_NO_DATASTREAM
86
87QDataStream &operator<<(QDataStream &stream, const QDoubleVector3D &vector)
88{
89 stream << double(vector.x()) << double(vector.y())
90 << double(vector.z());
91 return stream;
92}
93
94QDataStream &operator>>(QDataStream &stream, QDoubleVector3D &vector)
95{
96 double x, y, z;
97 stream >> x;
98 stream >> y;
99 stream >> z;
100 vector.setX(double(x));
101 vector.setY(double(y));
102 vector.setZ(double(z));
103 return stream;
104}
105
106#endif // QT_NO_DATASTREAM
107
108QT_END_NAMESPACE
Combined button and popup list for selecting options.
QDataStream & operator<<(QDataStream &stream, const QDoubleVector3D &vector)
QDebug operator<<(QDebug dbg, const QDoubleVector3D &vector)
QDataStream & operator>>(QDataStream &stream, QDoubleVector3D &vector)