From ec210e17504b188404dc65e65c685cf13b7415ab Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 26 Feb 2018 16:51:42 -0800 Subject: [PATCH] Move OctreeUtils to OctreeDataUtils --- assignment-client/src/octree/OctreeServer.cpp | 2 +- domain-server/src/DomainServer.cpp | 2 +- domain-server/src/EntitiesBackupHandler.cpp | 2 +- libraries/entities/src/EntityTree.h | 2 - libraries/octree/src/OctreeDataUtils.cpp | 128 ++++++++++++++++++ libraries/octree/src/OctreeDataUtils.h | 57 ++++++++ libraries/octree/src/OctreePersistThread.cpp | 1 + libraries/octree/src/OctreeUtils.cpp | 113 ---------------- libraries/octree/src/OctreeUtils.h | 43 ------ 9 files changed, 189 insertions(+), 161 deletions(-) create mode 100644 libraries/octree/src/OctreeDataUtils.cpp create mode 100644 libraries/octree/src/OctreeDataUtils.h diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index f11e96963b..0ca2bb9763 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -33,7 +33,7 @@ #include #include -#include +#include Q_LOGGING_CATEGORY(octree_server, "hifi.octree-server") diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 7ec86c8b3a..fef96b9700 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -53,7 +53,7 @@ #include -#include +#include Q_LOGGING_CATEGORY(domain_server, "hifi.domain_server") diff --git a/domain-server/src/EntitiesBackupHandler.cpp b/domain-server/src/EntitiesBackupHandler.cpp index 9b4ff786a2..599a730107 100644 --- a/domain-server/src/EntitiesBackupHandler.cpp +++ b/domain-server/src/EntitiesBackupHandler.cpp @@ -16,7 +16,7 @@ #include #include -#include +#include EntitiesBackupHandler::EntitiesBackupHandler(QString entitiesFilePath, QString entitiesReplacementFilePath) : _entitiesFilePath(entitiesFilePath), diff --git a/libraries/entities/src/EntityTree.h b/libraries/entities/src/EntityTree.h index 672cfbc931..cb3d6d57e4 100644 --- a/libraries/entities/src/EntityTree.h +++ b/libraries/entities/src/EntityTree.h @@ -115,8 +115,6 @@ public: virtual void update() override { update(true); } - std::unique_ptr getRawOctreeData(); - void update(bool simulate); // The newer API... diff --git a/libraries/octree/src/OctreeDataUtils.cpp b/libraries/octree/src/OctreeDataUtils.cpp new file mode 100644 index 0000000000..9c965a128b --- /dev/null +++ b/libraries/octree/src/OctreeDataUtils.cpp @@ -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 +#include + +#include +#include +#include +#include + +// 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; } \ No newline at end of file diff --git a/libraries/octree/src/OctreeDataUtils.h b/libraries/octree/src/OctreeDataUtils.h new file mode 100644 index 0000000000..ba70f3d079 --- /dev/null +++ b/libraries/octree/src/OctreeDataUtils.h @@ -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 + +#include +#include +#include + +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 \ No newline at end of file diff --git a/libraries/octree/src/OctreePersistThread.cpp b/libraries/octree/src/OctreePersistThread.cpp index d35bc6c869..23d6b6c2aa 100644 --- a/libraries/octree/src/OctreePersistThread.cpp +++ b/libraries/octree/src/OctreePersistThread.cpp @@ -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 diff --git a/libraries/octree/src/OctreeUtils.cpp b/libraries/octree/src/OctreeUtils.cpp index d1608280d7..8980504431 100644 --- a/libraries/octree/src/OctreeUtils.cpp +++ b/libraries/octree/src/OctreeUtils.cpp @@ -16,12 +16,6 @@ #include #include -#include -#include - -#include -#include -#include 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; } \ No newline at end of file diff --git a/libraries/octree/src/OctreeUtils.h b/libraries/octree/src/OctreeUtils.h index dd761c2a76..d5008376ea 100644 --- a/libraries/octree/src/OctreeUtils.h +++ b/libraries/octree/src/OctreeUtils.h @@ -14,52 +14,9 @@ #include "OctreeConstants.h" -#include -#include - -#include - -#include - 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,