QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#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, QStringLiteral( "b3dm" ), QVariant(), false, QStringLiteral( "B3DM (*.b3dm *.B3DM)" ) ) );
69
70 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output file" ), QStringLiteral( "GLTF (*.gltf *.GLTF);;GLB (*.glb *.GLB)" ) ) );
71}
72
73QVariantMap QgsB3DMToGltfAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
74{
75 const QString path = parameterAsFile( parameters, QStringLiteral( "INPUT" ), context );
76 const QString outputPath = parameterAsFile( parameters, QStringLiteral( "OUTPUT" ), context );
77
78 if ( !QFile::exists( path ) )
79 throw QgsProcessingException( QObject::tr( "Could not load source file %1." ).arg( path ) );
80
81 QFile f( path );
82 QByteArray fileContent;
83 if ( f.open( QIODevice::ReadOnly ) )
84 {
85 fileContent = f.readAll();
86 }
87 else
88 {
89 throw QgsProcessingException( QObject::tr( "Could not load source file %1." ).arg( path ) );
90 }
91
93
94 // load gltf and then rewrite -- this allows us to both validate the B3DM GLTF content, and
95 // also gives the opportunity to "upgrade" B3DM specific options (like CESIUM_RTC) to standard
96 // GLTF extensions
97 tinygltf::Model model;
98 QString errors;
99 QString warnings;
100 const bool res = QgsGltfUtils::loadGltfModel( b3dmContent.gltf, model, &errors, &warnings );
101 if ( !errors.isEmpty() )
102 {
103 feedback->reportError( errors );
104 }
105 if ( !warnings.isEmpty() )
106 {
107 feedback->pushWarning( warnings );
108 }
109 if ( !res )
110 {
111 // if we can't read the GLTF, then just write the original GLTF content to the output file
112 QFile outputFile( outputPath );
113 if ( !outputFile.open( QFile::WriteOnly ) )
114 {
115 throw QgsProcessingException( QObject::tr( "Could not create destination file %1." ).arg( outputPath ) );
116 }
117 outputFile.write( b3dmContent.gltf );
118 }
119 else
120 {
121 bool sceneOk = false;
122 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
123 if ( !sceneOk )
124 {
125 throw QgsProcessingException( QObject::tr( "No scenes found in model" ) );
126 }
127
128 feedback->pushDebugInfo( QObject::tr( "Found %1 scenes" ).arg( model.scenes.size() ) );
129
130 const tinygltf::Scene &scene = model.scenes[sceneIndex];
131 feedback->pushDebugInfo( QObject::tr( "Found %1 nodes in default scene [%2]" ).arg( scene.nodes.size() ).arg( sceneIndex ) );
132 if ( !scene.nodes.empty() )
133 {
134 const int nodeIndex = scene.nodes[0];
135 const tinygltf::Node &gltfNode = model.nodes[nodeIndex];
136 if ( gltfNode.mesh >= 0 )
137 {
138 const tinygltf::Mesh &mesh = model.meshes[gltfNode.mesh];
139 feedback->pushDebugInfo( QObject::tr( "Found %1 primitives in default scene node [%2]" ).arg( mesh.primitives.size() ).arg( nodeIndex ) );
140 }
141 }
142
143 if ( !b3dmContent.rtcCenter.isNull() )
144 {
145 // transfer B3DM RTC center to GLTF CESIUM_RTC extension
146 tinygltf::Value::Object cesiumRtc;
147 cesiumRtc["center"] = tinygltf::Value( tinygltf::Value::Array {
148 tinygltf::Value( b3dmContent.rtcCenter.x() ),
149 tinygltf::Value( b3dmContent.rtcCenter.y() ),
150 tinygltf::Value( b3dmContent.rtcCenter.z() )
151 } );
152
153 model.extensions["CESIUM_RTC"] = tinygltf::Value( cesiumRtc );
154 model.extensionsRequired.emplace_back( "CESIUM_RTC" );
155 model.extensionsUsed.emplace_back( "CESIUM_RTC" );
156 }
157
158 const QString outputExtension = QFileInfo( outputPath ).suffix();
159 const bool isGlb = outputExtension.compare( QLatin1String( "glb" ), Qt::CaseInsensitive ) == 0;
160 const QByteArray outputFile = QFile::encodeName( outputPath );
161 std::ofstream of( outputFile.constData(), std::ios::binary | std::ios::trunc );
162 if ( !of )
163 throw QgsProcessingException( QObject::tr( "Could not create destination file %1." ).arg( outputPath ) );
164
165 tinygltf::TinyGLTF writer;
166 if ( !writer.WriteGltfSceneToStream( &model, of, true, isGlb ) )
167 {
168 of.close();
169 throw QgsProcessingException( QObject::tr( "Could not write GLTF model to %1." ).arg( outputPath ) );
170 }
171 of.close();
172 }
173
174 QVariantMap outputs;
175 outputs.insert( QStringLiteral( "OUTPUT" ), outputPath );
176 return outputs;
177}
178
@ 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.
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.