QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgstiledscenetexturerenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstiledscenetexturerenderer.h
3 --------------------
4 begin : August 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsfillsymbol.h"
21#include "qgspainting.h"
22#include "qgssymbollayerutils.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
32
34
36{
37 return u"texture"_s;
38}
39
41{
42 auto res = std::make_unique< QgsTiledSceneTextureRenderer >();
43 res->setFillSymbol( mFillSymbol->clone() );
44
45 copyCommonProperties( res.get() );
46
47 return res.release();
48}
49
51{
52 auto r = std::make_unique< QgsTiledSceneTextureRenderer >();
53 {
54 const QDomElement fillSymbolElem = element.firstChildElement( u"fillSymbol"_s );
55 if ( !fillSymbolElem.isNull() )
56 {
57 const QDomElement symbolElem = fillSymbolElem.firstChildElement( u"symbol"_s );
58 std::unique_ptr< QgsFillSymbol > fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
59 if ( fillSymbol )
60 r->mFillSymbol = std::move( fillSymbol );
61 }
62 }
63
64 r->restoreCommonProperties( element, context );
65
66 return r.release();
67}
68
70{
71 QVariantMap properties;
72 properties.insert( u"color"_s, u"224,224,224"_s );
73 properties.insert( u"style"_s, u"solid"_s );
74 properties.insert( u"style_border"_s, u"solid"_s );
75 properties.insert( u"color_border"_s, u"124,124,124"_s );
76 properties.insert( u"width_border"_s, u"0.1"_s );
77 properties.insert( u"joinstyle"_s, u"round"_s );
78
79 return QgsFillSymbol::createSimple( properties );
80}
81
83{
84 return mFillSymbol.get();
85}
86
88{
89 mFillSymbol.reset( symbol );
90}
91
92QDomElement QgsTiledSceneTextureRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
93{
94 QDomElement rendererElem = doc.createElement( u"renderer"_s );
95
96 rendererElem.setAttribute( u"type"_s, u"texture"_s );
97
98 {
99 QDomElement fillSymbolElem = doc.createElement( u"fillSymbol"_s );
100 const QDomElement symbolElement = QgsSymbolLayerUtils::saveSymbol( QString(), mFillSymbol.get(), doc, context );
101 fillSymbolElem.appendChild( symbolElement );
102 rendererElem.appendChild( fillSymbolElem );
103 }
104
105 saveCommonProperties( rendererElem, context );
106
107 return rendererElem;
108}
109
111{
112 // force raster rendering for this renderer type -- there's no benefit in exporting these layers as a bunch
113 // of triangular images which are pieced together, that adds a lot of extra content to the exports and results
114 // in files which can be extremely slow to open and render in other viewers.
116}
117
119{
120 if ( context.textureImage().isNull() )
121 {
122 mFillSymbol->renderPolygon( triangle, nullptr, nullptr, context.renderContext() );
123 return;
124 }
125
126 float textureX1;
127 float textureY1;
128 float textureX2;
129 float textureY2;
130 float textureX3;
131 float textureY3;
132 context.textureCoordinates( textureX1, textureY1, textureX2, textureY2, textureX3, textureY3 );
133
134 QPainter *painter = context.renderContext().painter();
135 painter->setPen( Qt::NoPen );
136
137 auto unitNormal = []( const QPointF p1, const QPointF p2 ) {
138 const float dx = p2.x() - p1.x();
139 const float dy = p2.y() - p1.y();
140 QPointF n( -dy, dx );
141 const double length = std::sqrt( n.x() * n.x() + n.y() * n.y() );
142 return QPointF( n.x() / length, n.y() / length );
143 };
144
145 auto intersect = []( const QPointF p1, const QPointF p2, const QPointF q1, const QPointF q2 ) {
146 const double a1 = p2.y() - p1.y();
147 const double b1 = p1.x() - p2.x();
148 const double c1 = a1 * p1.x() + b1 * p1.y();
149
150 const double a2 = q2.y() - q1.y();
151 const double b2 = q1.x() - q2.x();
152 const double c2 = a2 * q1.x() + b2 * q1.y();
153
154 const double det = a1 * b2 - a2 * b1;
155
156 if ( qgsDoubleNear( det, 0 ) )
157 {
158 return QPointF();
159 }
160 else
161 {
162 return QPointF( ( b2 * c1 - b1 * c2 ) / det, ( a1 * c2 - a2 * c1 ) / det );
163 }
164 };
165
166 auto smallestAngleInTriangle = []( const QPolygonF &triangle ) {
167 const QPointF p1 = triangle.at( 0 );
168 const QPointF p2 = triangle.at( 1 );
169 const QPointF p3 = triangle.at( 2 );
170
171 const QPointF v1 = p2 - p1;
172 const QPointF v2 = p3 - p2;
173 const QPointF v3 = p1 - p3;
174
175 const double a = std::sqrt( v1.x() * v1.x() + v1.y() * v1.y() );
176 const double b = std::sqrt( v2.x() * v2.x() + v2.y() * v2.y() );
177 const double c = std::sqrt( v3.x() * v3.x() + v3.y() * v3.y() );
178
179 return std::min( std::min( std::acos( ( b * b + c * c - a * a ) / ( 2 * b * c ) ), std::acos( ( a * a + c * c - b * b ) / ( 2 * a * c ) ) ), std::acos( ( a * a + b * b - c * c ) / ( 2 * a * b ) ) );
180 };
181
182 auto growTriangle = [&unitNormal, &intersect]( const QPolygonF &triangle, float pixels ) {
183 QPair< QPointF, QPointF > offsetEdges[3];
184 for ( int i = 0; i < 3; ++i )
185 {
186 const QPointF p1 = triangle.at( i );
187 const QPointF p2 = triangle.at( i + 1 );
188 const QPointF n = unitNormal( p1, p2 );
189
190 const QPointF offsetP1( p1.x() + n.x() * pixels, p1.y() + n.y() * pixels );
191 const QPointF offsetP2( p2.x() + n.x() * pixels, p2.y() + n.y() * pixels );
192
193 offsetEdges[i] = { offsetP1, offsetP2 };
194 }
195
196 QPolygonF result;
197 result.reserve( 4 );
198 // limit triangle vertices to shifting AT MOST 2 pixels from their original locations, to avoid narrow triangles
199 // getting buffered too large
200 static double constexpr MAX_TRIANGLE_GROW_PIXELS_SQUARED = 3 * 3;
201 for ( int i = 0; i < 3; ++i )
202 {
203 const auto &edge1 = offsetEdges[i];
204 const auto &edge2 = offsetEdges[i == 0 ? 2 : ( i - 1 )];
205
206 const QPointF vertex = intersect( edge1.first, edge1.second, edge2.first, edge2.second );
207 if ( vertex.isNull() )
208 return triangle;
209
210 const QPointF originalPoint = triangle.at( i );
211
212 // don't allow triangle to grow too large
213 double delta = std::pow( vertex.x() - originalPoint.x(), 2 ) + std::pow( vertex.y() - originalPoint.y(), 2 );
214 if ( delta > MAX_TRIANGLE_GROW_PIXELS_SQUARED )
215 {
216 double dx = ( vertex.x() - originalPoint.x() ) * MAX_TRIANGLE_GROW_PIXELS_SQUARED / delta;
217 double dy = ( vertex.y() - originalPoint.y() ) * MAX_TRIANGLE_GROW_PIXELS_SQUARED / delta;
218 result << triangle.at( i ) + QPointF( dx, dy );
219 }
220 else
221 result << vertex;
222 }
223 result << result.at( 0 );
224 return result;
225 };
226
227 // buffer the triangles out slightly to reduce artifacts caused by antialiasing,
228 // but try to avoid new artifacts caused by buffering narrow triangles
229 const double minAngle = smallestAngleInTriangle( triangle ) * 180 / M_PI;
230 if ( std::isnan( minAngle ) || minAngle < 0.1 )
231 {
232 // don't try to draw slivers
233 return;
234 }
235
236 QgsPainting::drawTriangleUsingTexture( painter, growTriangle( triangle, 1 ), context.textureImage(), textureX1, textureY1, textureX2, textureY2, textureX3, textureY3 );
237}
238
241
243{
245 mFillSymbol->startRender( context.renderContext() );
246}
247
249{
250 mFillSymbol->stopRender( context.renderContext() );
251
253}
@ RequiresTextures
Renderer requires textures.
Definition qgis.h:6057
@ ForceRasterRender
Layer should always be rendered as a raster image.
Definition qgis.h:6058
@ RendersTriangles
Renderer can render triangle primitives.
Definition qgis.h:6059
QFlags< TiledSceneRendererFlag > TiledSceneRendererFlags
Flags which control how tiled scene 2D renderers behave.
Definition qgis.h:6069
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
static bool drawTriangleUsingTexture(QPainter *painter, const QPolygonF &triangle, const QImage &textureImage, float textureX1, float textureY1, float textureX2, float textureY2, float textureX3, float textureY3)
Draws a triangle onto a painter using a mapped texture image.
A container for the context for various read/write operations on objects.
QPainter * painter()
Returns the destination QPainter for the render operation.
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Encapsulates the render context for a 2D tiled scene rendering operation.
void textureCoordinates(float &textureX1, float &textureY1, float &textureX2, float &textureY2, float &textureX3, float &textureY3) const
Returns the current texture coordinates.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
QImage textureImage() const
Returns the current texture image.
void saveCommonProperties(QDomElement &element, const QgsReadWriteContext &context) const
Saves common renderer properties (such as point size and screen error) to the specified DOM element.
virtual void stopRender(QgsTiledSceneRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
QgsTiledSceneRenderer()=default
virtual void startRender(QgsTiledSceneRenderContext &context)
Must be called when a new render cycle is started.
void copyCommonProperties(QgsTiledSceneRenderer *destination) const
Copies common tiled scene renderer properties (such as screen error) to the destination renderer.
void startRender(QgsTiledSceneRenderContext &context) override
Must be called when a new render cycle is started.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Saves the renderer configuration to an XML element.
~QgsTiledSceneTextureRenderer() override
QgsTiledSceneTextureRenderer()
Constructor for QgsTiledSceneTextureRenderer.
QgsFillSymbol * fillSymbol() const
Returns the fill symbol used to render triangles without textures.
void renderLine(QgsTiledSceneRenderContext &context, const QPolygonF &line) override
Renders a line.
QgsTiledSceneRenderer * clone() const override
Create a deep copy of this renderer.
void setFillSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used to render triangles without textures.
QString type() const override
Returns the identifier of the renderer type.
static std::unique_ptr< QgsFillSymbol > createDefaultFillSymbol()
Returns a copy of the default fill symbol used to render triangles without textures.
static QgsTiledSceneRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates a textured renderer from an XML element.
void stopRender(QgsTiledSceneRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
void renderTriangle(QgsTiledSceneRenderContext &context, const QPolygonF &triangle) override
Renders a triangle.
Qgis::TiledSceneRendererFlags flags() const override
Returns flags which control how the renderer behaves.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975