QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include "qgsmessagelog.h"
19
20#include <QMatrix4x4>
21
22#include "moc_qgstessellatedpolygongeometry.cpp"
23
24#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
25#include <Qt3DRender/QAttribute>
26#include <Qt3DRender/QBuffer>
27typedef Qt3DRender::QAttribute Qt3DQAttribute;
28typedef Qt3DRender::QBuffer Qt3DQBuffer;
29#else
30#include <Qt3DCore/QAttribute>
31#include <Qt3DCore/QBuffer>
32typedef Qt3DCore::QAttribute Qt3DQAttribute;
33typedef Qt3DCore::QBuffer Qt3DQBuffer;
34#endif
35
36#include "qgstessellator.h"
37#include "qgspolygon.h"
38
39QgsTessellatedPolygonGeometry::QgsTessellatedPolygonGeometry( bool _withNormals, bool _invertNormals, bool _addBackFaces, bool _addTextureCoords, QNode *parent )
40 : QGeometry( parent )
41 , mWithNormals( _withNormals )
42 , mInvertNormals( _invertNormals )
43 , mAddBackFaces( _addBackFaces )
44 , mAddTextureCoords( _addTextureCoords )
45{
46 mVertexBuffer = new Qt3DQBuffer( this );
47
48 QgsTessellator tmpTess;
49 tmpTess.setAddNormals( mWithNormals );
50 tmpTess.setAddTextureUVs( mAddTextureCoords );
51 const int stride = tmpTess.stride();
52
53 mPositionAttribute = new Qt3DQAttribute( this );
54 mPositionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
55 mPositionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
56 mPositionAttribute->setVertexSize( 3 );
57 mPositionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
58 mPositionAttribute->setBuffer( mVertexBuffer );
59 mPositionAttribute->setByteStride( stride );
60 mPositionAttribute->setByteOffset( 0 );
61 addAttribute( mPositionAttribute );
62
63 if ( mWithNormals )
64 {
65 mNormalAttribute = new Qt3DQAttribute( this );
66 mNormalAttribute->setName( Qt3DQAttribute::defaultNormalAttributeName() );
67 mNormalAttribute->setVertexBaseType( Qt3DQAttribute::Float );
68 mNormalAttribute->setVertexSize( 3 );
69 mNormalAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
70 mNormalAttribute->setBuffer( mVertexBuffer );
71 mNormalAttribute->setByteStride( stride );
72 mNormalAttribute->setByteOffset( 3 * sizeof( float ) );
73 addAttribute( mNormalAttribute );
74 }
75 if ( mAddTextureCoords )
76 {
77 mTextureCoordsAttribute = new Qt3DQAttribute( this );
78 mTextureCoordsAttribute->setName( Qt3DQAttribute::defaultTextureCoordinateAttributeName() );
79 mTextureCoordsAttribute->setVertexBaseType( Qt3DQAttribute::Float );
80 mTextureCoordsAttribute->setVertexSize( 2 );
81 mTextureCoordsAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
82 mTextureCoordsAttribute->setBuffer( mVertexBuffer );
83 mTextureCoordsAttribute->setByteStride( stride );
84 mTextureCoordsAttribute->setByteOffset( mWithNormals ? 6 * sizeof( float ) : 3 * sizeof( float ) );
85 addAttribute( mTextureCoordsAttribute );
86 }
87}
88
89void QgsTessellatedPolygonGeometry::setData( const QByteArray &vertexBufferData, int vertexCount, const QVector<QgsFeatureId> &triangleIndexFids, const QVector<uint> &triangleIndexStartingIndices )
90{
91 mTriangleIndexStartingIndices = triangleIndexStartingIndices;
92 mTriangleIndexFids = triangleIndexFids;
93
94 mVertexBuffer->setData( vertexBufferData );
95 mPositionAttribute->setCount( vertexCount );
96 if ( mNormalAttribute )
97 mNormalAttribute->setCount( vertexCount );
98 if ( mTextureCoordsAttribute )
99 mTextureCoordsAttribute->setCount( vertexCount );
100}
101
102// run binary search on a sorted array, return index i where data[i] <= v < data[i+1]
103static int binary_search( uint v, const uint *data, int count )
104{
105 int idx0 = 0;
106 int idx1 = count - 1;
107
108 if ( v < data[0] )
109 return -1; // not in the array
110
111 if ( v >= data[count - 1] )
112 return count - 1; // for larger values the last bin is returned
113
114 while ( idx0 != idx1 )
115 {
116 const int idxPivot = ( idx0 + idx1 ) / 2;
117 const uint pivot = data[idxPivot];
118 if ( pivot <= v )
119 {
120 if ( data[idxPivot + 1] > v )
121 return idxPivot; // we're done!
122 else // continue searching values greater than the pivot
123 idx0 = idxPivot;
124 }
125 else // continue searching values lower than the pivot
126 idx1 = idxPivot;
127 }
128 return idx0;
129}
130
131
133{
134 const int i = binary_search( triangleIndex, mTriangleIndexStartingIndices.constData(), mTriangleIndexStartingIndices.count() );
135 return i != -1 ? mTriangleIndexFids[i] : FID_NULL;
136}
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...
Tessellates polygons into triangles.
int stride() const
Returns size of one vertex entry in bytes.
void setAddTextureUVs(bool addTextureUVs)
Sets whether texture UV coordinates should be added to the output data (true) or not (false).
void setAddNormals(bool addNormals)
Sets whether normals should be added to the output data (true) or not (false).
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
#define FID_NULL
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features