LVRAnimationDialog.cpp
Go to the documentation of this file.
1 
28 #include <QFileDialog>
29 #include "LVRAnimationDialog.hpp"
30 
31 //#include <vtkFFMPEGWriter.h>
32 #include <vtkWindowToImageFilter.h>
33 #include <vtkPNGWriter.h>
34 
35 #include <cstring>
36 
37 namespace lvr2
38 {
39 
40 LVRAnimationDialog::LVRAnimationDialog(vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor, vtkSmartPointer<vtkCameraRepresentation> pathCamera, QTreeWidget* treeWidget) :
41  m_renderWindowInteractor(renderWindowInteractor), m_pathCamera(pathCamera), m_treeWidget(treeWidget)
42 {
43  // Setup DialogUI and events
44  QDialog* dialog = new QDialog(m_treeWidget);
45  m_dialog = new AnimationDialog;
46  m_dialog->setupUi(dialog);
47 
48  m_timeline = m_dialog->timeline_list;
49  m_mainCamera = m_pathCamera->GetCamera();
50  m_frameCounter = 0;
51 
53 
54  dialog->show();
55  dialog->raise();
56  dialog->activateWindow();
57 }
58 
60 {
61  // TODO Auto-generated destructor stub
62 }
63 
65 {
66  QObject::connect(m_dialog->addFrame_button, SIGNAL(pressed()), this, SLOT(addFrame()));
67  QObject::connect(m_dialog->removeFrame_button, SIGNAL(pressed()), this, SLOT(removeFrame()));
68  QObject::connect(m_dialog->clearFrames_button, SIGNAL(pressed()), this, SLOT(clearFrames()));
69  QObject::connect(m_dialog->interpolation_box, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(changeInterpolation(const QString&)));
70  QObject::connect(m_dialog->savePath_button, SIGNAL(pressed()), this, SLOT(savePath()));
71  QObject::connect(m_dialog->loadPath_button, SIGNAL(pressed()), this, SLOT(loadPath()));
72  QObject::connect(m_dialog->saveVideo_button, SIGNAL(pressed()), this, SLOT(saveVideo()));
73  QObject::connect(m_dialog->play_button, SIGNAL(pressed()), this, SLOT(play()));
74 
75  QObject::connect(m_dialog->timeline_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(goToCamPosition(QListWidgetItem*)));
76 }
77 
78 void LVRAnimationDialog::goToCamPosition(QListWidgetItem *item)
79 {
80  LVRRecordedFrameItem* cam_item = static_cast<LVRRecordedFrameItem*>(item);
81  if(cam_item)
82  {
83  vtkRenderWindow* window = m_renderWindowInteractor->GetRenderWindow();
84  vtkRendererCollection* renderers = window->GetRenderers();
85  renderers->InitTraversal();
86  vtkRenderer* r = renderers->GetNextItem();
87  while(r != 0)
88  {
89  vtkCamera* ac = r->GetActiveCamera();
90  vtkCamera* frame = cam_item->getFrame();
91 
92  ac->SetPosition(frame->GetPosition());
93  ac->SetFocalPoint(frame->GetFocalPoint());
94  ac->SetViewUp(frame->GetViewUp());
95 
96  m_renderWindowInteractor->Render();
97  r = renderers->GetNextItem();
98  }
99 
100  }
101 }
102 
104 {
105  QString frameCount = QString("Frame no. %1").arg(++m_frameCounter);
106  QListWidgetItem* currentFrame = new LVRRecordedFrameItem(m_pathCamera, frameCount);
107  m_timeline->addItem(currentFrame);
108 }
109 
111 {
112  QListWidgetItem* currentItem = m_timeline->currentItem();
113  if(currentItem) delete currentItem;
114 }
115 
117 {
118  m_timeline->clear();
119  m_frameCounter = 0;
120 }
121 
123 {
124  if(text == "Linear")
125  {
126  m_pathCamera->GetInterpolator()->SetInterpolationTypeToLinear();
127  }
128  else if(text == "Spline")
129  {
130  m_pathCamera->GetInterpolator()->SetInterpolationTypeToSpline();
131  }
132  m_pathCamera->GetInterpolator()->Initialize();
133 }
134 
136 {
137  unsigned int frameCount = m_timeline->count();
138 
139  // remove all cameras from the buffer and add every single one currently in the timeline
140  m_pathCamera->InitializePath();
141  for(int i = 0; i < frameCount; i++)
142  {
143  LVRRecordedFrameItem* recordedFrame = static_cast<LVRRecordedFrameItem*>(m_timeline->item(i));
144  m_pathCamera->SetCamera(recordedFrame->getFrame());
145  m_pathCamera->AddCameraToPath();
146  }
147 
148  unsigned int frameMultiplier = m_dialog->frameMultiplier_box->value();
149  m_pathCamera->SetNumberOfFrames(frameCount * frameMultiplier);
150 
151  // reset camera to main camera to play animation
152  m_pathCamera->SetCamera(m_mainCamera);
154 }
155 
157 {
158  QString filename = QFileDialog::getSaveFileName(m_treeWidget, tr("Save Path"), "", tr("VCP files (*.vcp)"));
159  QFile pfile(filename);
160 
161  if (!pfile.open(QFile::WriteOnly | QIODevice::Text))
162  {
163  return;
164  }
165 
166  QTextStream out(&pfile);
167  // save settings from the dialog to the first line
168  QString interpolation = m_dialog->interpolation_box->currentText();
169  QString transitionFrames = QString::number(m_dialog->frameMultiplier_box->value());
170  out << "S:" << interpolation << ";" << transitionFrames << endl;
171 
172  // save settings for each camera in the timeline to a seperate line
173  for(int row = 0; row < m_timeline->count(); row++)
174  {
175  LVRRecordedFrameItem* recordedFrame = static_cast<LVRRecordedFrameItem*>(m_timeline->item(row));
176  recordedFrame->writeToStream(out);
177  }
178 
179  pfile.close();
180 }
181 
183 {
184  QFileDialog dialog(m_treeWidget);
185  dialog.setAcceptMode(QFileDialog::AcceptOpen);
186  dialog.setOption(QFileDialog::Option::DontUseNativeDialog, true);
187  dialog.setNameFilter("*.vcp");
188 
189  if(!dialog.exec())
190  {
191  return;
192  }
193 
194  QStringList files = dialog.selectedFiles();
195  QString filename = files.front();
196  QFile pfile(filename);
197  if (!pfile.open(QFile::ReadOnly | QIODevice::Text))
198  {
199  cout << "Error opening file " << filename.toStdString() << endl;
200  return;
201  }
202 
203  QTextStream in(&pfile);
204  QString line = in.readLine();
205 
206 
207 
208  // TODO: surround with try and catch to prevent errors
209  // very basic file validity checking
210  if(!line.startsWith("S:"))
211  {
212  cout << "Can't parse path settings from file!" << endl;
213  cout << "Reverting to defaults..." << endl;
214  m_dialog->frameMultiplier_box->setValue(30);
215  m_dialog->interpolation_box->setCurrentIndex(1);
216  m_pathCamera->GetInterpolator()->SetInterpolationTypeToSpline();
217  }
218 
219  line.remove(0,2);
220  QStringList parameters = line.trimmed().split(";");
221 
222  cout << parameters[0].toStdString() << endl;
223  cout << parameters[1].toStdString() << endl;
224 
225  if(parameters[0] == "Linear")
226  {
227  m_pathCamera->GetInterpolator()->SetInterpolationTypeToLinear();
228  m_dialog->interpolation_box->setCurrentIndex(0);
229  }
230  else if(parameters[0] == "Spline")
231  {
232  m_pathCamera->GetInterpolator()->SetInterpolationTypeToSpline();
233  m_dialog->interpolation_box->setCurrentIndex(1);
234  }
235 
236  int transitionFrames = parameters[1].toInt();
237  m_dialog->frameMultiplier_box->setValue(transitionFrames);
238 
239  m_timeline->clear();
240 
241  // read each line in the file and create a camera from it; add it to the timeline
242  while(!in.atEnd())
243  {
244  QListWidgetItem* recordedFrame = LVRRecordedFrameItem::createFromStream(in);
245  m_timeline->addItem(recordedFrame);
246  }
247 
248  pfile.close();
249 
250  m_frameCounter = m_timeline->count();
251 }
252 
254 {
255 //#ifdef VTK_USE_FFMPEG_ENCODER
256  QString filename = QFileDialog::getSaveFileName(m_treeWidget, tr("Save Path"), "", tr("AVI files (*.avi)"));
257 
258  this->m_renderWindowInteractor->GetRenderWindow()->SetOffScreenRendering( 1 );
259 
260  vtkCameraInterpolator* i = m_pathCamera->GetInterpolator();
261 
262  unsigned int frameCount = m_timeline->count();
263  // remove all cameras from the buffer and add every single one currently in the timeline
264  m_pathCamera->InitializePath();
265  for(int i = 0; i < frameCount; i++)
266  {
267  LVRRecordedFrameItem* recordedFrame = static_cast<LVRRecordedFrameItem*>(m_timeline->item(i));
268  m_pathCamera->SetCamera(recordedFrame->getFrame());
269  m_pathCamera->AddCameraToPath();
270  }
271 
272  unsigned int frameMultiplier = m_dialog->frameMultiplier_box->value();
273  m_pathCamera->SetNumberOfFrames(frameCount * frameMultiplier);
274 
275  // reset camera to main camera to play animation
276  m_pathCamera->SetCamera(m_mainCamera);
277 
278  double minT = i->GetMinimumT();
279  double maxT = i->GetMaximumT();
280  int n = frameMultiplier * frameCount;
281  double step = (maxT - minT) / (double)n;
282  vtkSmartPointer<vtkCamera> i_cam = vtkSmartPointer<vtkCamera>::New();
283 
284 
285 
286 
287  vtkRendererCollection* collection = m_renderWindowInteractor->GetRenderWindow()->GetRenderers();
288  vtkRenderer* renderer;
289  char buffer[256];
290  int c = 0;
291  double range[2];
292  for(double camT = 0; camT < maxT; camT += step)
293  {
294  i->InterpolateCamera(camT, i_cam);
295 
296  collection->InitTraversal();
297  renderer = collection->GetNextItem();
298  while(renderer)
299  {
300  renderer->GetActiveCamera()->DeepCopy(i_cam);
301  m_renderWindowInteractor->GetRenderWindow()->Render();
302  renderer = collection->GetNextItem();
303  }
304 
305  vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
306  vtkSmartPointer<vtkWindowToImageFilter>::New();
307 
308  windowToImageFilter->SetInput(m_renderWindowInteractor->GetRenderWindow());
309  windowToImageFilter->Update();
310 
311  vtkSmartPointer<vtkPNGWriter> writer =
312  vtkSmartPointer<vtkPNGWriter>::New();
313 
314  sprintf(buffer, "frame%04d.png", c);
315 
316  writer->SetFileName(buffer);
317  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
318  writer->Write();
319 
320  cout << c << " / " << frameCount * frameMultiplier << endl;
321  c++;
322 
323 
324  }
325 
326 
327 
328  this->m_renderWindowInteractor->GetRenderWindow()->SetOffScreenRendering( 0 );
329 
330 
331 
332  /* vtkSmartPointer<vtkFFMPEGWriter> videoWriter = vtkSmartPointer<vtkFFMPEGWriter>::New();
333  videoWriter->SetQuality(2);
334  videoWriter->SetRate(30);
335  videoWriter->SetFileName(filename.toUtf8().constData());
336 
337  vtkSmartPointer<vtkWindowToImageFilter> w2i = vtkSmartPointer<vtkWindowToImageFilter>::New();
338  w2i->SetInput(m_renderWindowInteractor->GetRenderWindow());
339  videoWriter->SetInputConnection(w2i->GetOutputPort());
340 
341  videoWriter->Start();
342  play(); // TODO: Capture video while playing!
343  videoWriter->End(); */
344 }
345 
346 } // namespace lvr2
LVRAnimationDialog(vtkSmartPointer< vtkRenderWindowInteractor > renderWindowInteractor, vtkSmartPointer< vtkCameraRepresentation > pathCamera, QTreeWidget *treeWidget)
void changeInterpolation(const QString &text)
void writeToStream(QTextStream &out)
void goToCamPosition(QListWidgetItem *item)
vtkSmartPointer< vtkRenderWindowInteractor > m_renderWindowInteractor
__kf_device__ Vec3f tr(const float4 &v)
Definition: device.hpp:79
vtkSmartPointer< vtkCamera > getFrame()
vtkSmartPointer< vtkCameraRepresentation > m_pathCamera
static LVRRecordedFrameItem * createFromStream(QTextStream &in)
vtkSmartPointer< vtkCamera > m_mainCamera


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