From d1d2e75143b110bbedd0e843fa0d898b716e4b69 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 28 May 2013 19:00:02 -0700 Subject: [PATCH] first cut at export and import of voxels --- interface/src/Application.cpp | 40 +++++++++++++++++++++++++++++- interface/src/Application.h | 5 +++- interface/src/VoxelSystem.cpp | 12 +++++++++ interface/src/VoxelSystem.h | 2 ++ libraries/voxels/src/VoxelTree.cpp | 11 +++++--- libraries/voxels/src/VoxelTree.h | 4 +-- voxel-server/src/main.cpp | 2 +- 7 files changed, 68 insertions(+), 8 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a0979eb4b9..b18a0095e7 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -853,7 +854,10 @@ void Application::idle() { // red indicates deletion _mouseVoxel.red = 255; _mouseVoxel.green = _mouseVoxel.blue = 0; - + } else if (_selectVoxelMode->isChecked()) { + // yellow indicates deletion + _mouseVoxel.red = _mouseVoxel.green = 255; + _mouseVoxel.blue = 0; } else { // _addVoxelMode->isChecked() || _colorVoxelMode->isChecked() QColor paintColor = _voxelPaintColor->data().value(); _mouseVoxel.red = paintColor.red(); @@ -1136,6 +1140,35 @@ void Application::chooseVoxelPaintColor() { // restore the main window's active state _window->activateWindow(); } + +void Application::exportVoxels() { + QString fileNameString = QFileDialog::getSaveFileName(_glWidget, tr("Export Voxels"), "~/voxels.hio2", + tr("High Fidelity Voxel Files (*.hio2)")); + QByteArray fileNameAscii = fileNameString.toAscii(); + const char* fileName = fileNameAscii.data(); + VoxelNode* selectedNode = _voxels.getVoxelAt(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s); + printf("exportVoxels() fileName: %s _mouseVoxel: %f,%f,%f-%f \n", fileName, + _mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s); + if (selectedNode) { + selectedNode->printDebugDetails("selected voxel"); + } + _voxels.writeToFileV2(fileName,selectedNode); +} + +void Application::importVoxels() { + QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Import Voxels"), "~", tr("High Fidelity Voxel Files (*.hio2)")); + QByteArray fileNameAscii = fileNameString.toAscii(); + const char* fileName = fileNameAscii.data(); + VoxelNode* selectedNode = _voxels.getVoxelAt(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s); + printf("importVoxels() fileName: %s _mouseVoxel: %f,%f,%f-%f \n", fileName, + _mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s); + if (selectedNode) { + selectedNode->printDebugDetails("selected voxel"); + } + + // not yet supported!!! + _voxels.readFromFileV2(fileName,selectedNode); +} void Application::initMenu() { QMenuBar* menuBar = new QMenuBar(); @@ -1202,6 +1235,9 @@ void Application::initMenu() { (_colorVoxelMode = voxelMenu->addAction( "Color Voxel Mode", this, SLOT(updateVoxelModeActions()), Qt::Key_3))->setCheckable(true); _voxelModeActions->addAction(_colorVoxelMode); + (_selectVoxelMode = voxelMenu->addAction( + "Select Voxel Mode", this, SLOT(updateVoxelModeActions()), Qt::Key_8))->setCheckable(true); + _voxelModeActions->addAction(_selectVoxelMode); voxelMenu->addAction("Place Voxel", this, SLOT(addVoxelInFrontOfAvatar()), Qt::Key_4); voxelMenu->addAction("Decrease Voxel Size", this, SLOT(decreaseVoxelSize()), Qt::Key_5); @@ -1212,6 +1248,8 @@ void Application::initMenu() { _voxelPaintColor->setData(paintColor); _voxelPaintColor->setIcon(createSwatchIcon(paintColor)); (_destructiveAddVoxel = voxelMenu->addAction("Create Voxel is Destructive"))->setCheckable(true); + voxelMenu->addAction("Export Voxels", this, SLOT(exportVoxels()), Qt::CTRL | Qt::Key_E); + voxelMenu->addAction("Import Voxels", this, SLOT(importVoxels()), Qt::CTRL | Qt::Key_I); QMenu* frustumMenu = menuBar->addMenu("Frustum"); (_frustumOn = frustumMenu->addAction("Display Frustum"))->setCheckable(true); diff --git a/interface/src/Application.h b/interface/src/Application.h index 35d49d275a..ed03708bd9 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -99,7 +99,9 @@ private slots: void decreaseVoxelSize(); void increaseVoxelSize(); void chooseVoxelPaintColor(); - + void exportVoxels(); + void importVoxels(); + private: void initMenu(); @@ -155,6 +157,7 @@ private: QAction* _addVoxelMode; // Whether add voxel mode is enabled QAction* _deleteVoxelMode; // Whether delete voxel mode is enabled QAction* _colorVoxelMode; // Whether color voxel mode is enabled + QAction* _selectVoxelMode; // Whether select voxel mode is enabled QAction* _voxelPaintColor; // The color with which to paint voxels QAction* _destructiveAddVoxel; // when doing voxel editing do we want them to be destructive QAction* _frustumOn; // Whether or not to display the debug view frustum diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index ea8787a902..2a0a7e7a7a 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -70,6 +70,18 @@ void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) setupNewVoxelsForDrawing(); } +void VoxelSystem::writeToFileV2(const char* filename, VoxelNode* node) const { + _tree->writeToFileV2(filename, node); +} + +bool VoxelSystem::readFromFileV2(const char* filename, VoxelNode* node) { + bool result = _tree->readFromFileV2(filename, node); + if (result) { + setupNewVoxelsForDrawing(); + } + return result; +} + long int VoxelSystem::getVoxelsCreated() { return _tree->voxelsCreated; } diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index 7bffb1d33c..1aeb66a7c6 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -44,6 +44,8 @@ public: void setViewerAvatar(Avatar *newViewerAvatar) { _viewerAvatar = newViewerAvatar; }; void setCamera(Camera* newCamera) { _camera = newCamera; }; void loadVoxelsFile(const char* fileName,bool wantColorRandomizer); + void writeToFileV2(const char* filename, VoxelNode* node) const; + bool readFromFileV2(const char* filename, VoxelNode* node); long int getVoxelsCreated(); long int getVoxelsColored(); diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 798b447d6c..89b584a541 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -1105,7 +1105,7 @@ int VoxelTree::encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEnco return bytesAtThisLevel; } -bool VoxelTree::readFromFileV2(const char* fileName) { +bool VoxelTree::readFromFileV2(const char* fileName, VoxelNode* node) { std::ifstream file(fileName, std::ios::in|std::ios::binary|std::ios::ate); if(file.is_open()) { printLog("loading file %s...\n", fileName); @@ -1126,7 +1126,7 @@ bool VoxelTree::readFromFileV2(const char* fileName) { return false; } -void VoxelTree::writeToFileV2(const char* fileName) const { +void VoxelTree::writeToFileV2(const char* fileName, VoxelNode* node) const { std::ofstream file(fileName, std::ios::out|std::ios::binary); @@ -1134,7 +1134,12 @@ void VoxelTree::writeToFileV2(const char* fileName) const { printLog("saving to file %s...\n", fileName); VoxelNodeBag nodeBag; - nodeBag.insert(rootNode); + // If we were given a specific node, start from there, otherwise start from root + if (node) { + nodeBag.insert(node); + } else { + nodeBag.insert(rootNode); + } static unsigned char outputBuffer[MAX_VOXEL_PACKET_SIZE - 1]; // save on allocs by making this static int bytesWritten = 0; diff --git a/libraries/voxels/src/VoxelTree.h b/libraries/voxels/src/VoxelTree.h index 0643b1038e..5e56be5062 100644 --- a/libraries/voxels/src/VoxelTree.h +++ b/libraries/voxels/src/VoxelTree.h @@ -91,8 +91,8 @@ public: void loadVoxelsFile(const char* fileName, bool wantColorRandomizer); // these will read/write files that match the wireformat, excluding the 'V' leading - void writeToFileV2(const char* filename) const; - bool readFromFileV2(const char* filename); + void writeToFileV2(const char* filename, VoxelNode* node = NULL) const; + bool readFromFileV2(const char* filename, VoxelNode* node = NULL); unsigned long getVoxelCount(); diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index 1a93021f09..70f7ab8752 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -517,7 +517,7 @@ int main(int argc, const char * argv[]) { const char* INPUT_FILE = "-i"; const char* voxelsFilename = getCmdOption(argc, argv, INPUT_FILE); if (voxelsFilename) { - randomTree.loadVoxelsFile(voxelsFilename,wantColorRandomizer); + randomTree.readFromFileV2(voxelsFilename); } // Check to see if the user passed in a command line option for setting packet send rate