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
1363 unsigned char* newBaseOctCode;1363 unsigned char* newBaseOctCode;
1364 unsigned char messageBuffer[MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE];1364 unsigned char messageBuffer[MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE];
1365 int bufferInUse;1365 int bufferInUse;
1366 1366 uint64_t lastSendTime;
1367 int packetsSent;
1368 uint64_t bytesSent;
1367};1369};
1368 1370
1369bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) {1371bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) {
@@ -1395,17 +1397,30 @@ bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { · 25 unmodified lines
1395 codeColorBuffer[bytesInCode + GREEN_INDEX] = node->getColor()[GREEN_INDEX];1397 codeColorBuffer[bytesInCode + GREEN_INDEX] = node->getColor()[GREEN_INDEX];
1396 codeColorBuffer[bytesInCode + BLUE_INDEX ] = node->getColor()[BLUE_INDEX ];1398 codeColorBuffer[bytesInCode + BLUE_INDEX ] = node->getColor()[BLUE_INDEX ];
1397 1399
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
1404 // if we have room don't have room in the buffer, then send the previously generated message first1400 // if we have room don't have room in the buffer, then send the previously generated message first
1405 if (args->bufferInUse + codeAndColorLength > MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE) {1401 if (args->bufferInUse + codeAndColorLength > MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE) {
1402
1403 args->packetsSent++;
1404 args->bytesSent += args->bufferInUse;
1405
1406 controlledBroadcastToNodes(args->messageBuffer, args->bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1);1406 controlledBroadcastToNodes(args->messageBuffer, args->bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1);
1407 args->bufferInUse = numBytesForPacketHeader((unsigned char*) &PACKET_TYPE_SET_VOXEL_DESTRUCTIVE)1407 args->bufferInUse = numBytesForPacketHeader((unsigned char*) &PACKET_TYPE_SET_VOXEL_DESTRUCTIVE)
1408 + sizeof(unsigned short int); // reset1408 + 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;
1409 }1424 }
1410 1425
1411 // copy this node's code color details into our buffer.1426 // copy this node's code color details into our buffer.
@@ -1434,14 +1449,15 @@ void Application::exportVoxels() { · 22 unmodified lines
1434 _window->activateWindow();1449 _window->activateWindow();
1435}1450}
1436 1451
1437void Application::importVoxels() {1452const char* IMPORT_FILE_TYPES = "Sparse Voxel Octree Files, Square PNG, Schematic Files (*.svo *.png *.schematic)";
1453void Application::importVoxelsToClipboard() {
1438 QString desktopLocation = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);1454 QString desktopLocation = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
1439 QString fileNameString = QFileDialog::getOpenFileName(1455 QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Import Voxels to Clipboard"), desktopLocation,
1440 _glWidget, tr("Import Voxels"), desktopLocation,1456 tr(IMPORT_FILE_TYPES));
1441 tr("Sparse Voxel Octree Files, Square PNG, Schematic Files (*.svo *.png *.schematic)"));
1442
1443 const char* fileName = fileNameString.toAscii().data();
1444 1457
1458 QByteArray fileNameAscii = fileNameString.toAscii();
1459 const char* fileName = fileNameAscii.data();
1460
1445 _clipboardTree.eraseAllVoxels();1461 _clipboardTree.eraseAllVoxels();
1446 if (fileNameString.endsWith(".png", Qt::CaseInsensitive)) {1462 if (fileNameString.endsWith(".png", Qt::CaseInsensitive)) {
1447 QImage pngImage = QImage(fileName);1463 QImage pngImage = QImage(fileName);
@@ -1457,7 +1473,6 @@ void Application::importVoxels() { · 9 unmodified lines
1457 QImage tmp = pngImage.convertToFormat(QImage::Format_ARGB32);1473 QImage tmp = pngImage.convertToFormat(QImage::Format_ARGB32);
1458 pixels = reinterpret_cast<const uint32_t*>(tmp.constBits());1474 pixels = reinterpret_cast<const uint32_t*>(tmp.constBits());
1459 }1475 }
1460
1461 _clipboardTree.readFromSquareARGB32Pixels(pixels, pngImage.height());1476 _clipboardTree.readFromSquareARGB32Pixels(pixels, pngImage.height());
1462 } else if (fileNameString.endsWith(".svo", Qt::CaseInsensitive)) {1477 } else if (fileNameString.endsWith(".svo", Qt::CaseInsensitive)) {
1463 _clipboardTree.readFromSVOFile(fileName);1478 _clipboardTree.readFromSVOFile(fileName);
@@ -1469,6 +1484,76 @@ void Application::importVoxels() { · 5 unmodified lines
1469 _window->activateWindow();1484 _window->activateWindow();
1470}1485}
1471 1486
1487void 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
1472void Application::cutVoxels() {1557void Application::cutVoxels() {
1473 copyVoxels();1558 copyVoxels();
1474 deleteVoxelUnderCursor();1559 deleteVoxelUnderCursor();
@@ -1492,6 +1577,9 @@ void Application::pasteVoxels() { · 17 unmodified lines
1492 // Recurse the clipboard tree, where everything is root relative, and send all the colored voxels to 1577 // Recurse the clipboard tree, where everything is root relative, and send all the colored voxels to
1493 // the server as an set voxel message, this will also rebase the voxels to the new location1578 // the server as an set voxel message, this will also rebase the voxels to the new location
1494 SendVoxelsOperationArgs args;1579 SendVoxelsOperationArgs args;
1580 args.lastSendTime = usecTimestampNow();
1581 args.packetsSent = 0;
1582 args.bytesSent = 0;
1495 1583
1496 int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE);1584 int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE);
1497 1585
@@ -1513,6 +1601,9 @@ void Application::pasteVoxels() { · 15 unmodified lines
1513 // If we have voxels left in the packet, then send the packet1601 // If we have voxels left in the packet, then send the packet
1514 if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) {1602 if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) {
1515 controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1);1603 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);
1516 }1607 }
1517 1608
1518 if (calculatedOctCode) {1609 if (calculatedOctCode) {
@@ -1630,6 +1721,7 @@ void Application::initMenu() { · 111 unmodified lines
1630 1721
1631 voxelMenu->addAction("Export Voxels", this, SLOT(exportVoxels()), Qt::CTRL | Qt::Key_E);1722 voxelMenu->addAction("Export Voxels", this, SLOT(exportVoxels()), Qt::CTRL | Qt::Key_E);
1632 voxelMenu->addAction("Import Voxels", this, SLOT(importVoxels()), Qt::CTRL | Qt::Key_I);1723 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);
1633 voxelMenu->addAction("Cut Voxels", this, SLOT(cutVoxels()), Qt::CTRL | Qt::Key_X);1725 voxelMenu->addAction("Cut Voxels", this, SLOT(cutVoxels()), Qt::CTRL | Qt::Key_X);
1634 voxelMenu->addAction("Copy Voxels", this, SLOT(copyVoxels()), Qt::CTRL | Qt::Key_C);1726 voxelMenu->addAction("Copy Voxels", this, SLOT(copyVoxels()), Qt::CTRL | Qt::Key_C);
1635 voxelMenu->addAction("Paste Voxels", this, SLOT(pasteVoxels()), Qt::CTRL | Qt::Key_V);1727 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