MainWindow.cpp
Go to the documentation of this file.
1 /*
2  * MainWindow.cpp
3  *
4  * Created on: Jan 26, 2016
5  * Author: twiemann
6  */
7 
8 #include "MainWindow.hpp"
9 
10 #include <vtkSmartPointer.h>
11 #include <vtkPolyData.h>
12 #include <vtkCellArray.h>
13 #include <vtkPolyDataMapper.h>
14 #include <vtkPoints.h>
15 #include <vtkActor.h>
16 #include <vtkTriangle.h>
17 #include <vtkProperty.h>
18 #include <vtkImageData.h>
19 #include <vtkTexture.h>
20 #include <vtkFloatArray.h>
21 #include <vtkPointData.h>
22 #include <vtkCellData.h>
23 
24 
25 #include <kfusion/LVRPipeline.hpp>
26 
27  void storePicPose(KinFu& kinfu, Affine3f pose, cv::Mat image)
28  {
29  ImgPose* imgpose = new ImgPose();
30  imgpose->pose = pose;
31  imgpose->image = image;//.clone();
32  //intrinsics wrong? changed rows and cols + /2
33  //kinfu.params().cols/2 - 0.5f
34  //kinfu.params().rows/2 - 0.5f
35  cv::Mat intrinsics = (cv::Mat_<float>(3,3) << kinfu.params()->intr.fx*2, 0, 1280/2-0.5f + 3,
36  0, kinfu.params()->intr.fx*2, 1024/2-0.5f,
37  0, 0, 1);
38  imgpose->intrinsics = intrinsics;
39  kinfu.cyclical().addImgPose(imgpose);
40  }
41 
42 MainWindow::MainWindow(QMainWindow* parent) : QMainWindow(parent)
43 {
44  setupUi(this);
45  setupVTK();
46 
47  // Create Kinfu object
48  KinFuParams* params = KinFuParams::default_params();
49  m_kinfu = KinFu::Ptr( new KinFu(params) );
50 
51  // Setup OpenNI Device
53  string device = "0";
54  if(device.find_first_not_of("0123456789") == std::string::npos)
55  {
56  cuda::setDevice (atoi(device.c_str()));
57  cuda::printShortCudaDeviceInfo (atoi(device.c_str()));
58 
59  if(cuda::checkIfPreFermiGPU(atoi(device.c_str())))
60  {
61  std::cout << std::endl << "Kinfu does not support pre-Fermi GPU architectures, and is not built for them by default. Exiting..." << std::endl;
62  }
63  m_openNISource->open(atoi(device.c_str()));
64  }
65  else
66  {
67  m_openNISource->open(device);
69  }
71 
72  // Generate timer for GPU polling
73  m_timer = new QTimer(this);
74  m_timer->setInterval(0);
75 
76  // Connect signals and slots
77  connect(m_pbStart, SIGNAL(pressed()), m_timer, SLOT(start()));
78  connect(m_timer, SIGNAL(timeout()), this, SLOT(pollGPUData()));
79  connect(m_pbStop, SIGNAL(pressed()), this, SLOT(finalizeMesh()));
80 
81  // Create seperate thread for mesh polling
83  //m_meshThread->start();
84 
85  //connect(m_meshThread, SIGNAL(meshUpdate(vtkActor*)), this, SLOT(updateMesh(vtkActor*)), Qt::DirectConnection);
86 }
87 
88 void MainWindow::updateMesh(vtkActor* actor)
89 {
90  if(m_meshActor)
91  {
92  //m_renderer->RemoveActor(m_meshActor);
93  }
94 
95  m_meshActor = actor;
96  m_renderer->AddActor(actor);
97 
98  m_renderer->ResetCamera();
99  m_renderer->ResetCameraClippingRange();
100  this->qvtkWidget->GetRenderWindow()->Render();
101  this->qvtkWidget->update();
102 
103 
104 
105 // cout << "BINGO" << endl;
106 //
107 // vtkSmartPointer<vtkPolyData> mesh = vtkSmartPointer<vtkPolyData>::New();
108 // vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
109 // vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
110 //
111 // vtkSmartPointer<vtkUnsignedCharArray> scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
112 // scalars->SetNumberOfComponents(3);
113 // scalars->SetName("Colors");
114 //
115 //
116 // for(size_t i = 0; i < m_meshThread->m_vertices.size() / 3; i++)
117 // {
118 // points->InsertNextPoint(
119 // m_meshThread->m_vertices[3 * i] / 3,
120 // m_meshThread->m_vertices[3 * i + 1] / 3,
121 // m_meshThread->m_vertices[3 * i + 2] / 3);
122 //
123 // unsigned char color[3] = {0, 255, 0};
124 // scalars->InsertNextTupleValue(color);
125 // }
126 //
127 // for(size_t i = 0; i < m_meshThread->m_faces.size() / 3; i++)
128 // {
129 // vtkSmartPointer<vtkTriangle> t = vtkSmartPointer<vtkTriangle>::New();
130 // t->GetPointIds()->SetId(0, m_meshThread->m_faces[3 * i]);
131 // t->GetPointIds()->SetId(0, m_meshThread->m_faces[3 * i + 1]);
132 // t->GetPointIds()->SetId(0, m_meshThread->m_faces[3 * i + 2]);
133 // triangles->InsertNextCell(t);
134 // }
135 //
136 // mesh->SetPoints(points);
137 // mesh->SetPolys(triangles);
138 //
139 //
140 // vtkSmartPointer<vtkPolyDataMapper> mesh_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
141 // // mesh_mapper->SetInputData(mesh); VTK 6
142 // mesh_mapper->SetInput(mesh);
143 // m_meshActor = vtkActor::New();
144 // m_meshActor->SetMapper(mesh_mapper);
145 //
146 // m_renderer->AddActor(actor);
147 //
148 // m_renderer->ResetCamera();
149 // m_renderer->ResetCameraClippingRange();
150 // this->qvtkWidget->GetRenderWindow()->Render();
151 // this->qvtkWidget->update();
152 }
153 
155 {
156  m_kinfu->performLastScan();
157 }
158 
160 {
161  // No mesh received yet
162  m_meshActor = 0;
163 
164  // Grab relevant entities from the qvtk widget
165  m_renderer = vtkSmartPointer<vtkRenderer>::New();
166  vtkSmartPointer<vtkRenderWindow> renderWindow = this->qvtkWidget->GetRenderWindow();
167 
168  m_renderWindowInteractor = this->qvtkWidget->GetInteractor();
169  m_renderWindowInteractor->Initialize();
170 
171  // Finalize QVTK setup by adding the renderer to the window
172  renderWindow->AddRenderer(m_renderer);
173  m_renderer->GradientBackgroundOn();
174  m_renderer->SetBackground(0.0, 0.0, 0.8);
175  m_renderer->SetBackground2(1.0, 1.0, 1.0);
176  m_renderer->TwoSidedLightingOn();
177 
178  m_axes = vtkSmartPointer<vtkAxesActor>::New();
179 
180  m_axesWidget = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
181  m_axesWidget->SetOutlineColor( 0.9300, 0.5700, 0.1300 );
182  m_axesWidget->SetOrientationMarker( m_axes );
183  m_axesWidget->SetInteractor( m_renderer->GetRenderWindow()->GetInteractor() );
184  m_axesWidget->SetDefaultRenderer(m_renderer);
185  m_axesWidget->SetViewport( 0.0, 0.0, 0.3, 0.3 );
186  m_axesWidget->SetEnabled( 1 );
187  m_axesWidget->InteractiveOff();
188 
189 }
190 
192 {
193  KinFu& kinfu = *m_kinfu;
194  cv::Mat depth, image, image_copy;
195  static int has_image = 0;
196  static int image_count = 0;
197  static int frame_count = 0;
198 
199  static std::vector<Affine3f> posen;
200  static std::vector<cv::Mat> rvecs;
201 
202  static Affine3f best_pose;
203  static cv::Mat best_rvec,best_image;
204  static float best_dist=0.0;
205 
206  if(!(m_kinfu->hasShifted() && m_kinfu->isLastScan()))
207  {
208  int has_frame = m_openNISource->grab(depth, image);
209  cv::flip(depth, depth, 1);
210  cv::flip(image, image, 1);
211 
212  if (has_frame == 0)
213  {
214  std::cout << "Can't grab" << std::endl;
215  return;
216  }
217 
218  // Check if oni file ended
219  if (has_frame == 2)
220  {
221  m_kinfu->performLastScan();
222  }
223  m_depth_device.upload(depth.data, depth.step, depth.rows, depth.cols);
224  has_image = kinfu(m_depth_device);
225  if(has_image) frame_count++;
226  }
227 
228 
229  if (!(m_kinfu->hasShifted() && m_kinfu->isLastScan()) && has_image)
230  {
231  //biggest rvec difference -> new pic
232  //
233  double ref_timer = cv::getTickCount();
234 
235  if(rvecs.size()<1){
236  image.copyTo(image_copy);
237 
238  //buffer of all imgposes
239  rvecs.push_back(cv::Mat(kinfu.getCameraPose().rvec()));
240  posen.push_back(kinfu.getCameraPose());
241 
242  //storePicPose(kinfu, image_copy);
243  //extractImage(kinfu, image_copy);
244  }
245  else
246  {
247  float dist = 0.0;
248  cv::Mat mom_rvec(kinfu.getCameraPose().rvec());
249  for(size_t z=0;z<rvecs.size();z++){
250  dist += norm(mom_rvec-rvecs[z]);
251  }
252  if(dist > best_dist){
253  best_dist = dist;
254  //mom_rvec.copyTo(best_rvec);
255  //image.copyTo(best_image);
256  best_rvec = mom_rvec.clone();
257  best_image = image.clone();
258  best_pose = kinfu.getCameraPose();
259  //std::cout << "better image found, sum rvec distances: " << best_dist << std::endl;
260  }
261  //if(time - 3.0 > 0)
262  if(true && (frame_count % 7 == 0))
263  {
264  cout <<"STORE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
265 
266  rvecs.push_back(best_rvec);
267  posen.push_back(best_pose);
268 
269  storePicPose(*m_kinfu, best_pose, best_image);
270  //extractImage(kinfu, best_image);
271  sample_poses_.push_back(m_kinfu->getCameraPose());
272  std::cout << "image taken "<< image_count++ << ", time: "<< time << std::endl;
273 
274 
275  }
276  }
277  }
278 
279 
280 
281 
282  const int mode = 4;
283 
284  // Raycast image and download from device
285  m_kinfu->renderImage(m_viewImage, mode);
286  m_deviceImg.create(m_viewImage.rows(), m_viewImage.cols(), CV_8UC4);
287  m_viewImage.download(m_deviceImg.ptr<void>(), m_deviceImg.step);
288 
289  // Convert cv mat to pixmap and render into label
290  m_displayRaycastLabel->setPixmap(
291  QPixmap::fromImage(
292  QImage((unsigned char*) m_deviceImg.data,
293  m_deviceImg.cols,
294  m_deviceImg.rows,
295  QImage::Format_RGB32)));
296 
297  m_displayImageLabel->setPixmap(
298  QPixmap::fromImage(
299  QImage((unsigned char*) image.data,
300  image.cols,
301  image.rows,
302  QImage::Format_RGB888).rgbSwapped()));
303 
304 
305 }
306 
307 
308 
309 
310 
312 {
313  if(m_timer)
314  {
315  delete m_timer;
316  }
317 
318  if(m_openNISource)
319  {
320  delete m_openNISource;
321  }
322 
323  m_kinfu.release();
324 
325  m_meshThread->quit();
326  m_meshThread->wait();
327  delete m_meshThread;
328 }
329 
kfusion::KinFu::getCameraPose
Affine3f getCameraPose(int time=-1) const
Definition: kinfu.cpp:117
kfusion::cuda::setDevice
KF_EXPORTS void setDevice(int device)
kfusion::cuda::checkIfPreFermiGPU
KF_EXPORTS bool checkIfPreFermiGPU(int device)
MainWindow.hpp
MainWindow::pollGPUData
void pollGPUData()
Definition: MainWindow.cpp:191
kfusion::OpenNISource::setRegistration
bool setRegistration(bool value=false)
Definition: capture.cpp:308
MainWindow::m_openNISource
OpenNISource * m_openNISource
Definition: MainWindow.hpp:51
MainWindow::finalizeMesh
void finalizeMesh()
Definition: MainWindow.cpp:154
kfusion::OpenNISource
Definition: capture.hpp:10
kfusion::OpenNISource::triggerPause
void triggerPause()
Definition: capture.cpp:164
LVRPipeline.hpp
kfusion::Affine3f
cv::Affine3f Affine3f
Definition: types.hpp:18
MainWindow::~MainWindow
virtual ~MainWindow()
Definition: MainWindow.cpp:311
MainWindow::m_timer
QTimer * m_timer
Definition: MainWindow.hpp:53
MainWindow::setupVTK
void setupVTK()
Definition: MainWindow.cpp:159
MainWindow::updateMesh
void updateMesh(vtkActor *)
Definition: MainWindow.cpp:88
kfusion::KinFuParams::intr
Intr intr
Definition: types.hpp:134
MainWindow::m_meshThread
MeshUpdateThread * m_meshThread
Definition: MainWindow.hpp:64
MainWindow::m_kinfu
kfusion::KinFu::Ptr m_kinfu
Definition: MainWindow.hpp:52
kfusion::cuda::CyclicalBuffer::addImgPose
void addImgPose(ImgPose *imgPose)
Definition: cyclical_buffer.h:232
kfusion::KinFu::cyclical
const cuda::CyclicalBuffer & cyclical() const
Definition: kinfu.cpp:52
kfusion::KinFu
Definition: kinfu.hpp:22
kfusion::cuda::printShortCudaDeviceInfo
KF_EXPORTS void printShortCudaDeviceInfo(int device)
kfusion::KinFuParams
Definition: types.hpp:127
storePicPose
void storePicPose(KinFu &kinfu, Affine3f pose, cv::Mat image)
Definition: MainWindow.cpp:27
kfusion::ImgPose::pose
Affine3f pose
Definition: types.hpp:32
kfusion::KinFu::params
const KinFuParams & params() const
Definition: kinfu.cpp:35
MeshUpdateThread
Definition: MeshUpdateThread.hpp:17
kfusion::KinFu::Ptr
cv::Ptr< KinFu > Ptr
Definition: kinfu.hpp:25
kfusion::ImgPose::image
cv::Mat image
Definition: types.hpp:31
MainWindow::MainWindow
MainWindow(QMainWindow *parent=0)
Definition: MainWindow.cpp:42
kfusion::device::norm
__kf_device__ float norm(const float3 &v)
Definition: temp_utils.hpp:87
kfusion::ImgPose
Definition: types.hpp:29
kfusion::ImgPose::intrinsics
cv::Mat intrinsics
Definition: types.hpp:33
kfusion::Intr::fx
float fx
Definition: types.hpp:22
kfusion::OpenNISource::open
void open(int device)
Definition: capture.cpp:45


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:24