QGIS API Documentation 3.41.0-Master (cea29feecf2)
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( const QgsTriangularMesh &triangularMesh,
343 const QgsMeshDataBlock &datasetVectorValues,
344 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
345 const QgsRectangle &layerExtent,
346 double magMax,
347 bool dataIsOnVertices,
348 QgsRenderContext &rendererContext,
349 const QgsInterpolatedLineColor &vectorColoring );
350
351 QgsMeshStreamlinesField( const QgsTriangularMesh &triangularMesh,
352 const QgsMeshDataBlock &datasetVectorValues,
353 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
354 const QVector<double> &datasetMagValues,
355 const QgsRectangle &layerExtent,
356 QgsMeshLayerRendererFeedback *feedBack,
357 double magMax,
358 bool dataIsOnVertices,
359 QgsRenderContext &rendererContext,
360 const QgsInterpolatedLineColor &vectorColoring );
361
362 void compose();
363
364 private:
365 void storeInField( const QPair<QPoint, FieldData> pixelData ) override;
366 void initField() override;
367 void initImage() override;
368 bool isTraceExists( const QPoint &pixel ) const override;
369 void drawTrace( const QPoint &start ) const override;
370
371 QVector<bool> mField;
372 QImage mDrawingTraceImage;
373 std::unique_ptr<QPainter> mDrawingTracePainter;
374
375 //** Needed data
376 QgsTriangularMesh mTriangularMesh;
377 QVector<double> mMagValues;
378 QgsMeshDataBlock mScalarActiveFaceFlagValues;
380 QgsMeshLayerRendererFeedback *mFeedBack = nullptr;
381
382};
383
384class QgsMeshParticleTracesField;
385
394struct QgsMeshTraceParticle
395{
396 double lifeTime = 0;
397 QPoint position;
398 std::list<QPoint> tail;
399 double remainingTime = 0; //time remaining to spend in the current pixel at the end of the time step
400};
401
410class QgsMeshParticleTracesField: public QgsMeshStreamField
411{
412 public:
414 QgsMeshParticleTracesField( const QgsTriangularMesh &triangularMesh,
415 const QgsMeshDataBlock &datasetVectorValues,
416 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
417 const QgsRectangle &layerExtent,
418 double magMax,
419 bool dataIsOnVertices,
420 const QgsRenderContext &rendererContext,
421 const QgsInterpolatedLineColor &vectorColoring );
422
423 QgsMeshParticleTracesField( const QgsMeshParticleTracesField &other );
424
426 void addParticle( const QPoint &startPoint, double lifeTime );
427
429 void addParticleXY( const QgsPointXY &startPoint, double lifeTime );
430
432 void addRandomParticles();
433
435 void moveParticles();
436
438 QImage imageRendered() const;
439
441 void setParticlesCount( int particlesCount );
442
444 void setParticlesLifeTime( double particlesLifeTime );
445
447 void stump();
448
454 void setStumpFactor( int sf );
455
457 void setTimeStep( double timeStep );
458
460 void setParticleSize( double particleSize );
461
463 void setTailFactor( double tailFactor );
464
466 void setMinTailLength( int minTailLength );
467
469 void setStumpParticleWithLifeTime( bool stumpParticleWithLifeTime );
470
472 void setParticlesColor( const QColor &c );
473
474 QgsMeshParticleTracesField &operator=( const QgsMeshParticleTracesField &other );
475
476 private:
477 QPoint direction( QPoint position ) const;
478
479 float time( QPoint position ) const;
480 float magnitude( QPoint position ) const;
481
482 void drawParticleTrace( const QgsMeshTraceParticle &particle );
483
484 void storeInField( const QPair<QPoint, FieldData> pixelData ) override;
485 void initField() override;
486 bool isTraceExists( const QPoint &pixel ) const override;
487
488 /* Nondimensional time
489 * This field store the time spent by the particle in the pixel
490 *
491 * This time is nondimensional and value 1 is equivalent to the time spent by the particle in a pixel
492 * for Vmax, the maximum magnitude of the vector field.
493 *
494 */
495 QVector<float> mTimeField;
496 QVector<float> mMagnitudeField;
497
498 QList<QgsMeshTraceParticle> mParticles;
499 QImage mStumpImage;
500
501 double mTimeStep = 200;
502 double mParticlesLifeTime = 5000;
503 int mParticlesCount = 1000;
504 double mTailFactor = 5;
505 int mMinTailLength = 3;
506 QColor mParticleColor = Qt::white;
507 double mParticleSize = 2.5;
508 int mStumpFactor = 50;
509 bool mStumpParticleWithLifeTime = true;
510};
511
522class QgsMeshVectorStreamlineRenderer: public QgsMeshVectorRenderer
523{
524 public:
526 Q_DECL_DEPRECATED QgsMeshVectorStreamlineRenderer(
527 const QgsTriangularMesh &triangularMesh,
528 const QgsMeshDataBlock &dataSetVectorValues,
529 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
530 bool dataIsOnVertices,
531 const QgsMeshRendererVectorSettings &settings,
532 QgsRenderContext &rendererContext,
533 const QgsRectangle &layerExtent,
534 double magMax );
535
536 QgsMeshVectorStreamlineRenderer(
537 const QgsTriangularMesh &triangularMesh,
538 const QgsMeshDataBlock &dataSetVectorValues,
539 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
540 const QVector<double> &datasetMagValues,
541 bool dataIsOnVertices,
542 const QgsMeshRendererVectorSettings &settings,
543 QgsRenderContext &rendererContext,
544 const QgsRectangle &layerExtent,
545 QgsMeshLayerRendererFeedback *feedBack,
546 double magMax );
547
548 void draw() override;
549
550 private:
551 std::unique_ptr<QgsMeshStreamlinesField> mStreamlineField;
552 QgsRenderContext &mRendererContext;
553};
554
555
566class QgsMeshVectorTraceRenderer: public QgsMeshVectorRenderer
567{
568 public:
570 QgsMeshVectorTraceRenderer(
571 const QgsTriangularMesh &triangularMesh,
572 const QgsMeshDataBlock &dataSetVectorValues,
573 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
574 bool dataIsOnVertices,
575 const QgsMeshRendererVectorSettings &settings,
576 QgsRenderContext &rendererContext,
577 const QgsRectangle &layerExtent,
578 double magMax );
579
580 void draw() override;
581
582 private:
583 std::unique_ptr<QgsMeshParticleTracesField> mParticleField;
584 QgsRenderContext &mRendererContext;
585};
586
587
588#endif //SIP_RUN
589
591
600{
601 public:
604 const QgsTriangularMesh &triangularMesh,
605 const QgsMeshDataBlock &dataSetVectorValues,
606 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
607 bool dataIsOnVertices,
608 const QgsRenderContext &rendererContext,
609 const QgsRectangle &layerExtent,
610 double magMax,
611 const QgsMeshRendererVectorSettings &vectorSettings ) SIP_SKIP;
612
615
617
619
621 void seedRandomParticles( int count );
622
625
627 void setFPS( int FPS );
628
630 void setMaxSpeedPixel( int max );
631
633 void setParticlesLifeTime( double particleLifeTime );
634
636 void setParticlesColor( const QColor &c );
637
639 void setParticlesSize( double width );
640
642 void setTailFactor( double fct );
643
645 void setMinimumTailLength( int l );
646
648 void setTailPersitence( double p );
649
651
652 private:
653 std::unique_ptr<QgsMeshParticleTracesField> mParticleField;
654 const QgsRenderContext &mRendererContext;
655 int mFPS = 15; //frame per second of the output, used to calculate orher parameters of the field
656 int mVpixMax = 2000; //is the number of pixels that are going through for 1 s
657 double mParticleLifeTime = 5;
658
659 void updateFieldParameter();
660};
661
662#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.