QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
29 class CORE_EXPORT QgsVector3D
30 {
31  public:
33  QgsVector3D() = default;
34 
36  QgsVector3D( double x, double y, double z )
37  : mX( x ), mY( y ), mZ( z ) {}
38 
40  bool isNull() const { return mX == 0 && mY == 0 && mZ == 0; }
41 
43  double x() const { return mX; }
45  double y() const { return mY; }
47  double z() const { return mZ; }
48 
50  void set( double x, double y, double z )
51  {
52  mX = x;
53  mY = y;
54  mZ = z;
55  }
56 
57  bool operator==( const QgsVector3D &other ) const
58  {
59  return mX == other.mX && mY == other.mY && mZ == other.mZ;
60  }
61  bool operator!=( const QgsVector3D &other ) const
62  {
63  return !operator==( other );
64  }
65 
67  QgsVector3D operator+( const QgsVector3D &other ) const
68  {
69  return QgsVector3D( mX + other.mX, mY + other.mY, mZ + other.mZ );
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 double factor ) const
80  {
81 
82  return QgsVector3D( mX * factor, mY * factor, mZ * factor );
83  }
84 
86  QgsVector3D operator /( const double factor ) const
87  {
88  return QgsVector3D( mX / factor, mY / factor, mZ / factor );
89  }
90 
92  static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2 )
93  {
94  return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
95  }
96 
98  static QgsVector3D crossProduct( const QgsVector3D &v1, const QgsVector3D &v2 )
99  {
100  return QgsVector3D( v1.y() * v2.z() - v1.z() * v2.y(),
101  v1.z() * v2.x() - v1.x() * v2.z(),
102  v1.x() * v2.y() - v1.y() * v2.x() );
103  }
104 
106  double length() const
107  {
108  return sqrt( mX * mX + mY * mY + mZ * mZ );
109  }
110 
112  void normalize()
113  {
114  double len = length();
115  if ( !qgsDoubleNear( len, 0.0 ) )
116  {
117  mX /= len;
118  mY /= len;
119  mZ /= len;
120  }
121  }
122 
123 
124  private:
125  double mX = 0, mY = 0, mZ = 0;
126 };
127 
128 #endif // QGSVECTOR3D_H
3 Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double preci...
Definition: qgsvector3d.h:29
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double length() const
Returns the length of the vector.
Definition: qgsvector3d.h:106
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:251
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:45
static double dotProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the dot product of two vectors.
Definition: qgsvector3d.h:92
static QgsVector3D crossProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the cross product of two vectors.
Definition: qgsvector3d.h:98
void normalize()
Normalizes the current vector in place.
Definition: qgsvector3d.h:112
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:47
QgsVector3D operator-(const QgsVector3D &other) const
Returns difference of two vectors.
Definition: qgsvector3d.h:73
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 operator+(const QgsVector3D &other) const
Returns sum of two vectors.
Definition: qgsvector3d.h:67
bool isNull() const
Returns true if all three coordinates are zero.
Definition: qgsvector3d.h:40
bool operator==(const QgsVector3D &other) const
Definition: qgsvector3d.h:57
bool operator!=(const QgsVector3D &other) const
Definition: qgsvector3d.h:61
QgsVector3D(double x, double y, double z)
Constructs a vector from given coordinates.
Definition: qgsvector3d.h:36
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
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:43