1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2011, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author: Bernhard Zeisl, (myname.mysurname@inf.ethz.ch)
*/
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/file_io.h>
#include <pcl/console/parse.h>
#include <pcl/console/print.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/point_cloud_handlers.h>
#include <pcl/gpu/kinfu_large_scale/kinfu.h>
#include <pcl/gpu/containers/device_array.h>
#include <pcl/gpu/containers/initialization.h>
#include "openni_capture.h"
#include "tsdf_volume.h"
#include "tsdf_volume.hpp"
namespace pc = pcl::console;
using PointT = pcl::PointXYZ;
using VoxelT = float;
using WeightT = short;
std::string cloud_file = "cloud.pcd";
std::string volume_file = "tsdf_volume.dat";
double min_trunc_dist = 30.0f;
bool quit = false, save = false;
bool extract_cloud_volume = false;
///////////////////////////////////////////////////////////////////////////////////////////////////////
/// DEVICE_VOLUME
/** \brief Class for storing and handling the TSDF Volume on the GPU */
class DeviceVolume
{
public:
using Ptr = shared_ptr<DeviceVolume>;
using ConstPtr = shared_ptr<const DeviceVolume>;
/** \brief Constructor
* param[in] volume_size size of the volume in mm
* param[in] volume_res volume grid resolution (typically device::VOLUME_X x device::VOLUME_Y x device::VOLUME_Z)
*/
DeviceVolume (const Eigen::Vector3f &volume_size, const Eigen::Vector3i &volume_res)
: volume_size_ (volume_size)
{
// initialize GPU
device_volume_.create (volume_res[1] * volume_res[2], volume_res[0]); // (device::VOLUME_Y * device::VOLUME_Z, device::VOLUME_X)
pcl::device::initVolume (device_volume_);
// truncation distance
Eigen::Vector3f voxel_size = volume_size.array() / volume_res.array().cast<float>();
trunc_dist_ = max ((float)min_trunc_dist, 2.1f * max (voxel_size[0], max (voxel_size[1], voxel_size[2])));
};
/** \brief Creates the TSDF volume on the GPU
* param[in] depth depth readings from the sensor
* param[in] intr camera intrinsics
*/
void
createFromDepth (const pcl::device::PtrStepSz<const unsigned short> &depth, const pcl::device::Intr &intr);
/** \brief Downloads the volume from the GPU
* param[out] volume volume structure where the data is written to (size needs to be appropriately set beforehand (is checked))
*/
bool
getVolume (pcl::TSDFVolume<VoxelT, WeightT>::Ptr &volume);
/** \brief Generates and returns a point cloud form the implicit surface in the TSDF volume
* param[out] cloud point cloud containing the surface
*/
bool
getCloud (pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud);
private:
template<class D, class Matx> D&
device_cast (Matx& matx)
{
return (*reinterpret_cast<D*>(matx.data ()));
};
pcl::gpu::DeviceArray2D<int> device_volume_;
Eigen::Vector3f volume_size_;
float trunc_dist_;
};
void
DeviceVolume::createFromDepth (const pcl::device::PtrStepSz<const unsigned short> &depth, const pcl::device::Intr &intr)
{
using namespace pcl;
using Matrix3frm = Eigen::Matrix<float, 3, 3, Eigen::RowMajor>;
const int rows = 480;
const int cols = 640;
// scale depth values
gpu::DeviceArray2D<float> device_depth_scaled;
device_depth_scaled.create (rows, cols);
// upload depth map on GPU
pcl::gpu::KinfuTracker::DepthMap device_depth;
device_depth.upload (depth.data, depth.step, depth.rows, depth.cols);
// initial camera rotation and translation
Matrix3frm init_Rcam = Eigen::Matrix3f::Identity ();
Eigen::Vector3f init_tcam = volume_size_ * 0.5f - Eigen::Vector3f (0, 0, volume_size_(2)/2 * 1.2f);
Matrix3frm init_Rcam_inv = init_Rcam.inverse ();
device::Mat33& device_Rcam_inv = device_cast<device::Mat33> (init_Rcam_inv);
float3& device_tcam = device_cast<float3> (init_tcam);
// integrate depth values into volume
float3 device_volume_size = device_cast<float3> (volume_size_);
device::integrateTsdfVolume (device_depth, intr, device_volume_size, device_Rcam_inv, device_tcam, trunc_dist_,
device_volume_, device_depth_scaled);
}
bool
DeviceVolume::getVolume (pcl::TSDFVolume<VoxelT, WeightT>::Ptr &volume)
{
int volume_size = device_volume_.rows() * device_volume_.cols();
if ((std::size_t)volume_size != volume->size())
{
pc::print_error ("Device volume size (%d) and tsdf volume size (%d) don't match. ABORTING!\n", volume_size, volume->size());
return false;
}
std::vector<VoxelT>& volume_vec = volume->volumeWriteable();
std::vector<WeightT>& weights_vec = volume->weightsWriteable();
device_volume_.download (&volume_vec[0], device_volume_.cols() * sizeof(int));
#pragma omp parallel for \
default(none) \
shared(volume, volume_vec, weights_vec)
for(int i = 0; i < (int) volume->size(); ++i)
{
short2 *elem = (short2*)&volume_vec[i];
volume_vec[i] = (float)(elem->x)/pcl::device::DIVISOR;
weights_vec[i] = (short)(elem->y);
}
return true;
}
bool
DeviceVolume::getCloud (pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud)
{
const int DEFAULT_VOLUME_CLOUD_BUFFER_SIZE = 10 * 1000 * 1000;
// point buffer on the device
pcl::gpu::DeviceArray<pcl::PointXYZ> device_cloud_buffer (DEFAULT_VOLUME_CLOUD_BUFFER_SIZE);
// do the extraction
float3 device_volume_size = device_cast<float3> (volume_size_);
/*size_t size =*/ pcl::device::extractCloud (device_volume_, device_volume_size, device_cloud_buffer);
// write into point cloud structure
device_cloud_buffer.download (cloud->points);
cloud->width = cloud->size ();
cloud->height = 1;
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
/// HELPER FUNCTIONS
/** \brief Converts depth and RGB sensor readings into a point cloud
* param[in] depth depth data from sensor
* param[in] rgb24 color data from sensor
* param[in] intr camera intrinsics
* param[out] cloud the generated point cloud
* \note: position in mm is converted to m
* \note: RGB reading not working!
*/
//TODO implement correct color reading (how does rgb24 look like?)
bool
convertDepthRGBToCloud (const pcl::device::PtrStepSz<const unsigned short> &depth, const pcl::device::PtrStepSz<const pcl::gpu::KinfuTracker::PixelRGB> &rgb24, const pcl::device::Intr &intr,
pcl::PointCloud<PointT>::Ptr &cloud)
{
// resize point cloud if it doesn't fit
if (depth.rows != (int)cloud->height || depth.cols != (int)cloud->width)
cloud = pcl::PointCloud<PointT>::Ptr (new pcl::PointCloud<PointT> (depth.cols, depth.rows));
// std::cout << "step = " << rgb24.step << std::endl;
// std::cout << "elem size = " << rgb24.elem_size << std::endl;
// iterate over all depth and rgb values
for (int y = 0; y < depth.rows; ++y)
{
// get pointers to the values in one row
const unsigned short *depth_row_ptr = depth.ptr(y);
// const pcl::gpu::KinfuTracker::RGB *rgb24_row_ptr = rgb24.ptr(y);
// const char* rgb24_row_ptr = (const char*) rgb24.ptr(y);
// iterate over row and store values
for (int x = 0; x < depth.cols; ++x)
{
float u = (x - intr.cx) / intr.fx;
float v = (y - intr.cy) / intr.fy;
PointT &point = cloud->at (x, y);
point.z = depth_row_ptr[x] / 1000.0f;
point.x = u * point.z;
point.y = v * point.z;
/* std::uint8_t r = *(rgb24_row_ptr + 0);
std::uint8_t g = *(rgb24_row_ptr + 1);
std::uint8_t b = *(rgb24_row_ptr + 2);
std::uint32_t rgb = ((std::uint32_t)r << 16 | (std::uint32_t)g << 8 | (std::uint32_t)b);
point.rgb = *reinterpret_cast<float*>(&rgb);
point.r = *((const char*)rgb24.data + y*rgb24.step + x*rgb24.elem_size);
point.g = *((const char*)rgb24.data + y*rgb24.step + x*rgb24.elem_size + 1);
point.b = *((const char*)rgb24.data + y*rgb24.step + x*rgb24.elem_size + 2);
*/
}
}
cloud->is_dense = false;
return true;
}
/** \brief Captures data from a sensor and generates a point cloud from it
* param[in] capture capturing device object
* param[out] depth the depth reading
* param[out] rgb24 the color reading
* param[out] intr camera intrinsics for this reading
* param[out] cloud point cloud generated from the readings
*/
bool
captureCloud (pcl::gpu::CaptureOpenNI &capture,
pcl::device::PtrStepSz<const unsigned short> &depth, pcl::device::PtrStepSz<const pcl::gpu::KinfuTracker::PixelRGB> &rgb24,
pcl::device::Intr &intr, pcl::PointCloud<PointT>::Ptr &cloud)
{
// capture frame
if (!capture.grab (depth, rgb24))
{
pc::print_error ("Can't capture via sensor.\n");
return false;
}
// get intrinsics from capture
float f = capture.depth_focal_length_VGA;
intr = pcl::device::Intr (f, f, depth.cols/2, depth.rows/2);
// generate point cloud
cloud = pcl::PointCloud<PointT>::Ptr (new pcl::PointCloud<PointT> (depth.cols, depth.rows));
if (!convertDepthRGBToCloud (depth, rgb24, intr, cloud))
{
pc::print_error ("Conversion depth --> cloud was not successful!\n");
return false;
}
return true;
}
/** \brief callback function for the PCLvisualizer */
void
keyboard_callback (const pcl::visualization::KeyboardEvent &event, void *cookie)
{
if (event.keyDown())
{
switch (event.getKeyCode())
{
case 27:
case (int)'q': case (int)'Q':
case (int)'e': case (int)'E':
std::cout << "Exiting program" << std::endl;
quit = true;
break;
case (int)'s': case (int)'S':
std::cout << "Saving volume and cloud" << std::endl;
save = true;
break;
default:
break;
}
}
}
/** \brief prints usage information for the executable */
void
printUsage (char* argv[])
{
pc::print_error ("usage: %s <options>\n\n", argv[0]);
pc::print_info (" where options are:\n");
pc::print_info (" -cf = cloud filename (default: ");
pc::print_value ("%s)", cloud_file.c_str()); pc::print_info (")\n");
pc::print_info (" -vf = volume filename (default: ");
pc::print_value ("%s", volume_file.c_str()); pc::print_info (")\n");
pc::print_info (" -ec = extract cloud from generated volume (default: ");
pc::print_value ("0 (false)", volume_file.c_str()); pc::print_info (")\n");
pc::print_info (" -td = minimal truncation distance (default: ");
pc::print_value ("%f", min_trunc_dist); pc::print_info (")\n");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
/// MAIN
/** \brief main loop for the program */
int
main (int argc, char* argv[])
{
pc::print_info ("Records a 2.5 point cloud (organized = depth map) as TSDF volume. For more information, use: %s -h\n", argv[0]);
/***
* PARSE COMMAND LINE
*/
// check for help
if (pc::find_argument (argc, argv, "-h") > 0)
{
pc::print_warn ("Showing help: \n\n");
printUsage (argv);
return (EXIT_SUCCESS);
}
// parse input cloud file
pc::parse_argument (argc, argv, "-cf", cloud_file);
// parse output volume file
pc::parse_argument (argc, argv, "-vf", volume_file);
// parse options to extract and save cloud from volume
pc::parse_argument (argc, argv, "-ec", extract_cloud_volume);
// parse minimual truncation distance
pc::parse_argument (argc, argv, "-td", min_trunc_dist);
/***
* SET UP AND VISUALIZATION
*/
pc::print_info (" -------------------- START OF ALGORITHM --------------------\n");
pcl::gpu::setDevice (0);
pcl::gpu::printShortCudaDeviceInfo (0);
pcl::gpu::CaptureOpenNI capture (0); // first OpenNI device;
pcl::device::PtrStepSz<const unsigned short> depth;
pcl::device::PtrStepSz<const pcl::gpu::KinfuTracker::PixelRGB> rgb24;
pcl::PointCloud<PointT>::Ptr cloud; // (new pcl::PointCloud<PointT>);
pcl::device::Intr intr;
// capture first frame
if (!captureCloud (capture, depth, rgb24, intr, cloud))
return EXIT_FAILURE;
// start visualizer
pcl::visualization::PCLVisualizer visualizer;
// pcl::visualization::PointCloudColorHandlerRGBField<PointT> color_handler (cloud);
// pcl::visualization::PointCloudColorHandlerCustom<PointT> color_handler (cloud, 0.5, 0.5, 0.5);
visualizer.addPointCloud<PointT> (cloud); //, color_handler, "cloud");
visualizer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1);
visualizer.addCoordinateSystem (1);
visualizer.initCameraParameters();
visualizer.registerKeyboardCallback (keyboard_callback);
visualizer.spinOnce();
/***
* CAPTURING DATA AND GENERATING CLOUD
*/
pc::print_highlight ("Capturing data ... \n");
while (!quit && !save)
{
// capture data and convert to point cloud
if (!captureCloud (capture, depth, rgb24, intr, cloud))
return EXIT_FAILURE;
// update visualization
visualizer.updatePointCloud<PointT> (cloud); //, color_handler, "cloud");
visualizer.spinOnce();
}
if (quit)
return (EXIT_SUCCESS);
/***
* GENERATE VOLUME
*/
// create volume object
pcl::TSDFVolume<VoxelT, WeightT>::Ptr volume (new pcl::TSDFVolume<VoxelT, WeightT>);
Eigen::Vector3i resolution (pcl::device::VOLUME_X, pcl::device::VOLUME_Y, pcl::device::VOLUME_Z);
Eigen::Vector3f volume_size = Eigen::Vector3f::Constant (3000);
volume->resize (resolution, volume_size);
DeviceVolume::Ptr device_volume (new DeviceVolume (volume->volumeSize(), volume->gridResolution()));
// integrate depth in device volume
pc::print_highlight ("Converting depth map to volume ... "); std::cout << std::flush;
device_volume->createFromDepth (depth, intr);
// get volume from device
if (!device_volume->getVolume (volume))
{
pc::print_error ("Coudln't get volume from device!\n");
return (EXIT_FAILURE);
}
pc::print_info ("done [%d voxels]\n", volume->size());
// generating TSDF cloud
pc::print_highlight ("Generating tsdf volume cloud ... "); std::cout << std::flush;
pcl::PointCloud<pcl::PointXYZI>::Ptr tsdf_cloud (new pcl::PointCloud<pcl::PointXYZI>);
volume->convertToTsdfCloud (tsdf_cloud);
pc::print_info ("done [%d points]\n", tsdf_cloud->size());
// get cloud from volume
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_volume (new pcl::PointCloud<pcl::PointXYZ>);
if (extract_cloud_volume)
{
pc::print_highlight ("Generating cloud from volume ... "); std::cout << std::flush;
if (!device_volume->getCloud (cloud_volume))
{
pc::print_error ("Cloudn't get cloud from device volume!\n");
return (EXIT_FAILURE);
}
pc::print_info ("done [%d points]\n", cloud_volume->size());
}
/***
* STORE RESULTS
*/
pc::print_highlight ("Storing results:\n");
// point cloud
pc::print_info ("Saving captured cloud to "); pc::print_value ("%s", cloud_file.c_str()); pc::print_info (" ... ");
if (pcl::io::savePCDFile (cloud_file, *cloud, true) < 0)
{
std::cout << std::endl;
pc::print_error ("Cloudn't save the point cloud to file %s.\n", cloud_file.c_str());
}
else
pc::print_info ("done [%d points].\n", cloud->size());
// volume
if (!volume->save (volume_file, true))
pc::print_error ("Cloudn't save the volume to file %s.\n", volume_file.c_str());
// TSDF point cloud
std::string tsdf_cloud_file (pcl::getFilenameWithoutExtension(volume_file) + "_cloud.pcd");
pc::print_info ("Saving volume cloud to "); pc::print_value ("%s", tsdf_cloud_file.c_str()); pc::print_info (" ... ");
if (pcl::io::savePCDFile (tsdf_cloud_file, *tsdf_cloud, true) < 0)
{
std::cout << std::endl;
pc::print_error ("Cloudn't save the volume point cloud to file %s.\n", tsdf_cloud_file.c_str());
}
else
pc::print_info ("done [%d points].\n", tsdf_cloud->size());
// point cloud from volume
if (extract_cloud_volume)
{
std::string cloud_volume_file (pcl::getFilenameWithoutExtension(cloud_file) + "_from_volume.pcd");
pc::print_info ("Saving cloud from volume to "); pc::print_value ("%s", cloud_volume_file.c_str()); pc::print_info (" ... ");
if (pcl::io::savePCDFile (cloud_volume_file, *cloud_volume, true) < 0)
{
std::cout << std::endl;
pc::print_error ("Cloudn't save the point cloud to file %s.\n", cloud_volume_file.c_str());
}
else
pc::print_info ("done [%d points].\n", cloud_volume->size());
}
pc::print_info (" -------------------- END OF ALGORITHM --------------------\n");
}
|