Skip to content

Instantly share code, notes, and snippets.

@vicrucann
Last active September 15, 2017 18:06
Show Gist options
  • Save vicrucann/0c69217328a227eb717c3048ce4b817a to your computer and use it in GitHub Desktop.
Save vicrucann/0c69217328a227eb717c3048ce4b817a to your computer and use it in GitHub Desktop.
QVTKWidget and depth camera interface running side by side
#include <pcl/PCLGrabber.h>
#include <thread>
#include <mutex>
#include <boost/shared_ptr.hpp>
#include <QApplication>
#include <QMainWindow>
#include <QThread>
#include <QVTKWidget.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#define CLOUD_NAME "cloud"
boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZI>> _cloud;
Voxel::Ptr<pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZI>> _handler;
Voxel::Ptr<pcl::visualization::PCLVisualizer> _viewer;
Voxel::Ptr<pcl::Grabber> interface;
std::thread _renderThread;
Voxel::Mutex _cloudUpdateMutex;
QVTKWidget* _widget = nullptr;
void _cloudRenderCallback(const pcl::PointCloud<pcl::PointXYZI> &call_cloud)
{
_cloud = boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZI>>(new pcl::PointCloud<pcl::PointXYZI>(call_cloud));
_handler = Voxel::Ptr<pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZI>>(new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZI>(_cloud, "intensity"));
}
int main(int argc, char *argv[])
{
int exit_code = 0;
Voxel::CameraSystem sys;
Voxel::DepthCameraPtr _depthCamera;
const Voxel::Vector<Voxel::DevicePtr>& devices = sys.scan();
_depthCamera = sys.connect(devices[0]);
QApplication qapp(argc, argv);
QMainWindow window;
_widget = new QVTKWidget();
_viewer = Voxel::Ptr<pcl::visualization::PCLVisualizer>(new pcl::visualization::PCLVisualizer("PCL Voxel Viewer", false));
_viewer->setBackgroundColor(0.3, 0.5, 0.2);
_viewer->addCoordinateSystem(1.0);
vtkSmartPointer<vtkRenderWindow> windowPtr = _viewer->getRenderWindow();
_widget->SetRenderWindow(windowPtr.Get());
window.setCentralWidget(_widget);
window.show();
interface = Voxel::Ptr<pcl::Grabber>(new Voxel::PCLGrabber(*_depthCamera));
boost::function<void (const pcl::PointCloud<pcl::PointXYZI> &)> f = boost::bind(&_cloudRenderCallback, _1);
interface->registerCallback(f);
interface->start();
exit_code = qapp.exec();
interface->stop();
return exit_code;
}
void testDepthCamera()
{
Voxel::CameraSystem sys;
Voxel::DepthCameraPtr depthCamera;
const Voxel::Vector<Voxel::DevicePtr>& devices = sys.scan();
depthCamera = sys.connect(devices[0]); // Connect to first available device
depthCamera->registerCallback(Voxel::DepthCamera::FRAME_RAW_FRAME_PROCESSED,
[&](Voxel::DepthCamera& dc,
const Voxel::Frame& frame,
Voxel::DepthCamera::FrameType c)
{
const Voxel::ToFRawFrame *d = dynamic_cast<const Voxel::ToFRawFrame*>(&frame);
if (!d)
{
std::cerr << "Null frame captured? or not of type ToFRawFrame" << std::endl;
return;
}
std::cout << "Capture frame " << d->id << "@" << d->timestamp << std::endl;
dc.stop();
std::cout << "Camera stopped\n";
});
if(depthCamera->start())
{
Voxel::FrameRate r;
if(depthCamera->getFrameRate(r))
std::cout << "Capturing at a frame rate of " << r.getFrameRate() << " fps" << std::endl;
depthCamera->wait();
}
}
void testViewer()
{
// connect to camera
Voxel::CameraSystem sys;
Voxel::DepthCameraPtr depthCamera;
const Voxel::Vector<Voxel::DevicePtr>& devices = sys.scan();
depthCamera = sys.connect(devices[0]); // Connect to first available device
Voxel::PCLViewer v;
v.setDepthCamera(depthCamera);
v.start();
while(v.isRunning())
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
void testVisualizer()
{
pcl::visualization::PCLVisualizer viewer;
while (!viewer.wasStopped())
{
viewer.spinOnce (100, false);
std::this_thread::sleep_for(std::chrono::microseconds(100000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment