QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgstessellatedpolygongeometry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstessellatedpolygongeometry.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgstessellatedpolygongeometry.cpp"
19#include "qgsmessagelog.h"
20
21#include <QMatrix4x4>
22
23#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
24#include <Qt3DRender/QAttribute>
25#include <Qt3DRender/QBuffer>
26typedef Qt3DRender::QAttribute Qt3DQAttribute;
27typedef Qt3DRender::QBuffer Qt3DQBuffer;
28#else
29#include <Qt3DCore/QAttribute>
30#include <Qt3DCore/QBuffer>
31typedef Qt3DCore::QAttribute Qt3DQAttribute;
32typedef Qt3DCore::QBuffer Qt3DQBuffer;
33#endif
34
35#include "qgstessellator.h"
36#include "qgspolygon.h"
37
38QgsTessellatedPolygonGeometry::QgsTessellatedPolygonGeometry( bool _withNormals, bool _invertNormals, bool _addBackFaces, bool _addTextureCoords, QNode *parent )
39 : QGeometry( parent )
40 , mWithNormals( _withNormals )
41 , mInvertNormals( _invertNormals )
42 , mAddBackFaces( _addBackFaces )
43 , mAddTextureCoords( _addTextureCoords )
44{
45 mVertexBuffer = new Qt3DQBuffer( this );
46
47 const QgsTessellator tmpTess( 0, 0, mWithNormals, false, false, false, mAddTextureCoords );
48 const int stride = tmpTess.stride();
49
50 mPositionAttribute = new Qt3DQAttribute( this );
51 mPositionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
52 mPositionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
53 mPositionAttribute->setVertexSize( 3 );
54 mPositionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
55 mPositionAttribute->setBuffer( mVertexBuffer );
56 mPositionAttribute->setByteStride( stride );
57 mPositionAttribute->setByteOffset( 0 );
58 addAttribute( mPositionAttribute );
59
60 if ( mWithNormals )
61 {
62 mNormalAttribute = new Qt3DQAttribute( this );
63 mNormalAttribute->setName( Qt3DQAttribute::defaultNormalAttributeName() );
64 mNormalAttribute->setVertexBaseType( Qt3DQAttribute::Float );
65 mNormalAttribute->setVertexSize( 3 );
66 mNormalAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
67 mNormalAttribute->setBuffer( mVertexBuffer );
68 mNormalAttribute->setByteStride( stride );
69 mNormalAttribute->setByteOffset( 3 * sizeof( float ) );
70 addAttribute( mNormalAttribute );
71 }
72 if ( mAddTextureCoords )
73 {
74 mTextureCoordsAttribute = new Qt3DQAttribute( this );
75 mTextureCoordsAttribute->setName( Qt3DQAttribute::defaultTextureCoordinateAttributeName() );
76 mTextureCoordsAttribute->setVertexBaseType( Qt3DQAttribute::Float );
77 mTextureCoordsAttribute->setVertexSize( 2 );
78 mTextureCoordsAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
79 mTextureCoordsAttribute->setBuffer( mVertexBuffer );
80 mTextureCoordsAttribute->setByteStride( stride );
81 mTextureCoordsAttribute->setByteOffset( mWithNormals ? 6 * sizeof( float ) : 3 * sizeof( float ) );
82 addAttribute( mTextureCoordsAttribute );
83 }
84}
85
86void QgsTessellatedPolygonGeometry::setData( const QByteArray &vertexBufferData, int vertexCount, const QVector<QgsFeatureId> &triangleIndexFids, const QVector<uint> &triangleIndexStartingIndices )
87{
88 mTriangleIndexStartingIndices = triangleIndexStartingIndices;
89 mTriangleIndexFids = triangleIndexFids;
90
91 mVertexBuffer->setData( vertexBufferData );
92 mPositionAttribute->setCount( vertexCount );
93 if ( mNormalAttribute )
94 mNormalAttribute->setCount( vertexCount );
95 if ( mTextureCoordsAttribute )
96 mTextureCoordsAttribute->setCount( vertexCount );
97}
98
99// run binary search on a sorted array, return index i where data[i] <= v < data[i+1]
100static int binary_search( uint v, const uint *data, int count )
101{
102 int idx0 = 0;
103 int idx1 = count - 1;
104
105 if ( v < data[0] )
106 return -1; // not in the array
107
108 if ( v >= data[count - 1] )
109 return count - 1; // for larger values the last bin is returned
110
111 while ( idx0 != idx1 )
112 {
113 const int idxPivot = ( idx0 + idx1 ) / 2;
114 const uint pivot = data[idxPivot];
115 if ( pivot <= v )
116 {
117 if ( data[idxPivot + 1] > v )
118 return idxPivot; // we're done!
119 else // continue searching values greater than the pivot
120 idx0 = idxPivot;
121 }
122 else // continue searching values lower than the pivot
123 idx1 = idxPivot;
124 }
125 return idx0;
126}
127
128
130{
131 const int i = binary_search( triangleIndex, mTriangleIndexStartingIndices.constData(), mTriangleIndexStartingIndices.count() );
132 return i != -1 ? mTriangleIndexFids[i] : FID_NULL;
133}
QgsFeatureId triangleIndexToFeatureId(uint triangleIndex) const
Returns ID of the feature to which given triangle index belongs (used for picking).
QgsTessellatedPolygonGeometry(bool _withNormals=true, bool invertNormals=false, bool addBackFaces=false, bool addTextureCoords=false, QNode *parent=nullptr)
Constructor.
void setData(const QByteArray &vertexBufferData, int vertexCount, const QVector< QgsFeatureId > &triangleIndexFids, const QVector< uint > &triangleIndexStartingIndices)
Initializes vertex buffer (and other members) from data that were already tessellated.
QVector< uint > triangleIndexStartingIndices() const
Returns triangle index for features. For a feature featureIds()[i], matching triangles start at trian...
Class that takes care of tessellation of polygons into triangles.
int stride() const
Returns size of one vertex entry in bytes.
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
#define FID_NULL
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer