QGIS API Documentation 3.38.0-Grenoble (exported)
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 Qgs3DMapSettings *map )
50{
51 altClamping = clamping;
52 altBinding = binding;
53 baseHeight = height;
54 mapSettings = map;
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, *mapSettings );
137
138 vertices << QVector3D( p.x() - mapSettings->origin().x(), z, -( p.y() - mapSettings->origin().y() ) );
139 indexes << vertices.count() - 1;
140 }
141
142 if ( withAdjacency )
143 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
144
145 indexes << 0; // add primitive restart
146}
147
148void QgsLineVertexData::addVerticalLines( const QgsLineString &lineString, float verticalLength, float extraHeightOffset )
149{
150 QgsPoint centroid;
151 switch ( altBinding )
152 {
154 break;
156 centroid = lineString.centroid();
157 break;
158 }
159
160 for ( int i = 0; i < lineString.vertexCount(); ++i )
161 {
162 QgsPoint p = lineString.pointN( i );
163 float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, *mapSettings );
164 float z2 = z + verticalLength;
165
166 if ( withAdjacency )
167 indexes << vertices.count(); // add the following vertex (for adjacency)
168
169 vertices << QVector3D( p.x() - mapSettings->origin().x(), z, -( p.y() - mapSettings->origin().y() ) );
170 indexes << vertices.count() - 1;
171 vertices << QVector3D( p.x() - mapSettings->origin().x(), z2, -( p.y() - mapSettings->origin().y() ) );
172 indexes << vertices.count() - 1;
173
174 if ( withAdjacency )
175 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
176
177 indexes << 0; // add primitive restart
178 }
179}
180
181
AltitudeClamping
Altitude clamping.
Definition qgis.h:3403
AltitudeBinding
Altitude binding.
Definition qgis.h:3416
@ 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 Qgs3DMapSettings &map)
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