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 
QgsRay3D(const QVector3D & origin,const QVector3D & direction)19 QgsRay3D::QgsRay3D( const QVector3D &origin, const QVector3D &direction )
20   : mOrigin( origin )
21   , mDirection( direction.normalized() )
22 {
23 
24 }
25 
setOrigin(const QVector3D & origin)26 void QgsRay3D::setOrigin( const QVector3D &origin )
27 {
28   mOrigin = origin;
29 }
30 
setDirection(const QVector3D direction)31 void QgsRay3D::setDirection( const QVector3D direction )
32 {
33   mDirection = direction.normalized();
34 }
35 
projectedPoint(const QVector3D & point) const36 QVector3D QgsRay3D::projectedPoint( const QVector3D &point ) const
37 {
38   return mOrigin + QVector3D::dotProduct( point - mOrigin, mDirection ) * mDirection;
39 }
40 
isInFront(const QVector3D & point) const41 bool QgsRay3D::isInFront( const QVector3D &point ) const
42 {
43   return QVector3D::dotProduct( ( point - mOrigin ).normalized(), mDirection ) >= 0.0;
44 }
45 
angleToPoint(const QVector3D & point) const46 double QgsRay3D::angleToPoint( const QVector3D &point ) const
47 {
48   // project point onto the ray
49   const QVector3D projPoint = projectedPoint( point );
50 
51   // calculate the angle between the point and the projected point
52   const QVector3D v1 = projPoint - mOrigin ;
53   const QVector3D v2 = point - projPoint;
54   return qRadiansToDegrees( std::atan2( v2.length(), v1.length() ) );
55 }
56