LVRCamDataItem.cpp
Go to the documentation of this file.
1 #include "LVRCamDataItem.hpp"
2 #include "LVRModelItem.hpp"
3 #include "LVRItemTypes.hpp"
4 #include "LVRScanDataItem.hpp"
5 
6 #include <vtkVersion.h>
7 #include <vtkFrustumSource.h>
8 #include <vtkPolyData.h>
9 #include <vtkPointData.h>
10 #include <vtkSmartPointer.h>
11 #include <vtkCamera.h>
12 #include <vtkPlanes.h>
13 #include <vtkMapper.h>
14 #include <vtkActor.h>
15 #include <vtkRenderWindow.h>
16 #include <vtkRenderer.h>
17 #include <vtkRenderWindowInteractor.h>
18 #include <vtkPolyDataMapper.h>
19 #include <vtkTriangle.h>
20 #include <vtkCellArray.h>
21 #include <vtkMatrix4x4.h>
22 #include <vtkTransform.h>
23 
25 
26 namespace lvr2
27 {
28 
30  ScanImage& data,
31  std::shared_ptr<ScanDataManager> sdm,
32  size_t cam_id,
33  vtkSmartPointer<vtkRenderer> renderer,
34  QString name,
35  QTreeWidgetItem *parent
36 )
37 : QTreeWidgetItem(parent, LVRCamDataItemType)
38 {
39  m_pItem = nullptr;
40  m_cvItem = nullptr;
41  m_data = data;
42  m_name = name;
43  m_sdm = sdm;
44  m_cam_id = cam_id;
45  m_renderer = renderer;
46 
47  // m_matrix = m_data.m_extrinsics;
48 
49  // change this to not inverse
50  bool dummy;
51  m_matrix = m_data.extrinsics;//.inv(dummy);
52 
53  // set Transform from
55 
56  // TODO: How do we get the intrincs into the item
57  // using the new structure??
58  m_intrinsics = Intrinsicsd::Identity();
59 
60 
61  // init pose
62  double pose[6];
63  extrinsicsToEuler<double>(m_data.extrinsics, pose);
64 
65  m_pose.x = pose[0];
66  m_pose.y = pose[1];
67  m_pose.z = pose[2];
68  m_pose.r = pose[3] * 57.295779513;
69  m_pose.t = pose[4] * 57.295779513;
70  m_pose.p = pose[5] * 57.295779513;
71 
73 
74  m_cvItem = new LVRCvImageItem(sdm, renderer, "Image", this);
75 
77 
79 
80  // load data
81  reload(renderer);
82 
83  setText(0, m_name);
84  setCheckState(0, Qt::Unchecked );
85 }
86 
87 void LVRCamDataItem::reload(vtkSmartPointer<vtkRenderer> renderer)
88 {
89 
90 }
91 
93 {
94  if(checkState(0) && visible)
95  {
96  std::cout << "ADD ACTOR" << std::endl;
97  m_renderer->AddActor(m_frustrum_actor);
98  } else {
99  m_renderer->RemoveActor(m_frustrum_actor);
100  }
101 }
102 
104 {
105  Transformd ret = m_matrix;
106  QTreeWidgetItem* parent_it = parent();
107 
108  while(parent_it != NULL)
109  {
110 
111  auto transform_obj = dynamic_cast< Transformable* >(parent_it);
112  if(transform_obj)
113  {
114  ret = ret * transform_obj->getTransform();
115  }
116  parent_it = parent_it->parent();
117  }
118 
119  return ret;
120 }
121 
123 {
124  auto cam = m_renderer->GetActiveCamera();
125 
126  double x,y,z;
127  cam->GetPosition(x,y,z);
128 
130 
131  // T.transpose();
132 
133 
134  Vector3d cam_origin(0.0, 0.0, -1.0);
135  Vector3d view_up(1.0, 0.0, 0.0);
136  Vector3d focal_point(0.0, 0.0, 0.0);
137 
138 
139  cam_origin = lvr2::multiply(T, cam_origin);
140  view_up = lvr2::multiply(T, view_up);
141  focal_point = lvr2::multiply(T, focal_point);
142 
143  cam->SetPosition(cam_origin.x(), cam_origin.y(), cam_origin.z());
144  cam->SetFocalPoint(focal_point.x(), focal_point.y(), focal_point.z());
145  cam->SetViewUp(view_up.x(), view_up.y(), view_up.z());
146 
147  // TODO: set intrinsics
148 
149 }
150 
151 std::vector<Vector3d > LVRCamDataItem::genFrustrumLVR(float scale)
152 {
153  Transformd T = getGlobalTransform().transpose();
154 
155 
156 
157  std::vector<Vector3d > cv_pixels;
158 
159  // TODO change this. get size of image
160 
161  Intrinsicsd intrisics_corrected = Intrinsicsd::Identity();
162 
163  intrisics_corrected(0,0) = m_intrinsics(1,1);
164  intrisics_corrected(1,1) = m_intrinsics(0,0);
165  intrisics_corrected(0,2) = m_intrinsics(1,2);
166  intrisics_corrected(1,2) = m_intrinsics(0,2);
167 
168  int u_max = intrisics_corrected(0, 2) * 2;
169  int v_max = intrisics_corrected(1, 2) * 2;
170 
171  Intrinsicsd cam_mat_inv = intrisics_corrected.inverse();
172 
173 
174  std::cout << "u,v max: "<< u_max << "," << v_max << std::endl;
175  // std::cout << v_max << std::endl;
176 
177  // opencv x,y,z
178  // top left
179  cv_pixels.push_back({0.0, 0.0, 1.0});
180  // bottom left
181  cv_pixels.push_back({0.0, double(v_max), 1.0});
182  // bottem right
183  cv_pixels.push_back({double(u_max), double(v_max), 1.0});
184  // top right
185  cv_pixels.push_back({double(u_max), 0.0, 1.0});
186 
187 
188  // generate frustrum points
189  std::vector<Vector3d > lvr_points;
190 
191 
192  // origin
193  lvr_points.push_back({0.0, 0.0, 0.0});
194 
195  std::cout << "cam space: " << std::endl;
196  for(int i=0; i<cv_pixels.size(); i++)
197  {
198  Vector3d pixel = cv_pixels[i];
199  //Vector3d p = lvr2::multiply(cam_mat_inv, pixel);
200 
201  Vector3d p = cam_mat_inv * pixel;
202 
203  // Vector3d p = pixel;
204 
205 
206  Vector3d tmp = openCvToLvr(p);
207 
208  std::cout << tmp.transpose() << std::endl;
209 
210  tmp *= scale;
211  lvr_points.push_back(tmp);
212  }
213 
214  std::cout << "world space:" << std::endl;
215  // transform frustrum
216  for(int i=0; i<lvr_points.size(); i++)
217  {
218  // lvr_points[i] = lvr2::multiply(T, lvr_points[i]);
219  std::cout << lvr_points[i].transpose() << std::endl;
220  // lvr_points[i] = lvr_points[i];
221  }
222 
223  return lvr_points;
224 }
225 
226 vtkSmartPointer<vtkActor> LVRCamDataItem::genFrustrum(float scale)
227 {
228 
229  std::vector<Vector3d > lvr_points = genFrustrumLVR(scale);
230 
231  // Setup points
232  vtkSmartPointer<vtkPoints> points =
233  vtkSmartPointer<vtkPoints>::New();
234 
235  // convert to vtk
236  points->SetNumberOfPoints(lvr_points.size());
237 
238  for(int i=0; i<lvr_points.size(); i++)
239  {
240  auto p = lvr_points[i];
241  points->SetPoint(i, p.x(), p.y(), p.z());
242  }
243 
244  // // Define some colors
245  unsigned char white[3] = {255, 255, 255}; // origin
246  unsigned char red[3] = {255, 0, 0}; // top left
247  unsigned char green[3] = {0, 255, 0}; // bottom left
248  unsigned char blue[3] = {0, 0, 255}; // bottom right
249  unsigned char yellow[3] = {255, 255, 0}; // top right
250 
251  // // Setup the colors array
252  vtkSmartPointer<vtkUnsignedCharArray> colors =
253  vtkSmartPointer<vtkUnsignedCharArray>::New();
254  colors->SetNumberOfComponents(3);
255  colors->SetNumberOfTuples(lvr_points.size());
256  colors->SetName("Colors");
257 
258 #if VTK_MAJOR_VERSION < 7
259  colors->SetTupleValue(0, white);
260  colors->SetTupleValue(1, red);
261  colors->SetTupleValue(2, green);
262  colors->SetTupleValue(3, blue);
263  colors->SetTupleValue(4, yellow);
264 #else
265  colors->SetTypedTuple(0, white); // no idea how the new method is called
266  colors->SetTypedTuple(1, red);
267  colors->SetTypedTuple(2, green);
268  colors->SetTypedTuple(3, blue);
269  colors->SetTypedTuple(4, yellow);
270 #endif
271 
272  // Create a triangle
273  vtkSmartPointer<vtkCellArray> triangles =
274  vtkSmartPointer<vtkCellArray>::New();
275 
276 
277  vtkSmartPointer<vtkTriangle> triangle =
278  vtkSmartPointer<vtkTriangle>::New();
279 
280  // left plane
281 
282  triangle->GetPointIds()->SetId(0, 0);
283  triangle->GetPointIds()->SetId(1, 1);
284  triangle->GetPointIds()->SetId(2, 2);
285 
286  triangles->InsertNextCell(triangle);
287 
288  // bottom plane
289  triangle->GetPointIds()->SetId(0, 0);
290  triangle->GetPointIds()->SetId(1, 2);
291  triangle->GetPointIds()->SetId(2, 3);
292 
293  triangles->InsertNextCell(triangle);
294 
295  // right plane
296  triangle->GetPointIds()->SetId(0, 0);
297  triangle->GetPointIds()->SetId(1, 3);
298  triangle->GetPointIds()->SetId(2, 4);
299 
300  triangles->InsertNextCell(triangle);
301 
302  // top plane
303  triangle->GetPointIds()->SetId(0, 0);
304  triangle->GetPointIds()->SetId(1, 4);
305  triangle->GetPointIds()->SetId(2, 1);
306 
307  triangles->InsertNextCell(triangle);
308 
309  // Create a polydata object and add everything to it
310  vtkSmartPointer<vtkPolyData> polydata =
311  vtkSmartPointer<vtkPolyData>::New();
312  polydata->SetPoints(points);
313  polydata->SetPolys(triangles);
314  polydata->GetPointData()->SetScalars(colors);
315 
316  // // Visualize
317  vtkSmartPointer<vtkPolyDataMapper> mapper =
318  vtkSmartPointer<vtkPolyDataMapper>::New();
319 #if VTK_MAJOR_VERSION <= 5
320  mapper->SetInputConnection(polydata->GetProducerPort());
321 #else
322  mapper->SetInputData(polydata);
323 #endif
324  vtkSmartPointer<vtkActor> actor =
325  vtkSmartPointer<vtkActor>::New();
326  actor->SetMapper(mapper);
327  return actor;
328 }
329 
331 {
332  // we don't want to do delete m_bbItem, m_pItem and m_pcItem here
333  // because QTreeWidgetItem deletes its childs automatically in its destructor.
334 }
335 
336 } // namespace lvr2
std::vector< Vector3d > genFrustrumLVR(float scale=1.0)
void reload(vtkSmartPointer< vtkRenderer > renderer)
vtkSmartPointer< vtkActor > m_frustrum_actor
vtkSmartPointer< vtkActor > genFrustrum(float scale=1.0)
Eigen::Vector3d Vector3d
Eigen 3D vector, double precision.
Main class for conversion of LVR model instances to vtk actors. This class parses the internal model ...
Transform< double > Transformd
4x4 double precision transformation matrix
Definition: MatrixTypes.hpp:71
void setTransform(Eigen::Matrix< T, 4, 4 > transform)
Interface for transformable objects.
boost::shared_ptr< LVRModelBridge > ModelBridgePtr
Intrinsics< double > Intrinsicsd
4x4 extrinsic calibration (double precision)
Vector3< T > multiply(const Transform< T > &transform, const Vector3< T > &p)
SharedPointer p
static Vector3< T > openCvToLvr(const Vector3< T > &in)
OpenCV to Lvr coordinate change: Point.
void setVisibility(bool visible)
std::shared_ptr< ScanDataManager > m_sdm
void setPose(const Pose &pose)
Definition: LVRPoseItem.cpp:69
std::shared_ptr< Model > ModelPtr
Definition: Model.hpp:80
Eigen::Matrix< T, 4, 4 > getTransform()
Transformd getGlobalTransform()
Get Transformation from Camera frame to Global. QTree used as TF tree, lvr2::Transformable types are ...
LVRCvImageItem * m_cvItem
vtkSmartPointer< vtkRenderer > m_renderer
LVRCamDataItem(ScanImage &data, std::shared_ptr< ScanDataManager > sdm, size_t cam_id, vtkSmartPointer< vtkRenderer > renderer, QString name="", QTreeWidgetItem *parent=NULL)
#define NULL
Definition: mydefs.hpp:141
Extrinsicsd extrinsics
Extrinsics.
Definition: ScanTypes.hpp:113


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 Mon Feb 28 2022 22:46:08