Merge pull request #630 from Atlante45/19373

Code Review for Job #19373
This commit is contained in:
Stephen Birarda 2013-07-10 14:25:18 -07:00
commit 790a48b9f8
6 changed files with 849 additions and 184 deletions

View file

@ -1363,8 +1363,10 @@ void Application::exportVoxels() {
void Application::importVoxels() {
QString desktopLocation = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Import Voxels"), desktopLocation,
tr("Sparse Voxel Octree Files, Square PNG (*.svo *.png)"));
QString fileNameString = QFileDialog::getOpenFileName(
_glWidget, tr("Import Voxels"), desktopLocation,
tr("Sparse Voxel Octree Files, Square PNG, Schematic Files (*.svo *.png *.schematic)"));
QByteArray fileNameAscii = fileNameString.toAscii();
const char* fileName = fileNameAscii.data();
@ -1385,8 +1387,10 @@ void Application::importVoxels() {
}
importVoxels.readFromSquareARGB32Pixels(pixels, pngImage.height());
} else {
} else if (fileNameString.endsWith(".svo", Qt::CaseInsensitive)) {
importVoxels.readFromSVOFile(fileName);
} else {
importVoxels.readFromSchematicFile(fileName);
}
VoxelNode* selectedNode = _voxels.getVoxelAt(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s);
@ -1582,11 +1586,11 @@ void Application::initMenu() {
_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);
voxelMenu->addAction("Cut Voxels", this, SLOT(cutVoxels()), Qt::CTRL | Qt::Key_X);
voxelMenu->addAction("Copy Voxels", this, SLOT(copyVoxels()), Qt::CTRL | Qt::Key_C);
voxelMenu->addAction("Paste Voxels", this, SLOT(pasteVoxels()), Qt::CTRL | Qt::Key_V);
voxelMenu->addAction("Export Voxels", this, SLOT(exportVoxels()), Qt::CTRL | Qt::Key_E);
voxelMenu->addAction("Import Voxels", this, SLOT(importVoxels()), Qt::CTRL | Qt::Key_I);
voxelMenu->addAction("Cut Voxels", this, SLOT(cutVoxels()), Qt::CTRL | Qt::Key_X);
voxelMenu->addAction("Copy Voxels", this, SLOT(copyVoxels()), Qt::CTRL | Qt::Key_C);
voxelMenu->addAction("Paste Voxels", this, SLOT(pasteVoxels()), Qt::CTRL | Qt::Key_V);
QMenu* debugMenu = menuBar->addMenu("Debug");

View file

@ -15,4 +15,9 @@ include(${MACRO_DIR}/IncludeGLM.cmake)
include_glm(${TARGET_NAME} ${ROOT_DIR})
include(${MACRO_DIR}/LinkHifiLibrary.cmake)
link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR})
link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR})
# link ZLIB
find_package(ZLIB)
include_directories(${ZLIB_INCLUDE_DIRS})
target_link_libraries(${TARGET_NAME} ${ZLIB_LIBRARIES})

View file

@ -0,0 +1,245 @@
//
// Tags.h
// hifi
//
// Created by Clement Brisset on 7/3/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
#include "Tags.h"
#include <Log.h>
#include <zlib.h>
#include <zconf.h>
#include <iostream>
Tag::Tag(int tagId, std::stringstream &ss) : _tagId(tagId) {
int size = ss.get() << 8 | ss.get();
_name.clear();
for (int i = 0; i < size; ++i) { _name += ss.get();
}
}
Tag* Tag::readTag(int tagId, std::stringstream &ss) {
switch (tagId) {
case TAG_Byte:
return new TagByte(ss);
case TAG_Short:
return new TagShort(ss);
case TAG_Int:
return new TagInt(ss);
case TAG_Long:
return new TagLong(ss);
case TAG_Float:
return new TagFloat(ss);
case TAG_Double:
return new TagDouble(ss);
case TAG_Byte_Array:
return new TagByteArray(ss);
case TAG_String:
return new TagString(ss);
case TAG_List:
return new TagList(ss);
case TAG_Compound:
return new TagCompound(ss);
case TAG_Int_Array:
return new TagIntArray(ss);
default:
return NULL;
}
}
TagByte::TagByte(std::stringstream &ss) : Tag(TAG_Byte, ss) {
_data = ss.get();
}
TagShort::TagShort(std::stringstream &ss) : Tag(TAG_Short, ss) {
_data = ss.get() << 8 | ss.get();
}
TagInt::TagInt(std::stringstream &ss) : Tag(TAG_Int, ss) {
_data = ss.get() << 24 | ss.get() << 16 | ss.get() << 8 | ss.get();
}
TagLong::TagLong(std::stringstream &ss) : Tag(TAG_Long, ss) {
_data = (((int64_t) ss.get()) << 56 | ((int64_t) ss.get()) << 48
|((int64_t) ss.get()) << 40 | ((int64_t) ss.get()) << 32
| ss.get() << 24 | ss.get() << 16
| ss.get() << 8 | ss.get());
}
// We don't need Float and double, so we just ignore the bytes
TagFloat::TagFloat(std::stringstream &ss) : Tag(TAG_Float, ss) {
ss.seekg(4, ss.cur);
}
TagDouble::TagDouble(std::stringstream &ss) : Tag(TAG_Double, ss) {
ss.seekg(8, ss.cur);
}
TagByteArray::TagByteArray(std::stringstream &ss) : Tag(TAG_Byte_Array, ss) {
_size = ss.get() << 24 | ss.get() << 16 | ss.get() << 8 | ss.get();
_data = new char[_size];
for (int i = 0; i < _size; ++i) {
_data[i] = ss.get();
}
}
TagString::TagString(std::stringstream &ss) : Tag(TAG_String, ss) {
_size = ss.get() << 8 | ss.get();
for (int i = 0; i < _size; ++i) {
_data += ss.get();
}
}
TagList::TagList(std::stringstream &ss) :
Tag(TAG_List, ss) {
_tagId = ss.get();
_size = ss.get() << 24 | ss.get() << 16 | ss.get() << 8 | ss.get();
for (int i = 0; i < _size; ++i) {
ss.putback(0);
ss.putback(0);
_data.push_back(readTag(_tagId, ss));
}
}
TagCompound::TagCompound(std::stringstream &ss) :
Tag(TAG_Compound, ss),
_size(0),
_width(0),
_length(0),
_height(0),
_blocksId(NULL),
_blocksData(NULL)
{
int tagId;
while (TAG_End != (tagId = ss.get())) {
_data.push_back(readTag(tagId, ss));
++_size;
if (NULL == _data.back()) {
_blocksId = NULL;
_blocksData = NULL;
return;
} else if (TAG_Short == tagId) {
if ("Width" == _data.back()->getName()) {
_width = ((TagShort*) _data.back())->getData();
} else if ("Height" == _data.back()->getName()) {
_height = ((TagShort*) _data.back())->getData();
} else if ("Length" == _data.back()->getName()) {
_length = ((TagShort*) _data.back())->getData();
}
} else if (TAG_Byte_Array == tagId) {
if ("Blocks" == _data.back()->getName()) {
_blocksId = ((TagByteArray*) _data.back())->getData();
} else if ("Data" == _data.back()->getName()) {
_blocksData = ((TagByteArray*) _data.back())->getData();
}
}
}
}
TagIntArray::TagIntArray(std::stringstream &ss) : Tag(TAG_Int_Array, ss) {
_size = ss.get() << 24 | ss.get() << 16 | ss.get() << 8 | ss.get();
_data = new int[_size];
for (int i = 0; i < _size; ++i) {
_data[i] = ss.get();
}
}
int retrieveData(std::string filename, std::stringstream &ss) {
std::ifstream file(filename.c_str(), std::ios::binary);
int type = file.peek();
if (type == 0x0A) {
ss.flush();
ss << file;
return 0;
}
if (type == 0x1F) {
return ungzip(file, ss);
}
return 1;
}
int ungzip(std::ifstream &file, std::stringstream &ss) {
std::string gzipedBytes;
gzipedBytes.clear();
ss.flush();
while (!file.eof()) {
gzipedBytes += (char) file.get();
}
file.close();
if (gzipedBytes.size() == 0) {
ss << gzipedBytes;
return 0;
}
unsigned int full_length = gzipedBytes.size();
unsigned int half_length = gzipedBytes.size()/2;
unsigned int uncompLength = full_length;
char* uncomp = (char*) calloc(sizeof(char), uncompLength);
z_stream strm;
strm.next_in = (Bytef *) gzipedBytes.c_str();
strm.avail_in = full_length;
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
bool done = false;
if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK) {
free(uncomp);
return 1;
}
while (!done) {
// If our output buffer is too small
if (strm.total_out >= uncompLength) {
// Increase size of output buffer
char* uncomp2 = (char*) calloc(sizeof(char), uncompLength + half_length);
memcpy(uncomp2, uncomp, uncompLength);
uncompLength += half_length;
free(uncomp);
uncomp = uncomp2;
}
strm.next_out = (Bytef *) (uncomp + strm.total_out);
strm.avail_out = uncompLength - strm.total_out;
// Inflate another chunk.
int err = inflate (&strm, Z_SYNC_FLUSH);
if (err == Z_STREAM_END) {
done = true;
} else if (err != Z_OK) {
break;
}
}
if (inflateEnd (&strm) != Z_OK) {
free(uncomp);
return 1;
}
for (size_t i = 0; i < strm.total_out; ++i) {
ss << uncomp[i];
}
free(uncomp);
return 0;
}

175
libraries/voxels/src/Tags.h Normal file
View file

@ -0,0 +1,175 @@
//
// Tags.h
// hifi
//
// Created by Clement Brisset on 7/3/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
#ifndef __hifi__Tags__
#define __hifi__Tags__
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <list>
#define TAG_End 0
#define TAG_Byte 1
#define TAG_Short 2
#define TAG_Int 3
#define TAG_Long 4
#define TAG_Float 5
#define TAG_Double 6
#define TAG_Byte_Array 7
#define TAG_String 8
#define TAG_List 9
#define TAG_Compound 10
#define TAG_Int_Array 11
int retrieveData(std::string filename, std::stringstream &ss);
int ungzip(std::ifstream &file, std::stringstream &ss);
class Tag {
public:
Tag(int tagId, std::stringstream &ss);
int getTagId() const {return _tagId;}
std::string getName () const {return _name; }
static Tag* readTag(int tagId, std::stringstream &ss);
protected:
int _tagId;
std::string _name;
};
class TagByte : public Tag {
public:
TagByte(std::stringstream &ss);
int8_t getData() const {return _data;}
private:
int8_t _data;
};
class TagShort : public Tag {
public:
TagShort(std::stringstream &ss);
int16_t getData() const {return _data;}
private:
int16_t _data;
};
class TagInt : public Tag {
public:
TagInt(std::stringstream &ss);
int32_t getData() const {return _data;}
private:
int32_t _data;
};
class TagLong : public Tag {
public:
TagLong(std::stringstream &ss);
int64_t getData() const {return _data;}
private:
int64_t _data;
};
class TagFloat : public Tag {
public:
TagFloat(std::stringstream &ss);
};
class TagDouble : public Tag {
public:
TagDouble(std::stringstream &ss);
};
class TagByteArray : public Tag {
public:
TagByteArray(std::stringstream &ss);
int getSize() const {return _size;}
char* getData() const {return _data;}
private:
int _size;
char* _data;
};
class TagString : public Tag {
public:
TagString(std::stringstream &ss);
int getSize() const {return _size;}
std::string getData() const {return _data;}
private:
int _size;
std::string _data;
};
class TagList : public Tag {
public:
TagList(std::stringstream &ss);
int getTagId() const {return _tagId;}
int getSize () const {return _size; }
std::list<Tag*> getData () const {return _data; }
private:
int _tagId;
int _size;
std::list<Tag*> _data;
};
class TagCompound : public Tag {
public:
TagCompound(std::stringstream &ss);
int getSize () const {return _size; }
std::list<Tag*> getData () const {return _data; }
int getWidth () const {return _width; }
int getLength () const {return _length; }
int getHeight () const {return _height; }
char* getBlocksId () const {return _blocksId; }
char* getBlocksData() const {return _blocksData;}
private:
int _size;
std::list<Tag*> _data;
// Specific to schematics file
int _width;
int _length;
int _height;
char* _blocksData;
char* _blocksId;
};
class TagIntArray : public Tag {
public:
TagIntArray(std::stringstream &ss);
~TagIntArray() {delete _data;}
int getSize() const {return _size;}
int* getData() const {return _data;}
private:
int _size;
int* _data;
};
#endif /* defined(__hifi__Tags__) */

File diff suppressed because it is too large Load diff

View file

@ -135,6 +135,8 @@ public:
bool readFromSVOFile(const char* filename);
// reads voxels from square image with alpha as a Y-axis
bool readFromSquareARGB32Pixels(const uint32_t* pixels, int dimension);
bool readFromSchematicFile(const char* filename);
void computeBlockColor(int id, int data, int& r, int& g, int& b, int& create);
unsigned long getVoxelCount();