QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
3d
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 "
qgs3dutils.h
"
19
20
#include <QDomDocument>
21
#include <QString>
22
#include <Qt3DRender/QCamera>
23
24
using namespace
Qt::StringLiterals;
25
26
QDomElement
QgsCameraPose::writeXml
( QDomDocument &doc )
const
27
{
28
QDomElement elemCamera = doc.createElement( u
"camera-pose"
_s );
29
elemCamera.setAttribute( u
"x"
_s, mCenterPoint.x() );
30
elemCamera.setAttribute( u
"y"
_s, mCenterPoint.y() );
31
elemCamera.setAttribute( u
"z"
_s, mCenterPoint.z() );
32
elemCamera.setAttribute( u
"dist"
_s, mDistanceFromCenterPoint );
33
elemCamera.setAttribute( u
"pitch"
_s, mPitchAngle );
34
elemCamera.setAttribute( u
"heading"
_s, mHeadingAngle );
35
return
elemCamera;
36
}
37
38
void
QgsCameraPose::readXml
(
const
QDomElement &elem )
39
{
40
const
double
x = elem.attribute( u
"x"
_s ).toDouble();
41
const
double
y = elem.attribute( u
"y"
_s ).toDouble();
42
const
double
z = elem.attribute( u
"z"
_s ).toDouble();
43
mCenterPoint =
QgsVector3D
( x, y, z );
44
45
mDistanceFromCenterPoint = elem.attribute( u
"dist"
_s ).toFloat();
46
mPitchAngle = elem.attribute( u
"pitch"
_s ).toFloat();
47
mHeadingAngle = elem.attribute( u
"heading"
_s ).toFloat();
48
}
49
50
void
QgsCameraPose::setCenterPoint
(
const
QgsVector3D
&point )
51
{
52
// something went horribly wrong. Prevent further errors
53
if
( std::isnan( point.
x
() ) || std::isnan( point.
y
() ) || std::isnan( point.
z
() ) )
54
qWarning() <<
"Not updating camera position: it cannot be NaN!"
;
55
else
56
mCenterPoint = point;
57
}
58
59
void
QgsCameraPose::setDistanceFromCenterPoint
(
float
distance )
60
{
61
mDistanceFromCenterPoint = std::max( distance, 10.0f );
62
}
63
64
void
QgsCameraPose::setPitchAngle
(
float
pitch )
65
{
66
// prevent going over the head
67
mPitchAngle = std::clamp( pitch, 0.0f, 180.0f );
68
}
69
70
void
QgsCameraPose::updateCamera
( Qt3DRender::QCamera *camera )
71
{
72
// first rotate by pitch angle around X axis, then by heading angle around Z axis
73
QQuaternion q =
Qgs3DUtils::rotationFromPitchHeadingAngles
( mPitchAngle, mHeadingAngle );
74
QVector3D cameraToCenter = q * QVector3D( 0, 0, -mDistanceFromCenterPoint );
75
camera->setUpVector( q * QVector3D( 0, 1, 0 ) );
76
camera->setPosition( mCenterPoint.toVector3D() - cameraToCenter );
77
camera->setViewCenter( mCenterPoint.toVector3D() );
78
}
79
80
void
QgsCameraPose::updateCameraGlobe
( Qt3DRender::QCamera *camera,
double
lat,
double
lon )
81
{
82
// how the camera setup works:
83
// - we are using ECEF coordinates (https://en.wikipedia.org/wiki/Earth-centered,_Earth-fixed_coordinate_system)
84
// - point (0,0,0) is in the center of reference ellipsoid
85
// - X and Y axes define equator plane
86
// - X axis grows towards the prime meridian (zero longitude)
87
// - Y axis grows towards 90 degrees of longitude
88
// - Z axis grows towards the north pole
89
90
QVector3D viewCenter = mCenterPoint.toVector3D();
91
92
// rotate camera so that it is looking towards the tangent plane to ellipsoid at given
93
// lat/lon coordinates
94
QQuaternion qLatLon = QQuaternion::fromAxisAndAngle( QVector3D( 0, 0, 1 ),
static_cast<
float
>
( lon ) ) * QQuaternion::fromAxisAndAngle( QVector3D( 0, -1, 0 ),
static_cast<
float
>
( lat ) );
95
96
// rotate camera using the pitch and heading angles
97
QQuaternion qPitchHeading = QQuaternion::fromAxisAndAngle( QVector3D( 1, 0, 0 ), mHeadingAngle ) * QQuaternion::fromAxisAndAngle( QVector3D( 0, 1, 0 ), mPitchAngle );
98
99
// combine the two rotations (the order is important: pitch/heading is applied first)
100
QQuaternion q = qLatLon * qPitchHeading;
101
102
QVector3D cameraToCenter = ( q * QVector3D( -1, 0, 0 ) ) * mDistanceFromCenterPoint;
103
camera->setUpVector( q * QVector3D( 0, 0, 1 ) );
104
camera->setPosition( viewCenter - cameraToCenter );
105
camera->setViewCenter( viewCenter );
106
}
Qgs3DUtils::rotationFromPitchHeadingAngles
static QQuaternion rotationFromPitchHeadingAngles(float pitchAngle, float headingAngle)
Returns rotation quaternion that performs rotation around X axis by pitchAngle, followed by rotation ...
Definition
qgs3dutils.cpp:1126
QgsCameraPose::updateCameraGlobe
void updateCameraGlobe(Qt3DRender::QCamera *camera, double lat, double lon)
Updates camera when using a globe scene.
Definition
qgscamerapose.cpp:80
QgsCameraPose::setPitchAngle
void setPitchAngle(float pitch)
Sets pitch angle in degrees.
Definition
qgscamerapose.cpp:64
QgsCameraPose::writeXml
QDomElement writeXml(QDomDocument &doc) const
Writes configuration to a new DOM element and returns it.
Definition
qgscamerapose.cpp:26
QgsCameraPose::setCenterPoint
void setCenterPoint(const QgsVector3D &point)
Sets center point (towards which point the camera is looking).
Definition
qgscamerapose.cpp:50
QgsCameraPose::readXml
void readXml(const QDomElement &elem)
Reads configuration from a DOM element previously written using writeXml().
Definition
qgscamerapose.cpp:38
QgsCameraPose::setDistanceFromCenterPoint
void setDistanceFromCenterPoint(float distance)
Sets distance of the camera from the center point.
Definition
qgscamerapose.cpp:59
QgsCameraPose::updateCamera
void updateCamera(Qt3DRender::QCamera *camera)
Update Qt3D camera view matrix based on the pose.
Definition
qgscamerapose.cpp:70
QgsVector3D
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition
qgsvector3d.h:33
QgsVector3D::y
double y() const
Returns Y coordinate.
Definition
qgsvector3d.h:60
QgsVector3D::z
double z() const
Returns Z coordinate.
Definition
qgsvector3d.h:62
QgsVector3D::x
double x() const
Returns X coordinate.
Definition
qgsvector3d.h:58
qgs3dutils.h
qgscamerapose.h
Generated on
for QGIS API Documentation by
1.17.0