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 & )
92std::unique_ptr<QgsAbstractGeometry> extractTriangles(
93 const tinygltf::Model &model,
94 const tinygltf::Primitive &primitive,
97 const QMatrix4x4 *gltfLocalTransform,
101 auto posIt = primitive.attributes.find(
"POSITION" );
102 if ( posIt == primitive.attributes.end() )
104 feedback->
reportError( QObject::tr(
"Could not find POSITION attribute for primitive" ) );
107 int positionAccessorIndex = posIt->second;
112 QgsGltfUtils::accessorToMapCoordinates( model, positionAccessorIndex,
QgsMatrix4x4(), &ecefTransform, tileTranslationEcef, gltfLocalTransform,
Qgis::Axis::Y, x, y, z );
114 auto mp = std::make_unique<QgsMultiPolygon>();
116 if ( primitive.indices == -1 )
118 Q_ASSERT( x.size() % 3 == 0 );
120 mp->reserve( x.size() );
121 for (
int i = 0; i < x.size(); i += 3 )
124 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] ) } )
130 const tinygltf::Accessor &primitiveAccessor = model.accessors[primitive.indices];
131 const tinygltf::BufferView &bvPrimitive = model.bufferViews[primitiveAccessor.bufferView];
132 const tinygltf::Buffer &bPrimitive = model.buffers[bvPrimitive.buffer];
135 ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT
136 || primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT
137 || primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE )
138 && primitiveAccessor.type == TINYGLTF_TYPE_SCALAR
141 const char *primitivePtr =
reinterpret_cast<const char *
>( bPrimitive.data.data() ) + bvPrimitive.byteOffset + primitiveAccessor.byteOffset;
143 mp->reserve( primitiveAccessor.count / 3 );
144 for ( std::size_t i = 0; i < primitiveAccessor.count / 3; i++ )
146 unsigned int index1 = 0;
147 unsigned int index2 = 0;
148 unsigned int index3 = 0;
150 if ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT )
152 const unsigned short *usPtrPrimitive =
reinterpret_cast<const unsigned short *
>( primitivePtr );
153 if ( bvPrimitive.byteStride )
154 primitivePtr += bvPrimitive.byteStride;
156 primitivePtr += 3 *
sizeof(
unsigned short );
158 index1 = usPtrPrimitive[0];
159 index2 = usPtrPrimitive[1];
160 index3 = usPtrPrimitive[2];
162 else if ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE )
164 const unsigned char *usPtrPrimitive =
reinterpret_cast<const unsigned char *
>( primitivePtr );
165 if ( bvPrimitive.byteStride )
166 primitivePtr += bvPrimitive.byteStride;
168 primitivePtr += 3 *
sizeof(
unsigned char );
170 index1 = usPtrPrimitive[0];
171 index2 = usPtrPrimitive[1];
172 index3 = usPtrPrimitive[2];
176 const unsigned int *uintPtrPrimitive =
reinterpret_cast<const unsigned int *
>( primitivePtr );
177 if ( bvPrimitive.byteStride )
178 primitivePtr += bvPrimitive.byteStride;
180 primitivePtr += 3 *
sizeof(
unsigned int );
182 index1 = uintPtrPrimitive[0];
183 index2 = uintPtrPrimitive[1];
184 index3 = uintPtrPrimitive[2];
188 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] ) }
195std::unique_ptr<QgsAbstractGeometry> extractLines(
196 const tinygltf::Model &model,
197 const tinygltf::Primitive &primitive,
200 const QMatrix4x4 *gltfLocalTransform,
204 auto posIt = primitive.attributes.find(
"POSITION" );
205 if ( posIt == primitive.attributes.end() )
207 feedback->
reportError( QObject::tr(
"Could not find POSITION attribute for primitive" ) );
210 int positionAccessorIndex = posIt->second;
215 QgsGltfUtils::accessorToMapCoordinates( model, positionAccessorIndex,
QgsMatrix4x4(), &ecefTransform, tileTranslationEcef, gltfLocalTransform,
Qgis::Axis::Y, x, y, z );
217 auto ml = std::make_unique<QgsMultiLineString>();
219 if ( primitive.indices == -1 )
221 Q_ASSERT( x.size() % 2 == 0 );
223 ml->reserve( x.size() );
224 for (
int i = 0; i < x.size(); i += 2 )
231 const tinygltf::Accessor &primitiveAccessor = model.accessors[primitive.indices];
232 const tinygltf::BufferView &bvPrimitive = model.bufferViews[primitiveAccessor.bufferView];
233 const tinygltf::Buffer &bPrimitive = model.buffers[bvPrimitive.buffer];
236 ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT
237 || primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT
238 || primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE )
239 && primitiveAccessor.type == TINYGLTF_TYPE_SCALAR
242 const char *primitivePtr =
reinterpret_cast<const char *
>( bPrimitive.data.data() ) + bvPrimitive.byteOffset + primitiveAccessor.byteOffset;
244 ml->reserve( primitiveAccessor.count / 2 );
245 for ( std::size_t i = 0; i < primitiveAccessor.count / 2; i++ )
247 unsigned int index1 = 0;
248 unsigned int index2 = 0;
250 if ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT )
252 const unsigned short *usPtrPrimitive =
reinterpret_cast<const unsigned short *
>( primitivePtr );
253 if ( bvPrimitive.byteStride )
254 primitivePtr += bvPrimitive.byteStride;
256 primitivePtr += 2 *
sizeof(
unsigned short );
258 index1 = usPtrPrimitive[0];
259 index2 = usPtrPrimitive[1];
261 else if ( primitiveAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE )
263 const unsigned char *usPtrPrimitive =
reinterpret_cast<const unsigned char *
>( primitivePtr );
264 if ( bvPrimitive.byteStride )
265 primitivePtr += bvPrimitive.byteStride;
267 primitivePtr += 2 *
sizeof(
unsigned char );
269 index1 = usPtrPrimitive[0];
270 index2 = usPtrPrimitive[1];
274 const unsigned int *uintPtrPrimitive =
reinterpret_cast<const unsigned int *
>( primitivePtr );
275 if ( bvPrimitive.byteStride )
276 primitivePtr += bvPrimitive.byteStride;
278 primitivePtr += 2 *
sizeof(
unsigned int );
280 index1 = uintPtrPrimitive[0];
281 index2 = uintPtrPrimitive[1];
284 ml->addGeometry(
new QgsLineString( QVector<QgsPoint> {
QgsPoint( x[index1], y[index1], z[index1] ),
QgsPoint( x[index2], y[index2], z[index2] ) } ) );
292 const QString path = parameterAsFile( parameters, u
"INPUT"_s, context );
298 std::unique_ptr<QgsFeatureSink> polygonSink( parameterAsSink( parameters, u
"OUTPUT_POLYGONS"_s, context, polygonDest, fields,
Qgis::WkbType::MultiPolygonZ, destCrs ) );
299 if ( !polygonSink && parameters.value( u
"OUTPUT_POLYGONS"_s ).isValid() )
302 std::unique_ptr<QgsFeatureSink> lineSink( parameterAsSink( parameters, u
"OUTPUT_LINES"_s, context, lineDest, fields,
Qgis::WkbType::MultiLineStringZ, destCrs ) );
303 if ( !lineSink && parameters.value( u
"OUTPUT_LINES"_s ).isValid() )
307 QByteArray fileContent;
308 if ( f.open( QIODevice::ReadOnly ) )
310 fileContent = f.readAll();
319 tinygltf::Model model;
322 if ( !QgsGltfUtils::loadGltfModel( fileContent, model, &errors, &warnings ) )
326 if ( !warnings.isEmpty() )
330 feedback->
pushDebugInfo( QObject::tr(
"Found %1 scenes" ).arg( model.scenes.size() ) );
332 bool sceneOk =
false;
333 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
339 const tinygltf::Scene &scene = model.scenes[sceneIndex];
340 feedback->
pushDebugInfo( QObject::tr(
"Found %1 nodes in default scene [%2]" ).arg( scene.nodes.size() ).arg( sceneIndex ) );
342 QSet<int> warnedPrimitiveTypes;
344 const QgsVector3D tileTranslationEcef = QgsGltfUtils::extractTileTranslation( model );
345 std::function<void(
int nodeIndex,
const QMatrix4x4 &transform )> traverseNode;
346 traverseNode = [&model, feedback, &polygonSink, &lineSink, &warnedPrimitiveTypes, &ecefTransform, &tileTranslationEcef, &traverseNode, ¶meters](
int nodeIndex,
const QMatrix4x4 &parentTransform ) {
347 const tinygltf::Node &gltfNode = model.nodes[nodeIndex];
348 std::unique_ptr<QMatrix4x4> gltfLocalTransform = QgsGltfUtils::parseNodeTransform( gltfNode );
349 if ( !parentTransform.isIdentity() )
351 if ( gltfLocalTransform )
352 *gltfLocalTransform = parentTransform * *gltfLocalTransform;
355 gltfLocalTransform = std::make_unique<QMatrix4x4>( parentTransform );
359 if ( gltfNode.mesh >= 0 )
361 const tinygltf::Mesh &mesh = model.meshes[gltfNode.mesh];
362 feedback->pushDebugInfo( QObject::tr(
"Found %1 primitives in node [%2]" ).arg( mesh.primitives.size() ).arg( nodeIndex ) );
364 for (
const tinygltf::Primitive &primitive : mesh.primitives )
366 switch ( primitive.mode )
368 case TINYGLTF_MODE_TRIANGLES:
372 std::unique_ptr<QgsAbstractGeometry> geometry = extractTriangles( model, primitive, ecefTransform, tileTranslationEcef, gltfLocalTransform.get(), feedback );
384 case TINYGLTF_MODE_LINE:
388 std::unique_ptr<QgsAbstractGeometry> geometry = extractLines( model, primitive, ecefTransform, tileTranslationEcef, gltfLocalTransform.get(), feedback );
400 case TINYGLTF_MODE_POINTS:
401 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_POINTS ) )
403 feedback->reportError( QObject::tr(
"Point objects are not supported" ) );
404 warnedPrimitiveTypes.insert( TINYGLTF_MODE_POINTS );
408 case TINYGLTF_MODE_LINE_LOOP:
409 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_LINE_LOOP ) )
411 feedback->reportError( QObject::tr(
"Line loops in are not supported" ) );
412 warnedPrimitiveTypes.insert( TINYGLTF_MODE_LINE_LOOP );
416 case TINYGLTF_MODE_LINE_STRIP:
417 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_LINE_STRIP ) )
419 feedback->reportError( QObject::tr(
"Line strips in are not supported" ) );
420 warnedPrimitiveTypes.insert( TINYGLTF_MODE_LINE_STRIP );
424 case TINYGLTF_MODE_TRIANGLE_STRIP:
425 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_TRIANGLE_STRIP ) )
427 feedback->reportError( QObject::tr(
"Triangular strips are not supported" ) );
428 warnedPrimitiveTypes.insert( TINYGLTF_MODE_TRIANGLE_STRIP );
432 case TINYGLTF_MODE_TRIANGLE_FAN:
433 if ( !warnedPrimitiveTypes.contains( TINYGLTF_MODE_TRIANGLE_FAN ) )
435 feedback->reportError( QObject::tr(
"Triangular fans are not supported" ) );
436 warnedPrimitiveTypes.insert( TINYGLTF_MODE_TRIANGLE_FAN );
441 if ( !warnedPrimitiveTypes.contains( primitive.mode ) )
443 feedback->reportError( QObject::tr(
"Primitive type %1 are not supported" ).arg( primitive.mode ) );
444 warnedPrimitiveTypes.insert( primitive.mode );
451 for (
int childNode : gltfNode.children )
453 traverseNode( childNode, gltfLocalTransform ? *gltfLocalTransform : QMatrix4x4() );
457 if ( !scene.nodes.empty() )
459 for (
const int nodeIndex : scene.nodes )
461 traverseNode( nodeIndex, QMatrix4x4() );
468 polygonSink->finalize();
469 outputs.insert( u
"OUTPUT_POLYGONS"_s, polygonDest );
473 lineSink->finalize();
474 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...