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