From 4033baa5caac02332a30a8cb0e39a0bc86c724a6 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 2 Dec 2013 12:15:43 -0800 Subject: [PATCH] Starting work on "metavoxels." --- interface/CMakeLists.txt | 1 + libraries/metavoxels/CMakeLists.txt | 20 ++++++++++ libraries/metavoxels/src/Bitstream.cpp | 53 ++++++++++++++++++++++++++ libraries/metavoxels/src/Bitstream.h | 36 +++++++++++++++++ libraries/metavoxels/src/Metavoxel.cpp | 50 ++++++++++++++++++++++++ libraries/metavoxels/src/Metavoxel.h | 38 ++++++++++++++++++ 6 files changed, 198 insertions(+) create mode 100644 libraries/metavoxels/CMakeLists.txt create mode 100644 libraries/metavoxels/src/Bitstream.cpp create mode 100644 libraries/metavoxels/src/Bitstream.h create mode 100644 libraries/metavoxels/src/Metavoxel.cpp create mode 100644 libraries/metavoxels/src/Metavoxel.h diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 735989dfbf..fe2f8b1c89 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -84,6 +84,7 @@ include(${MACRO_DIR}/LinkHifiLibrary.cmake) # link required hifi libraries link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR}) link_hifi_library(voxels ${TARGET_NAME} ${ROOT_DIR}) +link_hifi_library(metavoxels ${TARGET_NAME} ${ROOT_DIR}) link_hifi_library(avatars ${TARGET_NAME} ${ROOT_DIR}) link_hifi_library(audio ${TARGET_NAME} ${ROOT_DIR}) diff --git a/libraries/metavoxels/CMakeLists.txt b/libraries/metavoxels/CMakeLists.txt new file mode 100644 index 0000000000..92594af4ef --- /dev/null +++ b/libraries/metavoxels/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 2.8) + +set(ROOT_DIR ../..) +set(MACRO_DIR ${ROOT_DIR}/cmake/macros) + +# setup for find modules +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/modules/") + +set(TARGET_NAME metavoxels) + +find_package(Qt5Widgets REQUIRED) + +include(${MACRO_DIR}/SetupHifiLibrary.cmake) +setup_hifi_library(${TARGET_NAME}) + +qt5_use_modules(${TARGET_NAME} Widgets) + +include(${MACRO_DIR}/IncludeGLM.cmake) +include_glm(${TARGET_NAME} ${ROOT_DIR}) + diff --git a/libraries/metavoxels/src/Bitstream.cpp b/libraries/metavoxels/src/Bitstream.cpp new file mode 100644 index 0000000000..400cb4c1c6 --- /dev/null +++ b/libraries/metavoxels/src/Bitstream.cpp @@ -0,0 +1,53 @@ +// +// Bitstream.cpp +// metavoxels +// +// Created by Andrzej Kapolka on 12/2/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#include + +#include "Bitstream.h" + +Bitstream::Bitstream(QDataStream& underlying) + : _underlying(underlying), _byte(0), _position(0) { +} + +int Bitstream::write(const void* data, int bits) { + return bits; +} + +int Bitstream::read(void* data, int bits) { + return bits; +} + +void Bitstream::flush() { + if (_position != 0) { + _underlying << _byte; + _byte = 0; + _position = 0; + } +} + +Bitstream& Bitstream::operator<<(bool value) { + if (value) { + _byte |= (1 << _position); + } + const int LAST_BIT_POSITION = 7; + if (_position++ == LAST_BIT_POSITION) { + flush(); + } + return *this; +} + +Bitstream& Bitstream::operator>>(bool& value) { + if (_position == 0) { + _underlying >> _byte; + } + value = _byte & (1 << _position); + if (_position++ == 7) { + _position = 0; + } + return *this; +} diff --git a/libraries/metavoxels/src/Bitstream.h b/libraries/metavoxels/src/Bitstream.h new file mode 100644 index 0000000000..9b4e08c858 --- /dev/null +++ b/libraries/metavoxels/src/Bitstream.h @@ -0,0 +1,36 @@ +// +// Bitstream.h +// metavoxels +// +// Created by Andrzej Kapolka on 12/2/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#ifndef __interface__Bitstream__ +#define __interface__Bitstream__ + +class QDataStream; + +/// A stream for bit-aligned data. +class Bitstream { +public: + + Bitstream(QDataStream& underlying); + + Bitstream& write(const void* data, int bits); + Bitstream& read(void* data, int bits); + + /// Flushes any unwritten bits to the underlying stream. + void flush(); + + Bitstream& operator<<(bool value); + Bitstream& operator>>(bool& value); + +private: + + QDataStream& _underlying; + char _byte; + int _position; +}; + +#endif /* defined(__interface__Bitstream__) */ diff --git a/libraries/metavoxels/src/Metavoxel.cpp b/libraries/metavoxels/src/Metavoxel.cpp new file mode 100644 index 0000000000..34332c923a --- /dev/null +++ b/libraries/metavoxels/src/Metavoxel.cpp @@ -0,0 +1,50 @@ +// +// Metavoxels.cpp +// metavoxels +// +// Created by Andrzej Kapolka on 12/2/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#include "Bitstream.h" +#include "Metavoxel.h" + +bool Metavoxel::isLeaf() const { + for (int i = 0; i < CHILD_COUNT; i++) { + if (_children[i]) { + return false; + } + } + return true; +} + +Bitstream& operator<<(Bitstream& stream, const Metavoxel& voxel) { + for (int i = 0; i < Metavoxel::CHILD_COUNT; i++) { + const Metavoxel* child = voxel.getChild(i); + if (child) { + stream << true << *child; + + } else { + stream << false; + } + } + return stream; +} + +Bitstream& operator>>(Bitstream& stream, Metavoxel& voxel) { + for (int i = 0; i < Metavoxel::CHILD_COUNT; i++) { + bool childExists; + stream >> childExists; + Metavoxel* child = voxel.getChild(i); + if (childExists) { + if (!child) { + voxel.setChild(i, new Metavoxel); + } + stream >> *child; + + } else if (child) { + voxel.setChild(i, NULL); + } + } + return stream; +} diff --git a/libraries/metavoxels/src/Metavoxel.h b/libraries/metavoxels/src/Metavoxel.h new file mode 100644 index 0000000000..dcfb45309f --- /dev/null +++ b/libraries/metavoxels/src/Metavoxel.h @@ -0,0 +1,38 @@ +// +// Metavoxel.h +// metavoxels +// +// Created by Andrzej Kapolka on 12/2/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#ifndef __interface__Metavoxel__ +#define __interface__Metavoxel__ + +#include + +class Bitstream; + +/// A single node in a metavoxel tree. +class Metavoxel { +public: + + static const int CHILD_COUNT = 8; + + /// Sets the child at the specified index. Note that this object will assume ownership if non-null. + void setChild(int index, Metavoxel* child) { _children[index].reset(child); } + + const Metavoxel* getChild(int index) const { return _children[index].data(); } + Metavoxel* getChild(int index) { return _children[index].data(); } + + bool isLeaf() const; + +private: + + QScopedPointer _children[CHILD_COUNT]; +}; + +Bitstream& operator<<(Bitstream& stream, const Metavoxel& voxel); +Bitstream& operator>>(Bitstream& stream, Metavoxel& voxel); + +#endif /* defined(__interface__Metavoxel__) */