QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsalgorithmb3dmtogltf.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmb3dmtogltf.cpp
3 ---------------------
4 begin : August 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgscesiumutils.h"
20#include "qgsgltfutils.h"
21
22#define TINYGLTF_IMPLEMENTATION
23#define TINYGLTF_NO_STB_IMAGE // we use QImage-based reading of images
24#define TINYGLTF_NO_STB_IMAGE_WRITE // we don't need writing of images
25
26#include "tiny_gltf.h"
27#include <fstream>
28
30
31QString QgsB3DMToGltfAlgorithm::name() const
32{
33 return QStringLiteral( "b3dmtogltf" );
34}
35
36QString QgsB3DMToGltfAlgorithm::displayName() const
37{
38 return QObject::tr( "Convert B3DM to GLTF" );
39}
40
41QStringList QgsB3DMToGltfAlgorithm::tags() const
42{
43 return QObject::tr( "3d,tiles,cesium" ).split( ',' );
44}
45
46QString QgsB3DMToGltfAlgorithm::group() const
47{
48 return QObject::tr( "3D Tiles" );
49}
50
51QString QgsB3DMToGltfAlgorithm::groupId() const
52{
53 return QStringLiteral( "3dtiles" );
54}
55
56QString QgsB3DMToGltfAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "Converts files from the legacy B3DM format to GLTF." );
59}
60
61QgsB3DMToGltfAlgorithm *QgsB3DMToGltfAlgorithm::createInstance() const
62{
63 return new QgsB3DMToGltfAlgorithm();
64}
65
66void QgsB3DMToGltfAlgorithm::initAlgorithm( const QVariantMap & )
67{
68 addParameter( new QgsProcessingParameterFile( QStringLiteral( "INPUT" ), QObject::tr( "Input B3DM" ), Qgis::ProcessingFileParameterBehavior::File,
69 QStringLiteral( "b3dm" ), QVariant(), false, QStringLiteral( "B3DM (*.b3dm *.B3DM)" ) ) );
70
71 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output file" ), QStringLiteral( "GLTF (*.gltf *.GLTF);;GLB (*.glb *.GLB)" ) ) );
72}
73
74QVariantMap QgsB3DMToGltfAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75{
76 const QString path = parameterAsFile( parameters, QStringLiteral( "INPUT" ), context );
77 const QString outputPath = parameterAsFile( parameters, QStringLiteral( "OUTPUT" ), context );
78
79 if ( !QFile::exists( path ) )
80 throw QgsProcessingException( QObject::tr( "Could not load source file %1." ).arg( path ) );
81
82 QFile f( path );
83 QByteArray fileContent;
84 if ( f.open( QIODevice::ReadOnly ) )
85 {
86 fileContent = f.readAll();
87 }
88 else
89 {
90 throw QgsProcessingException( QObject::tr( "Could not load source file %1." ).arg( path ) );
91 }
92
94
95 // load gltf and then rewrite -- this allows us to both validate the B3DM GLTF content, and
96 // also gives the opportunity to "upgrade" B3DM specific options (like CESIUM_RTC) to standard
97 // GLTF extensions
98 tinygltf::Model model;
99 QString errors;
100 QString warnings;
101 const bool res = QgsGltfUtils::loadGltfModel( b3dmContent.gltf, model, &errors, &warnings );
102 if ( !errors.isEmpty() )
103 {
104 feedback->reportError( errors );
105 }
106 if ( !warnings.isEmpty() )
107 {
108 feedback->pushWarning( warnings );
109 }
110 if ( !res )
111 {
112 // if we can't read the GLTF, then just write the original GLTF content to the output file
113 QFile outputFile( outputPath );
114 if ( !outputFile.open( QFile::WriteOnly ) )
115 {
116 throw QgsProcessingException( QObject::tr( "Could not create destination file %1." ).arg( outputPath ) );
117 }
118 outputFile.write( b3dmContent.gltf );
119 }
120 else
121 {
122 bool sceneOk = false;
123 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
124 if ( !sceneOk )
125 {
126 throw QgsProcessingException( QObject::tr( "No scenes found in model" ) );
127 }
128
129 feedback->pushDebugInfo( QObject::tr( "Found %1 scenes" ).arg( model.scenes.size() ) );
130
131 const tinygltf::Scene &scene = model.scenes[sceneIndex];
132 feedback->pushDebugInfo( QObject::tr( "Found %1 nodes in default scene [%2]" ).arg( scene.nodes.size() ).arg( sceneIndex ) );
133 if ( !scene.nodes.empty() )
134 {
135 const int nodeIndex = scene.nodes[0];
136 const tinygltf::Node &gltfNode = model.nodes[nodeIndex];
137 if ( gltfNode.mesh >= 0 )
138 {
139 const tinygltf::Mesh &mesh = model.meshes[gltfNode.mesh];
140 feedback->pushDebugInfo( QObject::tr( "Found %1 primitives in default scene node [%2]" ).arg( mesh.primitives.size() ).arg( nodeIndex ) );
141 }
142 }
143
144 if ( !b3dmContent.rtcCenter.isNull() )
145 {
146 // transfer B3DM RTC center to GLTF CESIUM_RTC extension
147 tinygltf::Value::Object cesiumRtc;
148 cesiumRtc["center"] = tinygltf::Value( tinygltf::Value::Array
149 {
150 tinygltf::Value( b3dmContent.rtcCenter.x() ),
151 tinygltf::Value( b3dmContent.rtcCenter.y() ),
152 tinygltf::Value( b3dmContent.rtcCenter.z() )
153 } );
154
155 model.extensions["CESIUM_RTC"] = tinygltf::Value( cesiumRtc );
156 model.extensionsRequired.emplace_back( "CESIUM_RTC" );
157 model.extensionsUsed.emplace_back( "CESIUM_RTC" );
158 }
159
160 const QString outputExtension = QFileInfo( outputPath ).suffix();
161 const bool isGlb = outputExtension.compare( QLatin1String( "glb" ), Qt::CaseInsensitive ) == 0;
162 const QByteArray outputFile = QFile::encodeName( outputPath );
163 std::ofstream of( outputFile.constData(), std::ios::binary | std::ios::trunc );
164 if ( !of )
165 throw QgsProcessingException( QObject::tr( "Could not create destination file %1." ).arg( outputPath ) );
166
167 tinygltf::TinyGLTF writer;
168 if ( !writer.WriteGltfSceneToStream( &model, of, true, isGlb ) )
169 {
170 of.close();
171 throw QgsProcessingException( QObject::tr( "Could not write GLTF model to %1." ).arg( outputPath ) );
172 }
173 of.close();
174 }
175
176 QVariantMap outputs;
177 outputs.insert( QStringLiteral( "OUTPUT" ), outputPath );
178 return outputs;
179}
180
@ File
Parameter is a single file.
static B3DMContents extractGltfFromB3dm(const QByteArray &tileContent)
Extracts GLTF binary data and other contents from the legacy b3dm (Batched 3D Model) tile format.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
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 generic file based destination parameter, for specifying the destination path for a file (non-map l...
An input file or folder parameter for processing algorithms.
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:50
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:52
bool isNull() const
Returns true if all three coordinates are zero.
Definition: qgsvector3d.h:45
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:48
Encapsulates the contents of a B3DM file.
QByteArray gltf
GLTF binary content.
QgsVector3D rtcCenter
Optional RTC center.