History

Merge pull request #682 from ZappoMan/bug_fixes

Import Voxels and Paste Bug fixes

Stephen Birarda committed 13 years ago

Commit 3f986572 parents d38955a 440352c4 files · +111 −15Browse files

Showing the changes to interface/src/Application.cpp (1 of 4 files). Show all

Unified · Split

interface/src/Application.cpp+106−14
@@ -1363,7 +1363,9 @@ struct SendVoxelsOperationArgs { · 1362 unmodified lines
13631363 unsigned char* newBaseOctCode;
13641364 unsigned char messageBuffer[MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE];
13651365 int bufferInUse;
1366−
1366+ uint64_t lastSendTime;
1367+ int packetsSent;
1368+ uint64_t bytesSent;
13671369 };
13681370
13691371 bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) {
@@ -1395,17 +1397,30 @@ bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { · 25 unmodified lines
13951397 codeColorBuffer[bytesInCode + GREEN_INDEX] = node->getColor()[GREEN_INDEX];
13961398 codeColorBuffer[bytesInCode + BLUE_INDEX ] = node->getColor()[BLUE_INDEX ];
13971399
1398− // TODO: sendVoxelsOperation() is sending voxels too fast.
1399− // This printf function accidently slowed down sending
1400− // and hot-fixed the bug when importing
1401− // large PNG models (256x256 px and more)
1402− static unsigned int sendVoxelsOperationCalled = 0; printf("sending voxel #%u\n", ++sendVoxelsOperationCalled);
1403−
14041400 // if we have room don't have room in the buffer, then send the previously generated message first
14051401 if (args->bufferInUse + codeAndColorLength > MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE) {
1402+
1403+ args->packetsSent++;
1404+ args->bytesSent += args->bufferInUse;
1405+
14061406 controlledBroadcastToNodes(args->messageBuffer, args->bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1);
14071407 args->bufferInUse = numBytesForPacketHeader((unsigned char*) &PACKET_TYPE_SET_VOXEL_DESTRUCTIVE)
14081408 + sizeof(unsigned short int); // reset
1409+
1410+ uint64_t now = usecTimestampNow();
1411+ // dynamically sleep until we need to fire off the next set of voxels
1412+ const uint64_t CLIENT_TO_SERVER_VOXEL_SEND_INTERVAL_USECS = 1000 * 5; // 1 packet every 10 milliseconds
1413+ uint64_t elapsed = now - args->lastSendTime;
1414+ int usecToSleep = CLIENT_TO_SERVER_VOXEL_SEND_INTERVAL_USECS - elapsed;
1415+ if (usecToSleep > 0) {
1416+ printLog("sendVoxelsOperation: packet: %d bytes:%ld elapsed %ld usecs, sleeping for %d usecs!\n",
1417+ args->packetsSent, args->bytesSent, elapsed, usecToSleep);
1418+ usleep(usecToSleep);
1419+ } else {
1420+ printLog("sendVoxelsOperation: packet: %d bytes:%ld elapsed %ld usecs, no need to sleep!\n",
1421+ args->packetsSent, args->bytesSent, elapsed);
1422+ }
1423+ args->lastSendTime = now;
14091424 }
14101425
14111426 // copy this node's code color details into our buffer.
@@ -1434,14 +1449,15 @@ void Application::exportVoxels() { · 22 unmodified lines
14341449 _window->activateWindow();
14351450 }
14361451
1437−void Application::importVoxels() {
1452+const char* IMPORT_FILE_TYPES = "Sparse Voxel Octree Files, Square PNG, Schematic Files (*.svo *.png *.schematic)";
1453+void Application::importVoxelsToClipboard() {
14381454 QString desktopLocation = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
1439− QString fileNameString = QFileDialog::getOpenFileName(
1440− _glWidget, tr("Import Voxels"), desktopLocation,
1441− tr("Sparse Voxel Octree Files, Square PNG, Schematic Files (*.svo *.png *.schematic)"));
1442−
1443− const char* fileName = fileNameString.toAscii().data();
1455+ QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Import Voxels to Clipboard"), desktopLocation,
1456+ tr(IMPORT_FILE_TYPES));
14441457
1458+ QByteArray fileNameAscii = fileNameString.toAscii();
1459+ const char* fileName = fileNameAscii.data();
1460+
14451461 _clipboardTree.eraseAllVoxels();
14461462 if (fileNameString.endsWith(".png", Qt::CaseInsensitive)) {
14471463 QImage pngImage = QImage(fileName);
@@ -1457,7 +1473,6 @@ void Application::importVoxels() { · 9 unmodified lines
14571473 QImage tmp = pngImage.convertToFormat(QImage::Format_ARGB32);
14581474 pixels = reinterpret_cast<const uint32_t*>(tmp.constBits());
14591475 }
1460−
14611476 _clipboardTree.readFromSquareARGB32Pixels(pixels, pngImage.height());
14621477 } else if (fileNameString.endsWith(".svo", Qt::CaseInsensitive)) {
14631478 _clipboardTree.readFromSVOFile(fileName);
@@ -1469,6 +1484,76 @@ void Application::importVoxels() { · 5 unmodified lines
14691484 _window->activateWindow();
14701485 }
14711486
1487+void Application::importVoxels() {
1488+ QString desktopLocation = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
1489+ QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Import Voxels"), desktopLocation,
1490+ tr(IMPORT_FILE_TYPES));
1491+
1492+ QByteArray fileNameAscii = fileNameString.toAscii();
1493+ const char* fileName = fileNameAscii.data();
1494+
1495+ VoxelTree importVoxels;
1496+ if (fileNameString.endsWith(".png", Qt::CaseInsensitive)) {
1497+ QImage pngImage = QImage(fileName);
1498+ if (pngImage.height() != pngImage.width()) {
1499+ printLog("ERROR: Bad PNG size: height != width.\n");
1500+ return;
1501+ }
1502+
1503+ const uint32_t* pixels;
1504+ if (pngImage.format() == QImage::Format_ARGB32) {
1505+ pixels = reinterpret_cast<const uint32_t*>(pngImage.constBits());
1506+ } else {
1507+ QImage tmp = pngImage.convertToFormat(QImage::Format_ARGB32);
1508+ pixels = reinterpret_cast<const uint32_t*>(tmp.constBits());
1509+ }
1510+
1511+ importVoxels.readFromSquareARGB32Pixels(pixels, pngImage.height());
1512+ } else if (fileNameString.endsWith(".svo", Qt::CaseInsensitive)) {
1513+ importVoxels.readFromSVOFile(fileName);
1514+ } else if (fileNameString.endsWith(".schematic", Qt::CaseInsensitive)) {
1515+ importVoxels.readFromSchematicFile(fileName);
1516+ }
1517+
1518+ VoxelNode* selectedNode = _voxels.getVoxelAt(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s);
1519+
1520+ // Recurse the Import Voxels tree, where everything is root relative, and send all the colored voxels to
1521+ // the server as an set voxel message, this will also rebase the voxels to the new location
1522+ unsigned char* calculatedOctCode = NULL;
1523+ SendVoxelsOperationArgs args;
1524+ args.lastSendTime = usecTimestampNow();
1525+ args.packetsSent = 0;
1526+ args.bytesSent = 0;
1527+
1528+ int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE);
1529+
1530+ unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[numBytesPacketHeader];
1531+ *sequenceAt = 0;
1532+ args.bufferInUse = numBytesPacketHeader + sizeof(unsigned short int); // set to command + sequence
1533+
1534+ // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the
1535+ // voxel size/position details.
1536+ if (selectedNode) {
1537+ args.newBaseOctCode = selectedNode->getOctalCode();
1538+ } else {
1539+ args.newBaseOctCode = calculatedOctCode = pointToVoxel(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s);
1540+ }
1541+
1542+ importVoxels.recurseTreeWithOperation(sendVoxelsOperation, &args);
1543+
1544+ // If we have voxels left in the packet, then send the packet
1545+ if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) {
1546+ controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1);
1547+ }
1548+
1549+ if (calculatedOctCode) {
1550+ delete[] calculatedOctCode;
1551+ }
1552+
1553+ // restore the main window's active state
1554+ _window->activateWindow();
1555+}
1556+
14721557 void Application::cutVoxels() {
14731558 copyVoxels();
14741559 deleteVoxelUnderCursor();
@@ -1492,6 +1577,9 @@ void Application::pasteVoxels() { · 17 unmodified lines
14921577 // Recurse the clipboard tree, where everything is root relative, and send all the colored voxels to
14931578 // the server as an set voxel message, this will also rebase the voxels to the new location
14941579 SendVoxelsOperationArgs args;
1580+ args.lastSendTime = usecTimestampNow();
1581+ args.packetsSent = 0;
1582+ args.bytesSent = 0;
14951583
14961584 int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE);
14971585
@@ -1513,6 +1601,9 @@ void Application::pasteVoxels() { · 15 unmodified lines
15131601 // If we have voxels left in the packet, then send the packet
15141602 if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) {
15151603 controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1);
1604+ printLog("sending packet: %d\n", ++args.packetsSent);
1605+ args.bytesSent += args.bufferInUse;
1606+ printLog("total bytes sent: %ld\n", args.bytesSent);
15161607 }
15171608
15181609 if (calculatedOctCode) {
@@ -1630,6 +1721,7 @@ void Application::initMenu() { · 111 unmodified lines
16301721
16311722 voxelMenu->addAction("Export Voxels", this, SLOT(exportVoxels()), Qt::CTRL | Qt::Key_E);
16321723 voxelMenu->addAction("Import Voxels", this, SLOT(importVoxels()), Qt::CTRL | Qt::Key_I);
1724+ voxelMenu->addAction("Import Voxels to Clipboard", this, SLOT(importVoxelsToClipboard()), Qt::SHIFT | Qt::CTRL | Qt::Key_I);
16331725 voxelMenu->addAction("Cut Voxels", this, SLOT(cutVoxels()), Qt::CTRL | Qt::Key_X);
16341726 voxelMenu->addAction("Copy Voxels", this, SLOT(copyVoxels()), Qt::CTRL | Qt::Key_C);
16351727 voxelMenu->addAction("Paste Voxels", this, SLOT(pasteVoxels()), Qt::CTRL | Qt::Key_V);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t