Skip to content

Commit

Permalink
Move QgsVector methods to header to allow better compiler optimisation,
Browse files Browse the repository at this point in the history
also hold Python GIL when calling them
  • Loading branch information
nyalldawson committed Oct 16, 2020
1 parent 2a15c3b commit 6a570f3
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 143 deletions.
44 changes: 22 additions & 22 deletions python/core/auto_generated/qgsvector.sip.in
Expand Up @@ -33,30 +33,30 @@ Constructor for QgsVector taking x and y component values.
:param y: y-component
%End

QgsVector operator-() const;
QgsVector operator-() const /HoldGIL/;

QgsVector operator*( double scalar ) const;
QgsVector operator*( double scalar ) const /HoldGIL/;

QgsVector operator/( double scalar ) const;
QgsVector operator/( double scalar ) const /HoldGIL/;

double operator*( QgsVector v ) const;
double operator*( QgsVector v ) const /HoldGIL/;

QgsVector operator+( QgsVector other ) const;
QgsVector operator+( QgsVector other ) const /HoldGIL/;

QgsVector &operator+=( QgsVector other );
QgsVector &operator+=( QgsVector other ) /HoldGIL/;

QgsVector operator-( QgsVector other ) const;
QgsVector operator-( QgsVector other ) const /HoldGIL/;

QgsVector &operator-=( QgsVector other );
QgsVector &operator-=( QgsVector other ) /HoldGIL/;

double length() const;
double length() const /HoldGIL/;
%Docstring
Returns the length of the vector.

.. seealso:: :py:func:`lengthSquared`
%End

double lengthSquared() const;
double lengthSquared() const /HoldGIL/;
%Docstring
Returns the length of the vector.

Expand All @@ -65,62 +65,62 @@ Returns the length of the vector.
.. versionadded:: 3.2
%End

double x() const;
double x() const /HoldGIL/;
%Docstring
Returns the vector's x-component.

.. seealso:: y
%End

double y() const;
double y() const /HoldGIL/;
%Docstring
Returns the vector's y-component.

.. seealso:: x
%End

QgsVector perpVector() const;
QgsVector perpVector() const /HoldGIL/;
%Docstring
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
%End

double angle() const;
double angle() const /HoldGIL/;
%Docstring
Returns the angle of the vector in radians.
%End

double angle( QgsVector v ) const;
double angle( QgsVector v ) const /HoldGIL/;
%Docstring
Returns the angle between this vector and another vector in radians.
%End

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

.. versionadded:: 3.2
%End

QgsVector rotateBy( double rot ) const;
QgsVector rotateBy( double rot ) const /HoldGIL/;
%Docstring
Rotates the vector by a specified angle.

:param rot: angle in radians
%End

QgsVector normalized() const;
QgsVector normalized() const throw( QgsException );
%Docstring
Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0).
Will throw a QgsException if called on a vector with length of 0.

:raises QgsException: if called on a vector with length of 0.
%End

bool operator==( QgsVector other ) const;
bool operator==( QgsVector other ) const /HoldGIL/;

bool operator!=( QgsVector other ) const;


QString toString( int precision = 17 ) const;
QString toString( int precision = 17 ) const /HoldGIL/;
%Docstring
Returns a string representation of the vector.
Members will be truncated to the specified ``precision``.
Expand Down
96 changes: 0 additions & 96 deletions src/core/qgsvector.cpp
Expand Up @@ -18,92 +18,6 @@
#include "qgis.h"
#include "qgsexception.h"

QgsVector::QgsVector( double x, double y )
: mX( x )
, mY( y )
{
}

QgsVector QgsVector::operator-() const
{
return QgsVector( -mX, -mY );
}

QgsVector QgsVector::operator*( double scalar ) const
{
return QgsVector( mX * scalar, mY * scalar );
}

QgsVector QgsVector::operator/( double scalar ) const
{
return *this * ( 1.0 / scalar );
}

double QgsVector::operator*( QgsVector v ) const
{
return mX * v.mX + mY * v.mY;
}

QgsVector QgsVector::operator+( QgsVector other ) const
{
return QgsVector( mX + other.mX, mY + other.mY );
}

QgsVector &QgsVector::operator+=( QgsVector other )
{
mX += other.mX;
mY += other.mY;
return *this;
}

QgsVector QgsVector::operator-( QgsVector other ) const
{
return QgsVector( mX - other.mX, mY - other.mY );
}

QgsVector &QgsVector::operator-=( QgsVector other )
{
mX -= other.mX;
mY -= other.mY;
return *this;
}

double QgsVector::length() const
{
return std::sqrt( mX * mX + mY * mY );
}

double QgsVector::x() const
{
return mX;
}

double QgsVector::y() const
{
return mY;
}

QgsVector QgsVector::perpVector() const
{
return QgsVector( -mY, mX );
}

double QgsVector::angle() const
{
double angle = std::atan2( mY, mX );
return angle < 0.0 ? angle + 2.0 * M_PI : angle;
}

double QgsVector::angle( QgsVector v ) const
{
return v.angle() - angle();
}

double QgsVector::crossProduct( QgsVector v ) const
{
return mX * v.y() - mY * v.x();
}

QgsVector QgsVector::rotateBy( double rot ) const
{
double angle = std::atan2( mY, mX ) + rot;
Expand All @@ -122,13 +36,3 @@ QgsVector QgsVector::normalized() const

return *this / len;
}

bool QgsVector::operator==( QgsVector other ) const
{
return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
}

bool QgsVector::operator!=( QgsVector other ) const
{
return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
}

0 comments on commit 6a570f3

Please sign in to comment.