DeleteVoxelCommand::DeleteVoxelCommand(VoxelTree* tree, VoxelDetail& voxel, VoxelEditPacketSender* packetSender, QUndoCommand* parent) : QUndoCommand("Delete Voxel", parent), _tree(tree), _packetSender(packetSender), _voxel(voxel), _oldTree(NULL) { _tree->lockForRead(); VoxelTreeElement* element = _tree->getEnclosingVoxelAt(_voxel.x, _voxel.y, _voxel.z, _voxel.s); if (element->getScale() == _voxel.s) { if (!element->hasContent() && !element->isLeaf()) { _oldTree = new VoxelTree(); _tree->copySubTreeIntoNewTree(element, _oldTree, false); } else { _voxel.red = element->getColor()[0]; _voxel.green = element->getColor()[1]; _voxel.blue = element->getColor()[2]; } } else if (element->hasContent() && element->isLeaf()) { _voxel.red = element->getColor()[0]; _voxel.green = element->getColor()[1]; _voxel.blue = element->getColor()[2]; } else { _voxel.s = 0.0f; } _tree->unlock(); }
VoxelDetail VoxelsScriptingInterface::getVoxelEnclosingPoint(const glm::vec3& point) { VoxelDetail result = { 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0 }; if (_tree) { OctreeElement* element = _tree->getElementEnclosingPoint(point / (float)TREE_SCALE); if (element) { VoxelTreeElement* voxel = static_cast<VoxelTreeElement*>(element); result.x = voxel->getCorner().x; result.y = voxel->getCorner().y; result.z = voxel->getCorner().z; result.s = voxel->getScale(); result.red = voxel->getColor()[0]; result.green = voxel->getColor()[1]; result.blue = voxel->getColor()[2]; } } return result; }
bool copyAndFillOperation(OctreeElement* element, void* extraData) { VoxelTreeElement* voxel = (VoxelTreeElement*)element; copyAndFillArgs* args = (copyAndFillArgs*)extraData; char outputMessage[128]; args->inCount++; int percentDone = (100*args->inCount/args->originalCount); // For each leaf node... if (voxel->isLeaf()) { // create a copy of the leaf in the copy destination float x = voxel->getCorner().x; float y = voxel->getCorner().y; float z = voxel->getCorner().z; float s = voxel->getScale(); unsigned char red = voxel->getTrueColor()[RED_INDEX]; unsigned char green = voxel->getTrueColor()[GREEN_INDEX]; unsigned char blue = voxel->getTrueColor()[BLUE_INDEX]; bool destructive = true; args->destinationTree->createVoxel(x, y, z, s, red, green, blue, destructive); args->outCount++; sprintf(outputMessage,"Completed: %d%% (%lu of %lu) - Creating voxel %lu at [%f,%f,%f,%f]", percentDone,args->inCount,args->originalCount,args->outCount,x,y,z,s); printf("%s",outputMessage); for (int b = 0; b < strlen(outputMessage); b++) { printf("\b"); } // and create same sized leafs from this leaf voxel down to zero in the destination tree for (float yFill = y-s; yFill >= 0.0f; yFill -= s) { args->destinationTree->createVoxel(x, yFill, z, s, red, green, blue, destructive); args->outCount++; sprintf(outputMessage,"Completed: %d%% (%lu of %lu) - Creating fill voxel %lu at [%f,%f,%f,%f]", percentDone,args->inCount,args->originalCount,args->outCount,x,y,z,s); printf("%s",outputMessage); for (int b = 0; b < strlen(outputMessage); b++) { printf("\b"); } } } return true; }
RayToVoxelIntersectionResult VoxelsScriptingInterface::findRayIntersection(const PickRay& ray) { RayToVoxelIntersectionResult result; if (_tree) { OctreeElement* element; result.intersects = _tree->findRayIntersection(ray.origin, ray.direction, element, result.distance, result.face); if (result.intersects) { VoxelTreeElement* voxel = (VoxelTreeElement*)element; result.voxel.x = voxel->getCorner().x; result.voxel.y = voxel->getCorner().y; result.voxel.z = voxel->getCorner().z; result.voxel.s = voxel->getScale(); result.voxel.red = voxel->getColor()[0]; result.voxel.green = voxel->getColor()[1]; result.voxel.blue = voxel->getColor()[2]; result.intersection = ray.origin + (ray.direction * result.distance); } } return result; }
VoxelDetail VoxelsScriptingInterface::getVoxelAt(float x, float y, float z, float scale) { // setup a VoxelDetail struct with the data VoxelDetail result = {0,0,0,0,0,0,0}; if (_tree) { _tree->lockForRead(); VoxelTreeElement* voxel = static_cast<VoxelTreeElement*>(_tree->getOctreeElementAt(x / (float)TREE_SCALE, y / (float)TREE_SCALE, z / (float)TREE_SCALE, scale / (float)TREE_SCALE)); _tree->unlock(); if (voxel) { // Note: these need to be in voxel space because the VoxelDetail -> js converter will upscale result.x = voxel->getCorner().x; result.y = voxel->getCorner().y; result.z = voxel->getCorner().z; result.s = voxel->getScale(); result.red = voxel->getColor()[RED_INDEX]; result.green = voxel->getColor()[GREEN_INDEX]; result.blue = voxel->getColor()[BLUE_INDEX]; } } return result; }