QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsray3d.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsray3d.cpp
3 --------------------------------------
4 Date : January 2021
5 Copyright : (C) 2021 by Belgacem Nedjima
6 Email : belgacem dot nedjima at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#include "qgsray3d.h"
16
17#include <QtMath>
18
19QgsRay3D::QgsRay3D( const QVector3D &origin, const QVector3D &direction )
20 : mOrigin( origin )
21 , mDirection( direction.normalized() )
22 , mDirectionInversed( 1.0f / mDirection.x(),
23 1.0f / mDirection.y(),
24 1.0f / mDirection.z() )
25{
26
27}
28
29void QgsRay3D::setOrigin( const QVector3D &origin )
30{
31 mOrigin = origin;
32}
33
34void QgsRay3D::setDirection( const QVector3D direction )
35{
36 mDirection = direction.normalized();
37 mDirectionInversed = QVector3D( 1.0f / mDirection.x(),
38 1.0f / mDirection.y(),
39 1.0f / mDirection.z() );
40}
41
42QVector3D QgsRay3D::projectedPoint( const QVector3D &point ) const
43{
44 return mOrigin + QVector3D::dotProduct( point - mOrigin, mDirection ) * mDirection;
45}
46
47float QgsRay3D::projectedDistance( const QVector3D &point ) const
48{
49 return QVector3D::dotProduct( point - mOrigin, mDirection ) / mDirection.lengthSquared();
50}
51
52bool QgsRay3D::isInFront( const QVector3D &point ) const
53{
54 return QVector3D::dotProduct( ( point - mOrigin ).normalized(), mDirection ) >= 0.0;
55}
56
57double QgsRay3D::angleToPoint( const QVector3D &point ) const
58{
59 // project point onto the ray
60 const QVector3D projPoint = projectedPoint( point );
61
62 // calculate the angle between the point and the projected point
63 const QVector3D v1 = projPoint - mOrigin ;
64 const QVector3D v2 = point - projPoint;
65 return qRadiansToDegrees( std::atan2( v2.length(), v1.length() ) );
66}
67
68QVector3D QgsRay3D::point( float t ) const
69{
70 return mOrigin + t * mDirection;
71}
float projectedDistance(const QVector3D &point) const
Returns the distance of the projection of a point to the ray.
Definition qgsray3d.cpp:47
QVector3D origin() const
Returns the origin of the ray.
Definition qgsray3d.h:44
QgsRay3D(const QVector3D &origin, const QVector3D &direction)
Constructor.
Definition qgsray3d.cpp:19
void setDirection(const QVector3D direction)
Sets the direction of the ray.
Definition qgsray3d.cpp:34
QVector3D projectedPoint(const QVector3D &point) const
Returns the projection of the point on the ray (which is the closest point of the ray to point).
Definition qgsray3d.cpp:42
QVector3D direction() const
Returns the direction of the ray see setDirection().
Definition qgsray3d.h:50
double angleToPoint(const QVector3D &point) const
Returns the angle between the ray and the vector from the ray's origin and the point point.
Definition qgsray3d.cpp:57
void setOrigin(const QVector3D &origin)
Sets the origin of the ray.
Definition qgsray3d.cpp:29
bool isInFront(const QVector3D &point) const
Checks whether the point is in front of the ray.
Definition qgsray3d.cpp:52
QVector3D point(float distance) const
Returns the point along the ray with the specified distance from the ray's origin.
Definition qgsray3d.cpp:68