QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
30class CORE_EXPORT QgsVector3D
31{
32 public:
34 QgsVector3D() = default;
35
37 QgsVector3D( double x, double y, double z )
38 : mX( x ), mY( y ), mZ( z ) {}
39
41 QgsVector3D( const QVector3D &v )
42 : mX( v.x() ), mY( v.y() ), mZ( v.z() ) {}
43
45 bool isNull() const SIP_HOLDGIL { return mX == 0 && mY == 0 && mZ == 0; }
46
48 double x() const SIP_HOLDGIL { return mX; }
50 double y() const SIP_HOLDGIL { return mY; }
52 double z() const SIP_HOLDGIL { return mZ; }
53
58 void setX( double x ) SIP_HOLDGIL { mX = x; }
59
64 void setY( double y ) SIP_HOLDGIL { mY = y; }
65
70 void setZ( double z ) SIP_HOLDGIL { mZ = z; }
71
73 void set( double x, double y, double z ) SIP_HOLDGIL
74 {
75 mX = x;
76 mY = y;
77 mZ = z;
78 }
79
80 // TODO c++20 - replace with = default
81 bool operator==( const QgsVector3D &other ) const SIP_HOLDGIL
82 {
83 return mX == other.mX && mY == other.mY && mZ == other.mZ;
84 }
85 bool operator!=( const QgsVector3D &other ) const SIP_HOLDGIL
86 {
87 return !operator==( other );
88 }
89
92 {
93 return QgsVector3D( mX + other.mX, mY + other.mY, mZ + other.mZ );
94 }
95
98 {
99 return QgsVector3D( mX - other.mX, mY - other.mY, mZ - other.mZ );
100 }
101
103 QgsVector3D operator *( const double factor ) const SIP_HOLDGIL
104 {
105
106 return QgsVector3D( mX * factor, mY * factor, mZ * factor );
107 }
108
110 QgsVector3D operator /( const double factor ) const SIP_HOLDGIL
111 {
112 return QgsVector3D( mX / factor, mY / factor, mZ / factor );
113 }
114
116 static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 ) SIP_HOLDGIL
117 {
118 return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
119 }
120
123 {
124 return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
125 v1.z() * v2.x() - v1.x() * v2.z(),
126 v1.x() * v2.y() - v1.y() * v2.x() );
127 }
128
130 double length() const SIP_HOLDGIL
131 {
132 return sqrt( mX * mX + mY * mY + mZ * mZ );
133 }
134
137 {
138 const double len = length();
139 if ( !qgsDoubleNear( len, 0.0 ) )
140 {
141 mX /= len;
142 mY /= len;
143 mZ /= len;
144 }
145 }
146
148 double distance( const QgsVector3D &other ) const SIP_HOLDGIL
149 {
150 return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) +
151 ( mY - other.y() ) * ( mY - other.y() ) +
152 ( mZ - other.z() ) * ( mZ - other.z() ) );
153 }
154
157 {
158 const QgsVector3D d = ( v2 - v1 ) / v2.distance( v1 );
159 const QgsVector3D v = vp - v2;
160 const double t = dotProduct( v, d );
161 QgsVector3D P = v2 + ( d * t );
162 return P;
163 }
164
169 QString toString( int precision = 17 ) const SIP_HOLDGIL
170 {
171 QString str = "Vector3D (";
173 str += ", ";
175 str += ", ";
177 str += ')';
178 return str;
179 }
180
186 QVector3D toVector3D() const SIP_HOLDGIL { return QVector3D( static_cast< float >( mX ), static_cast< float >( mY ), static_cast< float >( mZ ) ); }
187
188#ifdef SIP_RUN
189 SIP_PYOBJECT __repr__();
190 % MethodCode
191 QString str = QStringLiteral( "<QgsVector3D: %1>" ).arg( sipCpp->toString() );
192 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
193 % End
194#endif
195 private:
196 double mX = 0, mY = 0, mZ = 0;
197};
198
199#endif // QGSVECTOR3D_H
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition: qgsvector3d.h:31
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:50
bool operator!=(const QgsVector3D &other) const
Definition: qgsvector3d.h:85
bool operator==(const QgsVector3D &other) const
Definition: qgsvector3d.h:81
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:52
void setZ(double z)
Sets Z coordinate.
Definition: qgsvector3d.h:70
QString toString(int precision=17) const
Returns a string representation of the 3D vector.
Definition: qgsvector3d.h:169
bool isNull() const
Returns true if all three coordinates are zero.
Definition: qgsvector3d.h:45
double distance(const QgsVector3D &other) const
Returns the distance with the other QgsVector3D.
Definition: qgsvector3d.h:148
QVector3D toVector3D() const
Converts the current object to QVector3D.
Definition: qgsvector3d.h:186
QgsVector3D(double x, double y, double z)
Constructs a vector from given coordinates.
Definition: qgsvector3d.h:37
QgsVector3D(const QVector3D &v)
Constructs a vector from single-precision QVector3D.
Definition: qgsvector3d.h:41
QgsVector3D operator+(const QgsVector3D &other) const
Returns sum of two vectors.
Definition: qgsvector3d.h:91
static double dotProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the dot product of two vectors.
Definition: qgsvector3d.h:116
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:48
QgsVector3D operator-(const QgsVector3D &other) const
Returns difference of two vectors.
Definition: qgsvector3d.h:97
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:156
void setX(double x)
Sets X coordinate.
Definition: qgsvector3d.h:58
void normalize()
Normalizes the current vector in place.
Definition: qgsvector3d.h:136
static QgsVector3D crossProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the cross product of two vectors.
Definition: qgsvector3d.h:122
void set(double x, double y, double z)
Sets vector coordinates.
Definition: qgsvector3d.h:73
void setY(double y)
Sets Y coordinate.
Definition: qgsvector3d.h:64
double length() const
Returns the length of the vector.
Definition: qgsvector3d.h:130
QgsVector3D()=default
Constructs a null vector.
#define str(x)
Definition: qgis.cpp:38
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:5124
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:5207
#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:241
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:261
int precision