21#include <nlohmann/json.hpp>
31#include <QtCore/QBuffer>
33using namespace Qt::StringLiterals;
42 const double west = region[0].get<
double>() * 180 / M_PI;
43 const double south = region[1].get<
double>() * 180 / M_PI;
44 const double east = region[2].get<
double>() * 180 / M_PI;
45 const double north = region[3].get<
double>() * 180 / M_PI;
46 double minHeight = region[4].get<
double>();
47 double maxHeight = region[5].get<
double>();
49 return QgsBox3D( west, south, minHeight, east, north, maxHeight );
51 catch ( nlohmann::json::exception & )
59 if ( region.size() != 6 )
67 if ( box.size() != 12 )
73 for (
int i = 0; i < 3; ++i )
75 res.mCenter[i] = box[i].get<
double>();
77 for (
int i = 0; i < 9; ++i )
79 res.mHalfAxes[i] = box[i + 3].get<
double>();
83 catch ( nlohmann::json::exception & )
91 if ( box.size() != 12 )
99 if ( sphere.size() != 4 )
104 const double centerX = sphere[0].get<
double>();
105 const double centerY = sphere[1].get<
double>();
106 const double centerZ = sphere[2].get<
double>();
107 const double radius = sphere[3].get<
double>();
108 return QgsSphere( centerX, centerY, centerZ, radius );
110 catch ( nlohmann::json::exception & )
118 if ( sphere.size() != 4 )
131 const double uniformScale = std::max(
146 return QgsSphere( center.
x(), center.
y(), center.
z(), sphere.
radius() * uniformScale );
155 unsigned char magic[4];
158 quint32 featureTableJsonByteLength;
159 quint32 featureTableBinaryByteLength;
160 quint32 batchTableJsonByteLength;
161 quint32 batchTableBinaryByteLength;
165 if ( tileContent.size() <
static_cast<int>(
sizeof( b3dmHeader ) ) )
169 memcpy( &hdr, tileContent.constData(),
sizeof( b3dmHeader ) );
171 const QString featureTableJson( tileContent.mid(
sizeof( b3dmHeader ), hdr.featureTableJsonByteLength ) );
172 if ( !featureTableJson.isEmpty() )
176 const json featureTable = json::parse( featureTableJson.toStdString() );
177 if ( featureTable.contains(
"RTC_CENTER" ) )
179 const auto &rtcCenterJson = featureTable[
"RTC_CENTER" ];
180 if ( rtcCenterJson.is_array() && rtcCenterJson.size() == 3 )
192 catch ( json::parse_error &ex )
194 QgsDebugError( u
"Error parsing feature table JSON: %1"_s.arg( ex.what() ) );
198 res.
gltf = tileContent.mid(
sizeof( b3dmHeader ) +
199 hdr.featureTableJsonByteLength + hdr.featureTableBinaryByteLength +
200 hdr.batchTableJsonByteLength + hdr.batchTableBinaryByteLength );
207 if ( tileContent.startsWith( QByteArray(
"b3dm" ) ) )
214 else if ( tileContent.startsWith( QByteArray(
"glTF" ) ) )
216 res.
gltf = tileContent;
A 3-dimensional box composed of x, y, z coordinates.
static QgsSphere parseSphere(const json &sphere)
Parses a sphere object from a Cesium JSON document.
static B3DMContents extractGltfFromB3dm(const QByteArray &tileContent)
Extracts GLTF binary data and other contents from the legacy b3dm (Batched 3D Model) tile format.
static QgsOrientedBox3D parseBox(const json &box)
Parses a box object from a Cesium JSON document to an oriented bounding box.
static QgsBox3D parseRegion(const json ®ion)
Parses a region object from a Cesium JSON object to a 3D box.
static QgsSphere transformSphere(const QgsSphere &sphere, const QgsMatrix4x4 &transform)
Applies a transform to a sphere.
static TileContents extractGltfFromTileContent(const QByteArray &tileContent)
Parses tile content.
static json jsonFromVariant(const QVariant &v)
Converts a QVariant v to a json object.
A simple 4x4 matrix implementation useful for transformation in 3D space.
bool isIdentity() const
Returns whether this matrix is an identity matrix.
QgsVector3D map(const QgsVector3D &vector) const
Matrix-vector multiplication (vector is converted to homogeneous coordinates [X,Y,...
const double * constData() const
Returns pointer to the matrix data (stored in column-major order).
Represents a oriented (rotated) box in 3 dimensions.
A spherical geometry object.
QgsVector3D centerVector() const
Returns the vector to the center of the sphere.
double radius() const
Returns the radius of the sphere.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
double y() const
Returns Y coordinate.
double z() const
Returns Z coordinate.
void setZ(double z)
Sets Z coordinate.
double x() const
Returns X coordinate.
void setX(double x)
Sets X coordinate.
void setY(double y)
Sets Y coordinate.
#define QgsDebugError(str)
Encapsulates the contents of a B3DM file.
QByteArray gltf
GLTF binary content.
QgsVector3D rtcCenter
Optional RTC center.
Encapsulates the contents of a 3D tile.
QgsVector3D rtcCenter
Center position of relative-to-center coordinates (when used).
QByteArray gltf
GLTF binary content.