QGIS API Documentation 3.41.0-Master (af5edcb665c)
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"
18
19#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
20#include <Qt3DRender/QAttribute>
21#include <Qt3DRender/QBuffer>
22#include <Qt3DRender/QGeometry>
23
24typedef Qt3DRender::QAttribute Qt3DQAttribute;
25typedef Qt3DRender::QBuffer Qt3DQBuffer;
26typedef Qt3DRender::QGeometry Qt3DQGeometry;
27#else
28#include <Qt3DCore/QAttribute>
29#include <Qt3DCore/QBuffer>
30#include <Qt3DCore/QGeometry>
31
32typedef Qt3DCore::QAttribute Qt3DQAttribute;
33typedef Qt3DCore::QBuffer Qt3DQBuffer;
34typedef Qt3DCore::QGeometry Qt3DQGeometry;
35#endif
36
37#include "qgslogger.h"
38#include "qgs3dutils.h"
39#include "qgslinestring.h"
40
42
43
44QgsLineVertexData::QgsLineVertexData()
45{
46 // the first index is invalid, we use it for primitive restart
47 vertices << QVector3D();
48}
49
50void QgsLineVertexData::init( Qgis::AltitudeClamping clamping, Qgis::AltitudeBinding binding, float height, const Qgs3DRenderContext &context, const QgsVector3D &chunkOrigin )
51{
52 altClamping = clamping;
53 altBinding = binding;
54 baseHeight = height;
55 renderContext = context;
56 origin = chunkOrigin;
57}
58
59QByteArray QgsLineVertexData::createVertexBuffer()
60{
61 QByteArray vertexBufferData;
62 vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
63 float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
64 int idx = 0;
65 for ( const auto &v : std::as_const( vertices ) )
66 {
67 rawVertexArray[idx++] = v.x();
68 rawVertexArray[idx++] = v.y();
69 rawVertexArray[idx++] = v.z();
70 }
71 return vertexBufferData;
72}
73
74QByteArray QgsLineVertexData::createIndexBuffer()
75{
76 QByteArray indexBufferData;
77 indexBufferData.resize( indexes.size() * sizeof( int ) );
78 unsigned int *rawIndexArray = reinterpret_cast<unsigned int *>( indexBufferData.data() );
79 int idx = 0;
80 for ( unsigned int indexVal : std::as_const( indexes ) )
81 {
82 rawIndexArray[idx++] = indexVal;
83 }
84 return indexBufferData;
85}
86
87Qt3DQGeometry *QgsLineVertexData::createGeometry( Qt3DCore::QNode *parent )
88{
89 Qt3DQBuffer *vertexBuffer = new Qt3DQBuffer( parent );
90 vertexBuffer->setData( createVertexBuffer() );
91
92 Qt3DQBuffer *indexBuffer = new Qt3DQBuffer( parent );
93 indexBuffer->setData( createIndexBuffer() );
94
95 QgsDebugMsgLevel( QString( "vertex buffer %1 MB index buffer %2 MB " ).arg( vertexBuffer->data().count() / 1024. / 1024. ).arg( indexBuffer->data().count() / 1024. / 1024. ), 2 );
96
97 Qt3DQAttribute *positionAttribute = new Qt3DQAttribute( parent );
98 positionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
99 positionAttribute->setBuffer( vertexBuffer );
100 positionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
101 positionAttribute->setVertexSize( 3 );
102 positionAttribute->setByteStride( 3 * sizeof( float ) );
103 positionAttribute->setByteOffset( 0 );
104 positionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
105
106 Qt3DQAttribute *indexAttribute = new Qt3DQAttribute( parent );
107 indexAttribute->setAttributeType( Qt3DQAttribute::IndexAttribute );
108 indexAttribute->setBuffer( indexBuffer );
109 indexAttribute->setByteOffset( 0 );
110 indexAttribute->setByteStride( sizeof( uint ) );
111 indexAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedInt );
112
113 Qt3DQGeometry *geom = new Qt3DQGeometry;
114 geom->addAttribute( positionAttribute );
115 geom->addAttribute( indexAttribute );
116
117 return geom;
118}
119
120void QgsLineVertexData::addLineString( const QgsLineString &lineString, float extraHeightOffset )
121{
122 if ( withAdjacency )
123 indexes << vertices.count(); // add the following vertex (for adjacency)
124
125 QgsPoint centroid;
126 switch ( altBinding )
127 {
129 break;
131 centroid = lineString.centroid();
132 break;
133 }
134
135 for ( int i = 0; i < lineString.vertexCount(); ++i )
136 {
137 QgsPoint p = lineString.pointN( i );
138 float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
139
140 vertices << QVector3D( static_cast<float>( p.x() - origin.x() ), static_cast<float>( p.y() - origin.y() ), z );
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() - origin.x() ), static_cast<float>( p.y() - origin.y() ), z );
172 indexes << vertices.count() - 1;
173 vertices << QVector3D( static_cast<float>( p.x() - origin.x() ), static_cast<float>( p.y() - origin.y() ), z2 );
174 indexes << vertices.count() - 1;
175
176 if ( withAdjacency )
177 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
178
179 indexes << 0; // add primitive restart
180 }
181}
182
183
AltitudeClamping
Altitude clamping.
Definition qgis.h:3762
AltitudeBinding
Altitude binding.
Definition qgis.h:3775
@ 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