From 892cc01dd95652b014d64c40815367639f2cb342 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sun, 23 Feb 2014 13:18:36 -0800 Subject: [PATCH] added Voxels.getVoxelAt() to inspect local tree --- examples/rayPickExample.js | 6 +++++ .../voxels/src/VoxelsScriptingInterface.cpp | 24 +++++++++++++++++++ .../voxels/src/VoxelsScriptingInterface.h | 8 +++++++ 3 files changed, 38 insertions(+) diff --git a/examples/rayPickExample.js b/examples/rayPickExample.js index 39dc910e78..9c34cca1de 100644 --- a/examples/rayPickExample.js +++ b/examples/rayPickExample.js @@ -26,6 +26,12 @@ function mouseMoveEvent(event) { print("intersection distance=" + intersection.distance); print("intersection intersection.x/y/z=" + intersection.intersection.x + ", " + intersection.intersection.y + ", " + intersection.intersection.z); + + // also test the getVoxelAt() api which should find and return same voxel + + var voxelAt = Voxels.getVoxelAt(intersection.voxel.x, intersection.voxel.y, intersection.voxel.z, intersection.voxel.s); + print("voxelAt.x/y/z/s=" + voxelAt.x + ", " + voxelAt.y + ", " + voxelAt.z + ": " + voxelAt.s); + print("voxelAt.red/green/blue=" + voxelAt.red + ", " + voxelAt.green + ", " + voxelAt.blue); } } diff --git a/libraries/voxels/src/VoxelsScriptingInterface.cpp b/libraries/voxels/src/VoxelsScriptingInterface.cpp index 719df8dccf..1a0a234e0a 100644 --- a/libraries/voxels/src/VoxelsScriptingInterface.cpp +++ b/libraries/voxels/src/VoxelsScriptingInterface.cpp @@ -12,6 +12,30 @@ void VoxelsScriptingInterface::queueVoxelAdd(PacketType addPacketType, VoxelDeta getVoxelPacketSender()->queueVoxelEditMessages(addPacketType, 1, &addVoxelDetails); } +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(_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; +} + void VoxelsScriptingInterface::setVoxelNonDestructive(float x, float y, float z, float scale, uchar red, uchar green, uchar blue) { // setup a VoxelDetail struct with the data diff --git a/libraries/voxels/src/VoxelsScriptingInterface.h b/libraries/voxels/src/VoxelsScriptingInterface.h index 88363bef39..fb0bd6ab03 100644 --- a/libraries/voxels/src/VoxelsScriptingInterface.h +++ b/libraries/voxels/src/VoxelsScriptingInterface.h @@ -30,6 +30,14 @@ public: void setVoxelTree(VoxelTree* tree) { _tree = tree; } public slots: + + /// checks the local voxel tree for a voxel at the specified location and scale + /// \param x the x-coordinate of the voxel (in meter units) + /// \param y the y-coordinate of the voxel (in meter units) + /// \param z the z-coordinate of the voxel (in meter units) + /// \param scale the scale of the voxel (in meter units) + VoxelDetail getVoxelAt(float x, float y, float z, float scale); + /// queues the creation of a voxel which will be sent by calling process on the PacketSender /// \param x the x-coordinate of the voxel (in meter units) /// \param y the y-coordinate of the voxel (in meter units)