QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsmeshtracerenderer.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsmeshtracerenderer.h
3 -------------------------
4 begin : November 2019
5 copyright : (C) 2019 by Vincent Cloarec
6 email : vcloarec 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
18#ifndef QGSMESHTRACERENDERER_H
19#define QGSMESHTRACERENDERER_H
20
21
22#include <QVector>
23#include <QSize>
24
25#include "qgis_core.h"
26#include "qgis.h"
27#include "qgstriangularmesh.h"
28#include "qgsmeshlayer.h"
30#include "qgsmaptopixel.h"
31#include "qgsrendercontext.h"
32
33class QgsMeshLayerInterpolator;
34class QgsMeshLayerRendererFeedback;
35
37
38#ifndef SIP_RUN
39
48class QgsMeshVectorValueInterpolator
49{
50 public:
52 QgsMeshVectorValueInterpolator(
53 const QgsTriangularMesh &triangularMesh,
54 const QgsMeshDataBlock &datasetVectorValues );
55
57 QgsMeshVectorValueInterpolator(
58 const QgsTriangularMesh &triangularMesh,
59 const QgsMeshDataBlock &datasetVectorValues,
60 const QgsMeshDataBlock &scalarActiveFaceFlagValues );
61
62 QgsMeshVectorValueInterpolator( const QgsMeshVectorValueInterpolator &other );
63
65 virtual QgsMeshVectorValueInterpolator *clone() = 0;
66
67 virtual ~QgsMeshVectorValueInterpolator() = default;
68
73 virtual QgsVector vectorValue( const QgsPointXY &point ) const;
74
76 QgsMeshVectorValueInterpolator &operator=( const QgsMeshVectorValueInterpolator &other );
77
78 protected:
79 void updateCacheFaceIndex( const QgsPointXY &point ) const;
80
81 QgsTriangularMesh mTriangularMesh;
82 QgsMeshDataBlock mDatasetValues;
83 QgsMeshDataBlock mActiveFaceFlagValues;
84 mutable QgsMeshFace mFaceCache;
85 mutable int mCacheFaceIndex = -1;
86 bool mUseScalarActiveFaceFlagValues = false;
87 bool isVectorValid( const QgsVector &v ) const;
88
89 private:
90
91 void activeFaceFilter( QgsVector &vector, int faceIndex ) const;
92
93 virtual QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const = 0;
94};
95
104class QgsMeshVectorValueInterpolatorFromVertex: public QgsMeshVectorValueInterpolator
105{
106 public:
108 QgsMeshVectorValueInterpolatorFromVertex(
109 const QgsTriangularMesh &triangularMesh,
110 const QgsMeshDataBlock &datasetVectorValues );
111
113 QgsMeshVectorValueInterpolatorFromVertex(
114 const QgsTriangularMesh &triangularMesh,
115 const QgsMeshDataBlock &datasetVectorValues,
116 const QgsMeshDataBlock &scalarActiveFaceFlagValues );
117
118 QgsMeshVectorValueInterpolatorFromVertex( const QgsMeshVectorValueInterpolatorFromVertex &other );
119
121 virtual QgsMeshVectorValueInterpolatorFromVertex *clone() override;
122
123 QgsMeshVectorValueInterpolatorFromVertex &operator=( const QgsMeshVectorValueInterpolatorFromVertex &other );
124
125 private:
126 QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const override;
127};
128
137class QgsMeshVectorValueInterpolatorFromFace: public QgsMeshVectorValueInterpolator
138{
139 public:
141 QgsMeshVectorValueInterpolatorFromFace(
142 const QgsTriangularMesh &triangularMesh,
143 const QgsMeshDataBlock &datasetVectorValues );
144
146 QgsMeshVectorValueInterpolatorFromFace(
147 const QgsTriangularMesh &triangularMesh,
148 const QgsMeshDataBlock &datasetVectorValues,
149 const QgsMeshDataBlock &scalarActiveFaceFlagValues );
150
151 QgsMeshVectorValueInterpolatorFromFace( const QgsMeshVectorValueInterpolatorFromFace &other );
152
154 virtual QgsMeshVectorValueInterpolatorFromFace *clone() override;
155
156 QgsMeshVectorValueInterpolatorFromFace &operator=( const QgsMeshVectorValueInterpolatorFromFace &other );
157
158 private:
159 QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const override;
160};
161
170class QgsMeshStreamField
171{
172 public:
173 struct FieldData
174 {
175 double magnitude;
176 float time;
177 int directionX;
178 int directionY;
179 };
180
182 QgsMeshStreamField(
183 const QgsTriangularMesh &triangularMesh,
184 const QgsMeshDataBlock &dataSetVectorValues,
185 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
186 const QgsRectangle &layerExtent,
187 double magnitudeMaximum,
188 bool dataIsOnVertices,
189 const QgsRenderContext &rendererContext,
190 const QgsInterpolatedLineColor &vectorColoring,
191 int resolution = 1 );
192
193 QgsMeshStreamField( const QgsMeshStreamField &other );
194 virtual ~QgsMeshStreamField();
195
202 void updateSize( const QgsRenderContext &renderContext );
203
208 void updateSize( const QgsRenderContext &renderContext, int resolution );
209
211 bool isValid() const;
212
214 QSize size() const;
215
217 QPoint topLeft() const;
218
220 void addTrace( QPoint startPixel );
221
223 void addTrace( QgsPointXY startPoint );
224
226 void addRandomTraces();
227
229 void addRandomTrace();
230
232 void addGriddedTraces( int dx, int dy );
233
235 void addTracesOnMesh( const QgsTriangularMesh &mesh, const QgsRectangle &extent );
236
238 void setResolution( int width );
239
241 int resolution() const;
242
244 QSize imageSize() const;
245
247 virtual QImage image() const;
248
250 void setPixelFillingDensity( double maxFilling );
251
253 void setColor( QColor color );
254
256 void setLineWidth( double width );
257
259 void setFilter( double min, double max );
260
262 void setMinimizeFieldSize( bool minimizeFieldSize );
263
265 QgsMeshStreamField &operator=( const QgsMeshStreamField &other );
266
267 protected:
268 virtual void initImage();
269 QPointF fieldToDevice( const QPoint &pixel ) const;
270 bool filterMag( double value ) const;
271 bool isTraceOutside( const QPoint &pixel ) const;
272
273 private:
274 QgsPointXY positionToMapCoordinates( const QPoint &pixelPosition, const QgsPointXY &positionInPixel );
275 bool addPixelToChunkTrace( QPoint &pixel,
276 QgsMeshStreamField::FieldData &data,
277 std::list<QPair<QPoint, QgsMeshStreamField::FieldData> > &chunkTrace );
278 void setChunkTrace( std::list<QPair<QPoint, FieldData>> &chunkTrace );
279 virtual void drawTrace( const QPoint & ) const {}
280 void clearChunkTrace( std::list<QPair<QPoint, FieldData>> &chunkTrace );
281 virtual void storeInField( const QPair<QPoint, FieldData> pixelData ) = 0;
282 virtual void initField() = 0;
283 void simplifyChunkTrace( std::list<QPair<QPoint, FieldData>> &shunkTrace );
284
285 virtual bool isTraceExists( const QPoint &pixel ) const = 0;
286
287 protected:
288
289 QSize mFieldSize;
290 std::unique_ptr<QPainter> mPainter = std::unique_ptr<QPainter>( nullptr );
291 int mFieldResolution = 1;
292 QPen mPen;
293 QImage mTraceImage;
294
295 QgsMapToPixel mMapToFieldPixel;
296 QgsRectangle mOutputExtent = QgsRectangle();
297 QgsInterpolatedLineColor mVectorColoring;
298
299 /*the direction for a pixel is defined with a char value
300 *
301 * 1 2 3
302 * 4 5 6
303 * 7 8 9
304 *
305 * convenient to retrieve the indexes of the next pixel from the direction d:
306 * Xnext= (d-1)%3-1
307 * Ynext = (d-1)/3-1
308 *
309 * and the direction is defined by :
310 * d=incX + 2 + (incY+1)*3
311 */
312 QVector<unsigned char> mDirectionField;
313 QgsRenderContext mRenderContext;
314
315 private:
316 int mPixelFillingCount = 0;
317 int mMaxPixelFillingCount = 0;
318 std::unique_ptr<QgsMeshVectorValueInterpolator> mVectorValueInterpolator;
319 QgsRectangle mLayerExtent;
320 QgsRectangle mMapExtent;
321 QPoint mFieldTopLeftInDeviceCoordinates;
322 bool mValid = false;
323 double mMaximumMagnitude = 0;
324 double mPixelFillingDensity = 0;
325 double mMinMagFilter = -1;
326 double mMaxMagFilter = -1;
327 bool mMinimizeFieldSize = true; //
328};
329
338class QgsMeshStreamlinesField: public QgsMeshStreamField
339{
340 public:
342 Q_DECL_DEPRECATED QgsMeshStreamlinesField(
343 const QgsTriangularMesh &triangularMesh,
344 const QgsMeshDataBlock &datasetVectorValues,
345 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
346 const QgsRectangle &layerExtent,
347 double magMax,
348 bool dataIsOnVertices,
349 QgsRenderContext &rendererContext,
350 const QgsInterpolatedLineColor vectorColoring );
351
352 QgsMeshStreamlinesField(
353 const QgsTriangularMesh &triangularMesh,
354 const QgsMeshDataBlock &datasetVectorValues,
355 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
356 const QVector<double> &datasetMagValues,
357 const QgsRectangle &layerExtent,
358 QgsMeshLayerRendererFeedback *feedBack,
359 double magMax,
360 bool dataIsOnVertices,
361 QgsRenderContext &rendererContext,
362 const QgsInterpolatedLineColor vectorColoring );
363
364 void compose();
365
366 private:
367 void storeInField( const QPair<QPoint, FieldData> pixelData ) override;
368 void initField() override;
369 void initImage() override;
370 bool isTraceExists( const QPoint &pixel ) const override;
371 void drawTrace( const QPoint &start ) const override;
372
373 QVector<bool> mField;
374 QImage mDrawingTraceImage;
375 std::unique_ptr<QPainter> mDrawingTracePainter;
376
377 //** Needed data
378 QgsTriangularMesh mTriangularMesh;
379 QVector<double> mMagValues;
380 QgsMeshDataBlock mScalarActiveFaceFlagValues;
382 QgsMeshLayerRendererFeedback *mFeedBack = nullptr;
383
384};
385
386class QgsMeshParticleTracesField;
387
396struct QgsMeshTraceParticle
397{
398 double lifeTime = 0;
399 QPoint position;
400 std::list<QPoint> tail;
401 double remainingTime = 0; //time remaining to spend in the current pixel at the end of the time step
402};
403
412class QgsMeshParticleTracesField: public QgsMeshStreamField
413{
414 public:
416 QgsMeshParticleTracesField(
417 const QgsTriangularMesh &triangularMesh,
418 const QgsMeshDataBlock &datasetVectorValues,
419 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
420 const QgsRectangle &layerExtent,
421 double magMax,
422 bool dataIsOnVertices,
423 const QgsRenderContext &rendererContext,
424 const QgsInterpolatedLineColor vectorColoring );
425
426 QgsMeshParticleTracesField( const QgsMeshParticleTracesField &other );
427
429 void addParticle( const QPoint &startPoint, double lifeTime );
430
432 void addParticleXY( const QgsPointXY &startPoint, double lifeTime );
433
435 void addRandomParticles();
436
438 void moveParticles();
439
441 QImage imageRendered() const;
442
444 void setParticlesCount( int particlesCount );
445
447 void setParticlesLifeTime( double particlesLifeTime );
448
450 void stump();
451
457 void setStumpFactor( int sf );
458
460 void setTimeStep( double timeStep );
461
463 void setParticleSize( double particleSize );
464
466 void setTailFactor( double tailFactor );
467
469 void setMinTailLength( int minTailLength );
470
472 void setStumpParticleWithLifeTime( bool stumpParticleWithLifeTime );
473
475 void setParticlesColor( const QColor &c );
476
477 QgsMeshParticleTracesField &operator=( const QgsMeshParticleTracesField &other );
478
479 private:
480 QPoint direction( QPoint position ) const;
481
482 float time( QPoint position ) const;
483 float magnitude( QPoint position ) const;
484
485 void drawParticleTrace( const QgsMeshTraceParticle &particle );
486
487 void storeInField( const QPair<QPoint, FieldData> pixelData ) override;
488 void initField() override;
489 bool isTraceExists( const QPoint &pixel ) const override;
490
491 /* Nondimensional time
492 * This field store the time spent by the particle in the pixel
493 *
494 * This time is nondimensional and value 1 is equivalent to the time spent by the particle in a pixel
495 * for Vmax, the maximum magnitude of the vector field.
496 *
497 */
498 QVector<float> mTimeField;
499 QVector<float> mMagnitudeField;
500
501 QList<QgsMeshTraceParticle> mParticles;
502 QImage mStumpImage;
503
504 double mTimeStep = 200;
505 double mParticlesLifeTime = 5000;
506 int mParticlesCount = 1000;
507 double mTailFactor = 5;
508 int mMinTailLength = 3;
509 QColor mParticleColor = Qt::white;
510 double mParticleSize = 2.5;
511 int mStumpFactor = 50;
512 bool mStumpParticleWithLifeTime = true;
513};
514
525class QgsMeshVectorStreamlineRenderer: public QgsMeshVectorRenderer
526{
527 public:
529 Q_DECL_DEPRECATED QgsMeshVectorStreamlineRenderer(
530 const QgsTriangularMesh &triangularMesh,
531 const QgsMeshDataBlock &dataSetVectorValues,
532 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
533 bool dataIsOnVertices,
534 const QgsMeshRendererVectorSettings &settings,
535 QgsRenderContext &rendererContext,
536 const QgsRectangle &layerExtent,
537 double magMax );
538
539 QgsMeshVectorStreamlineRenderer(
540 const QgsTriangularMesh &triangularMesh,
541 const QgsMeshDataBlock &dataSetVectorValues,
542 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
543 const QVector<double> &datasetMagValues,
544 bool dataIsOnVertices,
545 const QgsMeshRendererVectorSettings &settings,
546 QgsRenderContext &rendererContext,
547 const QgsRectangle &layerExtent,
548 QgsMeshLayerRendererFeedback *feedBack,
549 double magMax );
550
551 void draw() override;
552
553 private:
554 std::unique_ptr<QgsMeshStreamlinesField> mStreamlineField;
555 QgsRenderContext &mRendererContext;
556};
557
558
569class QgsMeshVectorTraceRenderer: public QgsMeshVectorRenderer
570{
571 public:
573 QgsMeshVectorTraceRenderer(
574 const QgsTriangularMesh &triangularMesh,
575 const QgsMeshDataBlock &dataSetVectorValues,
576 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
577 bool dataIsOnVertices,
578 const QgsMeshRendererVectorSettings &settings,
579 QgsRenderContext &rendererContext,
580 const QgsRectangle &layerExtent,
581 double magMax );
582
583 void draw() override;
584
585 private:
586 std::unique_ptr<QgsMeshParticleTracesField> mParticleField;
587 QgsRenderContext &mRendererContext;
588};
589
590
591#endif //SIP_RUN
592
594
603{
604 public:
607 const QgsTriangularMesh &triangularMesh,
608 const QgsMeshDataBlock &dataSetVectorValues,
609 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
610 bool dataIsOnVertices,
611 const QgsRenderContext &rendererContext,
612 const QgsRectangle &layerExtent,
613 double magMax,
614 const QgsMeshRendererVectorSettings &vectorSettings ) SIP_SKIP;
615
618
620
622
624 void seedRandomParticles( int count );
625
628
630 void setFPS( int FPS );
631
633 void setMaxSpeedPixel( int max );
634
636 void setParticlesLifeTime( double particleLifeTime );
637
639 void setParticlesColor( const QColor &c );
640
642 void setParticlesSize( double width );
643
645 void setTailFactor( double fct );
646
648 void setMinimumTailLength( int l );
649
651 void setTailPersitence( double p );
652
654
655 private:
656 std::unique_ptr<QgsMeshParticleTracesField> mParticleField;
657 const QgsRenderContext &mRendererContext;
658 int mFPS = 15; //frame per second of the output, used to calculate orher parameters of the field
659 int mVpixMax = 2000; //is the number of pixels that are going through for 1 s
660 double mParticleLifeTime = 5;
661
662 void updateFieldParameter();
663};
664
665#endif // QGSMESHTRACERENDERER_H
Class defining color to render mesh datasets.
Perform transforms between map coordinates and device coordinates.
QgsMeshDataBlock is a block of integers/doubles that can be used to retrieve: active flags (e....
DataType
Location of where data is specified for datasets in the dataset group.
@ DataOnVertices
Data is defined on vertices.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Represents a renderer settings for vector datasets.
A wrapper for QgsMeshParticuleTracesField used to render the particles.
void setParticlesLifeTime(double particleLifeTime)
Sets maximum life time of particles in seconds.
QgsMeshVectorTraceAnimationGenerator & operator=(const QgsMeshVectorTraceAnimationGenerator &other)
void setMinimumTailLength(int l)
Sets the minimum tail length.
QgsMeshVectorTraceAnimationGenerator(const QgsMeshVectorTraceAnimationGenerator &other)
void setTailPersitence(double p)
Sets the visual persistence of the tail.
void setParticlesColor(const QColor &c)
Sets colors of particle.
QImage imageRendered()
Moves all the particles using frame per second (fps) to calculate the displacement and return the ren...
QgsMeshVectorTraceAnimationGenerator(QgsMeshLayer *layer, const QgsRenderContext &rendererContext)
Constructor to use with Python binding.
void setTailFactor(double fct)
Sets the tail factor, used to adjust the length of the tail. 0 : minimum length, >1 increase the tail...
void setFPS(int FPS)
Sets the number of frames per seconds that will be rendered.
QgsMeshVectorTraceAnimationGenerator(const QgsTriangularMesh &triangularMesh, const QgsMeshDataBlock &dataSetVectorValues, const QgsMeshDataBlock &scalarActiveFaceFlagValues, bool dataIsOnVertices, const QgsRenderContext &rendererContext, const QgsRectangle &layerExtent, double magMax, const QgsMeshRendererVectorSettings &vectorSettings)
Constructor to use from QgsMeshVectorRenderer.
void setParticlesSize(double width)
Sets particle size in px.
void setMaxSpeedPixel(int max)
Sets the max number of pixels that can be go through by the particles in 1 second.
void seedRandomParticles(int count)
seeds particles in the vector fields
A class to represent a 2D point.
Definition qgspointxy.h:60
A rectangle specified with double values.
Contains information about the context of a rendering operation.
Triangular/Derived Mesh is mesh with vertices in map coordinates.
A class to represent a vector.
Definition qgsvector.h:30
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
#define SIP_SKIP
Definition qgis_sip.h:126
QVector< int > QgsMeshFace
List of vertex indexes.