QGIS API Documentation 3.43.0-Master (c67cf405802)
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
29class CORE_EXPORT QgsVector3D
30{
31 public:
33 QgsVector3D() = default;
34
36 QgsVector3D( double x, double y, double z )
37 : mX( x ), mY( y ), mZ( z ) {}
38
40 QgsVector3D( const QVector3D &v )
41 : mX( v.x() ), mY( v.y() ), mZ( v.z() ) {}
42
44 bool isNull() const SIP_HOLDGIL { return mX == 0 && mY == 0 && mZ == 0; }
45
47 double x() const SIP_HOLDGIL { return mX; }
49 double y() const SIP_HOLDGIL { return mY; }
51 double z() const SIP_HOLDGIL { return mZ; }
52
57 void setX( double x ) SIP_HOLDGIL { mX = x; }
58
63 void setY( double y ) SIP_HOLDGIL { mY = y; }
64
69 void setZ( double z ) SIP_HOLDGIL { mZ = z; }
70
72 void set( double x, double y, double z ) SIP_HOLDGIL
73 {
74 mX = x;
75 mY = y;
76 mZ = z;
77 }
78
79 // TODO c++20 - replace with = default
80 bool operator==( const QgsVector3D &other ) const SIP_HOLDGIL
81 {
82 return mX == other.mX && mY == other.mY && mZ == other.mZ;
83 }
84 bool operator!=( const QgsVector3D &other ) const SIP_HOLDGIL
85 {
86 return !operator==( other );
87 }
88
91 {
92 return QgsVector3D( mX + other.mX, mY + other.mY, mZ + other.mZ );
93 }
94
97 {
98 return QgsVector3D( mX - other.mX, mY - other.mY, mZ - other.mZ );
99 }
100
107 {
108 return QgsVector3D( -mX, -mY, -mZ );
109 }
110
112 QgsVector3D operator *( const double factor ) const SIP_HOLDGIL
113 {
114
115 return QgsVector3D( mX * factor, mY * factor, mZ * factor );
116 }
117
119 QgsVector3D operator /( const double factor ) const SIP_HOLDGIL
120 {
121 return QgsVector3D( mX / factor, mY / factor, mZ / factor );
122 }
123
125 static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 ) SIP_HOLDGIL
126 {
127 return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
128 }
129
132 {
133 return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
134 v1.z() * v2.x() - v1.x() * v2.z(),
135 v1.x() * v2.y() - v1.y() * v2.x() );
136 }
137
139 double length() const SIP_HOLDGIL
140 {
141 return sqrt( mX * mX + mY * mY + mZ * mZ );
142 }
143
146 {
147 const double len = length();
148 if ( !qgsDoubleNear( len, 0.0 ) )
149 {
150 mX /= len;
151 mY /= len;
152 mZ /= len;
153 }
154 }
155
157 double distance( const QgsVector3D &other ) const SIP_HOLDGIL
158 {
159 return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) +
160 ( mY - other.y() ) * ( mY - other.y() ) +
161 ( mZ - other.z() ) * ( mZ - other.z() ) );
162 }
163
166 {
167 const QgsVector3D d = ( v2 - v1 ) / v2.distance( v1 );
168 const QgsVector3D v = vp - v2;
169 const double t = dotProduct( v, d );
170 QgsVector3D P = v2 + ( d * t );
171 return P;
172 }
173
178 QString toString( int precision = 17 ) const SIP_HOLDGIL
179 {
180 QString str = "Vector3D (";
181 str += qgsDoubleToString( mX, precision );
182 str += ", ";
183 str += qgsDoubleToString( mY, precision );
184 str += ", ";
185 str += qgsDoubleToString( mZ, precision );
186 str += ')';
187 return str;
188 }
189
195 QVector3D toVector3D() const SIP_HOLDGIL { return QVector3D( static_cast< float >( mX ), static_cast< float >( mY ), static_cast< float >( mZ ) ); }
196
197#ifdef SIP_RUN
198 SIP_PYOBJECT __repr__();
199 % MethodCode
200 QString str = QStringLiteral( "<QgsVector3D: %1>" ).arg( sipCpp->toString() );
201 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
202 % End
203#endif
204 private:
205 double mX = 0, mY = 0, mZ = 0;
206};
207
208#endif // QGSVECTOR3D_H
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:30
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:49
bool operator!=(const QgsVector3D &other) const
Definition qgsvector3d.h:84
bool operator==(const QgsVector3D &other) const
Definition qgsvector3d.h:80
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:51
void setZ(double z)
Sets Z coordinate.
Definition qgsvector3d.h:69
QString toString(int precision=17) const
Returns a string representation of the 3D vector.
bool isNull() const
Returns true if all three coordinates are zero.
Definition qgsvector3d.h:44
double distance(const QgsVector3D &other) const
Returns the distance with the other QgsVector3D.
QVector3D toVector3D() const
Converts the current object to QVector3D.
QgsVector3D(double x, double y, double z)
Constructs a vector from given coordinates.
Definition qgsvector3d.h:36
QgsVector3D(const QVector3D &v)
Constructs a vector from single-precision QVector3D.
Definition qgsvector3d.h:40
QgsVector3D operator+(const QgsVector3D &other) const
Returns sum of two vectors.
Definition qgsvector3d.h:90
static double dotProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the dot product of two vectors.
double x() const
Returns X coordinate.
Definition qgsvector3d.h:47
QgsVector3D operator-(const QgsVector3D &other) const
Returns difference of two vectors.
Definition qgsvector3d.h:96
static QgsVector3D perpendicularPoint(const QgsVector3D &v1, const QgsVector3D &v2, const QgsVector3D &vp)
Returns the perpendicular point of vector vp from [v1 - v2].
void setX(double x)
Sets X coordinate.
Definition qgsvector3d.h:57
void normalize()
Normalizes the current vector in place.
const QgsVector3D operator-() const
Swaps the sign of the components of the vector.
static QgsVector3D crossProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the cross product of two vectors.
void set(double x, double y, double z)
Sets vector coordinates.
Definition qgsvector3d.h:72
void setY(double y)
Sets Y coordinate.
Definition qgsvector3d.h:63
double length() const
Returns the length of the vector.
QgsVector3D()=default
Constructs a null vector.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6203
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6286
#define SIP_HOLDGIL
Definition qgis_sip.h:171
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:249
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:269
int precision