QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgscamerapose.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscamerapose.cpp
3 --------------------------------------
4 Date : July 2018
5 Copyright : (C) 2018 by Martin Dobias
6 Email : wonder dot sk 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
16#include "qgscamerapose.h"
17
18#include <Qt3DRender/QCamera>
19
20#include <QDomDocument>
21
22QDomElement QgsCameraPose::writeXml( QDomDocument &doc ) const
23{
24 QDomElement elemCamera = doc.createElement( QStringLiteral( "camera-pose" ) );
25 elemCamera.setAttribute( QStringLiteral( "x" ), mCenterPoint.x() );
26 elemCamera.setAttribute( QStringLiteral( "y" ), mCenterPoint.y() );
27 elemCamera.setAttribute( QStringLiteral( "z" ), mCenterPoint.z() );
28 elemCamera.setAttribute( QStringLiteral( "dist" ), mDistanceFromCenterPoint );
29 elemCamera.setAttribute( QStringLiteral( "pitch" ), mPitchAngle );
30 elemCamera.setAttribute( QStringLiteral( "heading" ), mHeadingAngle );
31 return elemCamera;
32}
33
34void QgsCameraPose::readXml( const QDomElement &elem )
35{
36 const double x = elem.attribute( QStringLiteral( "x" ) ).toDouble();
37 const double y = elem.attribute( QStringLiteral( "y" ) ).toDouble();
38 const double z = elem.attribute( QStringLiteral( "z" ) ).toDouble();
39 mCenterPoint = QgsVector3D( x, y, z );
40
41 mDistanceFromCenterPoint = elem.attribute( QStringLiteral( "dist" ) ).toFloat();
42 mPitchAngle = elem.attribute( QStringLiteral( "pitch" ) ).toFloat();
43 mHeadingAngle = elem.attribute( QStringLiteral( "heading" ) ).toFloat();
44}
45
47{
48 // something went horribly wrong. Prevent further errors
49 if ( std::isnan( point.x() ) || std::isnan( point.y() ) || std::isnan( point.z() ) )
50 qWarning() << "Not updating camera position: it cannot be NaN!";
51 else
52 mCenterPoint = point;
53}
54
56{
57 mDistanceFromCenterPoint = std::max( distance, 10.0f );
58}
59
61{
62 // prevent going over the head
63 // prevent bug in QgsCameraPose::updateCamera when updating camera rotation.
64 // With a mPitchAngle < 0.2 or > 179.8, QQuaternion::fromEulerAngles( mPitchAngle, mHeadingAngle, 0 )
65 // will return bad rotation angle in Qt5.
66 // See https://bugreports.qt.io/browse/QTBUG-72103
67#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
68 mPitchAngle = std::clamp( pitch, 0.2f, 179.8f );
69#else
70 mPitchAngle = std::clamp( pitch, 0.0f, 180.0f );
71#endif
72}
73
74void QgsCameraPose::updateCamera( Qt3DRender::QCamera *camera )
75{
76 // first rotate by pitch angle around X axis, then by heading angle around Z axis
77 // (we use two separate fromEulerAngles() calls because one would not do rotations in order we need)
78 QQuaternion q = QQuaternion::fromEulerAngles( 0, 0, mHeadingAngle ) *
79 QQuaternion::fromEulerAngles( mPitchAngle, 0, 0 );
80 QVector3D cameraToCenter = q * QVector3D( 0, 0, -mDistanceFromCenterPoint );
81 camera->setUpVector( q * QVector3D( 0, 1, 0 ) );
82 camera->setPosition( mCenterPoint.toVector3D() - cameraToCenter );
83 camera->setViewCenter( mCenterPoint.toVector3D() );
84}
void setPitchAngle(float pitch)
Sets pitch angle in degrees.
QDomElement writeXml(QDomDocument &doc) const
Writes configuration to a new DOM element and returns it.
void setCenterPoint(const QgsVector3D &point)
Sets center point (towards which point the camera is looking)
void readXml(const QDomElement &elem)
Reads configuration from a DOM element previously written using writeXml()
void setDistanceFromCenterPoint(float distance)
Sets distance of the camera from the center point.
void updateCamera(Qt3DRender::QCamera *camera)
Update Qt3D camera view matrix based on the pose.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:50
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:52
QVector3D toVector3D() const
Converts the current object to QVector3D.
double x() const
Returns X coordinate.
Definition qgsvector3d.h:48