25using namespace Qt::StringLiterals;
27#define TINYGLTF_IMPLEMENTATION
28#define TINYGLTF_NO_STB_IMAGE
29#define TINYGLTF_NO_STB_IMAGE_WRITE
36QString QgsB3DMToGltfAlgorithm::name()
const
38 return u
"b3dmtogltf"_s;
41QString QgsB3DMToGltfAlgorithm::displayName()
const
43 return QObject::tr(
"Convert B3DM to GLTF" );
46QStringList QgsB3DMToGltfAlgorithm::tags()
const
48 return QObject::tr(
"3d,tiles,cesium" ).split(
',' );
51QString QgsB3DMToGltfAlgorithm::group()
const
53 return QObject::tr(
"3D Tiles" );
56QString QgsB3DMToGltfAlgorithm::groupId()
const
61QString QgsB3DMToGltfAlgorithm::shortHelpString()
const
63 return QObject::tr(
"This algorithm converts files from the legacy B3DM format to GLTF." );
66QString QgsB3DMToGltfAlgorithm::shortDescription()
const
68 return QObject::tr(
"Converts files from the legacy B3DM format to GLTF." );
71QgsB3DMToGltfAlgorithm *QgsB3DMToGltfAlgorithm::createInstance()
const
73 return new QgsB3DMToGltfAlgorithm();
76void QgsB3DMToGltfAlgorithm::initAlgorithm(
const QVariantMap & )
85 QGS_MARK_ALGORITHM_SOURCE
87 const QString path = parameterAsFile( parameters, u
"INPUT"_s, context );
88 const QString outputPath = parameterAsFile( parameters, u
"OUTPUT"_s, context );
90 if ( !QFile::exists( path ) )
94 QByteArray fileContent;
95 if ( f.open( QIODevice::ReadOnly ) )
97 fileContent = f.readAll();
109 tinygltf::Model model;
112 const bool res = QgsGltfUtils::loadGltfModel( b3dmContent.
gltf, model, &errors, &warnings );
113 if ( !errors.isEmpty() )
117 if ( !warnings.isEmpty() )
124 QFile outputFile( outputPath );
125 if ( !outputFile.open( QFile::WriteOnly ) )
129 outputFile.write( b3dmContent.
gltf );
133 bool sceneOk =
false;
134 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
140 feedback->
pushDebugInfo( QObject::tr(
"Found %1 scenes" ).arg( model.scenes.size() ) );
142 const tinygltf::Scene &scene = model.scenes[sceneIndex];
143 feedback->
pushDebugInfo( QObject::tr(
"Found %1 nodes in default scene [%2]" ).arg( scene.nodes.size() ).arg( sceneIndex ) );
144 if ( !scene.nodes.empty() )
146 const int nodeIndex = scene.nodes[0];
147 const tinygltf::Node &gltfNode = model.nodes[nodeIndex];
148 if ( gltfNode.mesh >= 0 )
150 const tinygltf::Mesh &mesh = model.meshes[gltfNode.mesh];
151 feedback->
pushDebugInfo( QObject::tr(
"Found %1 primitives in default scene node [%2]" ).arg( mesh.primitives.size() ).arg( nodeIndex ) );
158 tinygltf::Value::Object cesiumRtc;
159 cesiumRtc[
"center"] = tinygltf::Value(
160 tinygltf::Value::Array { tinygltf::Value( b3dmContent.
rtcCenter.
x() ), tinygltf::Value( b3dmContent.
rtcCenter.
y() ), tinygltf::Value( b3dmContent.
rtcCenter.
z() ) }
163 model.extensions[
"CESIUM_RTC"] = tinygltf::Value( cesiumRtc );
164 model.extensionsRequired.emplace_back(
"CESIUM_RTC" );
165 model.extensionsUsed.emplace_back(
"CESIUM_RTC" );
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 );
175 tinygltf::TinyGLTF writer;
176 if ( !writer.WriteGltfSceneToStream( &model, of,
true, isGlb ) )
185 outputs.insert( u
"OUTPUT"_s, 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.