Points&Forces (survey)
Software tools facilitating the task of surveying architecture
view_tri_interactor.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2008-2020 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include "view_tri_interactor.h"
18 #include "vtkRenderWindow.h"
19 #include "vtkRenderWindowInteractor.h"
20 #include "vtkRendererCollection.h"
21 #include "vtkCellArray.h"
22 #include "vtkPointData.h"
23 #include "vtkCellData.h"
24 #include "vtkPolyData.h"
25 #include "vtkShortArray.h"
26 #include "vtkLight.h"
27 #include "vtkLightCollection.h"
28 #include "vtkProperty.h"
29 #include "vtkPolyDataMapper2D.h"
30 #include "vtkMapper.h"
31 #include "vtkProperty2D.h"
32 #include "vtkObjectFactory.h"
33 #include "vtkCamera.h"
34 #include "vtkRenderLargeImage.h"
35 #include "vtkVRMLExporter.h"
36 #include "vtkTIFFWriter.h"
37 #include "vtkJPEGWriter.h"
38 #include "vtkRIBExporter.h"
39 #include "vtkLookupTable.h"
40 #include "g_point.h"
41 
42 #include "vtkCallbackCommand.h"
43 #include "vtkCommand.h"
44 
45 #include <iostream>
46 #include <sstream>
47 #include <iomanip>
48 
49 //vtkCxxRevisionMacro(view_tri_interactor, "$Revision: 1.3 $");
50 
51 //------------------------------------------------------------------------------
53 {
54  // First try to create the object from the vtkObjectFactory
55  vtkObject* ret = vtkObjectFactory::CreateInstance("view_tri_interactor");
56  if(ret)
57  return (view_tri_interactor*)ret;
58  // If the factory was unable to create the object, then create it here.
59  return new view_tri_interactor;
60 }
61 //---------------------------------------------------------------------------
63 {
64  initialised_ = false;
65  ren_ = 0;
66  n_ = 0;
67  bg_ = true;
68  is_changed_ = false;
69  delete_mode_ = false;
70  locator_ = vtkCellLocator::New();
71  magnification_ = 1;
72  size_ = 0;
73  black_wire_ = false;
74  flat_ = false;
75 }
76 //---------------------------------------------------------------------------
78 {
79  actor_->Delete();
80  locator_->Delete();
81 }
82 //---------------------------------------------------------------------------
84 {
85  renWin_ = this->GetInteractor()->GetRenderWindow();
86  vtkRendererCollection * rens = renWin_->GetRenderers();
87  rens->InitTraversal();
88  ren_ = rens->GetNextItem();
89  vtkIdType pt[2];
90  points_ = vtkPoints::New();
91  pt[0] = points_->InsertNextPoint(0,0,0);
92  pt[1] = points_->InsertNextPoint(1,0,0);
93  vtkCellArray * lines = vtkCellArray::New();
94  lines->InsertNextCell(2,pt);
95  vtkPolyData * data = vtkPolyData::New();
96  data->SetPoints(points_);
97  points_->Delete();
98  data->SetLines(lines);
99  lines->Delete();
100  vtkPolyDataMapper2D * map = vtkPolyDataMapper2D::New();
101  map->SetInputData(data);
102  data->Delete();
103  actor_ = vtkActor2D::New();
104  actor_->SetMapper(map);
105  map->Delete();
106  actor_->GetProperty()->SetColor(1,0.5,0.5);
107  vtkLight * light = vtkLight::New();
108  light->PositionalOff();
109  light->SetLightTypeToCameraLight();
110  ren_->GetLights()->InitTraversal();
111  ren_->GetLights()->GetNextItem()->SwitchOff();
112  ren_->AddLight(light);
113  size_ = renWin_->GetSize();
114  // this->GetInteractor()->LightFollowCameraOff();
115  initialised_ = true;
116 }
117 //---------------------------------------------------------------------------
119 {
120  if (!initialised_) reset();
121  vtkRenderWindowInteractor * rwi = this->Interactor;
122  int ctrl = rwi->GetControlKey();
123  int shift = rwi->GetShiftKey();
124  int X,Y;
125  rwi->GetEventPosition(X,Y);
126  if (((ctrl >= 1)&&(shift >= 1))||(n_==1))
127  {
128  if (n_==0)
129  {
130  Zbuffer_ = renWin_->GetZbufferData(0,0,size_[0]-1,size_[1]-1);
131  //a copy of the Zbuffer data is made when a line process starts
132  //this copy will then be used to compute the 3D position of point
133  //a better solution should be found
134  }
135  // float Z = ren_->GetZ(X,Y);
136  float Z = Zbuffer_[X+Y*size_[0]];
137  if ((Z!=0)&&(Z!=1))
138  {
139  ren_->SetDisplayPoint(X,Y,Z);
140  ren_->DisplayToWorld();
141  double * dp = ren_->GetWorldPoint();
142  if (delete_mode_)
143  {
144  is_changed_ = true;
145  //world coordinates of the select point
146  double x[3];
147  for (short i = 0; i < 3; i++) x[i] = dp[i]/dp[3];
148  //find the closest cell
149  vtkIdType cellId;
150  int subId;
151  double dist2;
152  locator_->FindClosestPoint(x,x,cellId,subId,dist2);
153  vtkDataArray * scalars = geometry_->GetCellData()->GetScalars();
154  int val = (int)scalars->GetTuple1(cellId);
155  if (val==1)
156  {
157  scalars->SetTuple1(cellId,0);
158  std::cerr << cellId << " 0" << std::endl;
159  }
160  else
161  {
162  scalars->SetTuple1(cellId,1);
163  std::cerr << cellId << " 1" << std::endl;
164  }
165  scalars->Modified();
166  rwi->GetRenderWindow()->Render();
167  }
168  else
169  {
170  n_++;
171  std::cout << "point " << n_ << " : ";
172  for (short k=0; k<3; k++)
173  {
174  x_[3*(n_-1)+k] = dp[k] / dp[3];
175  std::cout << x_[3*(n_-1)+k] << " ";
176  }
177  std::cout << std::endl;
178  if (n_==2)
179  {
180  n_ = 0;
181  g_point a(x_[0],x_[1],x_[2]);
182  g_point b(x_[3],x_[4],x_[5]);
183  g_point c= b-a;
184  ((g_point)(b-a)).print(std::cout);
185  delete[] Zbuffer_;
186  ren_->RemoveActor2D(actor_);
187  renWin_->Render();
188  }
189  else
190  {
191  float x2d[] = {static_cast<float>(X),static_cast<float>(Y),0.f};
192  points_->SetPoint(0,x2d);
193  ren_->AddActor2D(actor_);
194  renWin_->Render();
195  }
196  }
197  }
198  }
199  else
200  vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
201 }
202 //---------------------------------------------------------------------------
204 {
205  vtkRenderWindowInteractor *rwi = this->Interactor;
206  int ctrl = rwi->GetControlKey();
207  int shift = rwi->GetShiftKey();
208  if (((ctrl < 1)||(shift <1))&&(n_ != 1))
209  vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
210  /* vtkActor * actor = ren_->GetActors()->GetLastActor();
211  actor->SetBackfaceProperty(actor->GetProperty());
212  */
213 }
214 //---------------------------------------------------------------------------
216 {
217  vtkInteractorStyleTrackballCamera::OnRightButtonDown();
218 }
219 //---------------------------------------------------------------------------
221 {
222  vtkInteractorStyleTrackballCamera::OnRightButtonUp();
223 }
224 //---------------------------------------------------------------------------
226 {
227  vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();
228 }
229 //---------------------------------------------------------------------------
231 {
232  vtkInteractorStyleTrackballCamera::OnMiddleButtonUp();
233 }
234 //---------------------------------------------------------------------------
236 {
237  vtkRenderWindowInteractor *rwi = this->Interactor;
238  int X,Y;
239  rwi->GetEventPosition(X,Y);
240  if (n_ == 1)
241  {
242  float x2d[] = {static_cast<float>(X),static_cast<float>(Y),0.f};
243  points_->SetPoint(1,x2d);
244  renWin_->Render();
245  }
246  else
247  vtkInteractorStyleTrackballCamera::OnMouseMove();
248 }
249 //---------------------------------------------------------------------------
251 {
252  if (!initialised_) reset();
253  vtkRenderWindowInteractor *rwi = this->GetInteractor();
254  char c = rwi->GetKeyCode();
255  vtkRendererCollection * col = rwi->GetRenderWindow()->GetRenderers();
256  col->InitTraversal();
257  vtkCamera * cam = col->GetNextItem()->GetActiveCamera();
258  if (ren_==0)
259  {
260  vtkRendererCollection * col = rwi->GetRenderWindow()->GetRenderers();
261  col->InitTraversal();
262  ren_ = col->GetNextItem();
263  }
264  if ((c=='o')||(c=='O')) {}
265  else if ((c=='p')||(c=='P'))
266  {
267  cam->ParallelProjectionOn();
268  rwi->GetRenderWindow()->SetWindowName("view_tri (parallel)");
269  rwi->GetRenderWindow()->Render();
270  }
271  else if ((c=='c')||(c=='C'))
272  {
273  cam->ParallelProjectionOff();
274  rwi->GetRenderWindow()->SetWindowName("view_tri (central)");
275  rwi->GetRenderWindow()->Render();
276  }
277  else if ((c=='i')||(c=='I'))
278  {
279  vtkActor *anActor = ren_->GetActors()->GetLastActor();
280  if (bg_)
281  {
282  ren_->SetBackground(1.,1.,1.);
283  if (black_wire_)
284  {
285  if (flat_||(anActor->GetProperty()->GetRepresentation()==VTK_WIREFRAME))
286  anActor->GetProperty()->SetColor(0.,0.,0.);
287  anActor->SetBackfaceProperty(anActor->GetProperty());
288  }
289  }
290  else
291  {
292  ren_->SetBackground(0.,0.,0.);
293  if (black_wire_)
294  {
295  anActor->GetProperty()->SetColor(1.,1.,1.);
296  anActor->SetBackfaceProperty(anActor->GetProperty());
297  }
298  }
299  bg_ = !bg_;
300  rwi->GetRenderWindow()->Render();
301  }
302  else if ((c=='n')||(c=='N'))
303  {
304  static double dp0[] = {0.,0.,0.,0.};
305  int X,Y;
306  rwi->GetEventPosition(X,Y);
307  float Z = ren_->GetZ(X,Y);
308  if ((Z!=0)&&(Z!=1))
309  {
310  ren_->SetDisplayPoint(X,Y,Z);
311  ren_->DisplayToWorld();
312  double * dp = ren_->GetWorldPoint();
313  if ((dp[0]==dp0[0])&&(dp[1]==dp0[1])&&(dp[2]==dp0[2])&&(dp[3]==dp0[3]))
314  std::cout << std::endl;
315  else
316  {
317  for (short i=0; i<3; i++) std::cout << dp[i]/dp[3] << " ";
318  std::cout << std::flush;
319  }
320  for (short i=0; i<4; i++) dp0[i] = dp[i];
321  }
322 
323  }
324  else if ((c=='e')||(c=='E'))
325  {
327  if (delete_mode_)
328  {
329  std::cerr << "delete mode: on" << std::endl;
330  vtkDataArray * scalars = vtkShortArray::New();
331  scalars->SetNumberOfTuples(geometry_->GetNumberOfCells());
332  scalars->SetNumberOfComponents(1);
333  scalars->FillComponent(0,1);
334  geometry_->GetCellData()->SetScalars(scalars);
335  scalars->Delete();
336  //vtk6 geometry_->Update();
337  vtkLookupTable * lut = vtkLookupTable::New();
338  lut->SetNumberOfTableValues(2);
339  lut->SetTableValue(0,.85,.08,.08,1.);
340  lut->SetTableValue(1,1.,1.,1.,1.);
341  vtkMapper * map = ren_->GetActors()->GetLastActor()->GetMapper();
342  map->SetScalarModeToUseCellData();
343  map->SetScalarRange(0.,1.);
344  map->ScalarVisibilityOn();
345  map->SetLookupTable(lut);
346  map->Update();
347  lut->Delete();
348  locator_->SetDataSet(geometry_);
349  locator_->BuildLocator();
350  }
351  else
352  {
353  std::cerr << "delete mode: off" << std::endl;
354  vtkMapper * map = ren_->GetActors()->GetLastActor()->GetMapper();
355  map->SetScalarModeToUsePointData();
356  map->Update();
357  }
358  rwi->GetRenderWindow()->Render();
359  }
360  else if ((c=='m')||(c=='M'))
361  {
362  double po[3], fo[3];
363  vtkCamera * cam = ren_->GetActiveCamera();
364  cam->GetPosition(po);
365  cam->GetFocalPoint(fo);
366  vtkLightCollection * lights = ren_->GetLights();
367  lights->InitTraversal();
368  vtkLight * light = lights->GetNextItem();
369  light->SetPosition(po);
370  light->SetFocalPoint(fo);
371  light->SetIntensity(1);
372  ren_->LightFollowCameraOn();
373  rwi->Render();
374  }
375  else if ((c=='b')||(c=='B'))
376  {
377  double po[3], fo[3];
378  vtkCamera * cam = ren_->GetActiveCamera();
379  cam->GetPosition(po);
380  cam->GetFocalPoint(fo);
381  vtkLightCollection * lights = ren_->GetLights();
382  lights->InitTraversal();
383  vtkLight * light = lights->GetNextItem();
384  light->SetPosition(po);
385  light->SetFocalPoint(fo);
386  light->SetIntensity(1);
387  ren_->LightFollowCameraOn();
388  rwi->Render();
389  ren_->LightFollowCameraOff();
390  }
391  else if ((c=='x')||(c=='X'))
392  {
393  double po[3], fo[3];
394  vtkCamera * cam = ren_->GetActiveCamera();
395  cam->GetPosition(po);
396  cam->GetFocalPoint(fo);
397  a_point p_po(po);
398  a_point p_fo(fo);
399  a_point dir(1.,0.,0.);
400  if (c=='X')
401  dir.x(-1.);
402  double dist = (p_fo-p_po).norm();
403  a_point np_po = p_fo-dir*dist;
404  cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
405  cam->SetViewUp(0.,0.,1.);
406  rwi->Render();
407  }
408  else if ((c=='y')||(c=='Y'))
409  {
410  double po[3], fo[3];
411  vtkCamera * cam = ren_->GetActiveCamera();
412  cam->GetPosition(po);
413  cam->GetFocalPoint(fo);
414  a_point p_po(po);
415  a_point p_fo(fo);
416  a_point dir(0.,1.,0.);
417  if (c=='Y')
418  dir.y(-1.);
419  double dist = (p_fo-p_po).norm();
420  a_point np_po = p_fo-dir*dist;
421  cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
422  cam->SetViewUp(0.,0.,1.);
423  rwi->Render();
424  }
425  else if ((c=='z')||(c=='Z'))
426  {
427  double po[3], fo[3];
428  vtkCamera * cam = ren_->GetActiveCamera();
429  cam->GetPosition(po);
430  cam->GetFocalPoint(fo);
431  a_point p_po(po);
432  a_point p_fo(fo);
433  a_point dir(0.,0.,1.);
434  if (c=='Z')
435  dir.z(-1.);
436  double dist = (p_fo-p_po).norm();
437  a_point np_po = p_fo-dir*dist;
438  cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
439  cam->SetViewUp(1.,0.,0.);
440  rwi->Render();
441  }
442  else if ((c=='w')||(c=='W'))
443  {
444  vtkActor *anActor = ren_->GetActors()->GetLastActor();
445  anActor->GetProperty()->SetRepresentationToWireframe();
446  if (!flat_ && black_wire_ && ren_->GetBackground()[0]==1.)
447  anActor->GetProperty()->SetColor(0.,0.,0.);
448  anActor->GetProperty()->LightingOff();
449  anActor->SetBackfaceProperty(anActor->GetProperty());
450  rwi->Render();
451  }
452  else if ((c=='s')||(c=='S'))
453  {
454  vtkActor *anActor = ren_->GetActors()->GetLastActor();
455  anActor->GetProperty()->SetRepresentationToSurface();
456  if (!flat_ && black_wire_)
457  anActor->GetProperty()->SetColor(1.,1.,1.);
458  anActor->GetProperty()->LightingOn();
459  anActor->GetMapper()->ScalarVisibilityOn();
460  rwi->Render();
461  }
462  else if ((c=='v')||(c=='V'))
463  {
464  vtkVRMLExporter * writer = vtkVRMLExporter::New();
465  writer->SetFileName("view_tri.wrl");
466  writer->SetInput(rwi->GetRenderWindow());
467  writer->Write();
468  writer->Delete();
469  }
470  else if ((c=='j')||(c=='J'))
471  {
472  vtkRenderLargeImage * large = vtkRenderLargeImage::New();
473  large->SetInput(ren_);
474  large->SetMagnification(magnification_);
475  vtkJPEGWriter * writer = vtkJPEGWriter::New();
476  writer->SetFileName("view_tri.jpg");
477  writer->SetInputConnection(large->GetOutputPort());
478  //writer->SetInput(grab->GetOutput());
479  writer->SetQuality(100);//between 0 and 100
480  large->Delete();
481  writer->Write();
482  writer->Delete();
483  }
484  else if ((c=='t')||(c=='T'))
485  {
486  vtkRenderLargeImage * large = vtkRenderLargeImage::New();
487  large->SetInput(ren_);
488  large->SetMagnification(magnification_);
489  vtkTIFFWriter * writer = vtkTIFFWriter::New();
490  writer->SetFileName("view_tri.tif");
491  writer->SetInputConnection(large->GetOutputPort());
492  large->Delete();
493  writer->Write();
494  writer->Delete();
495  }
496  else if (c=='r')
497  {
498  double r = cam->GetRoll();
499  std::cout << r << std::endl;
500  }
501  else if (c=='R')
502  {
503  vtkRIBExporter * rib = vtkRIBExporter::New();
504  rib->SetInput(rwi->GetRenderWindow());
505  rib->SetFilePrefix("view_tri");
506  rib->Write();
507  rib->Delete();
508  }
509  else if ((c=='f')||(c=='F'))
510  {
511  double po[3], fo[3];
512  vtkCamera * cam = ren_->GetActiveCamera();
513  cam->GetPosition(po);
514  cam->GetFocalPoint(fo);
515  std::cout << po[0] << " " << po[1] << " " << po[2] << " ";
516  std::cout << fo[0] << " " << fo[1] << " " << fo[2] << std::endl;
517  }
518  else if ((c=='q')||(c=='Q'))
519  {
520  //std::cout << "exit triangles" << std::endl;
521  vtkInteractorStyleTrackballCamera::OnChar();
522  }
523  else if ((c!='r')&&(c!='R'))
524  vtkInteractorStyleTrackballCamera::OnChar();
525 }
526 //----------------------------------------------------------------------------
527 void view_tri_interactor::PrintSelf(ostream& os, vtkIndent indent)
528 {
529  this->Superclass::PrintSelf(os,indent);
530  os << indent << "P.Smars" << "\n";
531 }
532 
a geometric point
Definition: g_point.h:30
void PrintSelf(ostream &os, vtkIndent indent) override
static view_tri_interactor * New()
virtual void OnRightButtonDown() override
virtual void OnLeftButtonUp() override
virtual void OnMiddleButtonDown() override
virtual void OnMouseMove() override
virtual void OnLeftButtonDown() override
vtkCellLocator * locator_
virtual void OnRightButtonUp() override
vtkRenderWindow * renWin_
virtual void OnMiddleButtonUp() override
virtual void OnChar() override
a_point dir(int i, int l)
Definition: rib0.cxx:60
Definition: stlb2stla.cxx:21
std::string print(double s)
Definition: trisize.cxx:55