Skip to content

Commit 6a570f3

Browse files
committedOct 16, 2020
Move QgsVector methods to header to allow better compiler optimisation,
also hold Python GIL when calling them
1 parent 2a15c3b commit 6a570f3

File tree

3 files changed

+108
-143
lines changed

3 files changed

+108
-143
lines changed
 

‎python/core/auto_generated/qgsvector.sip.in

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,30 @@ Constructor for QgsVector taking x and y component values.
3333
:param y: y-component
3434
%End
3535

36-
QgsVector operator-() const;
36+
QgsVector operator-() const /HoldGIL/;
3737

38-
QgsVector operator*( double scalar ) const;
38+
QgsVector operator*( double scalar ) const /HoldGIL/;
3939

40-
QgsVector operator/( double scalar ) const;
40+
QgsVector operator/( double scalar ) const /HoldGIL/;
4141

42-
double operator*( QgsVector v ) const;
42+
double operator*( QgsVector v ) const /HoldGIL/;
4343

44-
QgsVector operator+( QgsVector other ) const;
44+
QgsVector operator+( QgsVector other ) const /HoldGIL/;
4545

46-
QgsVector &operator+=( QgsVector other );
46+
QgsVector &operator+=( QgsVector other ) /HoldGIL/;
4747

48-
QgsVector operator-( QgsVector other ) const;
48+
QgsVector operator-( QgsVector other ) const /HoldGIL/;
4949

50-
QgsVector &operator-=( QgsVector other );
50+
QgsVector &operator-=( QgsVector other ) /HoldGIL/;
5151

52-
double length() const;
52+
double length() const /HoldGIL/;
5353
%Docstring
5454
Returns the length of the vector.
5555

5656
.. seealso:: :py:func:`lengthSquared`
5757
%End
5858

59-
double lengthSquared() const;
59+
double lengthSquared() const /HoldGIL/;
6060
%Docstring
6161
Returns the length of the vector.
6262

@@ -65,62 +65,62 @@ Returns the length of the vector.
6565
.. versionadded:: 3.2
6666
%End
6767

68-
double x() const;
68+
double x() const /HoldGIL/;
6969
%Docstring
7070
Returns the vector's x-component.
7171

7272
.. seealso:: y
7373
%End
7474

75-
double y() const;
75+
double y() const /HoldGIL/;
7676
%Docstring
7777
Returns the vector's y-component.
7878

7979
.. seealso:: x
8080
%End
8181

82-
QgsVector perpVector() const;
82+
QgsVector perpVector() const /HoldGIL/;
8383
%Docstring
8484
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
8585
%End
8686

87-
double angle() const;
87+
double angle() const /HoldGIL/;
8888
%Docstring
8989
Returns the angle of the vector in radians.
9090
%End
9191

92-
double angle( QgsVector v ) const;
92+
double angle( QgsVector v ) const /HoldGIL/;
9393
%Docstring
9494
Returns the angle between this vector and another vector in radians.
9595
%End
9696

97-
double crossProduct( QgsVector v ) const;
97+
double crossProduct( QgsVector v ) const /HoldGIL/;
9898
%Docstring
9999
Returns the 2D cross product of this vector and another vector ``v``. (This is sometimes
100100
referred to as a "perpendicular dot product", and equals x1 * y1 - y1 * x2).
101101

102102
.. versionadded:: 3.2
103103
%End
104104

105-
QgsVector rotateBy( double rot ) const;
105+
QgsVector rotateBy( double rot ) const /HoldGIL/;
106106
%Docstring
107107
Rotates the vector by a specified angle.
108108

109109
:param rot: angle in radians
110110
%End
111111

112-
QgsVector normalized() const;
112+
QgsVector normalized() const throw( QgsException );
113113
%Docstring
114114
Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0).
115-
Will throw a QgsException if called on a vector with length of 0.
115+
116+
:raises QgsException: if called on a vector with length of 0.
116117
%End
117118

118-
bool operator==( QgsVector other ) const;
119+
bool operator==( QgsVector other ) const /HoldGIL/;
119120

120121
bool operator!=( QgsVector other ) const;
121122

122-
123-
QString toString( int precision = 17 ) const;
123+
QString toString( int precision = 17 ) const /HoldGIL/;
124124
%Docstring
125125
Returns a string representation of the vector.
126126
Members will be truncated to the specified ``precision``.

‎src/core/qgsvector.cpp

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -18,92 +18,6 @@
1818
#include "qgis.h"
1919
#include "qgsexception.h"
2020

21-
QgsVector::QgsVector( double x, double y )
22-
: mX( x )
23-
, mY( y )
24-
{
25-
}
26-
27-
QgsVector QgsVector::operator-() const
28-
{
29-
return QgsVector( -mX, -mY );
30-
}
31-
32-
QgsVector QgsVector::operator*( double scalar ) const
33-
{
34-
return QgsVector( mX * scalar, mY * scalar );
35-
}
36-
37-
QgsVector QgsVector::operator/( double scalar ) const
38-
{
39-
return *this * ( 1.0 / scalar );
40-
}
41-
42-
double QgsVector::operator*( QgsVector v ) const
43-
{
44-
return mX * v.mX + mY * v.mY;
45-
}
46-
47-
QgsVector QgsVector::operator+( QgsVector other ) const
48-
{
49-
return QgsVector( mX + other.mX, mY + other.mY );
50-
}
51-
52-
QgsVector &QgsVector::operator+=( QgsVector other )
53-
{
54-
mX += other.mX;
55-
mY += other.mY;
56-
return *this;
57-
}
58-
59-
QgsVector QgsVector::operator-( QgsVector other ) const
60-
{
61-
return QgsVector( mX - other.mX, mY - other.mY );
62-
}
63-
64-
QgsVector &QgsVector::operator-=( QgsVector other )
65-
{
66-
mX -= other.mX;
67-
mY -= other.mY;
68-
return *this;
69-
}
70-
71-
double QgsVector::length() const
72-
{
73-
return std::sqrt( mX * mX + mY * mY );
74-
}
75-
76-
double QgsVector::x() const
77-
{
78-
return mX;
79-
}
80-
81-
double QgsVector::y() const
82-
{
83-
return mY;
84-
}
85-
86-
QgsVector QgsVector::perpVector() const
87-
{
88-
return QgsVector( -mY, mX );
89-
}
90-
91-
double QgsVector::angle() const
92-
{
93-
double angle = std::atan2( mY, mX );
94-
return angle < 0.0 ? angle + 2.0 * M_PI : angle;
95-
}
96-
97-
double QgsVector::angle( QgsVector v ) const
98-
{
99-
return v.angle() - angle();
100-
}
101-
102-
double QgsVector::crossProduct( QgsVector v ) const
103-
{
104-
return mX * v.y() - mY * v.x();
105-
}
106-
10721
QgsVector QgsVector::rotateBy( double rot ) const
10822
{
10923
double angle = std::atan2( mY, mX ) + rot;
@@ -122,13 +36,3 @@ QgsVector QgsVector::normalized() const
12236

12337
return *this / len;
12438
}
125-
126-
bool QgsVector::operator==( QgsVector other ) const
127-
{
128-
return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
129-
}
130-
131-
bool QgsVector::operator!=( QgsVector other ) const
132-
{
133-
return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
134-
}

‎src/core/qgsvector.h

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,66 +41,101 @@ class CORE_EXPORT QgsVector
4141
* \param x x-component
4242
* \param y y-component
4343
*/
44-
QgsVector( double x, double y );
44+
QgsVector( double x, double y )
45+
: mX( x )
46+
, mY( y )
47+
{
48+
}
4549

4650
//! Swaps the sign of the x and y components of the vector.
47-
QgsVector operator-() const;
51+
QgsVector operator-() const SIP_HOLDGIL
52+
{
53+
return QgsVector( -mX, -mY );
54+
}
4855

4956
/**
5057
* Returns a vector where the components have been multiplied by a scalar value.
5158
* \param scalar factor to multiply by
5259
*/
53-
QgsVector operator*( double scalar ) const;
60+
QgsVector operator*( double scalar ) const SIP_HOLDGIL
61+
{
62+
return QgsVector( mX * scalar, mY * scalar );
63+
}
5464

5565
/**
5666
* Returns a vector where the components have been divided by a scalar value.
5767
* \param scalar factor to divide by
5868
*/
59-
QgsVector operator/( double scalar ) const;
69+
QgsVector operator/( double scalar ) const SIP_HOLDGIL
70+
{
71+
return *this * ( 1.0 / scalar );
72+
}
6073

6174
/**
6275
* Returns the dot product of two vectors, which is the sum of the x component
6376
* of this vector multiplied by the x component of another
6477
* vector plus the y component of this vector multiplied by the y component of another vector.
6578
*/
66-
double operator*( QgsVector v ) const;
79+
double operator*( QgsVector v ) const SIP_HOLDGIL
80+
{
81+
return mX * v.mX + mY * v.mY;
82+
}
6783

6884
/**
6985
* Adds another vector to this vector.
7086
* \since QGIS 3.0
7187
*/
72-
QgsVector operator+( QgsVector other ) const;
88+
QgsVector operator+( QgsVector other ) const SIP_HOLDGIL
89+
{
90+
return QgsVector( mX + other.mX, mY + other.mY );
91+
}
7392

7493
/**
7594
* Adds another vector to this vector in place.
7695
* \since QGIS 3.0
7796
*/
78-
QgsVector &operator+=( QgsVector other );
97+
QgsVector &operator+=( QgsVector other ) SIP_HOLDGIL
98+
{
99+
mX += other.mX;
100+
mY += other.mY;
101+
return *this;
102+
}
79103

80104
/**
81105
* Subtracts another vector to this vector.
82106
* \since QGIS 3.0
83107
*/
84-
QgsVector operator-( QgsVector other ) const;
108+
QgsVector operator-( QgsVector other ) const SIP_HOLDGIL
109+
{
110+
return QgsVector( mX - other.mX, mY - other.mY );
111+
}
85112

86113
/**
87114
* Subtracts another vector to this vector in place.
88115
* \since QGIS 3.0
89116
*/
90-
QgsVector &operator-=( QgsVector other );
117+
QgsVector &operator-=( QgsVector other ) SIP_HOLDGIL
118+
{
119+
mX -= other.mX;
120+
mY -= other.mY;
121+
return *this;
122+
}
91123

92124
/**
93125
* Returns the length of the vector.
94126
* \see lengthSquared()
95127
*/
96-
double length() const;
128+
double length() const SIP_HOLDGIL
129+
{
130+
return std::sqrt( mX * mX + mY * mY );
131+
}
97132

98133
/**
99134
* Returns the length of the vector.
100135
* \see length()
101136
* \since QGIS 3.2
102137
*/
103-
double lengthSquared() const
138+
double lengthSquared() const SIP_HOLDGIL
104139
{
105140
return mX * mX + mY * mY;
106141
}
@@ -109,61 +144,86 @@ class CORE_EXPORT QgsVector
109144
* Returns the vector's x-component.
110145
* \see y()
111146
*/
112-
double x() const;
147+
double x() const SIP_HOLDGIL
148+
{
149+
return mX;
150+
}
113151

114152
/**
115153
* Returns the vector's y-component.
116154
* \see x()
117155
*/
118-
double y() const;
156+
double y() const SIP_HOLDGIL
157+
{
158+
return mY;
159+
}
119160

120161
/**
121162
* Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
122163
*/
123-
QgsVector perpVector() const;
164+
QgsVector perpVector() const SIP_HOLDGIL
165+
{
166+
return QgsVector( -mY, mX );
167+
}
124168

125169
/**
126170
* Returns the angle of the vector in radians.
127171
*/
128-
double angle() const;
172+
double angle() const SIP_HOLDGIL
173+
{
174+
double angle = std::atan2( mY, mX );
175+
return angle < 0.0 ? angle + 2.0 * M_PI : angle;
176+
}
129177

130178
/**
131179
* Returns the angle between this vector and another vector in radians.
132180
*/
133-
double angle( QgsVector v ) const;
181+
double angle( QgsVector v ) const SIP_HOLDGIL
182+
{
183+
return v.angle() - angle();
184+
}
134185

135186
/**
136187
* Returns the 2D cross product of this vector and another vector \a v. (This is sometimes
137188
* referred to as a "perpendicular dot product", and equals x1 * y1 - y1 * x2).
138189
*
139190
* \since QGIS 3.2
140191
*/
141-
double crossProduct( QgsVector v ) const;
192+
double crossProduct( QgsVector v ) const SIP_HOLDGIL
193+
{
194+
return mX * v.y() - mY * v.x();
195+
}
142196

143197
/**
144198
* Rotates the vector by a specified angle.
145199
* \param rot angle in radians
146200
*/
147-
QgsVector rotateBy( double rot ) const;
201+
QgsVector rotateBy( double rot ) const SIP_HOLDGIL;
148202

149203
/**
150204
* Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0).
151-
* Will throw a QgsException if called on a vector with length of 0.
205+
*
206+
* \throws QgsException if called on a vector with length of 0.
152207
*/
153-
QgsVector normalized() const;
208+
QgsVector normalized() const SIP_THROW( QgsException );
154209

155210
//! Equality operator
156-
bool operator==( QgsVector other ) const;
211+
bool operator==( QgsVector other ) const SIP_HOLDGIL
212+
{
213+
return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
214+
}
157215

158216
//! Inequality operator
159-
bool operator!=( QgsVector other ) const;
160-
217+
bool operator!=( QgsVector other ) const
218+
{
219+
return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
220+
}
161221

162222
/**
163223
* Returns a string representation of the vector.
164224
* Members will be truncated to the specified \a precision.
165225
*/
166-
QString toString( int precision = 17 ) const
226+
QString toString( int precision = 17 ) const SIP_HOLDGIL
167227
{
168228
QString str = "Vector (";
169229
str += qgsDoubleToString( mX, precision );
@@ -182,7 +242,8 @@ class CORE_EXPORT QgsVector
182242
#endif
183243

184244
private:
185-
double mX = 0.0, mY = 0.0;
245+
double mX = 0.0;
246+
double mY = 0.0;
186247

187248
};
188249

0 commit comments

Comments
 (0)
Please sign in to comment.