QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 *
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 SIP_HOLDGIL { return mX == 0 && mY == 0 && mZ == 0; }
47
49 double x() const SIP_HOLDGIL { return mX; }
51 double y() const SIP_HOLDGIL { return mY; }
53 double z() const SIP_HOLDGIL { return mZ; }
54
59 void setX( double x ) SIP_HOLDGIL { mX = x; }
60
65 void setY( double y ) SIP_HOLDGIL { mY = y; }
66
71 void setZ( double z ) SIP_HOLDGIL { mZ = z; }
72
74 void set( double x, double y, double z ) SIP_HOLDGIL
75 {
76 mX = x;
77 mY = y;
78 mZ = z;
79 }
80
81 // TODO c++20 - replace with = default
82 bool operator==( const QgsVector3D &other ) const SIP_HOLDGIL
83 {
84 return mX == other.mX && mY == other.mY && mZ == other.mZ;
85 }
86 bool operator!=( const QgsVector3D &other ) const SIP_HOLDGIL
87 {
88 return !operator==( other );
89 }
90
93 {
94 return QgsVector3D( mX + other.mX, mY + other.mY, mZ + other.mZ );
95 }
96
99 {
100 return QgsVector3D( mX - other.mX, mY - other.mY, mZ - other.mZ );
101 }
102
104 QgsVector3D operator *( const double factor ) const SIP_HOLDGIL
105 {
106
107 return QgsVector3D( mX * factor, mY * factor, mZ * factor );
108 }
109
111 QgsVector3D operator /( const double factor ) const SIP_HOLDGIL
112 {
113 return QgsVector3D( mX / factor, mY / factor, mZ / factor );
114 }
115
117 static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 ) SIP_HOLDGIL
118 {
119 return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
120 }
121
124 {
125 return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
126 v1.z() * v2.x() - v1.x() * v2.z(),
127 v1.x() * v2.y() - v1.y() * v2.x() );
128 }
129
131 double length() const SIP_HOLDGIL
132 {
133 return sqrt( mX * mX + mY * mY + mZ * mZ );
134 }
135
138 {
139 const double len = length();
140 if ( !qgsDoubleNear( len, 0.0 ) )
141 {
142 mX /= len;
143 mY /= len;
144 mZ /= len;
145 }
146 }
147
149 double distance( const QgsVector3D &other ) const SIP_HOLDGIL
150 {
151 return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) +
152 ( mY - other.y() ) * ( mY - other.y() ) +
153 ( mZ - other.z() ) * ( mZ - other.z() ) );
154 }
155
158 {
159 const QgsVector3D d = ( v2 - v1 ) / v2.distance( v1 );
160 const QgsVector3D v = vp - v2;
161 const double t = dotProduct( v, d );
162 QgsVector3D P = v2 + ( d * t );
163 return P;
164 }
165
170 QString toString( int precision = 17 ) const SIP_HOLDGIL
171 {
172 QString str = "Vector3D (";
174 str += ", ";
176 str += ", ";
178 str += ')';
179 return str;
180 }
181
187 QVector3D toVector3D() const SIP_HOLDGIL { return QVector3D( static_cast< float >( mX ), static_cast< float >( mY ), static_cast< float >( mZ ) ); }
188
189#ifdef SIP_RUN
190 SIP_PYOBJECT __repr__();
191 % MethodCode
192 QString str = QStringLiteral( "<QgsVector3D: %1>" ).arg( sipCpp->toString() );
193 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
194 % End
195#endif
196 private:
197 double mX = 0, mY = 0, mZ = 0;
198};
199
200#endif // QGSVECTOR3D_H
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:32
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:51
bool operator!=(const QgsVector3D &other) const
Definition qgsvector3d.h:86
bool operator==(const QgsVector3D &other) const
Definition qgsvector3d.h:82
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:53
void setZ(double z)
Sets Z coordinate.
Definition qgsvector3d.h:71
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:46
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: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:92
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:49
QgsVector3D operator-(const QgsVector3D &other) const
Returns difference of two vectors.
Definition qgsvector3d.h:98
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:59
void normalize()
Normalizes the current vector in place.
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:74
void setY(double y)
Sets Y coordinate.
Definition qgsvector3d.h:65
double length() const
Returns the length of the vector.
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:4271
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:4332
#define SIP_HOLDGIL
Definition qgis_sip.h:166
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