LVRLabelInteractor.cpp
Go to the documentation of this file.
1 #include "LVRLabelInteractor.hpp"
2 #include <vtkCoordinate.h>
3 #include <QInputDialog>
4 // Define interaction style
5 //
6 
7 
9 
11 {
12  m_selectedIds = vtkSmartPointer<vtkIdTypeArray>::New();
13  SelectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
14  SelectedActor = vtkSmartPointer<vtkActor>::New();
15  m_labelList << QString("NoLabel");
16  //m_SelectedPoints = vtkSmartPointer<vtkPolyData>::New();
17  SelectedActor->SetMapper(SelectedMapper);
18  colors = {};
19  colors.push_back({255, 255, 102});
20  colors.push_back({127, 0, 255});
21  colors.push_back({255,0,255});
22  colors.push_back({0,255 ,0 });
23  colors.push_back({255, 0, 127});
24  colors.push_back({0,128 ,255});
25  colors.push_back({0,204 ,102});
26  colors.push_back({153, 204, 255});
27  colors.push_back({153, 255, 204});
28  colors.push_back({255, 153, 153});
29  colors.push_back({153, 76, 0});
30  colors.push_back({0, 255, 128});
31 }
33 {
34 
36 }
37 
39 {
40  calculateSelection(false);
41 }
43 {
44  calculateSelection(true);
45 }
46 bool isInside(std::vector<vtkVector2i> polygon, int& pX, int& pY)
47 {
48  int n = polygon.size();
49  if (n < 3) return false;
50 
51  int i, j, c = 0;
52  for(i = 0, j = n -1; i < n ; j = i++)
53  {
54  auto& vert = polygon[j];
55  auto& vert_next = polygon[i];
56 
57  if ( ((vert_next.GetY()>pY) != (vert.GetY()>pY)) &&
58  (pX < (vert.GetX()-vert_next.GetX()) * (pY-vert_next.GetY()) / (vert.GetY()-vert_next.GetY()) + vert_next.GetX()) )
59  {
60  c = !c;
61  }
62  }
63  return c;
64 }
65 
66 
68 {
69 
70  // Forward events
72 
73  if (!m_points)
74  {
75  return;
76  }
77 
78  this->CurrentRenderer->RemoveActor(SelectedActor);
79  SelectedActor = vtkSmartPointer<vtkActor>::New();
80  SelectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
81  SelectedActor->SetMapper(SelectedMapper);
82 
83 
84  vtkPlanes* frustum = static_cast<vtkAreaPicker*>(this->GetInteractor()->GetPicker())->GetFrustum();
85 
86  vtkSmartPointer<vtkExtractGeometry> extractGeometry =
87  vtkSmartPointer<vtkExtractGeometry>::New();
88  extractGeometry->SetImplicitFunction(frustum);
89 #if VTK_MAJOR_VERSION <= 5
90  extractGeometry->SetInput(m_points);
91 #else
92  extractGeometry->SetInputData(m_points);
93 #endif
94  extractGeometry->Update();
95 
96  vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
97  vtkSmartPointer<vtkVertexGlyphFilter>::New();
98  glyphFilter->SetInputConnection(extractGeometry->GetOutputPort());
99  glyphFilter->Update();
100 
101  vtkPolyData* selected = glyphFilter->GetOutput();
102 
103  vtkIdTypeArray* ids = vtkIdTypeArray::SafeDownCast(selected->GetPointData()->GetArray("OriginalIds"));
104  m_selectedIds = vtkIdTypeArray::SafeDownCast(selected->GetPointData()->GetArray("OriginalIds"));
105 
106  //TODO Check if smart Pointer realy necassary
107  vtkSmartPointer<vtkCoordinate> coordinate = vtkSmartPointer<vtkCoordinate>::New();
108  coordinate->SetCoordinateSystemToWorld();
109 
110  std::vector<vtkVector2i> polygonPoints = this->GetPolygonPoints();
111  std::vector<int> selectedPolyPoints;
112  selectedPolyPoints.resize(ids->GetNumberOfTuples());
113 
114  for (vtkIdType i = 0; i < ids->GetNumberOfTuples(); i++)
115  {
116  auto selectedPoint = m_points->GetPoint(ids->GetValue(i));
117  coordinate->SetValue(selectedPoint[0], selectedPoint[1], selectedPoint[2]);
118  int* displayCoord;
119  displayCoord = coordinate->GetComputedViewportValue(this->CurrentRenderer);
120  if (isInside(polygonPoints, displayCoord[0], displayCoord[1]))
121  {
122  selectedPolyPoints.push_back(ids->GetValue(i));
123  }
124  }
125 
126 #if VTK_MAJOR_VERSION <= 5
127  SelectedMapper->SetInput(selected);
128 #else
129  SelectedMapper->SetInputData(selected);
130 #endif
131  SelectedMapper->ScalarVisibilityOff();
132 /*
133  for(vtkIdType i = 0; i < ids->GetNumberOfTuples(); i++)
134  */
135 
136  for(auto selectedPolyPoint : selectedPolyPoints)
137  {
138  m_SelectedPoints[selectedPolyPoint] = select;
139  }
140 
141  auto vertexFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
142  auto foo = vtkSmartPointer<vtkPoints>::New();
143  auto bar = vtkSmartPointer<vtkPolyData>::New();
144  double point[3];
145 
146 
147  for(int i = 0; i < m_SelectedPoints.size(); i++)
148  {
149  if(m_SelectedPoints[i])
150  {
151  m_points->vtkDataSet::GetPoint(i,point);
152  foo->InsertNextPoint(point);
153  }
154  }
155  bar->SetPoints(foo);
156 
157 
158  vertexFilter->SetInputData(bar);
159  vertexFilter->Update();
160 
161  auto polyData = vtkSmartPointer<vtkPolyData>::New();
162  polyData->ShallowCopy(vertexFilter->GetOutput());
163 
164 #if VTK_MAJOR_VERSION <= 5
165  SelectedMapper->SetInput(polyData);
166 #else
167  SelectedMapper->SetInputData(polyData);
168 #endif
169  SelectedMapper->ScalarVisibilityOff();
170 
171 
172  SelectedActor->GetProperty()->SetColor(1.0, 0.0, 0.0); //(R,G,B)
173  //SelectedActor->GetProperty()->SetPointSize(3);
174 
175  this->CurrentRenderer->AddActor(SelectedActor);
176  this->GetInteractor()->GetRenderWindow()->Render();
177  this->HighlightProp(NULL);
178 }
179 
181 {
182 
183 }
184 
186 {
187  ofstream outfile("Test.txt");
188  for (int i = 0; i < m_pointLabels.size(); i++)
189  {
190  if (m_pointLabels[i] != 0)
191  {
192  outfile << i << ":" << unsigned(m_pointLabels[i]) << "\n";
193  }
194 
195  }
196  outfile.close();
197 
198 }
200 {
201  vtkRenderWindowInteractor *rwi = this->Interactor;
202  std::string key = rwi->GetKeySym();
203  if (key == "Left")
204  {
205  if (!m_points)
206  {
207  return;
208  }
209 
210  bool accepted;
211  QString label = QInputDialog::getItem(0, "Select Label", "Choose Label For SelectedPoints. You can press n to add a new Label:",m_labelList , 0, true, &accepted);
212  bool updateActors = false;
213  if (accepted)
214  {
215  uint8_t labelIndex = m_labelList.indexOf(label);
216  for(vtkIdType i = 0; i < m_selectedIds->GetNumberOfTuples(); i++)
217  {
218  auto id = m_selectedIds->GetValue(i);
219  //Check if actors need to updated
220  if(labelIndex != 0 && m_pointLabels[id] != 0 && m_pointLabels[id] != labelIndex)
221  {
222  updateActors = true;
223  } else if(labelIndex == 0 && (m_pointLabels[id] != 0))
224  {
225 
226  updateActors = true;
227  }
228  m_pointLabels[id] = labelIndex;
229  }
230  if (updateActors)
231  {
232  }
233  foo2[labelIndex - 1] = m_SelectedPoints;
234 
235 
236  m_SelectedPoints = std::vector<bool>(m_points->GetNumberOfPoints(), false);
237  auto newActor = vtkSmartPointer<vtkActor>::New();
238  newActor = SelectedActor;
239  newActor->GetProperty()->SetColor(colors[labelIndex - 1][0] /255.0,colors[labelIndex - 1][1] / 255.0,colors[labelIndex - 1][2] / 255.0); //(R,G,B)
240  this->CurrentRenderer->RemoveActor(m_labelActors[labelIndex - 1]);
241  m_labelActors[labelIndex - 1] = newActor;
242  this->CurrentRenderer->RemoveActor(SelectedActor);
243  SelectedActor = vtkSmartPointer<vtkActor>::New();
244  this->CurrentRenderer->AddActor(newActor);
245  this->GetInteractor()->GetRenderWindow()->Render();
246  this->HighlightProp(NULL);
247 
248  }
249 
250 
251  }
252  if (key == "m")
253  {
254  bool accepted;
255  QString label = QInputDialog::getItem(0, "Modify Selected Points", "Choose Label which should be modified:",m_labelList , 0, true, &accepted);
256  if (accepted)
257  {
258  m_SelectedPoints = foo2[m_labelList.indexOf(label) - 1];
259 
260  }
261 
262 
263  }
264  if (key == "n")
265  {
266  bool accepted;
267 
268  QString text = QInputDialog::getText(0, "Insert New Label", "New Label:", QLineEdit::Normal, "", &accepted);
269  if (accepted)
270  {
271  if(m_labelList.indexOf(text) != -1)
272  {
273  return;
274  }
275  foo2.push_back(std::vector<bool>(m_points->GetNumberOfPoints(), 0));
276  m_labelList << text;
277  m_labelActors.push_back(vtkSmartPointer<vtkActor>::New());
278  //TODO CHANGE ACTOR.
279  auto mapper = vtkSmartPointer<vtkDataSetMapper>::New();
280  m_labelActors.back()->SetMapper(mapper);
281 
282  }
283  }
284 }
std::vector< bool > m_SelectedPoints
vtkStandardNewMacro(LVRLabelInteractorStyle)
bool isInside(std::vector< vtkVector2i > polygon, int &pX, int &pY)
std::vector< std::vector< uint8_t > > colors
vtkSmartPointer< vtkIdTypeArray > m_selectedIds
void calculateSelection(bool select)
std::vector< vtkSmartPointer< vtkActor > > m_labelActors
vtkSmartPointer< vtkDataSetMapper > SelectedMapper
std::vector< std::vector< bool > > foo2
void labelSelectedPoints(QString label)
std::vector< uint8_t > m_pointLabels
vtkSmartPointer< vtkActor > SelectedActor
vtkSmartPointer< vtkPolyData > m_points
#define NULL
Definition: mydefs.hpp:141
std::vector< vtkVector2i > GetPolygonPoints()


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