QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
19
20#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
21#include <Qt3DRender/QAttribute>
22#include <Qt3DRender/QBuffer>
23#include <Qt3DRender/QGeometry>
24
25typedef Qt3DRender::QAttribute Qt3DQAttribute;
26typedef Qt3DRender::QBuffer Qt3DQBuffer;
27typedef Qt3DRender::QGeometry Qt3DQGeometry;
28#else
29#include <Qt3DCore/QAttribute>
30#include <Qt3DCore/QBuffer>
31#include <Qt3DCore/QGeometry>
32
33typedef Qt3DCore::QAttribute Qt3DQAttribute;
34typedef Qt3DCore::QBuffer Qt3DQBuffer;
35typedef Qt3DCore::QGeometry Qt3DQGeometry;
36#endif
37
38#include "qgslogger.h"
39#include "qgs3dutils.h"
40#include "qgslinestring.h"
41
43
44
45QgsLineVertexData::QgsLineVertexData()
46{
47 // the first index is invalid, we use it for primitive restart
48 vertices << QVector3D();
49}
50
51void QgsLineVertexData::init( Qgis::AltitudeClamping clamping, Qgis::AltitudeBinding binding, float height, const Qgs3DRenderContext &context, const QgsVector3D &chunkOrigin )
52{
53 altClamping = clamping;
54 altBinding = binding;
55 baseHeight = height;
56 renderContext = context;
57 origin = chunkOrigin;
58}
59
60QByteArray QgsLineVertexData::createVertexBuffer()
61{
62 QByteArray vertexBufferData;
63 vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
64 float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
65 int idx = 0;
66 for ( const auto &v : std::as_const( vertices ) )
67 {
68 rawVertexArray[idx++] = v.x();
69 rawVertexArray[idx++] = v.y();
70 rawVertexArray[idx++] = v.z();
71 }
72 return vertexBufferData;
73}
74
75QByteArray QgsLineVertexData::createIndexBuffer()
76{
77 QByteArray indexBufferData;
78 indexBufferData.resize( indexes.size() * sizeof( int ) );
79 unsigned int *rawIndexArray = reinterpret_cast<unsigned int *>( indexBufferData.data() );
80 int idx = 0;
81 for ( unsigned int indexVal : std::as_const( indexes ) )
82 {
83 rawIndexArray[idx++] = indexVal;
84 }
85 return indexBufferData;
86}
87
88Qt3DQGeometry *QgsLineVertexData::createGeometry( Qt3DCore::QNode *parent )
89{
90 Qt3DQBuffer *vertexBuffer = new Qt3DQBuffer( parent );
91 vertexBuffer->setData( createVertexBuffer() );
92
93 Qt3DQBuffer *indexBuffer = new Qt3DQBuffer( parent );
94 indexBuffer->setData( createIndexBuffer() );
95
96 QgsDebugMsgLevel( QString( "vertex buffer %1 MB index buffer %2 MB " ).arg( vertexBuffer->data().count() / 1024. / 1024. ).arg( indexBuffer->data().count() / 1024. / 1024. ), 2 );
97
98 Qt3DQAttribute *positionAttribute = new Qt3DQAttribute( parent );
99 positionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
100 positionAttribute->setBuffer( vertexBuffer );
101 positionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
102 positionAttribute->setVertexSize( 3 );
103 positionAttribute->setByteStride( 3 * sizeof( float ) );
104 positionAttribute->setByteOffset( 0 );
105 positionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
106
107 Qt3DQAttribute *indexAttribute = new Qt3DQAttribute( parent );
108 indexAttribute->setAttributeType( Qt3DQAttribute::IndexAttribute );
109 indexAttribute->setBuffer( indexBuffer );
110 indexAttribute->setByteOffset( 0 );
111 indexAttribute->setByteStride( sizeof( uint ) );
112 indexAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedInt );
113
114 Qt3DQGeometry *geom = new Qt3DQGeometry;
115 geom->addAttribute( positionAttribute );
116 geom->addAttribute( indexAttribute );
117
118 return geom;
119}
120
121void QgsLineVertexData::addLineString( const QgsLineString &lineString, float extraHeightOffset, bool closePolygon )
122{
123 if ( withAdjacency )
124 indexes << vertices.count(); // add the following vertex (for adjacency)
125
126 QgsPoint centroid;
127 switch ( altBinding )
128 {
130 break;
132 centroid = lineString.centroid();
133 break;
134 }
135
136 const int firstIndex = vertices.count();
137
138 for ( int i = 0; i < lineString.vertexCount(); ++i )
139 {
140 QgsPoint p = lineString.pointN( i );
141 if ( geocentricCoordinates )
142 {
143 // TODO: implement altitude clamping when dealing with geocentric coordinates
144 // where Z coordinate is not altitude and can't be used directly...
145 vertices << QVector3D( static_cast<float>( p.x() - origin.x() ), static_cast<float>( p.y() - origin.y() ), static_cast<float>( p.z() - origin.z() ) );
146 }
147 else
148 {
149 const float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
150 vertices << QVector3D( static_cast<float>( p.x() - origin.x() ), static_cast<float>( p.y() - origin.y() ), static_cast<float>( z - origin.z() ) );
151 }
152 indexes << vertices.count() - 1;
153 }
154
155 if ( closePolygon )
156 indexes << firstIndex; // repeat the first vertex
157
158 if ( withAdjacency )
159 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
160
161 indexes << 0; // add primitive restart
162}
163
164void QgsLineVertexData::addVerticalLines( const QgsLineString &lineString, float verticalLength, float extraHeightOffset )
165{
166 QgsPoint centroid;
167 switch ( altBinding )
168 {
170 break;
172 centroid = lineString.centroid();
173 break;
174 }
175
176 for ( int i = 0; i < lineString.vertexCount(); ++i )
177 {
178 QgsPoint p = lineString.pointN( i );
179 float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
180 float z2 = z + verticalLength;
181
182 if ( withAdjacency )
183 indexes << vertices.count(); // add the following vertex (for adjacency)
184
185 vertices << QVector3D( static_cast<float>( p.x() - origin.x() ), static_cast<float>( p.y() - origin.y() ), z );
186 indexes << vertices.count() - 1;
187 vertices << QVector3D( static_cast<float>( p.x() - origin.x() ), static_cast<float>( p.y() - origin.y() ), z2 );
188 indexes << vertices.count() - 1;
189
190 if ( withAdjacency )
191 indexes << vertices.count() - 1; // add the last vertex (for adjacency)
192
193 indexes << 0; // add primitive restart
194 }
195}
196
197
AltitudeClamping
Altitude clamping.
Definition qgis.h:3982
AltitudeBinding
Altitude binding.
Definition qgis.h:3995
@ Centroid
Clamp just centroid of feature.
Definition qgis.h:3997
@ Vertex
Clamp every vertex of feature.
Definition qgis.h:3996
Rendering context for preparation of 3D entities.
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:181
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 z
Definition qgspoint.h:54
double x
Definition qgspoint.h:52
double y
Definition qgspoint.h:53
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:30
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61