23#define TINYGLTF_IMPLEMENTATION
24#define TINYGLTF_NO_STB_IMAGE
25#define TINYGLTF_NO_STB_IMAGE_WRITE
32QString QgsB3DMToGltfAlgorithm::name()
const
34 return QStringLiteral(
"b3dmtogltf" );
37QString QgsB3DMToGltfAlgorithm::displayName()
const
39 return QObject::tr(
"Convert B3DM to GLTF" );
42QStringList QgsB3DMToGltfAlgorithm::tags()
const
44 return QObject::tr(
"3d,tiles,cesium" ).split(
',' );
47QString QgsB3DMToGltfAlgorithm::group()
const
49 return QObject::tr(
"3D Tiles" );
52QString QgsB3DMToGltfAlgorithm::groupId()
const
54 return QStringLiteral(
"3dtiles" );
57QString QgsB3DMToGltfAlgorithm::shortHelpString()
const
59 return QObject::tr(
"This algorithm converts files from the legacy B3DM format to GLTF." );
62QString QgsB3DMToGltfAlgorithm::shortDescription()
const
64 return QObject::tr(
"Converts files from the legacy B3DM format to GLTF." );
67QgsB3DMToGltfAlgorithm *QgsB3DMToGltfAlgorithm::createInstance()
const
69 return new QgsB3DMToGltfAlgorithm();
72void QgsB3DMToGltfAlgorithm::initAlgorithm(
const QVariantMap & )
81 const QString path = parameterAsFile( parameters, QStringLiteral(
"INPUT" ), context );
82 const QString outputPath = parameterAsFile( parameters, QStringLiteral(
"OUTPUT" ), context );
84 if ( !QFile::exists( path ) )
88 QByteArray fileContent;
89 if ( f.open( QIODevice::ReadOnly ) )
91 fileContent = f.readAll();
103 tinygltf::Model model;
106 const bool res = QgsGltfUtils::loadGltfModel( b3dmContent.
gltf, model, &errors, &warnings );
107 if ( !errors.isEmpty() )
111 if ( !warnings.isEmpty() )
118 QFile outputFile( outputPath );
119 if ( !outputFile.open( QFile::WriteOnly ) )
123 outputFile.write( b3dmContent.
gltf );
127 bool sceneOk =
false;
128 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
134 feedback->
pushDebugInfo( QObject::tr(
"Found %1 scenes" ).arg( model.scenes.size() ) );
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() )
140 const int nodeIndex = scene.nodes[0];
141 const tinygltf::Node &gltfNode = model.nodes[nodeIndex];
142 if ( gltfNode.mesh >= 0 )
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 ) );
152 tinygltf::Value::Object cesiumRtc;
153 cesiumRtc[
"center"] = tinygltf::Value( tinygltf::Value::Array {
159 model.extensions[
"CESIUM_RTC"] = tinygltf::Value( cesiumRtc );
160 model.extensionsRequired.emplace_back(
"CESIUM_RTC" );
161 model.extensionsUsed.emplace_back(
"CESIUM_RTC" );
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 );
171 tinygltf::TinyGLTF writer;
172 if ( !writer.WriteGltfSceneToStream( &model, of,
true, isGlb ) )
181 outputs.insert( QStringLiteral(
"OUTPUT" ), outputPath );
@ 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.
double z() const
Returns Z coordinate.
bool isNull() const
Returns true if all three coordinates are zero.
double x() const
Returns X coordinate.
Encapsulates the contents of a B3DM file.
QByteArray gltf
GLTF binary content.
QgsVector3D rtcCenter
Optional RTC center.