Move OctreeUtils to OctreeDataUtils

This commit is contained in:
Ryan Huffman 2018-02-26 16:51:42 -08:00
parent 3fa538d42d
commit ec210e1750
9 changed files with 189 additions and 161 deletions

View file

@ -33,7 +33,7 @@
#include <PathUtils.h>
#include <QtCore/QDir>
#include <OctreeUtils.h>
#include <OctreeDataUtils.h>
Q_LOGGING_CATEGORY(octree_server, "hifi.octree-server")

View file

@ -53,7 +53,7 @@
#include <Gzip.h>
#include <OctreeUtils.h>
#include <OctreeDataUtils.h>
Q_LOGGING_CATEGORY(domain_server, "hifi.domain_server")

View file

@ -16,7 +16,7 @@
#include <quazip5/quazip.h>
#include <quazip5/quazipfile.h>
#include <OctreeUtils.h>
#include <OctreeDataUtils.h>
EntitiesBackupHandler::EntitiesBackupHandler(QString entitiesFilePath, QString entitiesReplacementFilePath) :
_entitiesFilePath(entitiesFilePath),

View file

@ -115,8 +115,6 @@ public:
virtual void update() override { update(true); }
std::unique_ptr<OctreeUtils::RawOctreeData> getRawOctreeData();
void update(bool simulate);
// The newer API...

View file

@ -0,0 +1,128 @@
//
// OctreeDataUtils.cpp
// libraries/octree/src
//
// Created by Ryan Huffman 2018-02-26
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
#include "OctreeDataUtils.h"
#include <Gzip.h>
#include <udt/PacketHeaders.h>
#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
// Reads octree file and parses it into a QJsonDocument. Handles both gzipped and non-gzipped files.
// Returns true if the file was successfully opened and parsed, otherwise false.
// Example failures: file does not exist, gzipped file cannot be unzipped, invalid JSON.
bool readOctreeFile(QString path, QJsonDocument* doc) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Cannot open json file for reading: " << path;
return false;
}
QByteArray data = file.readAll();
QByteArray jsonData;
if (!gunzip(data, jsonData)) {
jsonData = data;
}
*doc = QJsonDocument::fromJson(jsonData);
return !doc->isNull();
}
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromJSON(QJsonObject root) {
if (root.contains("Id") && root.contains("DataVersion")) {
id = root["Id"].toVariant().toUuid();
version = root["DataVersion"].toInt();
}
readSubclassData(root);
return true;
}
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromData(QByteArray data) {
QByteArray jsonData;
if (gunzip(data, jsonData)) {
data = jsonData;
}
auto doc = QJsonDocument::fromJson(data);
if (doc.isNull()) {
return false;
}
auto root = doc.object();
return readOctreeDataInfoFromJSON(root);
}
// Reads octree file and parses it into a RawOctreeData object.
// Returns false if readOctreeFile fails.
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromFile(QString path) {
QJsonDocument doc;
if (!readOctreeFile(path, &doc)) {
return false;
}
auto root = doc.object();
return readOctreeDataInfoFromJSON(root);
}
QByteArray OctreeUtils::RawOctreeData::toByteArray() {
const auto protocolVersion = (int)versionForPacketType((PacketTypeEnum::Value)dataPacketType());
QJsonObject obj {
{ "DataVersion", QJsonValue((qint64)version) },
{ "Id", QJsonValue(id.toString()) },
{ "Version", protocolVersion },
};
writeSubclassData(obj);
QJsonDocument doc;
doc.setObject(obj);
return doc.toJson();
}
QByteArray OctreeUtils::RawOctreeData::toGzippedByteArray() {
auto data = toByteArray();
QByteArray gzData;
if (!gzip(data, gzData, -1)) {
qCritical("Unable to gzip data while converting json.");
return QByteArray();
}
return gzData;
}
PacketType OctreeUtils::RawOctreeData::dataPacketType() const {
Q_ASSERT(false);
qCritical() << "Attemping to read packet type for incomplete base type 'RawOctreeData'";
return (PacketType)0;
}
void OctreeUtils::RawOctreeData::resetIdAndVersion() {
id = QUuid::createUuid();
version = OctreeUtils::INITIAL_VERSION;
qDebug() << "Reset octree data to: " << id << version;
}
void OctreeUtils::RawEntityData::readSubclassData(QJsonObject root) {
if (root.contains("Entities")) {
entityData = root["Entities"].toArray();
}
}
void OctreeUtils::RawEntityData::writeSubclassData(QJsonObject root) const {
root["Entities"] = entityData;
}
PacketType OctreeUtils::RawEntityData::dataPacketType() const { return PacketType::EntityData; }

View file

@ -0,0 +1,57 @@
//
// OctreeDataUtils.h
// libraries/octree/src
//
// Created by Ryan Huffman 2018-02-26
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
#ifndef hifi_OctreeDataUtils_h
#define hifi_OctreeDataUtils_h
#include <udt/PacketHeaders.h>
#include <QJsonObject>
#include <QUuid>
#include <QJsonArray>
namespace OctreeUtils {
using Version = int64_t;
constexpr Version INITIAL_VERSION = 0;
//using PacketType = uint8_t;
// RawOctreeData is an intermediate format between JSON and a fully deserialized Octree.
class RawOctreeData {
public:
QUuid id { QUuid() };
Version version { -1 };
virtual PacketType dataPacketType() const;
virtual void readSubclassData(QJsonObject root) { }
virtual void writeSubclassData(QJsonObject root) const { }
void resetIdAndVersion();
QByteArray toByteArray();
QByteArray toGzippedByteArray();
bool readOctreeDataInfoFromData(QByteArray data);
bool readOctreeDataInfoFromFile(QString path);
bool readOctreeDataInfoFromJSON(QJsonObject root);
};
class RawEntityData : public RawOctreeData {
PacketType dataPacketType() const override;
void readSubclassData(QJsonObject root) override;
void writeSubclassData(QJsonObject root) const override;
QJsonArray entityData;
};
}
#endif // hifi_OctreeDataUtils_h

View file

@ -32,6 +32,7 @@
#include "OctreeLogging.h"
#include "OctreePersistThread.h"
#include "OctreeUtils.h"
#include "OctreeDataUtils.h"
const int OctreePersistThread::DEFAULT_PERSIST_INTERVAL = 1000 * 30; // every 30 seconds

View file

@ -16,12 +16,6 @@
#include <glm/glm.hpp>
#include <AABox.h>
#include <Gzip.h>
#include <udt/PacketHeaders.h>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
float calculateRenderAccuracy(const glm::vec3& position,
const AABox& bounds,
@ -79,111 +73,4 @@ float getOrthographicAccuracySize(float octreeSizeScale, int boundaryLevelAdjust
// Smallest visible element is 1cm
const float smallestSize = 0.01f;
return (smallestSize * MAX_VISIBILITY_DISTANCE_FOR_UNIT_ELEMENT) / boundaryDistanceForRenderLevel(boundaryLevelAdjust, octreeSizeScale);
}
// Reads octree file and parses it into a QJsonDocument. Handles both gzipped and non-gzipped files.
// Returns true if the file was successfully opened and parsed, otherwise false.
// Example failures: file does not exist, gzipped file cannot be unzipped, invalid JSON.
bool readOctreeFile(QString path, QJsonDocument* doc) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Cannot open json file for reading: " << path;
return false;
}
QByteArray data = file.readAll();
QByteArray jsonData;
if (!gunzip(data, jsonData)) {
jsonData = data;
}
*doc = QJsonDocument::fromJson(jsonData);
return !doc->isNull();
}
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromJSON(QJsonObject root) {
if (root.contains("Id") && root.contains("DataVersion")) {
id = root["Id"].toVariant().toUuid();
version = root["DataVersion"].toInt();
}
readSubclassData(root);
return true;
}
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromData(QByteArray data) {
QByteArray jsonData;
if (gunzip(data, jsonData)) {
data = jsonData;
}
auto doc = QJsonDocument::fromJson(data);
if (doc.isNull()) {
return false;
}
auto root = doc.object();
return readOctreeDataInfoFromJSON(root);
}
// Reads octree file and parses it into a RawOctreeData object.
// Returns false if readOctreeFile fails.
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromFile(QString path) {
QJsonDocument doc;
if (!readOctreeFile(path, &doc)) {
return false;
}
auto root = doc.object();
return readOctreeDataInfoFromJSON(root);
}
QByteArray OctreeUtils::RawOctreeData::toByteArray() {
const auto protocolVersion = (int)versionForPacketType(dataPacketType());
QJsonObject obj {
{ "DataVersion", QJsonValue((qint64)version) },
{ "Id", QJsonValue(id.toString()) },
{ "Version", protocolVersion },
};
writeSubclassData(obj);
QJsonDocument doc;
doc.setObject(obj);
return doc.toJson();
}
QByteArray OctreeUtils::RawOctreeData::toGzippedByteArray() {
auto data = toByteArray();
QByteArray gzData;
if (!gzip(data, gzData, -1)) {
qCritical("Unable to gzip data while converting json.");
return QByteArray();
}
return gzData;
}
PacketType OctreeUtils::RawOctreeData::dataPacketType() const {
Q_ASSERT(false);
qCritical() << "Attemping to read packet type for incomplete base type 'RawOctreeData'";
return (PacketType)0;
}
void OctreeUtils::RawOctreeData::resetIdAndVersion() {
id = QUuid::createUuid();
version = OctreeUtils::INITIAL_VERSION;
qDebug() << "Reset octree data to: " << id << version;
}
void OctreeUtils::RawEntityData::readSubclassData(QJsonObject root) {
if (root.contains("Entities")) {
entityData = root["Entities"].toArray();
}
}
void OctreeUtils::RawEntityData::writeSubclassData(QJsonObject root) const {
root["Entities"] = entityData;
}

View file

@ -14,52 +14,9 @@
#include "OctreeConstants.h"
#include <QUuid>
#include <QJsonArray>
#include <udt/PacketHeaders.h>
#include <QJsonObject>
class AABox;
class QJsonDocument;
namespace OctreeUtils {
using Version = int64_t;
constexpr Version INITIAL_VERSION = 0;
// RawOctreeData is an intermediate format between JSON and a fully deserialized Octree.
class RawOctreeData {
public:
QUuid id { QUuid() };
Version version { -1 };
virtual PacketType dataPacketType() const;
virtual void readSubclassData(QJsonObject root) { }
virtual void writeSubclassData(QJsonObject root) const { }
void resetIdAndVersion();
QByteArray toByteArray();
QByteArray toGzippedByteArray();
bool readOctreeDataInfoFromData(QByteArray data);
bool readOctreeDataInfoFromFile(QString path);
bool readOctreeDataInfoFromJSON(QJsonObject root);
};
class RawEntityData : public RawOctreeData {
PacketType dataPacketType() const override { return PacketType::EntityData; }
void readSubclassData(QJsonObject root) override;
void writeSubclassData(QJsonObject root) const override;
QJsonArray entityData;
};
}
/// renderAccuracy represents a floating point "visibility" of an object based on it's view from the camera. At a simple
/// level it returns 0.0f for things that are so small for the current settings that they could not be visible.
float calculateRenderAccuracy(const glm::vec3& position,