QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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
31 class 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 { return mX == 0 && mY == 0 && mZ == 0; }
47
49  double x() const { return mX; }
51  double y() const { return mY; }
53  double z() const { return mZ; }
54
56  void set( double x, double y, double z )
57  {
58  mX = x;
59  mY = y;
60  mZ = z;
61  }
62
63  bool operator==( const QgsVector3D &other ) const
64  {
65  return mX == other.mX && mY == other.mY && mZ == other.mZ;
66  }
67  bool operator!=( const QgsVector3D &other ) const
68  {
69  return !operator==( other );
70  }
71
73  QgsVector3D operator+( const QgsVector3D &other ) const
74  {
75  return QgsVector3D( mX + other.mX, mY + other.mY, mZ + other.mZ );
76  }
77
79  QgsVector3D operator-( const QgsVector3D &other ) const
80  {
81  return QgsVector3D( mX - other.mX, mY - other.mY, mZ - other.mZ );
82  }
83
85  QgsVector3D operator *( const double factor ) const
86  {
87
88  return QgsVector3D( mX * factor, mY * factor, mZ * factor );
89  }
90
92  QgsVector3D operator /( const double factor ) const
93  {
94  return QgsVector3D( mX / factor, mY / factor, mZ / factor );
95  }
96
98  static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 )
99  {
100  return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
101  }
102
104  static QgsVector3D crossProduct( const QgsVector3D &v1, const QgsVector3D &v2 )
105  {
106  return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
107  v1.z() * v2.x() - v1.x() * v2.z(),
108  v1.x() * v2.y() - v1.y() * v2.x() );
109  }
110
112  double length() const
113  {
114  return sqrt( mX * mX + mY * mY + mZ * mZ );
115  }
116
118  void normalize()
119  {
120  double len = length();
121  if ( !qgsDoubleNear( len, 0.0 ) )
122  {
123  mX /= len;
124  mY /= len;
125  mZ /= len;
126  }
127  }
128
130  double distance( const QgsVector3D &other ) const
131  {
132  return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) +
133  ( mY - other.y() ) * ( mY - other.y() ) +
134  ( mZ - other.z() ) * ( mZ - other.z() ) );
135  }
136
138  static QgsVector3D perpendicularPoint( const QgsVector3D &v1, const QgsVector3D &v2, const QgsVector3D &vp )
139  {
140  QgsVector3D d = ( v2 - v1 ) / v2.distance( v1 );
141  QgsVector3D v = vp - v2;
142  double t = dotProduct( v, d );
143  QgsVector3D P = v2 + ( d * t );
144  return P;
145  }
146
151  QString toString( int precision = 17 ) const
152  {
153  QString str = "Vector3D (";
154  str += qgsDoubleToString( mX, precision );
155  str += ", ";
156  str += qgsDoubleToString( mY, precision );
157  str += ", ";
158  str += qgsDoubleToString( mZ, precision );
159  str += ')';
160  return str;
161  }
162
163 #ifdef SIP_RUN
164  SIP_PYOBJECT __repr__();
165  % MethodCode
166  QString str = QStringLiteral( "<QgsVector3D: %1>" ).arg( sipCpp->toString() );
167  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
168  % End
169 #endif
170  private:
171  double mX = 0, mY = 0, mZ = 0;
172 };
173
174 #endif // QGSVECTOR3D_H
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:400
QgsVector3D::y
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:51
QgsVector3D
3 Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double preci...
Definition: qgsvector3d.h:32
QgsVector3D::toString
QString toString(int precision=17) const
Returns a string representation of the 3D vector.
Definition: qgsvector3d.h:151
QgsVector3D::normalize
void normalize()
Normalizes the current vector in place.
Definition: qgsvector3d.h:118
QgsVector3D::QgsVector3D
QgsVector3D()=default
Constructs a null vector.
qgis.h
operator/
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
QgsVector3D::set
void set(double x, double y, double z)
Sets vector coordinates.
Definition: qgsvector3d.h:56
QgsVector3D::operator-
QgsVector3D operator-(const QgsVector3D &other) const
Returns difference of two vectors.
Definition: qgsvector3d.h:79
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:275
QgsVector3D::QgsVector3D
QgsVector3D(const QVector3D &v)
Constructs a vector from single-precision QVector3D.
Definition: qgsvector3d.h:42
QgsVector3D::dotProduct
static double dotProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the dot product of two vectors.
Definition: qgsvector3d.h:98
precision
int precision
Definition: qgswfsgetfeature.cpp:49
QgsVector3D::z
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:53
QgsVector3D::perpendicularPoint
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:138
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsVector3D::crossProduct
static QgsVector3D crossProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the cross product of two vectors.
Definition: qgsvector3d.h:104
QgsVector3D::operator==
bool operator==(const QgsVector3D &other) const
Definition: qgsvector3d.h:63
operator*
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
QgsVector3D::isNull
bool isNull() const
Returns true if all three coordinates are zero.
Definition: qgsvector3d.h:46
QgsVector3D::distance
double distance(const QgsVector3D &other) const
Returns the distance with the other QgsVector3.
Definition: qgsvector3d.h:130
QgsVector3D::operator+
QgsVector3D operator+(const QgsVector3D &other) const
Returns sum of two vectors.
Definition: qgsvector3d.h:73
QgsVector3D::operator!=
bool operator!=(const QgsVector3D &other) const
Definition: qgsvector3d.h:67
QgsVector3D::QgsVector3D
QgsVector3D(double x, double y, double z)
Constructs a vector from given coordinates.
Definition: qgsvector3d.h:38
QgsVector3D::x
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:49
QgsVector3D::length
double length() const
Returns the length of the vector.
Definition: qgsvector3d.h:112