Skip to content
Permalink
Browse files

add some QgsRay3D tests

  • Loading branch information
NEDJIMAbelgacem authored and wonder-sk committed Jan 13, 2021
1 parent bfa835d commit be88fff95cc4da10fc1d7088726c8f537fea364b
@@ -25,24 +25,41 @@ A ray is composed of an origin point (the start of the ray) and a direction vect
QgsRay3D( const QVector3D &origin, const QVector3D &direction );
%Docstring
Constructor

.. note::

: the direction is automatically normalized
%End

QVector3D origin() const;
%Docstring
Returns the origin of the ray

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

QVector3D direction() const;
%Docstring
Returns the direction of the ray
see :py:func:`~QgsRay3D.setDirection`
%End

void setOrigin( const QVector3D &origin );
%Docstring
Sets the origin of the ray

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

void setDirection( const QVector3D direction );
%Docstring
Sets the direction of the ray

.. note::

: the direction is automatically normalized

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

bool operator==( const QgsRay3D &r );
@@ -20,7 +20,7 @@

QgsRay3D::QgsRay3D( const QVector3D &origin, const QVector3D &direction )
: mOrigin( origin )
, mDirection( direction )
, mDirection( direction.normalized() )
{

}
@@ -32,7 +32,7 @@ void QgsRay3D::setOrigin( const QVector3D &origin )

void QgsRay3D::setDirection( const QVector3D direction )
{
mDirection = direction;
mDirection = direction.normalized();
}

bool QgsRay3D::operator==( const QgsRay3D &r )
@@ -89,7 +89,7 @@ bool QgsRay3D::intersectsWith( const QgsBox3d &box ) const

bool QgsRay3D::isInFront( const QVector3D &point ) const
{
return QVector3D::dotProduct( point - mOrigin, mDirection ) > 0.0;
return QVector3D::dotProduct( point - mOrigin, mDirection ) >= 0.0;
}

double QgsRay3D::angleToPoint( const QVector3D &point ) const
@@ -98,7 +98,7 @@ double QgsRay3D::angleToPoint( const QVector3D &point ) const
QVector3D projectedPoint = mOrigin + QVector3D::dotProduct( point - mOrigin, mDirection ) * mDirection;

// calculate the angle between the point and the projected point
QVector3D v1 = ( projectedPoint - mOrigin ).normalized();
QVector3D v2 = ( point - mOrigin ).normalized();
return qRadiansToDegrees( std::acos( std::abs( QVector3D::dotProduct( v1, v2 ) ) ) );
QVector3D v1 = projectedPoint - mOrigin ;
QVector3D v2 = point - mOrigin;
return qRadiansToDegrees( std::atan2( v2.length(), v1.length() ) );
}
@@ -30,17 +30,35 @@
class CORE_EXPORT QgsRay3D
{
public:
//! Constructor
/*
* Constructor
* \note : the direction is automatically normalized
*/
QgsRay3D( const QVector3D &origin, const QVector3D &direction );

//! Returns the origin of the ray
/**
* Returns the origin of the ray
* \see setOrigin()
*/
QVector3D origin() const { return mOrigin; }
//! Returns the direction of the ray

/**
* Returns the direction of the ray
* see setDirection()
*/
QVector3D direction() const { return mDirection; }

//! Sets the origin of the ray
/**
* Sets the origin of the ray
* \see origin()
*/
void setOrigin( const QVector3D &origin );
//! Sets the direction of the ray

/**
* Sets the direction of the ray
* \note : the direction is automatically normalized
* \see direction()
*/
void setDirection( const QVector3D direction );

//! Comparison operator
@@ -18,6 +18,7 @@
#include "qgs3dutils.h"

#include "qgsbox3d.h"
#include "qgsray3d.h"

#include <QSize>

@@ -38,6 +39,7 @@ class TestQgs3DUtils : public QObject
void testTransforms();
void testRayFromScreenPoint();
void testQgsBox3DDistanceTo();
void testQgsRay3D();
private:
};

@@ -157,5 +159,39 @@ void TestQgs3DUtils::testQgsBox3DDistanceTo()
}
}

void TestQgs3DUtils::testQgsRay3D()
{
QgsRay3D ray( QVector3D( 0, 0, 0 ), QVector3D( 1, 1, 1 ) );
float t = 1.0f + ( float )( rand() % 1000 ) / 1000.0f;
QVector3D p1 = ray.origin() + t * ray.direction();
QVector3D p2 = ray.origin() - t * ray.direction();
// point already on the ray
QVERIFY( ray.projectedPoint( p1 ) == p1 );
QVERIFY( ray.projectedPoint( p2 ) == p2 );

// t >= 0 then the point is in front of the ray
QVERIFY( ray.isInFront( p1 ) );
// t < 0 then the point is in front of the ray
QVERIFY( !ray.isInFront( p2 ) );

for ( int i = 0; i < 8; ++i )
{
// random vector
QVector3D n = QVector3D( 1.0f + ( float )( rand() % 1000 ) / 1000.0f, 1.0f + ( float )( rand() % 1000 ) / 1000.0f, 1.0f + ( float )( rand() % 1000 ) / 1000.0f ).normalized();
// random point on the ray
float t = 1.0f + ( float )( rand() % 1000 ) / 1000.0f;
QVector3D p = ray.origin() + t * ray.direction();
// a random point that projects to p
QVector3D p2 = p + ( 1.0f + ( float )( rand() % 1000 ) / 1000.0f ) * QVector3D::crossProduct( ray.direction(), n );

QVERIFY( qFuzzyCompare( ray.projectedPoint( p2 ), p ) );

float angle = qRadiansToDegrees( std::atan2( ( p2 - ray.origin() ).length(), ( p - ray.origin() ).length() ) );

QVERIFY( qFuzzyCompare( ( float )ray.angleToPoint( p2 ), angle ) );
}

}

QGSTEST_MAIN( TestQgs3DUtils )
#include "testqgs3dutils.moc"

0 comments on commit be88fff

Please sign in to comment.
You can’t perform that action at this time.