Skip to content

Commit

Permalink
- Add some methods in QgsVector3D
Browse files Browse the repository at this point in the history
- Move QgsVector3D from 3d to core
- Use QgsVector3D instead of QVector3D
lbartoletti authored and nyalldawson committed Apr 20, 2018
1 parent 9d649e7 commit 0bacf0f
Showing 10 changed files with 264 additions and 125 deletions.
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Original file line number Diff line number Diff line change
@@ -136,6 +136,7 @@
%Include qgstestutils.sip
%Include qgsziputils.sip
%Include qgsvector.sip
%Include qgsvector3d.sip
%Include auth/qgsauthcertutils.sip
%Include auth/qgsauthconfig.sip
%Include auth/qgsauthmanager.sip
61 changes: 30 additions & 31 deletions python/core/geometry/qgsgeometryutils.sip.in
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@




class QgsGeometryUtils
{
%Docstring
@@ -493,8 +492,8 @@ Return the coefficients (a, b, c for equation "ax + by + c = 0") of a line defin
%End


static double skewLinesDistance( const QVector3D &P1, const QVector3D &P12,
const QVector3D &P2, const QVector3D &P22 );
static double skewLinesDistance( const QgsVector3D &P1, const QgsVector3D &P12,
const QgsVector3D &P2, const QgsVector3D &P22 );
%Docstring
An algorithm to calculate the shortest distance between two skew lines.

@@ -506,9 +505,9 @@ An algorithm to calculate the shortest distance between two skew lines.
:return: the shortest distance
%End

static bool skewLinesProjection( const QVector3D &P1, const QVector3D &P12,
const QVector3D &P2, const QVector3D &P22,
QVector3D &X1 /Out/,
static bool skewLinesProjection( const QgsVector3D &P1, const QgsVector3D &P12,
const QgsVector3D &P2, const QgsVector3D &P22,
QgsVector3D &X1 /Out/,
double epsilon = 0.0001 );
%Docstring
A method to project one skew line onto another.
@@ -522,9 +521,9 @@ A method to project one skew line onto another.
:return: true if such point exists, false - otherwise.
%End

static bool linesIntersection3D( const QVector3D &La1, const QVector3D &La2,
const QVector3D &Lb1, const QVector3D &Lb2,
QVector3D &intersection /Out/ );
static bool linesIntersection3D( const QgsVector3D &La1, const QgsVector3D &La2,
const QgsVector3D &Lb1, const QgsVector3D &Lb2,
QgsVector3D &intersection /Out/ );
%Docstring
An algorithm to calculate an (approximate) intersection of two lines in 3D.

@@ -538,28 +537,28 @@ An algorithm to calculate an (approximate) intersection of two lines in 3D.
example:
.. code-block:: python

QgsGeometryUtils.linesIntersection3D(QVector3D(0,0,0), QVector3D(5,0,0), QVector3D(2,1,0), QVector3D(2,3,0))
# (True, PyQt5.QtGui.QVector3D(2.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QVector3D(0,0,0), QVector3D(5,0,0), QVector3D(2,1,0), QVector3D(2,0,0))
# (True, PyQt5.QtGui.QVector3D(2.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QVector3D(0,0,0), QVector3D(5,0,0), QVector3D(0,1,0), QVector3D(0,3,0))
# (True, PyQt5.QtGui.QVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QVector3D(0,0,0), QVector3D(5,0,0), QVector3D(0,1,0), QVector3D(0,0,0))
# (True, PyQt5.QtGui.QVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QVector3D(0,0,0), QVector3D(5,0,0), QVector3D(5,1,0), QVector3D(5,3,0))
# (False, PyQt5.QtGui.QVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QVector3D(0,0,0), QVector3D(5,0,0), QVector3D(5,1,0), QVector3D(5,0,0))
# (False, PyQt5.QtGui.QVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QVector3D(1,1,0), QVector3D(2,2,0), QVector3D(3,1,0), QVector3D(3,2,0))
# (True, PyQt5.QtGui.QVector3D(3.0, 3.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QVector3D(1,1,0), QVector3D(2,2,0), QVector3D(3,2,0), QVector3D(3,1,0))
# (True, PyQt5.QtGui.QVector3D(3.0, 3.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QVector3D(5,5,5), QVector3D(0,0,0), QVector3D(0,5,5), QVector3D(5,0,0))
# (True, PyQt5.QtGui.QVector3D(2.5, 2.5, 2.5))
QgsGeometryUtils.linesIntersection3D(QVector3D(2.5,2.5,2.5), QVector3D(0,5,0), QVector3D(2.5,2.5,2.5), QVector3D(5,0,0))
# (True, PyQt5.QtGui.QVector3D(2.5, 2.5, 2.5))
QgsGeometryUtils.linesIntersection3D(QVector3D(2.5,2.5,2.5), QVector3D(5,0,0), QVector3D(0,5,5), QVector3D(5,5,5))
# (True, PyQt5.QtGui.QVector3D(0.0, 5.0, 5.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(2,1,0), QgsVector3D(2,3,0))
# (True, PyQt5.QtGui.QgsVector3D(2.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(2,1,0), QgsVector3D(2,0,0))
# (True, PyQt5.QtGui.QgsVector3D(2.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(0,1,0), QgsVector3D(0,3,0))
# (True, PyQt5.QtGui.QgsVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(0,1,0), QgsVector3D(0,0,0))
# (True, PyQt5.QtGui.QgsVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(5,1,0), QgsVector3D(5,3,0))
# (False, PyQt5.QtGui.QgsVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(5,1,0), QgsVector3D(5,0,0))
# (False, PyQt5.QtGui.QgsVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(1,1,0), QgsVector3D(2,2,0), QgsVector3D(3,1,0), QgsVector3D(3,2,0))
# (True, PyQt5.QtGui.QgsVector3D(3.0, 3.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(1,1,0), QgsVector3D(2,2,0), QgsVector3D(3,2,0), QgsVector3D(3,1,0))
# (True, PyQt5.QtGui.QgsVector3D(3.0, 3.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(5,5,5), QgsVector3D(0,0,0), QgsVector3D(0,5,5), QgsVector3D(5,0,0))
# (True, PyQt5.QtGui.QgsVector3D(2.5, 2.5, 2.5))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(2.5,2.5,2.5), QgsVector3D(0,5,0), QgsVector3D(2.5,2.5,2.5), QgsVector3D(5,0,0))
# (True, PyQt5.QtGui.QgsVector3D(2.5, 2.5, 2.5))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(2.5,2.5,2.5), QgsVector3D(5,0,0), QgsVector3D(0,5,5), QgsVector3D(5,5,5))
# (True, PyQt5.QtGui.QgsVector3D(0.0, 5.0, 5.0))
%End

static bool setZValueFromPoints( const QgsPointSequence &points, QgsPoint &point );
96 changes: 96 additions & 0 deletions python/core/qgsvector3d.sip.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsvector3d.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsVector3D
{
%Docstring
Class for storage of 3D vectors similar to QgsVector3D, with the difference that it uses double precision
instead of single precision floating point numbers.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsvector3d.h"
%End
public:
QgsVector3D();
%Docstring
Constructs a null vector
%End

QgsVector3D( double x, double y, double z );
%Docstring
Constructs a vector from given coordinates
%End
bool isNull() const;
%Docstring
Returns true if all three coordinates are zero
%End

double x() const;
%Docstring
Returns X coordinate
%End
double y() const;
%Docstring
Returns Y coordinate
%End
double z() const;
%Docstring
Returns Z coordinate
%End

void set( double x, double y, double z );
%Docstring
Sets vector coordinates
%End

bool operator==( const QgsVector3D &other ) const;
bool operator!=( const QgsVector3D &other ) const;

QgsVector3D operator+( const QgsVector3D &other ) const;

QgsVector3D operator-( const QgsVector3D &other ) const;

QgsVector3D operator *( const double factor) const;

QgsVector3D operator /( const double factor) const;

static double dotProduct( const QgsVector3D &v1, const QgsVector3D &v2);
%Docstring
Returns the dot product of two vectors
%End

static QgsVector3D crossProduct(const QgsVector3D& v1, const QgsVector3D& v2);
%Docstring
Returns the cross product of two vectors
%End

double length() const;
%Docstring
Returns the length of the vector
%End

void normalize();
%Docstring
Normalizes the currect vector in place.
%End


};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsvector3d.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 0 additions & 2 deletions src/3d/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ SET(QGIS_3D_SRCS
qgstessellatedpolygongeometry.cpp
qgstessellator.cpp
qgstilingscheme.cpp
qgsvector3d.cpp
qgsvectorlayer3drenderer.cpp

chunks/qgschunkboundsentity_p.cpp
@@ -85,7 +84,6 @@ SET(QGIS_3D_HDRS
qgstessellatedpolygongeometry.h
qgstessellator.h
qgstilingscheme.h
qgsvector3d.h
qgsvectorlayer3drenderer.h

chunks/qgschunkboundsentity_p.h
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -299,6 +299,7 @@ SET(QGIS_CORE_SRCS
qgstransactiongroup.cpp
qgsunittypes.cpp
qgsvector.cpp
qgsvector3d.cpp
qgsvectordataprovider.cpp
qgsvectorfilewriter.cpp
qgsvectorfilewritertask.cpp
@@ -946,6 +947,7 @@ SET(QGIS_CORE_HDRS
qgstestutils.h
qgsziputils.h
qgsvector.h
qgsvector3d.h
qgslocalec.h

annotations/qgsannotationregistry.h
62 changes: 30 additions & 32 deletions src/core/geometry/qgsgeometryutils.cpp
Original file line number Diff line number Diff line change
@@ -1331,28 +1331,28 @@ double QgsGeometryUtils::averageAngle( double a1, double a2 )
return normalizedAngle( resultAngle );
}

double QgsGeometryUtils::skewLinesDistance( const QVector3D &P1, const QVector3D &P12,
const QVector3D &P2, const QVector3D &P22 )
double QgsGeometryUtils::skewLinesDistance( const QgsVector3D &P1, const QgsVector3D &P12,
const QgsVector3D &P2, const QgsVector3D &P22 )
{
QVector3D u1 = P12 - P1;
QVector3D u2 = P22 - P2;
QVector3D u3 = QVector3D::crossProduct( u1, u2 );
QgsVector3D u1 = P12 - P1;
QgsVector3D u2 = P22 - P2;
QgsVector3D u3 = QgsVector3D::crossProduct( u1, u2 );
if ( u3.length() == 0 ) return 1;
u3.normalize();
QVector3D dir = P1 - P2;
return std::fabs( ( QVector3D::dotProduct( dir, u3 ) ) ); // u3 is already normalized
QgsVector3D dir = P1 - P2;
return std::fabs( ( QgsVector3D::dotProduct( dir, u3 ) ) ); // u3 is already normalized
}

bool QgsGeometryUtils::skewLinesProjection( const QVector3D &P1, const QVector3D &P12,
const QVector3D &P2, const QVector3D &P22,
QVector3D &X1, double epsilon )
bool QgsGeometryUtils::skewLinesProjection( const QgsVector3D &P1, const QgsVector3D &P12,
const QgsVector3D &P2, const QgsVector3D &P22,
QgsVector3D &X1, double epsilon )
{
QVector3D d = P2 - P1;
QVector3D u1 = P12 - P1;
QgsVector3D d = P2 - P1;
QgsVector3D u1 = P12 - P1;
u1.normalize();
QVector3D u2 = P22 - P2;
QgsVector3D u2 = P22 - P2;
u2.normalize();
QVector3D u3 = QVector3D::crossProduct( u1, u2 );
QgsVector3D u3 = QgsVector3D::crossProduct( u1, u2 );

if ( std::fabs( u3.x() ) <= epsilon &&
std::fabs( u3.y() ) <= epsilon &&
@@ -1366,8 +1366,8 @@ bool QgsGeometryUtils::skewLinesProjection( const QVector3D &P1, const QVector3D
// we want to find X1 (lies on u1)
// solving the linear equation in r1 and r2: Xi = Pi + ri*ui
// we are only interested in X1 so we only solve for r1.
float a1 = QVector3D::dotProduct( u1, u1 ), b1 = QVector3D::dotProduct( u1, u2 ), c1 = QVector3D::dotProduct( u1, d );
float a2 = QVector3D::dotProduct( u1, u2 ), b2 = QVector3D::dotProduct( u2, u2 ), c2 = QVector3D::dotProduct( u2, d );
float a1 = QgsVector3D::dotProduct( u1, u1 ), b1 = QgsVector3D::dotProduct( u1, u2 ), c1 = QgsVector3D::dotProduct( u1, d );
float a2 = QgsVector3D::dotProduct( u1, u2 ), b2 = QgsVector3D::dotProduct( u2, u2 ), c2 = QgsVector3D::dotProduct( u2, d );
if ( !( std::fabs( b1 ) > epsilon ) )
{
// Denominator is close to zero.
@@ -1385,9 +1385,9 @@ bool QgsGeometryUtils::skewLinesProjection( const QVector3D &P1, const QVector3D
return true;
}

bool QgsGeometryUtils::linesIntersection3D( const QVector3D &La1, const QVector3D &La2,
const QVector3D &Lb1, const QVector3D &Lb2,
QVector3D &intersection )
bool QgsGeometryUtils::linesIntersection3D( const QgsVector3D &La1, const QgsVector3D &La2,
const QgsVector3D &Lb1, const QgsVector3D &Lb2,
QgsVector3D &intersection )
{

// if all Vector are on the same plane (have the same Z), use the 2D intersection
@@ -1404,9 +1404,7 @@ bool QgsGeometryUtils::linesIntersection3D( const QVector3D &La1, const QVector3
isIntersection,
1e-8,
true );
intersection.setX( ptInter.x() );
intersection.setY( ptInter.y() );
intersection.setZ( La1.z() );
intersection.set( ptInter.x(), ptInter.y(), La1.z() );
return true;
}

@@ -1416,33 +1414,33 @@ bool QgsGeometryUtils::linesIntersection3D( const QVector3D &La1, const QVector3
if ( qgsDoubleNear( distance, 0.0 ) )
{
// 3d lines have exact intersection point.
QVector3D C = La2;
QVector3D D = Lb2;
QVector3D e = La1 - La2;
QVector3D f = Lb1 - Lb2;
QVector3D g = D - C;
if ( qgsDoubleNear( ( QVector3D::crossProduct( f, g ) ).length(), 0.0 ) || qgsDoubleNear( ( QVector3D::crossProduct( f, e ) ).length(), 0.0 ) )
QgsVector3D C = La2;
QgsVector3D D = Lb2;
QgsVector3D e = La1 - La2;
QgsVector3D f = Lb1 - Lb2;
QgsVector3D g = D - C;
if ( qgsDoubleNear( ( QgsVector3D::crossProduct( f, g ) ).length(), 0.0 ) || qgsDoubleNear( ( QgsVector3D::crossProduct( f, e ) ).length(), 0.0 ) )
{
// Lines have no intersection, are they parallel?
return false;
}

QVector3D fgn = QVector3D::crossProduct( f, g );
QgsVector3D fgn = QgsVector3D::crossProduct( f, g );
fgn.normalize();

QVector3D fen = QVector3D::crossProduct( f, e );
QgsVector3D fen = QgsVector3D::crossProduct( f, e );
fen.normalize();

int di = -1;
if ( fgn == fen ) // same direction?
di *= -1;

intersection = C + e * di * ( QVector3D::crossProduct( f, g ).length() / QVector3D::crossProduct( f, e ).length() );
intersection = C + e * di * ( QgsVector3D::crossProduct( f, g ).length() / QgsVector3D::crossProduct( f, e ).length() );
return true;
}

// try to calculate the approximate intersection point
QVector3D X1, X2;
QgsVector3D X1, X2;
bool firstIsDone = skewLinesProjection( La1, La2, Lb1, Lb2, X1 );
bool secondIsDone = skewLinesProjection( Lb1, Lb2, La1, La2, X2 );

Loading

0 comments on commit 0bacf0f

Please sign in to comment.