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
129
130  private:
131  double mX = 0, mY = 0, mZ = 0;
132 };
133
134 #endif // QGSVECTOR3D_H
3 Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double preci...
Definition: qgsvector3d.h:31
bool operator!=(const QgsVector3D &other) const
Definition: qgsvector3d.h:67
QgsVector3D operator+(const QgsVector3D &other) const
Returns sum of two vectors.
Definition: qgsvector3d.h:73
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:278
double length() const
Returns the length of the vector.
Definition: qgsvector3d.h:112
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:53
QgsVector3D operator-(const QgsVector3D &other) const
Returns difference of two vectors.
Definition: qgsvector3d.h:79
static double dotProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the dot product of two vectors.
Definition: qgsvector3d.h:98
static QgsVector3D crossProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the cross product of two vectors.
Definition: qgsvector3d.h:104
void normalize()
Normalizes the current vector in place.
Definition: qgsvector3d.h:118
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:49
QgsVector3D(const QVector3D &v)
Constructs a vector from single-precision QVector3D.
Definition: qgsvector3d.h:42
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
bool isNull() const
Returns true if all three coordinates are zero.
Definition: qgsvector3d.h:46
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:51
bool operator==(const QgsVector3D &other) const
Definition: qgsvector3d.h:63
QgsVector3D(double x, double y, double z)
Constructs a vector from given coordinates.
Definition: qgsvector3d.h:38
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