QGIS API Documentation 3.39.0-Master (bca3cdb6021)
Loading...
Searching...
No Matches
qgslinevertexdata_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslinevertexdata_p.cpp
3 --------------------------------------
4 Date : Apr 2019
5 Copyright : (C) 2019 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
16#include "qgslinevertexdata_p.h"
17
18#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
19#include <Qt3DRender/QAttribute>
20#include <Qt3DRender/QBuffer>
21#include <Qt3DRender/QGeometry>
22
23typedef Qt3DRender::QAttribute Qt3DQAttribute;
24typedef Qt3DRender::QBuffer Qt3DQBuffer;
25typedef Qt3DRender::QGeometry Qt3DQGeometry;
26#else
27#include <Qt3DCore/QAttribute>
28#include <Qt3DCore/QBuffer>
29#include <Qt3DCore/QGeometry>
30
31typedef Qt3DCore::QAttribute Qt3DQAttribute;
32typedef Qt3DCore::QBuffer Qt3DQBuffer;
33typedef Qt3DCore::QGeometry Qt3DQGeometry;
34#endif
35
36#include "qgslogger.h"
37#include "qgs3dutils.h"
38#include "qgslinestring.h"
39
41
42
43QgsLineVertexData::QgsLineVertexData()
44{
45 // the first index is invalid, we use it for primitive restart
46 vertices << QVector3D();
47}
48
49void QgsLineVertexData::init( Qgis::AltitudeClamping clamping, Qgis::AltitudeBinding binding, float height, const Qgs3DRenderContext &context )
50{
51 altClamping = clamping;
52 altBinding = binding;
53 baseHeight = height;
54 renderContext = context;
55}
56
57QByteArray QgsLineVertexData::createVertexBuffer()
58{
59 QByteArray vertexBufferData;
60 vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
61 float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
62 int idx = 0;
63 for ( const auto &v : std::as_const( vertices ) )
64 {
65 rawVertexArray[idx++] = v.x();
66 rawVertexArray[idx++] = v.y();
67 rawVertexArray[idx++] = v.z();
68 }
69 return vertexBufferData;
70}
71
72QByteArray QgsLineVertexData::createIndexBuffer()
73{
74 QByteArray indexBufferData;
75 indexBufferData.resize( indexes.size() * sizeof( int ) );
76 unsigned int *rawIndexArray = reinterpret_cast<unsigned int *>( indexBufferData.data() );
77 int idx = 0;
78 for ( unsigned int indexVal : std::as_const( indexes ) )
79 {
80 rawIndexArray[idx++] = indexVal;
81 }
82 return indexBufferData;
83}
84
85Qt3DQGeometry *QgsLineVertexData::createGeometry( Qt3DCore::QNode *parent )
86{
87 Qt3DQBuffer *vertexBuffer = new Qt3DQBuffer( parent );
88 vertexBuffer->setData( createVertexBuffer() );
89
90 Qt3DQBuffer *indexBuffer = new Qt3DQBuffer( parent );
91 indexBuffer->setData( createIndexBuffer() );
92
93 QgsDebugMsgLevel( QString( "vertex buffer %1 MB index buffer %2 MB " ).arg( vertexBuffer->data().count() / 1024. / 1024. ).arg( indexBuffer->data().count() / 1024. / 1024. ), 2 );
94
95 Qt3DQAttribute *positionAttribute = new Qt3DQAttribute( parent );
96 positionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
97 positionAttribute->setBuffer( vertexBuffer );
98 positionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
99 positionAttribute->setVertexSize( 3 );
100 positionAttribute->setByteStride( 3 * sizeof( float ) );
101 positionAttribute->setByteOffset( 0 );
102 positionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
103
104 Qt3DQAttribute *indexAttribute = new Qt3DQAttribute( parent );
105 indexAttribute->setAttributeType( Qt3DQAttribute::IndexAttribute );
106 indexAttribute->setBuffer( indexBuffer );
107 indexAttribute->setByteOffset( 0 );
108 indexAttribute->setByteStride( sizeof( uint ) );
109 indexAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedInt );
110
111 Qt3DQGeometry *geom = new Qt3DQGeometry;
112 geom->addAttribute( positionAttribute );
113 geom->addAttribute( indexAttribute );
114
115 return geom;
116}
117
118void QgsLineVertexData::addLineString( const QgsLineString &lineString, float extraHeightOffset )
119{
120 if ( withAdjacency )
121 indexes << vertices.count(); // add the following vertex (for adjacency)
122
123 QgsPoint centroid;
124 switch ( altBinding )
125 {
127 break;
129 centroid = lineString.centroid();
130 break;
131 }
132
133 for ( int i = 0; i < lineString.vertexCount(); ++i )
134 {
135 QgsPoint p = lineString.pointN( i );
136 float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
137
138 vertices << QVector3D( static_cast< float >( p.x() - renderContext.origin().x() ),
139 z,
140 static_cast< float >( -( p.y() - renderContext.origin().y() ) ) );
141 indexes << vertices.count() - 1;
142 }
143
144 if ( withAdjacency )
145 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
146
147 indexes << 0; // add primitive restart
148}
149
150void QgsLineVertexData::addVerticalLines( const QgsLineString &lineString, float verticalLength, float extraHeightOffset )
151{
152 QgsPoint centroid;
153 switch ( altBinding )
154 {
156 break;
158 centroid = lineString.centroid();
159 break;
160 }
161
162 for ( int i = 0; i < lineString.vertexCount(); ++i )
163 {
164 QgsPoint p = lineString.pointN( i );
165 float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
166 float z2 = z + verticalLength;
167
168 if ( withAdjacency )
169 indexes << vertices.count(); // add the following vertex (for adjacency)
170
171 vertices << QVector3D( static_cast< float >( p.x() - renderContext.origin().x() ),
172 z,
173 static_cast< float >( -( p.y() - renderContext.origin().y() ) ) );
174 indexes << vertices.count() - 1;
175 vertices << QVector3D( static_cast< float >( p.x() - renderContext.origin().x() ),
176 z2,
177 static_cast< float >( -( p.y() - renderContext.origin().y() ) ) );
178 indexes << vertices.count() - 1;
179
180 if ( withAdjacency )
181 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
182
183 indexes << 0; // add primitive restart
184 }
185}
186
187
AltitudeClamping
Altitude clamping.
Definition qgis.h:3671
AltitudeBinding
Altitude binding.
Definition qgis.h:3684
@ Centroid
Clamp just centroid of feature.
@ Vertex
Clamp every vertex of feature.
static float clampAltitude(const QgsPoint &p, Qgis::AltitudeClamping altClamp, Qgis::AltitudeBinding altBind, float offset, const QgsPoint &centroid, const Qgs3DRenderContext &context)
Clamps altitude of a vertex according to the settings, returns Z value.
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Definition qgscurve.cpp:180
Line string geometry type, with support for z-dimension and m-values.
QgsPoint pointN(int i) const
Returns the specified point from inside the line string.
QgsPoint centroid() const override
Returns the centroid of the geometry.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double x
Definition qgspoint.h:52
double y
Definition qgspoint.h:53
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39