QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 : qgis::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 : qgis::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( Qt3DRender::QBuffer::VertexBuffer, parent );
74  vertexBuffer->setData( createVertexBuffer() );
75 
76  Qt3DRender::QBuffer *indexBuffer = new Qt3DRender::QBuffer( Qt3DRender::QBuffer::IndexBuffer, parent );
77  indexBuffer->setData( createIndexBuffer() );
78 
79  QgsDebugMsg( QString( "vertex buffer %1 MB index buffer %2 MB " ).arg( vertexBuffer->data().count() / 1024. / 1024. ).arg( indexBuffer->data().count() / 1024. / 1024. ) );
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->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
87 
88  Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute( parent );
89  indexAttribute->setAttributeType( Qt3DRender::QAttribute::IndexAttribute );
90  indexAttribute->setBuffer( indexBuffer );
91  indexAttribute->setVertexBaseType( Qt3DRender::QAttribute::UnsignedInt );
92 
93  Qt3DRender::QGeometry *geom = new Qt3DRender::QGeometry;
94  geom->addAttribute( positionAttribute );
95  geom->addAttribute( indexAttribute );
96  return geom;
97 }
98 
99 void QgsLineVertexData::addLineString( const QgsLineString &lineString, float extraHeightOffset )
100 {
101  if ( withAdjacency )
102  indexes << vertices.count(); // add the following vertex (for adjacency)
103 
104  QgsPoint centroid;
105  if ( altBinding == Qgs3DTypes::AltBindCentroid )
106  centroid = lineString.centroid();
107 
108  for ( int i = 0; i < lineString.vertexCount(); ++i )
109  {
110  QgsPoint p = lineString.pointN( i );
111  float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, *mapSettings );
112 
113  vertices << QVector3D( p.x() - mapSettings->origin().x(), z, -( p.y() - mapSettings->origin().y() ) );
114  indexes << vertices.count() - 1;
115  }
116 
117  if ( withAdjacency )
118  indexes << vertices.count() - 1; // add the last vertex (for adjacency)
119 
120  indexes << 0; // add primitive restart
121 }
122 
123 
124 void QgsLineVertexData::addVerticalLines( const QgsLineString &lineString, float verticalLength )
125 {
126  QgsPoint centroid;
127  if ( altBinding == Qgs3DTypes::AltBindCentroid )
128  centroid = lineString.centroid();
129 
130  for ( int i = 0; i < lineString.vertexCount(); ++i )
131  {
132  QgsPoint p = lineString.pointN( i );
133  float z = Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight, centroid, *mapSettings );
134  float z2 = z + verticalLength;
135 
136  if ( withAdjacency )
137  indexes << vertices.count(); // add the following vertex (for adjacency)
138 
139  vertices << QVector3D( p.x() - mapSettings->origin().x(), z, -( p.y() - mapSettings->origin().y() ) );
140  indexes << vertices.count() - 1;
141  vertices << QVector3D( p.x() - mapSettings->origin().x(), z2, -( p.y() - mapSettings->origin().y() ) );
142  indexes << vertices.count() - 1;
143 
144  if ( withAdjacency )
145  indexes << vertices.count() - 1; // add the last vertex (for adjacency)
146 
147  indexes << 0; // add primitive restart
148  }
149 }
150 
151 
AltitudeClamping
how to handle altitude of vector features
Definition: qgs3dtypes.h:34
double y
Definition: qgspoint.h:42
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsPoint centroid() const override
Returns the centroid of the geometry.
3 Definition of the world
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Definition: qgscurve.cpp:152
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:250
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
AltitudeBinding
how to handle clamping of vertices of individual features
Definition: qgs3dtypes.h:42
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
QgsPoint pointN(int i) const
Returns the specified point from inside the line string.
Clamp just centroid of feature.
Definition: qgs3dtypes.h:45
double x
Definition: qgspoint.h:41