Skip to content
Permalink
Browse files

Move QgsVector methods to header to allow better compiler optimisation,

also hold Python GIL when calling them
  • Loading branch information
nyalldawson committed Oct 16, 2020
1 parent 2a15c3b commit 6a570f33ebac78fe32111cb72acdb31f5e557a62
Showing with 108 additions and 143 deletions.
  1. +22 −22 python/core/auto_generated/qgsvector.sip.in
  2. +0 −96 src/core/qgsvector.cpp
  3. +86 −25 src/core/qgsvector.h
@@ -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.

@@ -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``.
@@ -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;
@@ -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.
You can’t perform that action at this time.