22#define TINYGLTF_IMPLEMENTATION
23#define TINYGLTF_NO_STB_IMAGE
24#define TINYGLTF_NO_STB_IMAGE_WRITE
31QString QgsB3DMToGltfAlgorithm::name()
const
33 return QStringLiteral(
"b3dmtogltf" );
36QString QgsB3DMToGltfAlgorithm::displayName()
const
38 return QObject::tr(
"Convert B3DM to GLTF" );
41QStringList QgsB3DMToGltfAlgorithm::tags()
const
43 return QObject::tr(
"3d,tiles,cesium" ).split(
',' );
46QString QgsB3DMToGltfAlgorithm::group()
const
48 return QObject::tr(
"3D Tiles" );
51QString QgsB3DMToGltfAlgorithm::groupId()
const
53 return QStringLiteral(
"3dtiles" );
56QString QgsB3DMToGltfAlgorithm::shortHelpString()
const
58 return QObject::tr(
"This algorithm converts files from the legacy B3DM format to GLTF." );
61QString QgsB3DMToGltfAlgorithm::shortDescription()
const
63 return QObject::tr(
"Converts files from the legacy B3DM format to GLTF." );
66QgsB3DMToGltfAlgorithm *QgsB3DMToGltfAlgorithm::createInstance()
const
68 return new QgsB3DMToGltfAlgorithm();
71void QgsB3DMToGltfAlgorithm::initAlgorithm(
const QVariantMap & )
80 const QString path = parameterAsFile( parameters, QStringLiteral(
"INPUT" ), context );
81 const QString outputPath = parameterAsFile( parameters, QStringLiteral(
"OUTPUT" ), context );
83 if ( !QFile::exists( path ) )
87 QByteArray fileContent;
88 if ( f.open( QIODevice::ReadOnly ) )
90 fileContent = f.readAll();
102 tinygltf::Model model;
105 const bool res = QgsGltfUtils::loadGltfModel( b3dmContent.
gltf, model, &errors, &warnings );
106 if ( !errors.isEmpty() )
110 if ( !warnings.isEmpty() )
117 QFile outputFile( outputPath );
118 if ( !outputFile.open( QFile::WriteOnly ) )
122 outputFile.write( b3dmContent.
gltf );
126 bool sceneOk =
false;
127 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
133 feedback->
pushDebugInfo( QObject::tr(
"Found %1 scenes" ).arg( model.scenes.size() ) );
135 const tinygltf::Scene &scene = model.scenes[sceneIndex];
136 feedback->
pushDebugInfo( QObject::tr(
"Found %1 nodes in default scene [%2]" ).arg( scene.nodes.size() ).arg( sceneIndex ) );
137 if ( !scene.nodes.empty() )
139 const int nodeIndex = scene.nodes[0];
140 const tinygltf::Node &gltfNode = model.nodes[nodeIndex];
141 if ( gltfNode.mesh >= 0 )
143 const tinygltf::Mesh &mesh = model.meshes[gltfNode.mesh];
144 feedback->
pushDebugInfo( QObject::tr(
"Found %1 primitives in default scene node [%2]" ).arg( mesh.primitives.size() ).arg( nodeIndex ) );
151 tinygltf::Value::Object cesiumRtc;
152 cesiumRtc[
"center"] = tinygltf::Value( tinygltf::Value::Array {
158 model.extensions[
"CESIUM_RTC"] = tinygltf::Value( cesiumRtc );
159 model.extensionsRequired.emplace_back(
"CESIUM_RTC" );
160 model.extensionsUsed.emplace_back(
"CESIUM_RTC" );
163 const QString outputExtension = QFileInfo( outputPath ).suffix();
164 const bool isGlb = outputExtension.compare( QLatin1String(
"glb" ), Qt::CaseInsensitive ) == 0;
165 const QByteArray outputFile = QFile::encodeName( outputPath );
166 std::ofstream of( outputFile.constData(), std::ios::binary | std::ios::trunc );
170 tinygltf::TinyGLTF writer;
171 if ( !writer.WriteGltfSceneToStream( &model, of,
true, isGlb ) )
180 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.