QGIS API Documentation 3.41.0-Master (af5edcb665c)
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_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
108 {
109 return QgsVector3D( -mX, -mY, -mZ );
110 }
111
113 QgsVector3D operator *( const double factor ) const SIP_HOLDGIL
114 {
115
116 return QgsVector3D( mX * factor, mY * factor, mZ * factor );
117 }
118
120 QgsVector3D operator /( const double factor ) const SIP_HOLDGIL
121 {
122 return QgsVector3D( mX / factor, mY / factor, mZ / factor );
123 }
124
126 static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 ) SIP_HOLDGIL
127 {
128 return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
129 }
130
133 {
134 return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
135 v1.z() * v2.x() - v1.x() * v2.z(),
136 v1.x() * v2.y() - v1.y() * v2.x() );
137 }
138
140 double length() const SIP_HOLDGIL
141 {
142 return sqrt( mX * mX + mY * mY + mZ * mZ );
143 }
144
147 {
148 const double len = length();
149 if ( !qgsDoubleNear( len, 0.0 ) )
150 {
151 mX /= len;
152 mY /= len;
153 mZ /= len;
154 }
155 }
156
158 double distance( const QgsVector3D &other ) const SIP_HOLDGIL
159 {
160 return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) +
161 ( mY - other.y() ) * ( mY - other.y() ) +
162 ( mZ - other.z() ) * ( mZ - other.z() ) );
163 }
164
167 {
168 const QgsVector3D d = ( v2 - v1 ) / v2.distance( v1 );
169 const QgsVector3D v = vp - v2;
170 const double t = dotProduct( v, d );
171 QgsVector3D P = v2 + ( d * t );
172 return P;
173 }
174
179 QString toString( int precision = 17 ) const SIP_HOLDGIL
180 {
181 QString str = "Vector3D (";
182 str += qgsDoubleToString( mX, precision );
183 str += ", ";
184 str += qgsDoubleToString( mY, precision );
185 str += ", ";
186 str += qgsDoubleToString( mZ, precision );
187 str += ')';
188 return str;
189 }
190
196 QVector3D toVector3D() const SIP_HOLDGIL { return QVector3D( static_cast< float >( mX ), static_cast< float >( mY ), static_cast< float >( mZ ) ); }
197
198#ifdef SIP_RUN
199 SIP_PYOBJECT __repr__();
200 % MethodCode
201 QString str = QStringLiteral( "<QgsVector3D: %1>" ).arg( sipCpp->toString() );
202 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
203 % End
204#endif
205 private:
206 double mX = 0, mY = 0, mZ = 0;
207};
208
209#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.
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.
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: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.
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].
void setX(double x)
Sets X coordinate.
Definition qgsvector3d.h:58
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:73
void setY(double y)
Sets Y coordinate.
Definition qgsvector3d.h:64
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:5983
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6066
#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