QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #include <Qt3DRender/QAttribute>
19 #include <Qt3DRender/QBuffer>
20 #include <Qt3DRender/QGeometry>
21 
22 #include "qgslogger.h"
23 #include "qgs3dutils.h"
24 #include "qgslinestring.h"
25 
27 
28 
29 QgsLineVertexData::QgsLineVertexData()
30 {
31  // the first index is invalid, we use it for primitive restart
32  vertices << QVector3D();
33 }
34 
35 void QgsLineVertexData::init( Qgs3DTypes::AltitudeClamping clamping, Qgs3DTypes::AltitudeBinding binding, float height, const Qgs3DMapSettings *map )
36 {
37  altClamping = clamping;
38  altBinding = binding;
39  baseHeight = height;
40  mapSettings = map;
41 }
42 
43 QByteArray QgsLineVertexData::createVertexBuffer()
44 {
45  QByteArray vertexBufferData;
46  vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
47  float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
48  int idx = 0;
49  for ( const auto &v : std::as_const( vertices ) )
50  {
51  rawVertexArray[idx++] = v.x();
52  rawVertexArray[idx++] = v.y();
53  rawVertexArray[idx++] = v.z();
54  }
55  return vertexBufferData;
56 }
57 
58 QByteArray QgsLineVertexData::createIndexBuffer()
59 {
60  QByteArray indexBufferData;
61  indexBufferData.resize( indexes.size() * sizeof( int ) );
62  unsigned int *rawIndexArray = reinterpret_cast<unsigned int *>( indexBufferData.data() );
63  int idx = 0;
64  for ( unsigned int indexVal : std::as_const( indexes ) )
65  {
66  rawIndexArray[idx++] = indexVal;
67  }
68  return indexBufferData;
69 }
70 
71 Qt3DRender::QGeometry *QgsLineVertexData::createGeometry( Qt3DCore::QNode *parent )
72 {
73  Qt3DRender::QBuffer *vertexBuffer = new Qt3DRender::QBuffer( parent );
74  vertexBuffer->setData( createVertexBuffer() );
75 
76  Qt3DRender::QBuffer *indexBuffer = new Qt3DRender::QBuffer( parent );
77  indexBuffer->setData( createIndexBuffer() );
78 
79  QgsDebugMsgLevel( QString( "vertex buffer %1 MB index buffer %2 MB " ).arg( vertexBuffer->data().count() / 1024. / 1024. ).arg( indexBuffer->data().count() / 1024. / 1024. ), 2 );
80 
81  Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute( parent );
82  positionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
83  positionAttribute->setBuffer( vertexBuffer );
84  positionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
85  positionAttribute->setVertexSize( 3 );
86  positionAttribute->setByteStride( 3 * sizeof( float ) );
87  positionAttribute->setByteOffset( 0 );
88  positionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
89 
90  Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute( parent );
91  indexAttribute->setAttributeType( Qt3DRender::QAttribute::IndexAttribute );
92  indexAttribute->setBuffer( indexBuffer );
93  indexAttribute->setByteOffset( 0 );
94  indexAttribute->setByteStride( sizeof( uint ) );
95  indexAttribute->setVertexBaseType( Qt3DRender::QAttribute::UnsignedInt );
96 
97  Qt3DRender::QGeometry *geom = new Qt3DRender::QGeometry;
98  geom->addAttribute( positionAttribute );
99  geom->addAttribute( indexAttribute );
100 
101  return geom;
102 }
103 
104 void QgsLineVertexData::addLineString( const QgsLineString &lineString, float extraHeightOffset )
105 {
106  if ( withAdjacency )
107  indexes << vertices.count(); // add the following vertex (for adjacency)
108 
109  QgsPoint centroid;
110  if ( altBinding == Qgs3DTypes::AltBindCentroid )
111  centroid = lineString.centroid();
112 
113  for ( int i = 0; i < lineString.vertexCount(); ++i )
114  {
115  QgsPoint p = lineString.pointN( i );
116  float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, *mapSettings );
117 
118  vertices << QVector3D( p.x() - mapSettings->origin().x(), z, -( p.y() - mapSettings->origin().y() ) );
119  indexes << vertices.count() - 1;
120  }
121 
122  if ( withAdjacency )
123  indexes << vertices.count() - 1; // add the last vertex (for adjacency)
124 
125  indexes << 0; // add primitive restart
126 }
127 
128 void QgsLineVertexData::addVerticalLines( const QgsLineString &lineString, float verticalLength, float extraHeightOffset )
129 {
130  QgsPoint centroid;
131  if ( altBinding == Qgs3DTypes::AltBindCentroid )
132  centroid = lineString.centroid();
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, *mapSettings );
138  float z2 = z + verticalLength;
139 
140  if ( withAdjacency )
141  indexes << vertices.count(); // add the following vertex (for adjacency)
142 
143  vertices << QVector3D( p.x() - mapSettings->origin().x(), z, -( p.y() - mapSettings->origin().y() ) );
144  indexes << vertices.count() - 1;
145  vertices << QVector3D( p.x() - mapSettings->origin().x(), z2, -( p.y() - mapSettings->origin().y() ) );
146  indexes << vertices.count() - 1;
147 
148  if ( withAdjacency )
149  indexes << vertices.count() - 1; // add the last vertex (for adjacency)
150 
151  indexes << 0; // add primitive restart
152  }
153 }
154 
155 
AltitudeClamping
how to handle altitude of vector features
Definition: qgs3dtypes.h:35
AltitudeBinding
how to handle clamping of vertices of individual features
Definition: qgs3dtypes.h:43
@ AltBindCentroid
Clamp just centroid of feature.
Definition: qgs3dtypes.h:45
static float clampAltitude(const QgsPoint &p, Qgs3DTypes::AltitudeClamping altClamp, Qgs3DTypes::AltitudeBinding altBind, float height, const QgsPoint &centroid, const Qgs3DMapSettings &map)
Clamps altitude of a vertex according to the settings, returns Z value.
Definition: qgs3dutils.cpp:253
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Definition: qgscurve.cpp:179
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
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
Q_GADGET double x
Definition: qgspoint.h:52
double y
Definition: qgspoint.h:53
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39