QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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(), 1.0f / mDirection.y(), 1.0f / mDirection.z() )
23{}
24
25void QgsRay3D::setOrigin( const QVector3D &origin )
26{
27 mOrigin = origin;
28}
29
30void QgsRay3D::setDirection( const QVector3D direction )
31{
32 mDirection = direction.normalized();
33 mDirectionInversed = QVector3D( 1.0f / mDirection.x(), 1.0f / mDirection.y(), 1.0f / mDirection.z() );
34}
35
36QVector3D QgsRay3D::projectedPoint( const QVector3D &point ) const
37{
38 return mOrigin + QVector3D::dotProduct( point - mOrigin, mDirection ) * mDirection;
39}
40
41float QgsRay3D::projectedDistance( const QVector3D &point ) const
42{
43 return QVector3D::dotProduct( point - mOrigin, mDirection ) / mDirection.lengthSquared();
44}
45
46bool QgsRay3D::isInFront( const QVector3D &point ) const
47{
48 return QVector3D::dotProduct( ( point - mOrigin ).normalized(), mDirection ) >= 0.0;
49}
50
51double QgsRay3D::angleToPoint( const QVector3D &point ) const
52{
53 // project point onto the ray
54 const QVector3D projPoint = projectedPoint( point );
55
56 // calculate the angle between the point and the projected point
57 const QVector3D v1 = projPoint - mOrigin;
58 const QVector3D v2 = point - projPoint;
59 return qRadiansToDegrees( std::atan2( v2.length(), v1.length() ) );
60}
61
62QVector3D QgsRay3D::point( float distance ) const
63{
64 return mOrigin + distance * mDirection;
65}
float projectedDistance(const QVector3D &point) const
Returns the distance of the projection of a point to the ray.
Definition qgsray3d.cpp:41
QVector3D origin() const
Returns the origin of the ray.
Definition qgsray3d.h:43
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:30
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:36
QVector3D direction() const
Returns the direction of the ray see setDirection().
Definition qgsray3d.h:49
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:51
void setOrigin(const QVector3D &origin)
Sets the origin of the ray.
Definition qgsray3d.cpp:25
bool isInFront(const QVector3D &point) const
Checks whether the point is in front of the ray.
Definition qgsray3d.cpp:46
QVector3D point(float distance) const
Returns the point along the ray with the specified distance from the ray's origin.
Definition qgsray3d.cpp:62