QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsvector3d.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsvector3d.h
3 --------------------------------------
4 Date : November 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSVECTOR3D_H
17#define QGSVECTOR3D_H
18
19#include "qgis_core.h"
20#include "qgis.h"
21
22#include <QVector3D>
23
31class CORE_EXPORT QgsVector3D
32{
33 public:
35 QgsVector3D() = default;
36
38 QgsVector3D( double x, double y, double z )
39 : mX( x ), mY( y ), mZ( z ) {}
40
42 QgsVector3D( const QVector3D &v )
43 : mX( v.x() ), mY( v.y() ), mZ( v.z() ) {}
44
46 bool isNull() const { return mX == 0 && mY == 0 && mZ == 0; }
47
49 double x() const { return mX; }
51 double y() const { return mY; }
53 double z() const { return mZ; }
54
56 void set( double x, double y, double z )
57 {
58 mX = x;
59 mY = y;
60 mZ = z;
61 }
62
63 // TODO c++20 - replace with = default
64 bool operator==( const QgsVector3D &other ) const
65 {
66 return mX == other.mX && mY == other.mY && mZ == other.mZ;
67 }
68 bool operator!=( const QgsVector3D &other ) const
69 {
70 return !operator==( other );
71 }
72
74 QgsVector3D operator+( const QgsVector3D &other ) const
75 {
76 return QgsVector3D( mX + other.mX, mY + other.mY, mZ + other.mZ );
77 }
78
80 QgsVector3D operator-( const QgsVector3D &other ) const
81 {
82 return QgsVector3D( mX - other.mX, mY - other.mY, mZ - other.mZ );
83 }
84
86 QgsVector3D operator *( const double factor ) const
87 {
88
89 return QgsVector3D( mX * factor, mY * factor, mZ * factor );
90 }
91
93 QgsVector3D operator /( const double factor ) const
94 {
95 return QgsVector3D( mX / factor, mY / factor, mZ / factor );
96 }
97
99 static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 )
100 {
101 return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
102 }
103
105 static QgsVector3D crossProduct( const QgsVector3D &v1, const QgsVector3D &v2 )
106 {
107 return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
108 v1.z() * v2.x() - v1.x() * v2.z(),
109 v1.x() * v2.y() - v1.y() * v2.x() );
110 }
111
113 double length() const
114 {
115 return sqrt( mX * mX + mY * mY + mZ * mZ );
116 }
117
120 {
121 const double len = length();
122 if ( !qgsDoubleNear( len, 0.0 ) )
123 {
124 mX /= len;
125 mY /= len;
126 mZ /= len;
127 }
128 }
129
131 double distance( const QgsVector3D &other ) const
132 {
133 return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) +
134 ( mY - other.y() ) * ( mY - other.y() ) +
135 ( mZ - other.z() ) * ( mZ - other.z() ) );
136 }
137
139 static QgsVector3D perpendicularPoint( const QgsVector3D &v1, const QgsVector3D &v2, const QgsVector3D &vp )
140 {
141 const QgsVector3D d = ( v2 - v1 ) / v2.distance( v1 );
142 const QgsVector3D v = vp - v2;
143 const double t = dotProduct( v, d );
144 QgsVector3D P = v2 + ( d * t );
145 return P;
146 }
147
152 QString toString( int precision = 17 ) const
153 {
154 QString str = "Vector3D (";
156 str += ", ";
158 str += ", ";
160 str += ')';
161 return str;
162 }
163
169 QVector3D toVector3D() const { return QVector3D( mX, mY, mZ ); }
170
171#ifdef SIP_RUN
172 SIP_PYOBJECT __repr__();
173 % MethodCode
174 QString str = QStringLiteral( "<QgsVector3D: %1>" ).arg( sipCpp->toString() );
175 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
176 % End
177#endif
178 private:
179 double mX = 0, mY = 0, mZ = 0;
180};
181
182#endif // QGSVECTOR3D_H
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:51
bool operator!=(const QgsVector3D &other) const
Definition: qgsvector3d.h:68
bool operator==(const QgsVector3D &other) const
Definition: qgsvector3d.h:64
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:53
QString toString(int precision=17) const
Returns a string representation of the 3D vector.
Definition: qgsvector3d.h:152
bool isNull() const
Returns true if all three coordinates are zero.
Definition: qgsvector3d.h:46
double distance(const QgsVector3D &other) const
Returns the distance with the other QgsVector3.
Definition: qgsvector3d.h:131
QVector3D toVector3D() const
Converts the current object to QVector3D.
Definition: qgsvector3d.h:169
QgsVector3D(double x, double y, double z)
Constructs a vector from given coordinates.
Definition: qgsvector3d.h:38
QgsVector3D(const QVector3D &v)
Constructs a vector from single-precision QVector3D.
Definition: qgsvector3d.h:42
QgsVector3D operator+(const QgsVector3D &other) const
Returns sum of two vectors.
Definition: qgsvector3d.h:74
static double dotProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the dot product of two vectors.
Definition: qgsvector3d.h:99
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:49
QgsVector3D operator-(const QgsVector3D &other) const
Returns difference of two vectors.
Definition: qgsvector3d.h:80
static QgsVector3D perpendicularPoint(const QgsVector3D &v1, const QgsVector3D &v2, const QgsVector3D &vp)
Returns the perpendicular point of vector vp from [v1 - v2].
Definition: qgsvector3d.h:139
void normalize()
Normalizes the current vector in place.
Definition: qgsvector3d.h:119
static QgsVector3D crossProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the cross product of two vectors.
Definition: qgsvector3d.h:105
void set(double x, double y, double z)
Sets vector coordinates.
Definition: qgsvector3d.h:56
double length() const
Returns the length of the vector.
Definition: qgsvector3d.h:113
QgsVector3D()=default
Constructs a null vector.
#define str(x)
Definition: qgis.cpp:37
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:3448
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3509
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsMargins operator*(const QgsMargins &margins, double factor)
Returns a QgsMargins object that is formed by multiplying each component of the given margins by fact...
Definition: qgsmargins.h:242
QgsMargins operator/(const QgsMargins &margins, double divisor)
Returns a QgsMargins object that is formed by dividing the components of the given margins by the giv...
Definition: qgsmargins.h:262
int precision