QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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( Qgis::AltitudeClamping clamping, Qgis::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 
110  switch ( altBinding )
111  {
113  break;
115  centroid = lineString.centroid();
116  break;
117  }
118 
119  for ( int i = 0; i < lineString.vertexCount(); ++i )
120  {
121  QgsPoint p = lineString.pointN( i );
122  float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, *mapSettings );
123 
124  vertices << QVector3D( p.x() - mapSettings->origin().x(), z, -( p.y() - mapSettings->origin().y() ) );
125  indexes << vertices.count() - 1;
126  }
127 
128  if ( withAdjacency )
129  indexes << vertices.count() - 1; // add the last vertex (for adjacency)
130 
131  indexes << 0; // add primitive restart
132 }
133 
134 void QgsLineVertexData::addVerticalLines( const QgsLineString &lineString, float verticalLength, float extraHeightOffset )
135 {
137  switch ( altBinding )
138  {
140  break;
142  centroid = lineString.centroid();
143  break;
144  }
145 
146  for ( int i = 0; i < lineString.vertexCount(); ++i )
147  {
148  QgsPoint p = lineString.pointN( i );
149  float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, *mapSettings );
150  float z2 = z + verticalLength;
151 
152  if ( withAdjacency )
153  indexes << vertices.count(); // add the following vertex (for adjacency)
154 
155  vertices << QVector3D( p.x() - mapSettings->origin().x(), z, -( p.y() - mapSettings->origin().y() ) );
156  indexes << vertices.count() - 1;
157  vertices << QVector3D( p.x() - mapSettings->origin().x(), z2, -( p.y() - mapSettings->origin().y() ) );
158  indexes << vertices.count() - 1;
159 
160  if ( withAdjacency )
161  indexes << vertices.count() - 1; // add the last vertex (for adjacency)
162 
163  indexes << 0; // add primitive restart
164  }
165 }
166 
167 
Qgis::AltitudeBinding::Vertex
@ Vertex
Clamp every vertex of feature.
QgsLineString::pointN
QgsPoint pointN(int i) const
Returns the specified point from inside the line string.
Definition: qgslinestring.cpp:996
qgslinestring.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
Qgis::AltitudeBinding::Centroid
@ Centroid
Clamp just centroid of feature.
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsMeshUtils::centroid
CORE_EXPORT QgsMeshVertex centroid(const QgsMeshFace &face, const QVector< QgsMeshVertex > &vertices)
Returns the centroid of the face.
Definition: qgstriangularmesh.cpp:955
QgsLineString::centroid
QgsPoint centroid() const override
Returns the centroid of the geometry.
Definition: qgslinestring.cpp:1893
QgsLineString
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
QgsPoint::y
double y
Definition: qgspoint.h:70
Qgis::AltitudeBinding
AltitudeBinding
Altitude binding.
Definition: qgis.h:1770
Qgs3DUtils::clampAltitude
static float clampAltitude(const QgsPoint &p, Qgis::AltitudeClamping altClamp, Qgis::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:315
qgs3dutils.h
Qgs3DMapSettings
Definition of the world.
Definition: qgs3dmapsettings.h:57
QgsCurve::vertexCount
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
qgslogger.h
Qgis::AltitudeClamping
AltitudeClamping
Altitude clamping.
Definition: qgis.h:1757
qgslinevertexdata_p.h
QgsPoint::x
double x
Definition: qgspoint.h:69