QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
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  // TODO c++20 - replace with = default
64  bool operator==( const QgsVector3D &other ) const
65  {
66  return mX == other.mX && mY == other.mY && mZ == other.mZ;
67  }
68  bool operator!=( const QgsVector3D &other ) const
69  {
70  return !operator==( other );
71  }
72 
74  QgsVector3D operator+( const QgsVector3D &other ) const
75  {
76  return QgsVector3D( mX + other.mX, mY + other.mY, mZ + other.mZ );
77  }
78 
80  QgsVector3D operator-( const QgsVector3D &other ) const
81  {
82  return QgsVector3D( mX - other.mX, mY - other.mY, mZ - other.mZ );
83  }
84 
86  QgsVector3D operator *( const double factor ) const
87  {
88 
89  return QgsVector3D( mX * factor, mY * factor, mZ * factor );
90  }
91 
93  QgsVector3D operator /( const double factor ) const
94  {
95  return QgsVector3D( mX / factor, mY / factor, mZ / factor );
96  }
97 
99  static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 )
100  {
101  return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
102  }
103 
105  static QgsVector3D crossProduct( const QgsVector3D &v1, const QgsVector3D &v2 )
106  {
107  return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
108  v1.z() * v2.x() - v1.x() * v2.z(),
109  v1.x() * v2.y() - v1.y() * v2.x() );
110  }
111 
113  double length() const
114  {
115  return sqrt( mX * mX + mY * mY + mZ * mZ );
116  }
117 
119  void normalize()
120  {
121  const double len = length();
122  if ( !qgsDoubleNear( len, 0.0 ) )
123  {
124  mX /= len;
125  mY /= len;
126  mZ /= len;
127  }
128  }
129 
131  double distance( const QgsVector3D &other ) const
132  {
133  return std::sqrt( ( mX - other.x() ) * ( mX - other.x() ) +
134  ( mY - other.y() ) * ( mY - other.y() ) +
135  ( mZ - other.z() ) * ( mZ - other.z() ) );
136  }
137 
139  static QgsVector3D perpendicularPoint( const QgsVector3D &v1, const QgsVector3D &v2, const QgsVector3D &vp )
140  {
141  const QgsVector3D d = ( v2 - v1 ) / v2.distance( v1 );
142  const QgsVector3D v = vp - v2;
143  const double t = dotProduct( v, d );
144  QgsVector3D P = v2 + ( d * t );
145  return P;
146  }
147 
152  QString toString( int precision = 17 ) const
153  {
154  QString str = "Vector3D (";
155  str += qgsDoubleToString( mX, precision );
156  str += ", ";
157  str += qgsDoubleToString( mY, precision );
158  str += ", ";
159  str += qgsDoubleToString( mZ, precision );
160  str += ')';
161  return str;
162  }
163 
169  QVector3D toVector3D() const { return QVector3D( mX, mY, mZ ); }
170 
171 #ifdef SIP_RUN
172  SIP_PYOBJECT __repr__();
173  % MethodCode
174  QString str = QStringLiteral( "<QgsVector3D: %1>" ).arg( sipCpp->toString() );
175  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
176  % End
177 #endif
178  private:
179  double mX = 0, mY = 0, mZ = 0;
180 };
181 
182 #endif // QGSVECTOR3D_H
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:425
QgsVector3D::y
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:64
QgsVector3D
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition: qgsvector3d.h:31
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
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:430
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:2204
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsVector3D::z
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:66
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:2265
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::distance
double distance(const QgsVector3D &other) const
Returns the distance with the other QgsVector3.
Definition: qgsvector3d.h:144
str
#define str(x)
Definition: qgis.cpp:37
operator-
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
Definition: qgsinterval.cpp:299
operator+
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
Definition: qgsinterval.cpp:305
QgsVector3D::x
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:62