26#include <QRegularExpression>
28#define TINYGLTF_IMPLEMENTATION
33#define TINYGLTF_ENABLE_DRACO
36#define TINYGLTF_NO_STB_IMAGE
37#define TINYGLTF_NO_STB_IMAGE_WRITE
46bool QgsGltfUtils::accessorToMapCoordinates(
const tinygltf::Model &model,
int accessorIndex,
const QgsMatrix4x4 &tileTransform,
const QgsCoordinateTransform *ecefToTargetCrs,
const QgsVector3D &tileTranslationEcef,
const QMatrix4x4 *nodeTransform,
Qgis::Axis gltfUpAxis, QVector<double> &vx, QVector<double> &vy, QVector<double> &vz )
48 const tinygltf::Accessor &accessor = model.accessors[accessorIndex];
49 const tinygltf::BufferView &bv = model.bufferViews[accessor.bufferView];
50 const tinygltf::Buffer &b = model.buffers[bv.buffer];
52 if ( accessor.componentType != TINYGLTF_PARAMETER_TYPE_FLOAT || accessor.type != TINYGLTF_TYPE_VEC3 )
58 const unsigned char *ptr = b.data.data() + bv.byteOffset + accessor.byteOffset;
60 vx.resize( accessor.count );
61 vy.resize( accessor.count );
62 vz.resize( accessor.count );
63 double *vxOut = vx.data();
64 double *vyOut = vy.data();
65 double *vzOut = vz.data();
66 for (
int i = 0; i < static_cast<int>( accessor.count ); ++i )
68 const float *fptr =
reinterpret_cast<const float *
>( ptr );
69 QVector3D vOrig( fptr[0], fptr[1], fptr[2] );
72 vOrig = nodeTransform->map( vOrig );
79 QgsDebugError( QStringLiteral(
"X up translation not yet supported" ) );
80 v = tileTransform.
map( tileTranslationEcef );
87 QVector3D vFlip( vOrig.x(), -vOrig.z(), vOrig.y() );
88 v = tileTransform.
map(
QgsVector3D( vFlip ) + tileTranslationEcef );
94 v = tileTransform.
map(
QgsVector3D( vOrig ) + tileTranslationEcef );
104 ptr += bv.byteStride;
106 ptr += 3 *
sizeof( float );
109 if ( ecefToTargetCrs )
113 ecefToTargetCrs->
transformCoords( accessor.count, vx.data(), vy.data(), vz.data() );
124bool QgsGltfUtils::extractTextureCoordinates(
const tinygltf::Model &model,
int accessorIndex, QVector<float> &x, QVector<float> &y )
126 const tinygltf::Accessor &accessor = model.accessors[accessorIndex];
127 const tinygltf::BufferView &bv = model.bufferViews[accessor.bufferView];
128 const tinygltf::Buffer &b = model.buffers[bv.buffer];
130 if ( accessor.componentType != TINYGLTF_PARAMETER_TYPE_FLOAT || accessor.type != TINYGLTF_TYPE_VEC2 )
135 const unsigned char *ptr = b.data.data() + bv.byteOffset + accessor.byteOffset;
136 x.resize( accessor.count );
137 y.resize( accessor.count );
139 float *xOut = x.data();
140 float *yOut = y.data();
142 for ( std::size_t i = 0; i < accessor.count; i++ )
144 const float *fptr =
reinterpret_cast< const float *
>( ptr );
150 ptr += bv.byteStride;
152 ptr += 2 *
sizeof( float );
157QgsGltfUtils::ResourceType QgsGltfUtils::imageResourceType(
const tinygltf::Model &model,
int index )
159 const tinygltf::Image &img = model.images[index];
161 if ( !img.image.empty() )
163 return ResourceType::Embedded;
167 return ResourceType::Linked;
171QImage QgsGltfUtils::extractEmbeddedImage(
const tinygltf::Model &model,
int index )
173 const tinygltf::Image &img = model.images[index];
174 if ( !img.image.empty() )
175 return QImage( img.image.data(), img.width, img.height, QImage::Format_ARGB32 );
180QString QgsGltfUtils::linkedImagePath(
const tinygltf::Model &model,
int index )
182 const tinygltf::Image &img = model.images[index];
183 return QString::fromStdString( img.uri );
186std::unique_ptr<QMatrix4x4> QgsGltfUtils::parseNodeTransform(
const tinygltf::Node &node )
190 std::unique_ptr<QMatrix4x4> matrix;
191 if ( !node.matrix.empty() )
193 matrix.reset(
new QMatrix4x4 );
194 float *mdata = matrix->data();
195 for (
int i = 0; i < 16; ++i )
196 mdata[i] =
static_cast< float >( node.matrix[i] );
198 else if ( node.translation.size() || node.rotation.size() || node.scale.size() )
200 matrix.reset(
new QMatrix4x4 );
201 if ( node.scale.size() )
203 matrix->scale(
static_cast< float >( node.scale[0] ),
static_cast< float >( node.scale[1] ),
static_cast< float >( node.scale[2] ) );
205 if ( node.rotation.size() )
207 matrix->rotate( QQuaternion(
static_cast< float >( node.rotation[3] ),
static_cast< float >( node.rotation[0] ),
static_cast< float >( node.rotation[1] ),
static_cast< float >( node.rotation[2] ) ) );
209 if ( node.translation.size() )
211 matrix->translate(
static_cast< float >( node.translation[0] ),
static_cast< float >( node.translation[1] ),
static_cast< float >( node.translation[2] ) );
220 const tinygltf::Scene &scene = model.scenes[model.defaultScene];
223 auto it = model.extensions.find(
"CESIUM_RTC" );
224 if ( it != model.extensions.end() )
226 const tinygltf::Value v = it->second;
227 if ( v.IsObject() && v.Has(
"center" ) )
229 const tinygltf::Value center = v.Get(
"center" );
230 if ( center.IsArray() && center.Size() == 3 )
232 tileTranslationEcef =
QgsVector3D( center.Get( 0 ).GetNumberAsDouble(), center.Get( 1 ).GetNumberAsDouble(), center.Get( 2 ).GetNumberAsDouble() );
237 if ( scene.nodes.size() == 0 )
240 int rootNodeIndex = scene.nodes[0];
241 tinygltf::Node &rootNode = model.nodes[rootNodeIndex];
243 if ( tileTranslationEcef.
isNull() && rootNode.translation.size() )
245 QgsVector3D rootTranslation( rootNode.translation[0], rootNode.translation[1], rootNode.translation[2] );
249 if ( rootTranslation.length() > 1e6 )
254 QgsDebugError( QStringLiteral(
"X up translation not yet supported" ) );
259 tileTranslationEcef =
QgsVector3D( rootTranslation.x(), -rootTranslation.z(), rootTranslation.y() );
260 rootNode.translation[0] = rootNode.translation[1] = rootNode.translation[2] = 0;
265 tileTranslationEcef =
QgsVector3D( rootTranslation.x(), rootTranslation.y(), rootTranslation.z() );
266 rootNode.translation[0] = rootNode.translation[1] = rootNode.translation[2] = 0;
273 return tileTranslationEcef;
277bool QgsGltfUtils::loadImageDataWithQImage(
278 tinygltf::Image *image,
const int image_idx, std::string *err,
279 std::string *warn,
int req_width,
int req_height,
280 const unsigned char *bytes,
int size,
void *user_data )
283 if ( req_width != 0 || req_height != 0 )
287 ( *err ) +=
"Expecting zero req_width/req_height.\n";
296 if ( !img.loadFromData( bytes, size ) )
301 "Unknown image format. QImage cannot decode image data for image[" +
302 std::to_string( image_idx ) +
"] name = \"" + image->name +
"\".\n";
307 if ( img.format() != QImage::Format_RGB32 && img.format() != QImage::Format_ARGB32 )
310 img.convertTo( QImage::Format_RGB32 );
313 image->width = img.width();
314 image->height = img.height();
315 image->component = 4;
317 image->pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE;
319 image->image.resize(
static_cast<size_t>( image->width * image->height * image->component ) *
size_t( image->bits / 8 ) );
320 std::copy( img.constBits(), img.constBits() +
static_cast< std::size_t
>( image->width ) * image->height * image->component * ( image->bits / 8 ), image->image.begin() );
325bool QgsGltfUtils::loadGltfModel(
const QByteArray &data, tinygltf::Model &model, QString *errors, QString *warnings )
327 tinygltf::TinyGLTF loader;
329 loader.SetImageLoader( QgsGltfUtils::loadImageDataWithQImage,
nullptr );
335 loader.SetParseStrictness( tinygltf::ParseStrictness::Permissive );
338 std::string err, warn;
341 if ( data.startsWith(
"glTF" ) )
343 if ( data.at( 4 ) == 1 )
345 *errors = QObject::tr(
"GLTF version 1 tiles cannot be loaded" );
348 res = loader.LoadBinaryFromMemory( &model, &err, &warn,
349 (
const unsigned char * )data.constData(), data.size(), baseDir );
353 res = loader.LoadASCIIFromString( &model, &err, &warn,
354 data.constData(), data.size(), baseDir );
358 *errors = QString::fromStdString( err );
361 *warnings = QString::fromStdString( warn );
364 const thread_local QRegularExpression rxFailedToLoadExternalUriForImage( QStringLiteral(
"Failed to load external 'uri' for image\\[\\d+\\] name = \".*?\"\\n?" ) );
365 warnings->replace( rxFailedToLoadExternalUriForImage, QString() );
366 const thread_local QRegularExpression rxFileNotFound( QStringLiteral(
"File not found : .*?\\n" ) );
367 warnings->replace( rxFileNotFound, QString() );
Custom exception class for Coordinate Reference System related exceptions.
A simple 4x4 matrix implementation useful for transformation in 3D space.
QgsVector3D map(const QgsVector3D &vector) const
Matrix-vector multiplication (vector is converted to homogenous coordinates [X,Y,Z,...
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
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.
#define QgsDebugError(str)