23#include <QDomDocument>
25#include <Qt3DCore/QEntity>
26#include <Qt3DExtras/QPhongMaterial>
27#include <Qt3DExtras/QSphereMesh>
28#include <Qt3DRender/QDirectionalLight>
30using namespace Qt::StringLiterals;
39 auto res = std::make_unique< QgsSunLightSettings >( *
this );
58 const float azimuthRad = M_PI * sunResult.
azimuth / 180.0;
61 const float x = std::cos( elevationRad ) * std::sin( azimuthRad );
62 const float y = std::cos( elevationRad ) * std::cos( azimuthRad );
63 const float z = std::sin( elevationRad );
64 const QVector3D sunVector( x, y, z );
65 return -sunVector.normalized();
70 Qt3DCore::QEntity *lightEntity =
new Qt3DCore::QEntity( parent );
72 Qt3DRender::QDirectionalLight *light =
new Qt3DRender::QDirectionalLight;
73 light->setColor(
color() );
74 light->setIntensity(
static_cast< float >(
intensity() ) );
75 light->setWorldDirection(
direction( map ).toVector3D() );
77 lightEntity->addComponent( light );
84 QDomElement elemLight = doc.createElement( u
"sun-light"_s );
85 elemLight.setAttribute( u
"id"_s,
mId );
87 elemLight.setAttribute( u
"intensity"_s, mIntensity );
88 elemLight.setAttribute( u
"sun-time"_s, mSunTime.toString( Qt::ISODate ) );
89 elemLight.setAttribute( u
"reference-elevation"_s, mReferenceElevation );
90 elemLight.setAttribute( u
"atmospheric-pressure"_s, mAtmosphericPressure );
91 elemLight.setAttribute( u
"temperature"_s, mTemperature );
98 if ( elem.hasAttribute( u
"id"_s ) )
99 mId = elem.attribute( u
"id"_s );
101 mIntensity = elem.attribute( u
"intensity"_s ).toDouble();
102 mSunTime = QDateTime::fromString( elem.attribute( u
"sun-time"_s ), Qt::ISODate );
103 if ( !mSunTime.isValid() )
105 mSunTime = QDateTime::currentDateTimeUtc();
107 mReferenceElevation = elem.attribute( u
"reference-elevation"_s, u
"0.0"_s ).toDouble();
108 mAtmosphericPressure = elem.attribute( u
"atmospheric-pressure"_s, u
"1013.25"_s ).toDouble();
109 mTemperature = elem.attribute( u
"temperature"_s, u
"15.0"_s ).toDouble();
115 && mSunTime == other.mSunTime
116 && mColor == other.mColor
117 && mIntensity == other.mIntensity
118 &&
qgsDoubleNear( mAtmosphericPressure, other.mAtmosphericPressure )
120 &&
qgsDoubleNear( mReferenceElevation, other.mReferenceElevation );
LightSourceType
Light source types for 3D scenes.
@ Sun
Sun based light source.
QgsRectangle extent() const
Returns the 3D scene's 2D extent in the 3D scene's CRS.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Defines a QGIS exception class.
QString mId
Unique light ID.
A container for the context for various read/write operations on objects.
bool operator==(const QgsSunLightSettings &other) const
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const override
Writes the light source's configuration to a new DOM element and returns it.
double intensity() const
Returns the base intensity of the sunlight.
QgsSunLightSettings()=default
QgsSunLightSettings * clone() const override
Returns a copy of the light source.
QColor color() const
Returns the base color of the sunlight.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext()) override
Reads configuration from a DOM element previously written using writeXml().
QgsVector3D direction(const Qgs3DMapSettings &map) const
Returns the calculated direction of the light.
Qt3DCore::QEntity * createEntity(const Qgs3DMapSettings &map, Qt3DCore::QEntity *parent) const override
Creates an entity representing the light source.
Qgis::LightSourceType type() const override
Returns the light source type.
static QgsSunPositionResult calculate(const QgsPointXY &point, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context, const QDateTime &dateTime, double elevationMeters=0.0, double pressure=1013.25, double temperature=15.0)
Calculates the solar position and events for a given point and time.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Contains the results of a solar position calculation.
double azimuth
Azimuth angle in degrees clockwise from North.
double apparentElevation
Apparent topocentric elevation angle in degrees (corrected for atmospheric refraction).