QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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.h"
20#include "qgis_core.h"
21
22#include <QString>
23#include <QVector3D>
24
25using namespace Qt::StringLiterals;
26
32class CORE_EXPORT QgsVector3D
33{
34 public:
36 QgsVector3D() = default;
37
39 QgsVector3D( double x, double y, double z )
40 : mX( x ), mY( y ), mZ( z ) {}
41
43 QgsVector3D( const QVector3D &v )
44 : mX( v.x() ), mY( v.y() ), mZ( v.z() ) {}
45
47 bool isNull() const SIP_HOLDGIL { return mX == 0 && mY == 0 && mZ == 0; }
48
50 double x() const SIP_HOLDGIL { return mX; }
52 double y() const SIP_HOLDGIL { return mY; }
54 double z() const SIP_HOLDGIL { return mZ; }
55
60 void setX( double x ) SIP_HOLDGIL { mX = x; }
61
66 void setY( double y ) SIP_HOLDGIL { mY = y; }
67
72 void setZ( double z ) SIP_HOLDGIL { mZ = z; }
73
75 void set( double x, double y, double z ) SIP_HOLDGIL
76 {
77 mX = x;
78 mY = y;
79 mZ = z;
80 }
81
82 // TODO c++20 - replace with = default
83 bool operator==( const QgsVector3D &other ) const SIP_HOLDGIL
84 {
85 return mX == other.mX && mY == other.mY && mZ == other.mZ;
86 }
87 bool operator!=( const QgsVector3D &other ) const SIP_HOLDGIL
88 {
89 return !operator==( other );
90 }
91
94 {
95 return QgsVector3D( mX + other.mX, mY + other.mY, mZ + other.mZ );
96 }
97
104 {
105 mX += other.mX;
106 mY += other.mY;
107 mZ += other.mZ;
108 return *this;
109 }
110
113 {
114 return QgsVector3D( mX - other.mX, mY - other.mY, mZ - other.mZ );
115 }
116
123 {
124 return QgsVector3D( -mX, -mY, -mZ );
125 }
126
128 QgsVector3D operator *( const double factor ) const SIP_HOLDGIL
129 {
130
131 return QgsVector3D( mX * factor, mY * factor, mZ * factor );
132 }
133
135 QgsVector3D operator /( const double factor ) const SIP_HOLDGIL
136 {
137 return QgsVector3D( mX / factor, mY / factor, mZ / factor );
138 }
139
141 static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 ) SIP_HOLDGIL
142 {
143 return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
144 }
145
148 {
149 return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
150 v1.z() * v2.x() - v1.x() * v2.z(),
151 v1.x() * v2.y() - v1.y() * v2.x() );
152 }
153
158 double length() const SIP_HOLDGIL
159 {
160 return sqrt( mX * mX + mY * mY + mZ * mZ );
161 }
162
170 {
171 return mX * mX + mY * mY + mZ * mZ;
172 }
173
176 {
177 const double len = length();
178 if ( !qgsDoubleNear( len, 0.0 ) )
179 {
180 mX /= len;
181 mY /= len;
182 mZ /= len;
183 }
184 }
185
187 double distance( const QgsVector3D &other ) const SIP_HOLDGIL
188 {
189 return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) +
190 ( mY - other.y() ) * ( mY - other.y() ) +
191 ( mZ - other.z() ) * ( mZ - other.z() ) );
192 }
193
196 {
197 const QgsVector3D d = ( v2 - v1 ) / v2.distance( v1 );
198 const QgsVector3D v = vp - v2;
199 const double t = dotProduct( v, d );
200 QgsVector3D P = v2 + ( d * t );
201 return P;
202 }
203
208 QString toString( int precision = 17 ) const SIP_HOLDGIL
209 {
210 QString str = "Vector3D (";
211 str += qgsDoubleToString( mX, precision );
212 str += ", ";
213 str += qgsDoubleToString( mY, precision );
214 str += ", ";
215 str += qgsDoubleToString( mZ, precision );
216 str += ')';
217 return str;
218 }
219
225 QVector3D toVector3D() const SIP_HOLDGIL { return QVector3D( static_cast< float >( mX ), static_cast< float >( mY ), static_cast< float >( mZ ) ); }
226
227#ifdef SIP_RUN
228 SIP_PYOBJECT __repr__();
229 % MethodCode
230 QString str = u"<QgsVector3D: %1>"_s.arg( sipCpp->toString() );
231 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
232 % End
233#endif
234 private:
235 double mX = 0, mY = 0, mZ = 0;
236};
237
238#endif // QGSVECTOR3D_H
double lengthSquared() const
Returns the squared length of the vector.
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:52
bool operator!=(const QgsVector3D &other) const
Definition qgsvector3d.h:87
bool operator==(const QgsVector3D &other) const
Definition qgsvector3d.h:83
QgsVector3D & operator+=(const QgsVector3D &other)
Adds another vector to this vector in place.
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:54
void setZ(double z)
Sets Z coordinate.
Definition qgsvector3d.h:72
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:47
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:39
QgsVector3D(const QVector3D &v)
Constructs a vector from single-precision QVector3D.
Definition qgsvector3d.h:43
QgsVector3D operator+(const QgsVector3D &other) const
Returns sum of two vectors.
Definition qgsvector3d.h:93
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:50
QgsVector3D operator-(const QgsVector3D &other) const
Returns difference of two vectors.
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:60
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:75
void setY(double y)
Sets Y coordinate.
Definition qgsvector3d.h:66
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:6817
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900
#define SIP_HOLDGIL
Definition qgis_sip.h:179
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:252
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:272