fix somethings and testRayFromScreenPoint

This commit is contained in:
Belgacem 2021-01-12 11:46:17 +01:00 committed by Martin Dobias
parent fa7e3d4f7d
commit 2c9c6f94b9
9 changed files with 80 additions and 7 deletions

View File

@ -229,6 +229,8 @@ Converts the box to a 2D rectangle.
%Docstring %Docstring
Returns the smallest distance between the box and the point ``point`` Returns the smallest distance between the box and the point ``point``
(returns 0 if the point is inside the box) (returns 0 if the point is inside the box)
.. versionadded:: 3.18
%End %End
bool operator==( const QgsBox3d &other ) const; bool operator==( const QgsBox3d &other ) const;

View File

@ -45,6 +45,8 @@ Sets the origin of the ray
Sets the direction of the ray Sets the direction of the ray
%End %End
bool operator==( const QgsRay3D &r );
QVector3D projectedPoint( const QVector3D &point ) const; QVector3D projectedPoint( const QVector3D &point ) const;
%Docstring %Docstring
Returns the projection of the point on the ray Returns the projection of the point on the ray

View File

@ -108,7 +108,7 @@ Returns the points that are on a ray
%End %End
%MethodCode %MethodCode
{ {
QVector<QMap<QString, QVariant>> res = sipCpp->getPointsOnRay( *a0, a2, a3, a4, a5, a6 ); QVector<QMap<QString, QVariant>> res = sipCpp->getPointsOnRay( *a0, a1, a2, a3, a4, a5 );
sipRes = PyList_New( res.size() ); sipRes = PyList_New( res.size() );
for ( int i = 0; i < res.size(); ++i ) for ( int i = 0; i < res.size(); ++i )
{ {

View File

@ -575,18 +575,18 @@ QgsRay3D Qgs3DUtils::rayFromScreenPoint( const QPoint &point, const QSize &windo
// clip coordinates // clip coordinates
QVector4D rayClip( normDeviceCoords.x(), normDeviceCoords.y(), -1.0, 0.0 ); QVector4D rayClip( normDeviceCoords.x(), normDeviceCoords.y(), -1.0, 0.0 );
QMatrix4x4 projMatrix = camera->projectionMatrix(); QMatrix4x4 invertedProjMatrix = camera->projectionMatrix().inverted();
QMatrix4x4 viewMatrix = camera->viewMatrix(); QMatrix4x4 invertedViewMatrix = camera->viewMatrix().inverted();
// ray direction in view coordinates // ray direction in view coordinates
QVector4D rayDirView = projMatrix.inverted() * rayClip; QVector4D rayDirView = invertedProjMatrix * rayClip;
// ray origin in world coordinates // ray origin in world coordinates
QVector4D rayOriginWorld = viewMatrix.inverted() * QVector4D( 0.0f, 0.0f, 0.0f, 1.0f ); QVector4D rayOriginWorld = invertedViewMatrix * QVector4D( 0.0f, 0.0f, 0.0f, 1.0f );
// ray direction in world coordinates // ray direction in world coordinates
rayDirView.setZ( -1.0f ); rayDirView.setZ( -1.0f );
rayDirView.setW( 0.0f ); rayDirView.setW( 0.0f );
QVector4D rayDirWorld4D = viewMatrix.inverted() * rayDirView; QVector4D rayDirWorld4D = invertedViewMatrix * rayDirView;
QVector3D rayDirWorld( rayDirWorld4D.x(), rayDirWorld4D.y(), rayDirWorld4D.z() ); QVector3D rayDirWorld( rayDirWorld4D.x(), rayDirWorld4D.y(), rayDirWorld4D.z() );
rayDirWorld = rayDirWorld.normalized(); rayDirWorld = rayDirWorld.normalized();

View File

@ -208,6 +208,8 @@ class CORE_EXPORT QgsBox3d
/** /**
* Returns the smallest distance between the box and the point \a point * Returns the smallest distance between the box and the point \a point
* (returns 0 if the point is inside the box) * (returns 0 if the point is inside the box)
*
* \since QGIS 3.18
*/ */
double distanceTo( const QVector3D &point ) const; double distanceTo( const QVector3D &point ) const;

View File

@ -35,6 +35,11 @@ void QgsRay3D::setDirection( const QVector3D direction )
mDirection = direction; mDirection = direction;
} }
bool QgsRay3D::operator==( const QgsRay3D &r )
{
return this->mOrigin == r.origin() && this->direction() == r.direction();
}
QVector3D QgsRay3D::projectedPoint( const QVector3D &point ) const QVector3D QgsRay3D::projectedPoint( const QVector3D &point ) const
{ {
return mOrigin + QVector3D::dotProduct( point - mOrigin, mDirection ) * mDirection; return mOrigin + QVector3D::dotProduct( point - mOrigin, mDirection ) * mDirection;

View File

@ -43,6 +43,9 @@ class CORE_EXPORT QgsRay3D
//! Sets the direction of the ray //! Sets the direction of the ray
void setDirection( const QVector3D direction ); void setDirection( const QVector3D direction );
//! Comparison operator
bool operator==( const QgsRay3D &r );
/** /**
* Returns the projection of the point on the ray * Returns the projection of the point on the ray
* (which is the closest point of the ray to \a point) * (which is the closest point of the ray to \a point)

View File

@ -159,7 +159,7 @@ class CORE_EXPORT QgsPointCloudDataProvider: public QgsDataProvider
SIP_PYLIST getPointsOnRay( const QgsRay3D &ray, double maxScreenError, double cameraFov, int screenSizePx, double pointAngle, int pointsLimit = 1000 ); SIP_PYLIST getPointsOnRay( const QgsRay3D &ray, double maxScreenError, double cameraFov, int screenSizePx, double pointAngle, int pointsLimit = 1000 );
% MethodCode % MethodCode
{ {
QVector<QMap<QString, QVariant>> res = sipCpp->getPointsOnRay( *a0, a2, a3, a4, a5, a6 ); QVector<QMap<QString, QVariant>> res = sipCpp->getPointsOnRay( *a0, a1, a2, a3, a4, a5 );
sipRes = PyList_New( res.size() ); sipRes = PyList_New( res.size() );
for ( int i = 0; i < res.size(); ++i ) for ( int i = 0; i < res.size(); ++i )
{ {

View File

@ -34,6 +34,7 @@ class TestQgs3DUtils : public QObject
void cleanupTestCase();// will be called after the last testfunction was executed. void cleanupTestCase();// will be called after the last testfunction was executed.
void testTransforms(); void testTransforms();
void testRayFromScreenPoint();
private: private:
}; };
@ -82,5 +83,63 @@ void TestQgs3DUtils::testTransforms()
QCOMPARE( mapPoint1, mapPoint2 ); QCOMPARE( mapPoint1, mapPoint2 );
} }
void TestQgs3DUtils::testRayFromScreenPoint()
{
Qt3DRender::QCamera camera;
{
camera.setFieldOfView( 45.0f );
camera.setNearPlane( 10.0f );
camera.setFarPlane( 100.0f );
camera.setAspectRatio( 2 );
camera.setPosition( QVector3D( 9.0f, 15.0f, 30.0f ) );
camera.setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
camera.setViewCenter( QVector3D( 0.0f, 0.0f, 0.0f ) );
{
QgsRay3D ray1 = Qgs3DUtils::rayFromScreenPoint( QPoint( 50, 50 ), QSize( 100, 100 ), &camera );
QgsRay3D ray2( QVector3D( 8.99999904632568, 14.9999980926514, 29.9999980926514 ), QVector3D( -0.25916051864624, -0.431934207677841, -0.863868415355682 ) );
QVERIFY( ray1 == ray2 );
}
{
QgsRay3D ray1 = Qgs3DUtils::rayFromScreenPoint( QPoint( 0, 0 ), QSize( 100, 100 ), &camera );
QgsRay3D ray2( QVector3D( 8.99999904632568, 14.9999980926514, 29.9999980926514 ), QVector3D( -0.810001313686371, -0.0428109727799892, -0.584863305091858 ) );
QVERIFY( ray1 == ray2 );
}
{
QgsRay3D ray1 = Qgs3DUtils::rayFromScreenPoint( QPoint( 100, 100 ), QSize( 100, 100 ), &camera );
QgsRay3D ray2( QVector3D( 8.99999904632568, 14.9999980926514, 29.9999980926514 ), QVector3D( 0.429731547832489, -0.590972006320953, -0.682702660560608 ) );
QVERIFY( ray1 == ray2 );
}
}
{
camera.setFieldOfView( 60.0f );
camera.setNearPlane( 1.0f );
camera.setFarPlane( 1000.0f );
camera.setAspectRatio( 2 );
camera.setPosition( QVector3D( 0.0f, 0.0f, 0.0f ) );
camera.setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
camera.setViewCenter( QVector3D( 0.0f, 100.0f, -100.0f ) );
{
QgsRay3D ray1 = Qgs3DUtils::rayFromScreenPoint( QPoint( 500, 500 ), QSize( 1000, 1000 ), &camera );
QgsRay3D ray2( QVector3D( 0, 0, 0 ), QVector3D( 0, 0.70710676908493, -0.70710676908493 ) );
QVERIFY( ray1 == ray2 );
}
{
QgsRay3D ray1 = Qgs3DUtils::rayFromScreenPoint( QPoint( 0, 0 ), QSize( 1000, 1000 ), &camera );
QgsRay3D ray2( QVector3D( 0, 0, 0 ), QVector3D( -0.70710676908493, 0.683012664318085, -0.183012709021568 ) );
QVERIFY( ray1 == ray2 );
}
{
QgsRay3D ray1 = Qgs3DUtils::rayFromScreenPoint( QPoint( 500, 1000 ), QSize( 1000, 1000 ), &camera );
QgsRay3D ray2( QVector3D( 0, 0, 0 ), QVector3D( 0, 0.258819073438644, -0.965925812721252 ) );
QVERIFY( ray1 == ray2 );
}
}
}
QGSTEST_MAIN( TestQgs3DUtils ) QGSTEST_MAIN( TestQgs3DUtils )
#include "testqgs3dutils.moc" #include "testqgs3dutils.moc"