QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
63 QgsMeshVectorValueInterpolator( const QgsMeshVectorValueInterpolator &other );
64
66 virtual QgsMeshVectorValueInterpolator *clone() = 0;
67
69 virtual ~QgsMeshVectorValueInterpolator() = default;
70
75 virtual QgsVector vectorValue( const QgsPointXY &point ) const;
76
78 QgsMeshVectorValueInterpolator &operator=( const QgsMeshVectorValueInterpolator &other );
79
80 protected:
81 void updateCacheFaceIndex( const QgsPointXY &point ) const;
82
83 QgsTriangularMesh mTriangularMesh;
84 QgsMeshDataBlock mDatasetValues;
85 QgsMeshDataBlock mActiveFaceFlagValues;
86 mutable QgsMeshFace mFaceCache;
87 mutable int mCacheFaceIndex = -1;
88 bool mUseScalarActiveFaceFlagValues = false;
89 bool isVectorValid( const QgsVector &v ) const;
90
91 private:
92
93 void activeFaceFilter( QgsVector &vector, int faceIndex ) const;
94
95 virtual QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const = 0;
96};
97
106class QgsMeshVectorValueInterpolatorFromVertex: public QgsMeshVectorValueInterpolator
107{
108 public:
110 QgsMeshVectorValueInterpolatorFromVertex(
111 const QgsTriangularMesh &triangularMesh,
112 const QgsMeshDataBlock &datasetVectorValues );
113
115 QgsMeshVectorValueInterpolatorFromVertex(
116 const QgsTriangularMesh &triangularMesh,
117 const QgsMeshDataBlock &datasetVectorValues,
118 const QgsMeshDataBlock &scalarActiveFaceFlagValues );
119
121 QgsMeshVectorValueInterpolatorFromVertex( const QgsMeshVectorValueInterpolatorFromVertex &other );
122
124 virtual QgsMeshVectorValueInterpolatorFromVertex *clone() override;
125
127 QgsMeshVectorValueInterpolatorFromVertex &operator=( const QgsMeshVectorValueInterpolatorFromVertex &other );
128
129 private:
130 QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const override;
131};
132
141class QgsMeshVectorValueInterpolatorFromFace: public QgsMeshVectorValueInterpolator
142{
143 public:
145 QgsMeshVectorValueInterpolatorFromFace(
146 const QgsTriangularMesh &triangularMesh,
147 const QgsMeshDataBlock &datasetVectorValues );
148
150 QgsMeshVectorValueInterpolatorFromFace(
151 const QgsTriangularMesh &triangularMesh,
152 const QgsMeshDataBlock &datasetVectorValues,
153 const QgsMeshDataBlock &scalarActiveFaceFlagValues );
154
156 QgsMeshVectorValueInterpolatorFromFace( const QgsMeshVectorValueInterpolatorFromFace &other );
157
159 virtual QgsMeshVectorValueInterpolatorFromFace *clone() override;
160
162 QgsMeshVectorValueInterpolatorFromFace &operator=( const QgsMeshVectorValueInterpolatorFromFace &other );
163
164 private:
165 QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const override;
166};
167
176class QgsMeshStreamField
177{
178 public:
179 struct FieldData
180 {
181 double magnitude;
182 float time;
183 int directionX;
184 int directionY;
185 };
186
188 QgsMeshStreamField(
189 const QgsTriangularMesh &triangularMesh,
190 const QgsMeshDataBlock &dataSetVectorValues,
191 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
192 const QgsRectangle &layerExtent,
193 double magnitudeMaximum,
194 bool dataIsOnVertices,
195 const QgsRenderContext &rendererContext,
196 const QgsInterpolatedLineColor &vectorColoring,
197 int resolution = 1 );
198
200 QgsMeshStreamField( const QgsMeshStreamField &other );
201
203 virtual ~QgsMeshStreamField();
204
211 void updateSize( const QgsRenderContext &renderContext );
212
217 void updateSize( const QgsRenderContext &renderContext, int resolution );
218
220 bool isValid() const;
221
223 QSize size() const;
224
226 QPoint topLeft() const;
227
229 void addTrace( QPoint startPixel );
230
232 void addTrace( QgsPointXY startPoint );
233
235 void addRandomTraces();
236
238 void addRandomTrace();
239
241 void addGriddedTraces( int dx, int dy );
242
244 void addTracesOnMesh( const QgsTriangularMesh &mesh, const QgsRectangle &extent );
245
247 void setResolution( int width );
248
250 int resolution() const;
251
253 QSize imageSize() const;
254
256 virtual QImage image() const;
257
259 void setPixelFillingDensity( double maxFilling );
260
262 void setColor( QColor color );
263
265 void setLineWidth( double width );
266
268 void setFilter( double min, double max );
269
271 void setMinimizeFieldSize( bool minimizeFieldSize );
272
274 QgsMeshStreamField &operator=( const QgsMeshStreamField &other );
275
276 protected:
277 virtual void initImage();
278 QPointF fieldToDevice( const QPoint &pixel ) const;
279 bool filterMag( double value ) const;
280 bool isTraceOutside( const QPoint &pixel ) const;
281
282 private:
283 QgsPointXY positionToMapCoordinates( const QPoint &pixelPosition, const QgsPointXY &positionInPixel );
284 bool addPixelToChunkTrace( QPoint &pixel,
285 QgsMeshStreamField::FieldData &data,
286 std::list<QPair<QPoint, QgsMeshStreamField::FieldData> > &chunkTrace );
287 void setChunkTrace( std::list<QPair<QPoint, FieldData>> &chunkTrace );
288 virtual void drawTrace( const QPoint & ) const {}
289 void clearChunkTrace( std::list<QPair<QPoint, FieldData>> &chunkTrace );
290 virtual void storeInField( const QPair<QPoint, FieldData> pixelData ) = 0;
291 virtual void initField() = 0;
292 void simplifyChunkTrace( std::list<QPair<QPoint, FieldData>> &shunkTrace );
293
294 virtual bool isTraceExists( const QPoint &pixel ) const = 0;
295
296 protected:
297
298 QSize mFieldSize;
299 std::unique_ptr<QPainter> mPainter = std::unique_ptr<QPainter>( nullptr );
300 int mFieldResolution = 1;
301 QPen mPen;
302 QImage mTraceImage;
303
304 QgsMapToPixel mMapToFieldPixel;
305 QgsRectangle mOutputExtent = QgsRectangle();
306 QgsInterpolatedLineColor mVectorColoring;
307
308 /*the direction for a pixel is defined with a char value
309 *
310 * 1 2 3
311 * 4 5 6
312 * 7 8 9
313 *
314 * convenient to retrieve the indexes of the next pixel from the direction d:
315 * Xnext= (d-1)%3-1
316 * Ynext = (d-1)/3-1
317 *
318 * and the direction is defined by :
319 * d=incX + 2 + (incY+1)*3
320 */
321 QVector<unsigned char> mDirectionField;
322 QgsRenderContext mRenderContext;
323
324 private:
325 int mPixelFillingCount = 0;
326 int mMaxPixelFillingCount = 0;
327 std::unique_ptr<QgsMeshVectorValueInterpolator> mVectorValueInterpolator;
328 QgsRectangle mLayerExtent;
329 QgsRectangle mMapExtent;
330 QPoint mFieldTopLeftInDeviceCoordinates;
331 bool mValid = false;
332 double mMaximumMagnitude = 0;
333 double mPixelFillingDensity = 0;
334 double mMinMagFilter = -1;
335 double mMaxMagFilter = -1;
336 bool mMinimizeFieldSize = true; //
337};
338
347class QgsMeshStreamlinesField: public QgsMeshStreamField
348{
349 public:
351 Q_DECL_DEPRECATED QgsMeshStreamlinesField(
352 const QgsTriangularMesh &triangularMesh,
353 const QgsMeshDataBlock &datasetVectorValues,
354 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
355 const QgsRectangle &layerExtent,
356 double magMax,
357 bool dataIsOnVertices,
358 QgsRenderContext &rendererContext,
359 const QgsInterpolatedLineColor vectorColoring );
360
361 QgsMeshStreamlinesField(
362 const QgsTriangularMesh &triangularMesh,
363 const QgsMeshDataBlock &datasetVectorValues,
364 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
365 const QVector<double> &datasetMagValues,
366 const QgsRectangle &layerExtent,
367 QgsMeshLayerRendererFeedback *feedBack,
368 double magMax,
369 bool dataIsOnVertices,
370 QgsRenderContext &rendererContext,
371 const QgsInterpolatedLineColor vectorColoring );
372
373 void compose();
374
375 private:
376 void storeInField( const QPair<QPoint, FieldData> pixelData ) override;
377 void initField() override;
378 void initImage() override;
379 bool isTraceExists( const QPoint &pixel ) const override;
380 void drawTrace( const QPoint &start ) const override;
381
382 QVector<bool> mField;
383 QImage mDrawingTraceImage;
384 std::unique_ptr<QPainter> mDrawingTracePainter;
385
386 //** Needed data
387 QgsTriangularMesh mTriangularMesh;
388 QVector<double> mMagValues;
389 QgsMeshDataBlock mScalarActiveFaceFlagValues;
391 QgsMeshLayerRendererFeedback *mFeedBack = nullptr;
392
393};
394
395class QgsMeshParticleTracesField;
396
405struct QgsMeshTraceParticle
406{
407 double lifeTime = 0;
408 QPoint position;
409 std::list<QPoint> tail;
410 double remainingTime = 0; //time remaining to spend in the current pixel at the end of the time step
411};
412
421class QgsMeshParticleTracesField: public QgsMeshStreamField
422{
423 public:
425 QgsMeshParticleTracesField(
426 const QgsTriangularMesh &triangularMesh,
427 const QgsMeshDataBlock &datasetVectorValues,
428 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
429 const QgsRectangle &layerExtent,
430 double magMax,
431 bool dataIsOnVertices,
432 const QgsRenderContext &rendererContext,
433 const QgsInterpolatedLineColor vectorColoring );
434
436 QgsMeshParticleTracesField( const QgsMeshParticleTracesField &other );
437
439 void addParticle( const QPoint &startPoint, double lifeTime );
440
442 void addParticleXY( const QgsPointXY &startPoint, double lifeTime );
443
445 void addRandomParticles();
446
448 void moveParticles();
449
451 QImage imageRendered() const;
452
454 void setParticlesCount( int particlesCount );
455
457 void setParticlesLifeTime( double particlesLifeTime );
458
460 void stump();
461
467 void setStumpFactor( int sf );
468
470 void setTimeStep( double timeStep );
471
473 void setParticleSize( double particleSize );
474
476 void setTailFactor( double tailFactor );
477
479 void setMinTailLength( int minTailLength );
480
482 void setStumpParticleWithLifeTime( bool stumpParticleWithLifeTime );
483
485 void setParticlesColor( const QColor &c );
486
487 QgsMeshParticleTracesField &operator=( const QgsMeshParticleTracesField &other );
488
489 private:
490 QPoint direction( QPoint position ) const;
491
492 float time( QPoint position ) const;
493 float magnitude( QPoint position ) const;
494
495 void drawParticleTrace( const QgsMeshTraceParticle &particle );
496
497 void storeInField( const QPair<QPoint, FieldData> pixelData ) override;
498 void initField() override;
499 bool isTraceExists( const QPoint &pixel ) const override;
500
501 /* Nondimensional time
502 * This field store the time spent by the particle in the pixel
503 *
504 * This time is nondimensional and value 1 is equivalent to the time spent by the particle in a pixel
505 * for Vmax, the maximum magnitude of the vector field.
506 *
507 */
508 QVector<float> mTimeField;
509 QVector<float> mMagnitudeField;
510
511 QList<QgsMeshTraceParticle> mParticles;
512 QImage mStumpImage;
513
514 double mTimeStep = 200;
515 double mParticlesLifeTime = 5000;
516 int mParticlesCount = 1000;
517 double mTailFactor = 5;
518 int mMinTailLength = 3;
519 QColor mParticleColor = Qt::white;
520 double mParticleSize = 2.5;
521 int mStumpFactor = 50;
522 bool mStumpParticleWithLifeTime = true;
523};
524
535class QgsMeshVectorStreamlineRenderer: public QgsMeshVectorRenderer
536{
537 public:
539 Q_DECL_DEPRECATED QgsMeshVectorStreamlineRenderer(
540 const QgsTriangularMesh &triangularMesh,
541 const QgsMeshDataBlock &dataSetVectorValues,
542 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
543 bool dataIsOnVertices,
544 const QgsMeshRendererVectorSettings &settings,
545 QgsRenderContext &rendererContext,
546 const QgsRectangle &layerExtent,
547 double magMax );
548
549 QgsMeshVectorStreamlineRenderer(
550 const QgsTriangularMesh &triangularMesh,
551 const QgsMeshDataBlock &dataSetVectorValues,
552 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
553 const QVector<double> &datasetMagValues,
554 bool dataIsOnVertices,
555 const QgsMeshRendererVectorSettings &settings,
556 QgsRenderContext &rendererContext,
557 const QgsRectangle &layerExtent,
558 QgsMeshLayerRendererFeedback *feedBack,
559 double magMax );
560
561 void draw() override;
562
563 private:
564 std::unique_ptr<QgsMeshStreamlinesField> mStreamlineField;
565 QgsRenderContext &mRendererContext;
566};
567
568
579class QgsMeshVectorTraceRenderer: public QgsMeshVectorRenderer
580{
581 public:
583 QgsMeshVectorTraceRenderer(
584 const QgsTriangularMesh &triangularMesh,
585 const QgsMeshDataBlock &dataSetVectorValues,
586 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
587 bool dataIsOnVertices,
588 const QgsMeshRendererVectorSettings &settings,
589 QgsRenderContext &rendererContext,
590 const QgsRectangle &layerExtent,
591 double magMax );
592
593 void draw() override;
594
595 private:
596 std::unique_ptr<QgsMeshParticleTracesField> mParticleField;
597 QgsRenderContext &mRendererContext;
598};
599
600
601#endif //SIP_RUN
602
604
613{
614 public:
617 const QgsTriangularMesh &triangularMesh,
618 const QgsMeshDataBlock &dataSetVectorValues,
619 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
620 bool dataIsOnVertices,
621 const QgsRenderContext &rendererContext,
622 const QgsRectangle &layerExtent,
623 double magMax,
624 const QgsMeshRendererVectorSettings &vectorSettings ) SIP_SKIP;
625
628
631
634
636 void seedRandomParticles( int count );
637
640
642 void setFPS( int FPS );
643
645 void setMaxSpeedPixel( int max );
646
648 void setParticlesLifeTime( double particleLifeTime );
649
651 void setParticlesColor( const QColor &c );
652
654 void setParticlesSize( double width );
655
657 void setTailFactor( double fct );
658
660 void setMinimumTailLength( int l );
661
663 void setTailPersitence( double p );
664
667
668 private:
669 std::unique_ptr<QgsMeshParticleTracesField> mParticleField;
670 const QgsRenderContext &mRendererContext;
671 int mFPS = 15; //frame per second of the output, used to calculate orher parameters of the field
672 int mVpixMax = 2000; //is the number of pixels that are going through for 1 s
673 double mParticleLifeTime = 5;
674
675 void updateFieldParameter();
676};
677
678#endif // QGSMESHTRACERENDERER_H
Class defining color to render mesh datasets.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
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.
Definition: qgsmeshlayer.h:101
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)
Assignment operator.
void setMinimumTailLength(int l)
Sets the minimum tail length.
QgsMeshVectorTraceAnimationGenerator(const QgsMeshVectorTraceAnimationGenerator &other)
Copy constructor.
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...
~QgsMeshVectorTraceAnimationGenerator()=default
Destructor.
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.
Definition: qgsrectangle.h:42
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.