QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
20#include "qgscesiumutils.h"
21#include "qgsgltfutils.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
27#define TINYGLTF_IMPLEMENTATION
28#define TINYGLTF_NO_STB_IMAGE // we use QImage-based reading of images
29#define TINYGLTF_NO_STB_IMAGE_WRITE // we don't need writing of images
30
31#include "tiny_gltf.h"
32#include <fstream>
33
35
36QString QgsB3DMToGltfAlgorithm::name() const
37{
38 return u"b3dmtogltf"_s;
39}
40
41QString QgsB3DMToGltfAlgorithm::displayName() const
42{
43 return QObject::tr( "Convert B3DM to GLTF" );
44}
45
46QStringList QgsB3DMToGltfAlgorithm::tags() const
47{
48 return QObject::tr( "3d,tiles,cesium" ).split( ',' );
49}
50
51QString QgsB3DMToGltfAlgorithm::group() const
52{
53 return QObject::tr( "3D Tiles" );
54}
55
56QString QgsB3DMToGltfAlgorithm::groupId() const
57{
58 return u"3dtiles"_s;
59}
60
61QString QgsB3DMToGltfAlgorithm::shortHelpString() const
62{
63 return QObject::tr( "This algorithm converts files from the legacy B3DM format to GLTF." );
64}
65
66QString QgsB3DMToGltfAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Converts files from the legacy B3DM format to GLTF." );
69}
70
71QgsB3DMToGltfAlgorithm *QgsB3DMToGltfAlgorithm::createInstance() const
72{
73 return new QgsB3DMToGltfAlgorithm();
74}
75
76void QgsB3DMToGltfAlgorithm::initAlgorithm( const QVariantMap & )
77{
78 addParameter( new QgsProcessingParameterFile( u"INPUT"_s, QObject::tr( "Input B3DM" ), Qgis::ProcessingFileParameterBehavior::File, u"b3dm"_s, QVariant(), false, u"B3DM (*.b3dm *.B3DM)"_s ) );
79
80 addParameter( new QgsProcessingParameterFileDestination( u"OUTPUT"_s, QObject::tr( "Output file" ), u"GLTF (*.gltf *.GLTF);;GLB (*.glb *.GLB)"_s ) );
81}
82
83QVariantMap QgsB3DMToGltfAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
84{
85 const QString path = parameterAsFile( parameters, u"INPUT"_s, context );
86 const QString outputPath = parameterAsFile( parameters, u"OUTPUT"_s, context );
87
88 if ( !QFile::exists( path ) )
89 throw QgsProcessingException( QObject::tr( "Could not load source file %1." ).arg( path ) );
90
91 QFile f( path );
92 QByteArray fileContent;
93 if ( f.open( QIODevice::ReadOnly ) )
94 {
95 fileContent = f.readAll();
96 }
97 else
98 {
99 throw QgsProcessingException( QObject::tr( "Could not load source file %1." ).arg( path ) );
100 }
101
102 const QgsCesiumUtils::B3DMContents b3dmContent = QgsCesiumUtils::extractGltfFromB3dm( fileContent );
103
104 // load gltf and then rewrite -- this allows us to both validate the B3DM GLTF content, and
105 // also gives the opportunity to "upgrade" B3DM specific options (like CESIUM_RTC) to standard
106 // GLTF extensions
107 tinygltf::Model model;
108 QString errors;
109 QString warnings;
110 const bool res = QgsGltfUtils::loadGltfModel( b3dmContent.gltf, model, &errors, &warnings );
111 if ( !errors.isEmpty() )
112 {
113 feedback->reportError( errors );
114 }
115 if ( !warnings.isEmpty() )
116 {
117 feedback->pushWarning( warnings );
118 }
119 if ( !res )
120 {
121 // if we can't read the GLTF, then just write the original GLTF content to the output file
122 QFile outputFile( outputPath );
123 if ( !outputFile.open( QFile::WriteOnly ) )
124 {
125 throw QgsProcessingException( QObject::tr( "Could not create destination file %1." ).arg( outputPath ) );
126 }
127 outputFile.write( b3dmContent.gltf );
128 }
129 else
130 {
131 bool sceneOk = false;
132 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
133 if ( !sceneOk )
134 {
135 throw QgsProcessingException( QObject::tr( "No scenes found in model" ) );
136 }
137
138 feedback->pushDebugInfo( QObject::tr( "Found %1 scenes" ).arg( model.scenes.size() ) );
139
140 const tinygltf::Scene &scene = model.scenes[sceneIndex];
141 feedback->pushDebugInfo( QObject::tr( "Found %1 nodes in default scene [%2]" ).arg( scene.nodes.size() ).arg( sceneIndex ) );
142 if ( !scene.nodes.empty() )
143 {
144 const int nodeIndex = scene.nodes[0];
145 const tinygltf::Node &gltfNode = model.nodes[nodeIndex];
146 if ( gltfNode.mesh >= 0 )
147 {
148 const tinygltf::Mesh &mesh = model.meshes[gltfNode.mesh];
149 feedback->pushDebugInfo( QObject::tr( "Found %1 primitives in default scene node [%2]" ).arg( mesh.primitives.size() ).arg( nodeIndex ) );
150 }
151 }
152
153 if ( !b3dmContent.rtcCenter.isNull() )
154 {
155 // transfer B3DM RTC center to GLTF CESIUM_RTC extension
156 tinygltf::Value::Object cesiumRtc;
157 cesiumRtc["center"] = tinygltf::Value( tinygltf::Value::Array {
158 tinygltf::Value( b3dmContent.rtcCenter.x() ),
159 tinygltf::Value( b3dmContent.rtcCenter.y() ),
160 tinygltf::Value( b3dmContent.rtcCenter.z() )
161 } );
162
163 model.extensions["CESIUM_RTC"] = tinygltf::Value( cesiumRtc );
164 model.extensionsRequired.emplace_back( "CESIUM_RTC" );
165 model.extensionsUsed.emplace_back( "CESIUM_RTC" );
166 }
167
168 const QString outputExtension = QFileInfo( outputPath ).suffix();
169 const bool isGlb = outputExtension.compare( "glb"_L1, Qt::CaseInsensitive ) == 0;
170 const QByteArray outputFile = QFile::encodeName( outputPath );
171 std::ofstream of( outputFile.constData(), std::ios::binary | std::ios::trunc );
172 if ( !of )
173 throw QgsProcessingException( QObject::tr( "Could not create destination file %1." ).arg( outputPath ) );
174
175 tinygltf::TinyGLTF writer;
176 if ( !writer.WriteGltfSceneToStream( &model, of, true, isGlb ) )
177 {
178 of.close();
179 throw QgsProcessingException( QObject::tr( "Could not write GLTF model to %1." ).arg( outputPath ) );
180 }
181 of.close();
182 }
183
184 QVariantMap outputs;
185 outputs.insert( u"OUTPUT"_s, outputPath );
186 return outputs;
187}
188
@ File
Parameter is a single file.
Definition qgis.h:3860
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.
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:52
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:54
bool isNull() const
Returns true if all three coordinates are zero.
Definition qgsvector3d.h:47
double x() const
Returns X coordinate.
Definition qgsvector3d.h:50
Encapsulates the contents of a B3DM file.
QByteArray gltf
GLTF binary content.
QgsVector3D rtcCenter
Optional RTC center.