33using namespace Qt::StringLiterals;
35#define TINYGLTF_NO_STB_IMAGE
36#define TINYGLTF_NO_STB_IMAGE_WRITE
42QString QgsGltfToVectorFeaturesAlgorithm::name()
const
44 return u
"gltftovector"_s;
47QString QgsGltfToVectorFeaturesAlgorithm::displayName()
const
49 return QObject::tr(
"Convert GLTF to vector features" );
52QStringList QgsGltfToVectorFeaturesAlgorithm::tags()
const
54 return QObject::tr(
"3d,tiles,cesium" ).split(
',' );
57QString QgsGltfToVectorFeaturesAlgorithm::group()
const
59 return QObject::tr(
"3D Tiles" );
62QString QgsGltfToVectorFeaturesAlgorithm::groupId()
const
67QString QgsGltfToVectorFeaturesAlgorithm::shortHelpString()
const
69 return QObject::tr(
"This algorithm converts GLTF content to standard vector layer formats." );
72QString QgsGltfToVectorFeaturesAlgorithm::shortDescription()
const
74 return QObject::tr(
"Converts GLTF content to standard vector layer formats." );
77QgsGltfToVectorFeaturesAlgorithm *QgsGltfToVectorFeaturesAlgorithm::createInstance()
const
79 return new QgsGltfToVectorFeaturesAlgorithm();
82void QgsGltfToVectorFeaturesAlgorithm::initAlgorithm(
const QVariantMap & )
90std::unique_ptr<QgsAbstractGeometry> extractTriangles(
91 const tinygltf::Model &model,
92 const tinygltf::Primitive &primitive,
95 const QMatrix4x4 *gltfLocalTransform,
99 auto posIt = primitive.attributes.find(
"POSITION" );
100 if ( posIt == primitive.attributes.end() )
102 feedback->
reportError( QObject::tr(
"Could not find POSITION attribute for primitive" ) );
105 int positionAccessorIndex = posIt->second;
110 QgsGltfUtils::accessorToMapCoordinates(
119 auto mp = std::make_unique<QgsMultiPolygon>();
121 if ( primitive.indices == -1 )
123 Q_ASSERT( x.size() % 3 == 0 );
125 mp->reserve( x.size() );
126 for (
int i = 0; i < x.size(); i += 3 )
128 mp->addGeometry(
new QgsPolygon(
new QgsLineString( QVector<QgsPoint> {
QgsPoint( x[i], y[i], z[i] ),
QgsPoint( x[i + 1], y[i + 1], z[i + 1] ),
QgsPoint( x[i + 2], y[i + 2], z[i + 2] ),
QgsPoint( x[i], y[i], z[i] ) } ) ) );
133 const tinygltf::Accessor &primitiveAccessor = model.accessors[primitive.indices];
134 const tinygltf::BufferView &bvPrimitive = model.bufferViews[primitiveAccessor.bufferView];
135 const tinygltf::Buffer &bPrimitive = model.buffers[bvPrimitive.buffer];
137 Q_ASSERT( ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT || primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT || primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE ) && primitiveAccessor.type == TINYGLTF_TYPE_SCALAR );
139 const char *primitivePtr =
reinterpret_cast<const char *
>( bPrimitive.data.data() ) + bvPrimitive.byteOffset + primitiveAccessor.byteOffset;
141 mp->reserve( primitiveAccessor.count / 3 );
142 for ( std::size_t i = 0; i < primitiveAccessor.count / 3; i++ )
144 unsigned int index1 = 0;
145 unsigned int index2 = 0;
146 unsigned int index3 = 0;
148 if ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT )
150 const unsigned short *usPtrPrimitive =
reinterpret_cast<const unsigned short *
>( primitivePtr );
151 if ( bvPrimitive.byteStride )
152 primitivePtr += bvPrimitive.byteStride;
154 primitivePtr += 3 *
sizeof(
unsigned short );
156 index1 = usPtrPrimitive[0];
157 index2 = usPtrPrimitive[1];
158 index3 = usPtrPrimitive[2];
160 else if ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE )
162 const unsigned char *usPtrPrimitive =
reinterpret_cast<const unsigned char *
>( primitivePtr );
163 if ( bvPrimitive.byteStride )
164 primitivePtr += bvPrimitive.byteStride;
166 primitivePtr += 3 *
sizeof(
unsigned char );
168 index1 = usPtrPrimitive[0];
169 index2 = usPtrPrimitive[1];
170 index3 = usPtrPrimitive[2];
174 const unsigned int *uintPtrPrimitive =
reinterpret_cast<const unsigned int *
>( primitivePtr );
175 if ( bvPrimitive.byteStride )
176 primitivePtr += bvPrimitive.byteStride;
178 primitivePtr += 3 *
sizeof(
unsigned int );
180 index1 = uintPtrPrimitive[0];
181 index2 = uintPtrPrimitive[1];
182 index3 = uintPtrPrimitive[2];
185 mp->addGeometry(
new QgsPolygon(
new QgsLineString( QVector<QgsPoint> {
QgsPoint( x[index1], y[index1], z[index1] ),
QgsPoint( x[index2], y[index2], z[index2] ),
QgsPoint( x[index3], y[index3], z[index3] ),
QgsPoint( x[index1], y[index1], z[index1] ) } ) ) );
191std::unique_ptr<QgsAbstractGeometry> extractLines(
192 const tinygltf::Model &model,
193 const tinygltf::Primitive &primitive,
196 const QMatrix4x4 *gltfLocalTransform,
200 auto posIt = primitive.attributes.find(
"POSITION" );
201 if ( posIt == primitive.attributes.end() )
203 feedback->
reportError( QObject::tr(
"Could not find POSITION attribute for primitive" ) );
206 int positionAccessorIndex = posIt->second;
211 QgsGltfUtils::accessorToMapCoordinates(
220 auto ml = std::make_unique<QgsMultiLineString>();
222 if ( primitive.indices == -1 )
224 Q_ASSERT( x.size() % 2 == 0 );
226 ml->reserve( x.size() );
227 for (
int i = 0; i < x.size(); i += 2 )
234 const tinygltf::Accessor &primitiveAccessor = model.accessors[primitive.indices];
235 const tinygltf::BufferView &bvPrimitive = model.bufferViews[primitiveAccessor.bufferView];
236 const tinygltf::Buffer &bPrimitive = model.buffers[bvPrimitive.buffer];
238 Q_ASSERT( ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT || primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT || primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE ) && primitiveAccessor.type == TINYGLTF_TYPE_SCALAR );
240 const char *primitivePtr =
reinterpret_cast<const char *
>( bPrimitive.data.data() ) + bvPrimitive.byteOffset + primitiveAccessor.byteOffset;
242 ml->reserve( primitiveAccessor.count / 2 );
243 for ( std::size_t i = 0; i < primitiveAccessor.count / 2; i++ )
245 unsigned int index1 = 0;
246 unsigned int index2 = 0;
248 if ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT )
250 const unsigned short *usPtrPrimitive =
reinterpret_cast<const unsigned short *
>( primitivePtr );
251 if ( bvPrimitive.byteStride )
252 primitivePtr += bvPrimitive.byteStride;
254 primitivePtr += 2 *
sizeof(
unsigned short );
256 index1 = usPtrPrimitive[0];
257 index2 = usPtrPrimitive[1];
259 else if ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE )
261 const unsigned char *usPtrPrimitive =
reinterpret_cast<const unsigned char *
>( primitivePtr );
262 if ( bvPrimitive.byteStride )
263 primitivePtr += bvPrimitive.byteStride;
265 primitivePtr += 2 *
sizeof(
unsigned char );
267 index1 = usPtrPrimitive[0];
268 index2 = usPtrPrimitive[1];
272 const unsigned int *uintPtrPrimitive =
reinterpret_cast<const unsigned int *
>( primitivePtr );
273 if ( bvPrimitive.byteStride )
274 primitivePtr += bvPrimitive.byteStride;
276 primitivePtr += 2 *
sizeof(
unsigned int );
278 index1 = uintPtrPrimitive[0];
279 index2 = uintPtrPrimitive[1];
282 ml->addGeometry(
new QgsLineString( QVector<QgsPoint> {
QgsPoint( x[index1], y[index1], z[index1] ),
QgsPoint( x[index2], y[index2], z[index2] ) } ) );
290 const QString path = parameterAsFile( parameters, u
"INPUT"_s, context );
296 std::unique_ptr<QgsFeatureSink> polygonSink( parameterAsSink( parameters, u
"OUTPUT_POLYGONS"_s, context, polygonDest, fields,
Qgis::WkbType::MultiPolygonZ, destCrs ) );
297 if ( !polygonSink && parameters.value( u
"OUTPUT_POLYGONS"_s ).isValid() )
300 std::unique_ptr<QgsFeatureSink> lineSink( parameterAsSink( parameters, u
"OUTPUT_LINES"_s, context, lineDest, fields,
Qgis::WkbType::MultiLineStringZ, destCrs ) );
301 if ( !lineSink && parameters.value( u
"OUTPUT_LINES"_s ).isValid() )
305 QByteArray fileContent;
306 if ( f.open( QIODevice::ReadOnly ) )
308 fileContent = f.readAll();
317 tinygltf::Model model;
320 if ( !QgsGltfUtils::loadGltfModel( fileContent, model, &errors, &warnings ) )
324 if ( !warnings.isEmpty() )
328 feedback->
pushDebugInfo( QObject::tr(
"Found %1 scenes" ).arg( model.scenes.size() ) );
330 bool sceneOk =
false;
331 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
337 const tinygltf::Scene &scene = model.scenes[sceneIndex];
338 feedback->
pushDebugInfo( QObject::tr(
"Found %1 nodes in default scene [%2]" ).arg( scene.nodes.size() ).arg( sceneIndex ) );
340 QSet<int> warnedPrimitiveTypes;
342 const QgsVector3D tileTranslationEcef = QgsGltfUtils::extractTileTranslation( model );
343 std::function<void(
int nodeIndex,
const QMatrix4x4 &transform )> traverseNode;
344 traverseNode = [&model, feedback, &polygonSink, &lineSink, &warnedPrimitiveTypes, &ecefTransform, &tileTranslationEcef, &traverseNode, ¶meters](
int nodeIndex,
const QMatrix4x4 &parentTransform ) {
345 const tinygltf::Node &gltfNode = model.nodes[nodeIndex];
346 std::unique_ptr<QMatrix4x4> gltfLocalTransform = QgsGltfUtils::parseNodeTransform( gltfNode );
347 if ( !parentTransform.isIdentity() )
349 if ( gltfLocalTransform )
350 *gltfLocalTransform = parentTransform * *gltfLocalTransform;
353 gltfLocalTransform = std::make_unique<QMatrix4x4>( parentTransform );
357 if ( gltfNode.mesh >= 0 )
359 const tinygltf::Mesh &mesh = model.meshes[gltfNode.mesh];
360 feedback->pushDebugInfo( QObject::tr(
"Found %1 primitives in node [%2]" ).arg( mesh.primitives.size() ).arg( nodeIndex ) );
362 for (
const tinygltf::Primitive &primitive : mesh.primitives )
364 switch ( primitive.mode )
366 case TINYGLTF_MODE_TRIANGLES:
370 std::unique_ptr<QgsAbstractGeometry> geometry = extractTriangles( model, primitive, ecefTransform, tileTranslationEcef, gltfLocalTransform.get(), feedback );
382 case TINYGLTF_MODE_LINE:
386 std::unique_ptr<QgsAbstractGeometry> geometry = extractLines( model, primitive, ecefTransform, tileTranslationEcef, gltfLocalTransform.get(), feedback );
398 case TINYGLTF_MODE_POINTS:
399 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_POINTS ) )
401 feedback->reportError( QObject::tr(
"Point objects are not supported" ) );
402 warnedPrimitiveTypes.insert( TINYGLTF_MODE_POINTS );
406 case TINYGLTF_MODE_LINE_LOOP:
407 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_LINE_LOOP ) )
409 feedback->reportError( QObject::tr(
"Line loops in are not supported" ) );
410 warnedPrimitiveTypes.insert( TINYGLTF_MODE_LINE_LOOP );
414 case TINYGLTF_MODE_LINE_STRIP:
415 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_LINE_STRIP ) )
417 feedback->reportError( QObject::tr(
"Line strips in are not supported" ) );
418 warnedPrimitiveTypes.insert( TINYGLTF_MODE_LINE_STRIP );
422 case TINYGLTF_MODE_TRIANGLE_STRIP:
423 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_TRIANGLE_STRIP ) )
425 feedback->reportError( QObject::tr(
"Triangular strips are not supported" ) );
426 warnedPrimitiveTypes.insert( TINYGLTF_MODE_TRIANGLE_STRIP );
430 case TINYGLTF_MODE_TRIANGLE_FAN:
431 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_TRIANGLE_FAN ) )
433 feedback->reportError( QObject::tr(
"Triangular fans are not supported" ) );
434 warnedPrimitiveTypes.insert( TINYGLTF_MODE_TRIANGLE_FAN );
439 if ( !warnedPrimitiveTypes.contains( primitive.mode ) )
441 feedback->reportError( QObject::tr(
"Primitive type %1 are not supported" ).arg( primitive.mode ) );
442 warnedPrimitiveTypes.insert( primitive.mode );
449 for (
int childNode : gltfNode.children )
451 traverseNode( childNode, gltfLocalTransform ? *gltfLocalTransform : QMatrix4x4() );
455 if ( !scene.nodes.empty() )
457 for (
const int nodeIndex : scene.nodes )
459 traverseNode( nodeIndex, QMatrix4x4() );
466 polygonSink->finalize();
467 outputs.insert( u
"OUTPUT_POLYGONS"_s, polygonDest );
471 lineSink->finalize();
472 outputs.insert( u
"OUTPUT_LINES"_s, lineDest );
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
@ File
Parameter is a single file.
@ MultiLineStringZ
MultiLineStringZ.
@ MultiPolygonZ
MultiPolygonZ.
Represents a coordinate reference system (CRS).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Container of fields for a vector layer.
Line string geometry type, with support for z-dimension and m-values.
A simple 4x4 matrix implementation useful for transformation in 3D space.
Point geometry type, with support for z-dimension and m-values.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A feature sink output for processing algorithms.
An input file or folder parameter for processing algorithms.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...