QGIS API Documentation 3.41.0-Master (3440c17df1d)
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, const QgsVector3D &chunkOrigin )
50{
51 altClamping = clamping;
52 altBinding = binding;
53 baseHeight = height;
54 renderContext = context;
55 origin = chunkOrigin;
56}
57
58QByteArray QgsLineVertexData::createVertexBuffer()
59{
60 QByteArray vertexBufferData;
61 vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
62 float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
63 int idx = 0;
64 for ( const auto &v : std::as_const( vertices ) )
65 {
66 rawVertexArray[idx++] = v.x();
67 rawVertexArray[idx++] = v.y();
68 rawVertexArray[idx++] = v.z();
69 }
70 return vertexBufferData;
71}
72
73QByteArray QgsLineVertexData::createIndexBuffer()
74{
75 QByteArray indexBufferData;
76 indexBufferData.resize( indexes.size() * sizeof( int ) );
77 unsigned int *rawIndexArray = reinterpret_cast<unsigned int *>( indexBufferData.data() );
78 int idx = 0;
79 for ( unsigned int indexVal : std::as_const( indexes ) )
80 {
81 rawIndexArray[idx++] = indexVal;
82 }
83 return indexBufferData;
84}
85
86Qt3DQGeometry *QgsLineVertexData::createGeometry( Qt3DCore::QNode *parent )
87{
88 Qt3DQBuffer *vertexBuffer = new Qt3DQBuffer( parent );
89 vertexBuffer->setData( createVertexBuffer() );
90
91 Qt3DQBuffer *indexBuffer = new Qt3DQBuffer( parent );
92 indexBuffer->setData( createIndexBuffer() );
93
94 QgsDebugMsgLevel( QString( "vertex buffer %1 MB index buffer %2 MB " ).arg( vertexBuffer->data().count() / 1024. / 1024. ).arg( indexBuffer->data().count() / 1024. / 1024. ), 2 );
95
96 Qt3DQAttribute *positionAttribute = new Qt3DQAttribute( parent );
97 positionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
98 positionAttribute->setBuffer( vertexBuffer );
99 positionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
100 positionAttribute->setVertexSize( 3 );
101 positionAttribute->setByteStride( 3 * sizeof( float ) );
102 positionAttribute->setByteOffset( 0 );
103 positionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
104
105 Qt3DQAttribute *indexAttribute = new Qt3DQAttribute( parent );
106 indexAttribute->setAttributeType( Qt3DQAttribute::IndexAttribute );
107 indexAttribute->setBuffer( indexBuffer );
108 indexAttribute->setByteOffset( 0 );
109 indexAttribute->setByteStride( sizeof( uint ) );
110 indexAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedInt );
111
112 Qt3DQGeometry *geom = new Qt3DQGeometry;
113 geom->addAttribute( positionAttribute );
114 geom->addAttribute( indexAttribute );
115
116 return geom;
117}
118
119void QgsLineVertexData::addLineString( const QgsLineString &lineString, float extraHeightOffset )
120{
121 if ( withAdjacency )
122 indexes << vertices.count(); // add the following vertex (for adjacency)
123
124 QgsPoint centroid;
125 switch ( altBinding )
126 {
128 break;
130 centroid = lineString.centroid();
131 break;
132 }
133
134 for ( int i = 0; i < lineString.vertexCount(); ++i )
135 {
136 QgsPoint p = lineString.pointN( i );
137 float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
138
139 vertices << QVector3D( static_cast< float >( p.x() - origin.x() ),
140 static_cast< float >( p.y() - origin.y() ),
141 z );
142 indexes << vertices.count() - 1;
143 }
144
145 if ( withAdjacency )
146 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
147
148 indexes << 0; // add primitive restart
149}
150
151void QgsLineVertexData::addVerticalLines( const QgsLineString &lineString, float verticalLength, float extraHeightOffset )
152{
153 QgsPoint centroid;
154 switch ( altBinding )
155 {
157 break;
159 centroid = lineString.centroid();
160 break;
161 }
162
163 for ( int i = 0; i < lineString.vertexCount(); ++i )
164 {
165 QgsPoint p = lineString.pointN( i );
166 float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
167 float z2 = z + verticalLength;
168
169 if ( withAdjacency )
170 indexes << vertices.count(); // add the following vertex (for adjacency)
171
172 vertices << QVector3D( static_cast< float >( p.x() - origin.x() ),
173 static_cast< float >( p.y() - origin.y() ),
174 z );
175 indexes << vertices.count() - 1;
176 vertices << QVector3D( static_cast< float >( p.x() - origin.x() ),
177 static_cast< float >( p.y() - origin.y() ),
178 z2 );
179 indexes << vertices.count() - 1;
180
181 if ( withAdjacency )
182 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
183
184 indexes << 0; // add primitive restart
185 }
186}
187
188
AltitudeClamping
Altitude clamping.
Definition qgis.h:3699
AltitudeBinding
Altitude binding.
Definition qgis.h:3712
@ 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
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
double x() const
Returns X coordinate.
Definition qgsvector3d.h:48
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