QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 "qgis.h"
23#include "qgis_core.h"
24#include "qgsmaptopixel.h"
25#include "qgsmeshlayer.h"
27#include "qgsrendercontext.h"
28#include "qgstriangularmesh.h"
29
30#include <QSize>
31#include <QVector>
32
33class QgsMeshLayerInterpolator;
34class QgsMeshLayerRendererFeedback;
35
37
38#ifndef SIP_RUN
39
48class QgsMeshVectorValueInterpolator
49{
50 public:
52 QgsMeshVectorValueInterpolator( const QgsTriangularMesh &triangularMesh, const QgsMeshDataBlock &datasetVectorValues );
53
55 QgsMeshVectorValueInterpolator( const QgsTriangularMesh &triangularMesh, const QgsMeshDataBlock &datasetVectorValues, const QgsMeshDataBlock &scalarActiveFaceFlagValues );
56
57 QgsMeshVectorValueInterpolator( const QgsMeshVectorValueInterpolator &other );
58
60 virtual QgsMeshVectorValueInterpolator *clone() = 0;
61
62 virtual ~QgsMeshVectorValueInterpolator() = default;
63
68 virtual QgsVector vectorValue( const QgsPointXY &point ) const;
69
71 QgsMeshVectorValueInterpolator &operator=( const QgsMeshVectorValueInterpolator &other );
72
73 protected:
74 void updateCacheFaceIndex( const QgsPointXY &point ) const;
75
76 QgsTriangularMesh mTriangularMesh;
77 QgsMeshDataBlock mDatasetValues;
78 QgsMeshDataBlock mActiveFaceFlagValues;
79 mutable QgsMeshFace mFaceCache;
80 mutable int mCacheFaceIndex = -1;
81 bool mUseScalarActiveFaceFlagValues = false;
82 bool isVectorValid( const QgsVector &v ) const;
83
84 private:
85 void activeFaceFilter( QgsVector &vector, int faceIndex ) const;
86
87 virtual QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const = 0;
88};
89
98class QgsMeshVectorValueInterpolatorFromVertex : public QgsMeshVectorValueInterpolator
99{
100 public:
102 QgsMeshVectorValueInterpolatorFromVertex( const QgsTriangularMesh &triangularMesh, const QgsMeshDataBlock &datasetVectorValues );
103
105 QgsMeshVectorValueInterpolatorFromVertex( const QgsTriangularMesh &triangularMesh, const QgsMeshDataBlock &datasetVectorValues, const QgsMeshDataBlock &scalarActiveFaceFlagValues );
106
107 QgsMeshVectorValueInterpolatorFromVertex( const QgsMeshVectorValueInterpolatorFromVertex &other );
108
110 QgsMeshVectorValueInterpolatorFromVertex *clone() override;
111
112 QgsMeshVectorValueInterpolatorFromVertex &operator=( const QgsMeshVectorValueInterpolatorFromVertex &other );
113
114 private:
115 QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const override;
116};
117
126class QgsMeshVectorValueInterpolatorFromFace : public QgsMeshVectorValueInterpolator
127{
128 public:
130 QgsMeshVectorValueInterpolatorFromFace( const QgsTriangularMesh &triangularMesh, const QgsMeshDataBlock &datasetVectorValues );
131
133 QgsMeshVectorValueInterpolatorFromFace( const QgsTriangularMesh &triangularMesh, const QgsMeshDataBlock &datasetVectorValues, const QgsMeshDataBlock &scalarActiveFaceFlagValues );
134
135 QgsMeshVectorValueInterpolatorFromFace( const QgsMeshVectorValueInterpolatorFromFace &other );
136
138 QgsMeshVectorValueInterpolatorFromFace *clone() override;
139
140 QgsMeshVectorValueInterpolatorFromFace &operator=( const QgsMeshVectorValueInterpolatorFromFace &other );
141
142 private:
143 QgsVector interpolatedValuePrivate( int faceIndex, const QgsPointXY point ) const override;
144};
145
154class QgsMeshStreamField
155{
156 public:
157 struct FieldData
158 {
159 double magnitude;
160 float time;
161 int directionX;
162 int directionY;
163 };
164
166 QgsMeshStreamField(
167 const QgsTriangularMesh &triangularMesh,
168 const QgsMeshDataBlock &dataSetVectorValues,
169 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
170 const QgsRectangle &layerExtent,
171 double magnitudeMaximum,
172 bool dataIsOnVertices,
173 const QgsRenderContext &rendererContext,
174 const QgsInterpolatedLineColor &vectorColoring,
175 int resolution = 1
176 );
177
178 QgsMeshStreamField( const QgsMeshStreamField &other );
179 virtual ~QgsMeshStreamField();
180
187 void updateSize( const QgsRenderContext &renderContext );
188
193 void updateSize( const QgsRenderContext &renderContext, int resolution );
194
196 bool isValid() const;
197
199 QSize size() const;
200
202 QPoint topLeft() const;
203
205 void addTrace( QPoint startPixel );
206
208 void addTrace( QgsPointXY startPoint );
209
211 void addRandomTraces();
212
214 void addRandomTrace();
215
217 void addGriddedTraces( int dx, int dy );
218
220 void addTracesOnMesh( const QgsTriangularMesh &mesh, const QgsRectangle &extent );
221
223 void setResolution( int width );
224
226 int resolution() const;
227
229 QSize imageSize() const;
230
232 virtual QImage image() const;
233
235 void setPixelFillingDensity( double maxFilling );
236
238 void setColor( QColor color );
239
241 void setLineWidth( double width );
242
244 void setFilter( double min, double max );
245
247 void setMinimizeFieldSize( bool minimizeFieldSize );
248
250 QgsMeshStreamField &operator=( const QgsMeshStreamField &other );
251
252 protected:
253 virtual void initImage();
254 QPointF fieldToDevice( const QPoint &pixel ) const;
255 bool filterMag( double value ) const;
256 bool isTraceOutside( const QPoint &pixel ) const;
257
258 private:
259 QgsPointXY positionToMapCoordinates( const QPoint &pixelPosition, const QgsPointXY &positionInPixel );
260 bool addPixelToChunkTrace( QPoint &pixel, QgsMeshStreamField::FieldData &data, std::list<QPair<QPoint, QgsMeshStreamField::FieldData> > &chunkTrace );
261 void setChunkTrace( std::list<QPair<QPoint, FieldData>> &chunkTrace );
262 virtual void drawTrace( const QPoint & ) const {}
263 void clearChunkTrace( std::list<QPair<QPoint, FieldData>> &chunkTrace );
264 virtual void storeInField( const QPair<QPoint, FieldData> pixelData ) = 0;
265 virtual void initField() = 0;
266 void simplifyChunkTrace( std::list<QPair<QPoint, FieldData>> &shunkTrace );
267
268 virtual bool isTraceExists( const QPoint &pixel ) const = 0;
269
270 protected:
271 QSize mFieldSize;
272 std::unique_ptr<QPainter> mPainter = std::unique_ptr<QPainter>( nullptr );
273 int mFieldResolution = 1;
274 QPen mPen;
275 QImage mTraceImage;
276
277 QgsMapToPixel mMapToFieldPixel;
278 QgsRectangle mOutputExtent = QgsRectangle();
279 QgsInterpolatedLineColor mVectorColoring;
280
281 /*the direction for a pixel is defined with a char value
282 *
283 * 1 2 3
284 * 4 5 6
285 * 7 8 9
286 *
287 * convenient to retrieve the indexes of the next pixel from the direction d:
288 * Xnext= (d-1)%3-1
289 * Ynext = (d-1)/3-1
290 *
291 * and the direction is defined by :
292 * d=incX + 2 + (incY+1)*3
293 */
294 QVector<unsigned char> mDirectionField;
295 QgsRenderContext mRenderContext;
296
297 private:
298 int mPixelFillingCount = 0;
299 int mMaxPixelFillingCount = 0;
300 std::unique_ptr<QgsMeshVectorValueInterpolator> mVectorValueInterpolator;
301 QgsRectangle mLayerExtent;
302 QgsRectangle mMapExtent;
303 QPoint mFieldTopLeftInDeviceCoordinates;
304 bool mValid = false;
305 double mMaximumMagnitude = 0;
306 double mPixelFillingDensity = 0;
307 double mMinMagFilter = -1;
308 double mMaxMagFilter = -1;
309 bool mMinimizeFieldSize = true; //
310};
311
320class QgsMeshStreamlinesField : public QgsMeshStreamField
321{
322 public:
324 Q_DECL_DEPRECATED QgsMeshStreamlinesField(
325 const QgsTriangularMesh &triangularMesh,
326 const QgsMeshDataBlock &datasetVectorValues,
327 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
328 const QgsRectangle &layerExtent,
329 double magMax,
330 bool dataIsOnVertices,
331 QgsRenderContext &rendererContext,
332 const QgsInterpolatedLineColor &vectorColoring
333 );
334
335 QgsMeshStreamlinesField(
336 const QgsTriangularMesh &triangularMesh,
337 const QgsMeshDataBlock &datasetVectorValues,
338 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
339 const QVector<double> &datasetMagValues,
340 const QgsRectangle &layerExtent,
341 QgsMeshLayerRendererFeedback *feedBack,
342 double magMax,
343 bool dataIsOnVertices,
344 QgsRenderContext &rendererContext,
345 const QgsInterpolatedLineColor &vectorColoring
346 );
347
348 void compose();
349
350 private:
351 void storeInField( const QPair<QPoint, FieldData> pixelData ) override;
352 void initField() override;
353 void initImage() override;
354 bool isTraceExists( const QPoint &pixel ) const override;
355 void drawTrace( const QPoint &start ) const override;
356
357 QVector<bool> mField;
358 QImage mDrawingTraceImage;
359 std::unique_ptr<QPainter> mDrawingTracePainter;
360
361 //** Needed data
362 QgsTriangularMesh mTriangularMesh;
363 QVector<double> mMagValues;
364 QgsMeshDataBlock mScalarActiveFaceFlagValues;
366 QgsMeshLayerRendererFeedback *mFeedBack = nullptr;
367};
368
369class QgsMeshParticleTracesField;
370
379struct QgsMeshTraceParticle
380{
381 double lifeTime = 0;
382 QPoint position;
383 std::list<QPoint> tail;
384 double remainingTime = 0; //time remaining to spend in the current pixel at the end of the time step
385};
386
395class QgsMeshParticleTracesField : public QgsMeshStreamField
396{
397 public:
399 QgsMeshParticleTracesField(
400 const QgsTriangularMesh &triangularMesh,
401 const QgsMeshDataBlock &datasetVectorValues,
402 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
403 const QgsRectangle &layerExtent,
404 double magMax,
405 bool dataIsOnVertices,
406 const QgsRenderContext &rendererContext,
407 const QgsInterpolatedLineColor &vectorColoring
408 );
409
410 QgsMeshParticleTracesField( const QgsMeshParticleTracesField &other );
411
413 void addParticle( const QPoint &startPoint, double lifeTime );
414
416 void addParticleXY( const QgsPointXY &startPoint, double lifeTime );
417
419 void addRandomParticles();
420
422 void moveParticles();
423
425 QImage imageRendered() const;
426
428 void setParticlesCount( int particlesCount );
429
431 void setParticlesLifeTime( double particlesLifeTime );
432
434 void stump();
435
441 void setStumpFactor( int sf );
442
444 void setTimeStep( double timeStep );
445
447 void setParticleSize( double particleSize );
448
450 void setTailFactor( double tailFactor );
451
453 void setMinTailLength( int minTailLength );
454
456 void setStumpParticleWithLifeTime( bool stumpParticleWithLifeTime );
457
459 void setParticlesColor( const QColor &c );
460
461 QgsMeshParticleTracesField &operator=( const QgsMeshParticleTracesField &other );
462
463 private:
464 QPoint direction( QPoint position ) const;
465
466 float time( QPoint position ) const;
467 float magnitude( QPoint position ) const;
468
469 void drawParticleTrace( const QgsMeshTraceParticle &particle );
470
471 void storeInField( const QPair<QPoint, FieldData> pixelData ) override;
472 void initField() override;
473 bool isTraceExists( const QPoint &pixel ) const override;
474
475 /* Nondimensional time
476 * This field store the time spent by the particle in the pixel
477 *
478 * This time is nondimensional and value 1 is equivalent to the time spent by the particle in a pixel
479 * for Vmax, the maximum magnitude of the vector field.
480 *
481 */
482 QVector<float> mTimeField;
483 QVector<float> mMagnitudeField;
484
485 QList<QgsMeshTraceParticle> mParticles;
486 QImage mStumpImage;
487
488 double mTimeStep = 200;
489 double mParticlesLifeTime = 5000;
490 int mParticlesCount = 1000;
491 double mTailFactor = 5;
492 int mMinTailLength = 3;
493 QColor mParticleColor = Qt::white;
494 double mParticleSize = 2.5;
495 int mStumpFactor = 50;
496 bool mStumpParticleWithLifeTime = true;
497};
498
509class QgsMeshVectorStreamlineRenderer : public QgsMeshVectorRenderer
510{
511 public:
513 Q_DECL_DEPRECATED QgsMeshVectorStreamlineRenderer(
514 const QgsTriangularMesh &triangularMesh,
515 const QgsMeshDataBlock &dataSetVectorValues,
516 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
517 bool dataIsOnVertices,
518 const QgsMeshRendererVectorSettings &settings,
519 QgsRenderContext &rendererContext,
520 const QgsRectangle &layerExtent,
521 double magMax
522 );
523
524 QgsMeshVectorStreamlineRenderer(
525 const QgsTriangularMesh &triangularMesh,
526 const QgsMeshDataBlock &dataSetVectorValues,
527 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
528 const QVector<double> &datasetMagValues,
529 bool dataIsOnVertices,
530 const QgsMeshRendererVectorSettings &settings,
531 QgsRenderContext &rendererContext,
532 const QgsRectangle &layerExtent,
533 QgsMeshLayerRendererFeedback *feedBack,
534 double magMax
535 );
536
537 void draw() override;
538
539 private:
540 std::unique_ptr<QgsMeshStreamlinesField> mStreamlineField;
541 QgsRenderContext &mRendererContext;
542};
543
544
555class QgsMeshVectorTraceRenderer : public QgsMeshVectorRenderer
556{
557 public:
559 QgsMeshVectorTraceRenderer(
560 const QgsTriangularMesh &triangularMesh,
561 const QgsMeshDataBlock &dataSetVectorValues,
562 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
563 bool dataIsOnVertices,
564 const QgsMeshRendererVectorSettings &settings,
565 QgsRenderContext &rendererContext,
566 const QgsRectangle &layerExtent,
567 double magMax
568 );
569
570 void draw() override;
571
572 private:
573 std::unique_ptr<QgsMeshParticleTracesField> mParticleField;
574 QgsRenderContext &mRendererContext;
575};
576
577
578#endif //SIP_RUN
579
581
592{
593 public:
596 const QgsTriangularMesh &triangularMesh,
597 const QgsMeshDataBlock &dataSetVectorValues,
598 const QgsMeshDataBlock &scalarActiveFaceFlagValues,
599 bool dataIsOnVertices,
600 const QgsRenderContext &rendererContext,
601 const QgsRectangle &layerExtent,
602 double magMax,
603 const QgsMeshRendererVectorSettings &vectorSettings
604 ) SIP_SKIP;
605
608
610
612
614 void seedRandomParticles( int count );
615
618
620 void setFPS( int FPS );
621
623 void setMaxSpeedPixel( int max );
624
626 void setParticlesLifeTime( double particleLifeTime );
627
629 void setParticlesColor( const QColor &c );
630
632 void setParticlesSize( double width );
633
635 void setTailFactor( double fct );
636
638 void setMinimumTailLength( int l );
639
641 void setTailPersitence( double p );
642
644
645 private:
646 std::unique_ptr<QgsMeshParticleTracesField> mParticleField;
647 const QgsRenderContext &mRendererContext;
648 int mFPS = 15; //frame per second of the output, used to calculate orher parameters of the field
649 int mVpixMax = 2000; //is the number of pixels that are going through for 1 s
650 double mParticleLifeTime = 5;
651
652 void updateFieldParameter();
653};
654
655#endif // QGSMESHTRACERENDERER_H
A block of integers/doubles from a mesh dataset.
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.
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 rectangle specified with double values.
Contains information about the context of a rendering operation.
A triangular/derived mesh with vertices in map coordinates.
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:133
QVector< int > QgsMeshFace
List of vertex indexes.