Points&Forces (survey)
Software tools facilitating the task of surveying architecture
a_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 "a_interactor.h"
18 #include "vtkRenderWindow.h"
19 #include "vtkRendererCollection.h"
20 #include "vtkRenderer.h"
21 #include "vtkRenderWindow.h"
22 #include "vtkRenderWindowInteractor.h"
23 #include "vtkObjectFactory.h"
24 #include "vtkCellPicker.h"
25 #include "vtkLight.h"
26 #include "vtkLightCollection.h"
27 #include "vtkMapper.h"
28 #include "vtkRenderLargeImage.h"
29 #include "vtkVRMLExporter.h"
30 #include "vtkTIFFWriter.h"
31 #include "vtkJPEGWriter.h"
32 #include "vtkCamera.h"
33 #include "vtkProperty.h"
34 
35 #include "a_point.h"
36 
37 #include <iostream>
38 #include <sstream>
39 #include <iomanip>
40 
41 //vtkCxxRevisionMacro(a_interactor, "$Revision: 1.3 $");
42 
43 //------------------------------------------------------------------------------
45 {
46  // First try to create the object from the vtkObjectFactory
47  vtkObject* ret = vtkObjectFactory::CreateInstance("a_interactor");
48  if(ret)
49  return (a_interactor*)ret;
50  // If the factory was unable to create the object, then create it here.
51  return new a_interactor;
52 }
53 //---------------------------------------------------------------------------
55 {
56  bg_ = true;
57  magnification_ = 1;
58  name_ = "noname";
59  keep_color_ = false;
60  // JoystickOrTrackball = VTKIS_TRACKBALL;
61  // CameraOrActor = VTKIS_CAMERA;
62 }
63 //---------------------------------------------------------------------------
65 {
66  vtkRenderWindowInteractor * rwi = this->Interactor;
67  int ctrl = rwi->GetControlKey();
68  int shift = rwi->GetShiftKey();
69  vtkRendererCollection * col = rwi->GetRenderWindow()->GetRenderers();
70  col->InitTraversal();
71  vtkRenderer * ren = col->GetNextItem();
72  vtkRenderWindow * renWin = this->GetInteractor()->GetRenderWindow();
73  int X,Y;
74  int * size = renWin->GetSize();
75  rwi->GetEventPosition(X,Y);
76  if ((ctrl >= 1)&&(shift >= 1))
77  {
78  //pick a point and return its coordinates
79  float * Zbuffer = renWin->GetZbufferData(0,0,size[0]-1,size[1]-1);
80  float Z = Zbuffer[X+Y*size[0]];
81  if ((Z!=0)&&(Z!=1))
82  {
83  //works only if point is perfectly picked
84  ren->SetDisplayPoint(X,Y,Z);
85  ren->DisplayToWorld();
86  double * dp = ren->GetWorldPoint();
87  std::cout << dp[0]/dp[3] << " " << dp[1]/dp[3] << " " << dp[2]/dp[3] << std::endl;
88  }
89  else
90  {
91  //look two pixels around
92  for (int i=-2; i<3; i++) {
93  for (int j=-2; j<3; j++) {
94  Z = Zbuffer[X+i+(Y+j)*size[0]];
95  if ((Z!=0)&&(Z!=1))
96  {
97  ren->SetDisplayPoint(X+i,Y+j,Z);
98  ren->DisplayToWorld();
99  double * dp = ren->GetWorldPoint();
100  std::cout << dp[0]/dp[3] << " " << dp[1]/dp[3] << " " << dp[2]/dp[3] << std::endl;
101  //std::cout << "( " << i << ", " << j << ")" << std::endl;
102  return;
103  }
104  }
105  }
106  }
107  }
108  else
109  vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
110 }
111 //---------------------------------------------------------------------------
113 {
114  vtkInteractorStyleTrackballCamera::OnRightButtonDown();
115 }
116 //---------------------------------------------------------------------------
118 {
119  vtkInteractorStyleTrackballCamera::OnRightButtonUp();
120 }
121 //---------------------------------------------------------------------------
123 {
124  vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();
125 }
126 //---------------------------------------------------------------------------
128 {
129  vtkInteractorStyleTrackballCamera::OnMiddleButtonUp();
130 }
131 //---------------------------------------------------------------------------
133 {
134  vtkRenderWindowInteractor *rwi = this->Interactor;
135  char c = rwi->GetKeyCode();
136  int ctrl = rwi->GetControlKey();
137  vtkRendererCollection * col = rwi->GetRenderWindow()->GetRenderers();
138  col->InitTraversal();
139  vtkRenderer * ren = col->GetNextItem();
140  vtkCamera * cam = ren->GetActiveCamera();
141  if ((c=='p')||(c=='P'))
142  {
143  cam->ParallelProjectionOn();
144  std::string name = name_ + std::string(" (parallel)");
145  rwi->GetRenderWindow()->SetWindowName(name.c_str());
146  rwi->GetRenderWindow()->Render();
147  }
148  else if ((c=='i')||(c=='I'))
149  {
150  vtkActorCollection *ac;
151  vtkActor *anActor;
152  int X,Y;
153  rwi->GetEventPosition(X,Y);
154  this->FindPokedRenderer(X,Y);
155  ac = this->CurrentRenderer->GetActors();
156  if (bg_)
157  {
158  ren->SetBackground(1.,1.,1.);
159  if (!keep_color_)
160  {
161  for (ac->InitTraversal(); (anActor = ac->GetNextItem()); )
162  anActor->GetProperty()->SetColor(0.,0.,0.);
163  }
164  }
165  else
166  {
167  ren->SetBackground(0.,0.,0.);
168  if (!keep_color_)
169  {
170  for (ac->InitTraversal(); (anActor = ac->GetNextItem()); )
171  anActor->GetProperty()->SetColor(1.,1.,1.);
172  }
173  }
174  bg_ = !bg_;
175  rwi->GetRenderWindow()->Render();
176  }
177  else if ((c=='c')||(c=='C'))
178  {
179  cam->ParallelProjectionOff();
180  std::string name = name_ + std::string(" (central)");
181  rwi->GetRenderWindow()->SetWindowName(name.c_str());
182  rwi->GetRenderWindow()->Render();
183  }
184  else if ((c=='v')||(c=='V'))
185  {
186  std::string name = name_ + std::string(".wrl");
187  vtkVRMLExporter * writer = vtkVRMLExporter::New();
188  writer->SetFileName(name.c_str());
189  writer->SetInput(rwi->GetRenderWindow());
190  writer->Write();
191  writer->Delete();
192  }
193  else if ((c=='j')||(c=='J'))
194  {
195  vtkRenderLargeImage * large = vtkRenderLargeImage::New();
196  large->SetInput(ren);
197  large->SetMagnification(magnification_);
198  std::string name = name_ + std::string(".jpg");
199  vtkJPEGWriter * writer = vtkJPEGWriter::New();
200  writer->SetFileName(name.c_str());
201  writer->SetInputConnection(large->GetOutputPort());
202  writer->SetQuality(100);//between 0 and 100
203  large->Delete();
204  writer->Write();
205  writer->Delete();
206  }
207  else if ((c=='t')||(c=='T'))
208  {
209  vtkRenderLargeImage * large = vtkRenderLargeImage::New();
210  large->SetInput(ren);
211  large->SetMagnification(magnification_);
212  std::string name = name_ + std::string(".tif");
213  vtkTIFFWriter * writer = vtkTIFFWriter::New();
214  writer->SetFileName(name.c_str());
215  writer->SetInputConnection(large->GetOutputPort());
216  large->Delete();
217  writer->Write();
218  writer->Delete();
219  }
220  else if ((c=='m')||(c=='M'))
221  {
222  double po[3], fo[3];
223  cam->GetPosition(po);
224  cam->GetFocalPoint(fo);
225  vtkLightCollection * lights = ren->GetLights();
226  lights->InitTraversal();
227  vtkLight * light = lights->GetNextItem();
228  light->SetPosition(po);
229  light->SetFocalPoint(fo);
230  light->SetIntensity(1);
231  ren->LightFollowCameraOn();
232  rwi->Render();
233  }
234  else if ((c=='b')||(c=='B'))
235  {
236  double po[3], fo[3];
237  cam->GetPosition(po);
238  cam->GetFocalPoint(fo);
239  vtkLightCollection * lights = ren->GetLights();
240  lights->InitTraversal();
241  vtkLight * light = lights->GetNextItem();
242  light->SetPosition(po);
243  light->SetFocalPoint(fo);
244  light->SetIntensity(1);
245  ren->LightFollowCameraOn();
246  rwi->Render();
247  ren->LightFollowCameraOff();
248  }
249  else if ((c=='w')||(c=='W'))
250  {
251  vtkActorCollection *ac;
252  vtkActor *anActor, *aPart;
253  int X,Y;
254  rwi->GetEventPosition(X,Y);
255  this->FindPokedRenderer(X,Y);
256  ac = this->CurrentRenderer->GetActors();
257  for (ac->InitTraversal(); (anActor = ac->GetNextItem()); )
258  {
259  anActor->GetProperty()->SetRepresentationToWireframe();
260  anActor->GetProperty()->LightingOff();
261  anActor->SetBackfaceProperty(anActor->GetProperty());
262  }
263  rwi->Render();
264  }
265  else if ((c=='s')||(c=='S'))
266  {
267  vtkActorCollection *ac;
268  vtkActor *anActor, *aPart;
269  int X,Y;
270  rwi->GetEventPosition(X,Y);
271  this->FindPokedRenderer(X,Y);
272  ac = this->CurrentRenderer->GetActors();
273  for (ac->InitTraversal(); (anActor = ac->GetNextItem()); )
274  {
275  anActor->GetProperty()->SetRepresentationToSurface();
276  anActor->GetProperty()->LightingOn();
277  anActor->GetMapper()->ScalarVisibilityOn();
278  }
279  rwi->Render();
280  }
281  else if ((c=='x')||(c=='X'))
282  {
283  double po[3], fo[3];
284  cam->GetPosition(po);
285  cam->GetFocalPoint(fo);
286  a_point p_po(po);
287  a_point p_fo(fo);
288  a_point dir(1.,0.,0.);
289  if (c=='X')
290  dir.x(-1.);
291  double dist = (p_fo-p_po).norm();
292  a_point np_po = p_fo-dir*dist;
293  cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
294  cam->SetViewUp(0.,0.,1.);
295  rwi->Render();
296  }
297  else if ((c=='y')||(c=='Y'))
298  {
299  double po[3], fo[3];
300  cam->GetPosition(po);
301  cam->GetFocalPoint(fo);
302  a_point p_po(po);
303  a_point p_fo(fo);
304  a_point dir(0.,1.,0.);
305  if (c=='Y')
306  dir.y(-1.);
307  double dist = (p_fo-p_po).norm();
308  a_point np_po = p_fo-dir*dist;
309  cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
310  cam->SetViewUp(0.,0.,1.);
311  rwi->Render();
312  }
313  else if ((c=='z')||(c=='Z'))
314  {
315  double po[3], fo[3];
316  cam->GetPosition(po);
317  cam->GetFocalPoint(fo);
318  a_point p_po(po);
319  a_point p_fo(fo);
320  a_point dir(0.,0.,1.);
321  if (c=='Z')
322  dir.z(-1.);
323  double dist = (p_fo-p_po).norm();
324  a_point np_po = p_fo-dir*dist;
325  cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
326  cam->SetViewUp(1.,0.,0.);
327  rwi->Render();
328  }
329  else if ((c=='o')||(c=='O'))
330  {
331  double po[3], fo[3];
332  cam->GetPosition(po);
333  cam->GetFocalPoint(fo);
334  a_point p_po(po);
335  a_point p_fo(fo);
336  a_point dir = (p_fo-p_po).normalise();
337  bool moved = false;
338  if (fabs(dir.x())<0.2) {dir.x(0.); moved=true;}
339  if (fabs(dir.y())<0.2) {dir.y(0.); moved=true;}
340  if (fabs(dir.z())<0.2) {dir.z(0.); moved=true;}
341  if (moved)
342  {
343  dir.normalise();
344  double dist = (p_fo-p_po).norm();
345  a_point np_fo = p_po+dir*dist;
346  std::cerr << "view direction: " << dir << std::endl;
347  cam->SetFocalPoint(np_fo.x(),np_fo.y(),np_fo.z());
348  rwi->Render();
349  }
350  }
351  else if (c=='F')
352  {
353  double fo[3];
354  cam->GetFocalPoint(fo);
355  std::cout << fo[0] << " " << fo[1] << " " << fo[2] << std::endl;
356  }
357  else if (c=='r')
358  {
359  double r = cam->GetRoll();
360  std::cout << r << std::endl;
361  }
362  else if (c=='f')
363  {
364  double po[3], fo[3];
365  cam->GetPosition(po);
366  cam->GetFocalPoint(fo);
367  std::cout << po[0] << " " << po[1] << " " << po[2] << " ";
368  std::cout << fo[0] << " " << fo[1] << " " << fo[2] << std::endl;
369  }
370  else if ((c!='r')&&(c!='R'))
371  vtkInteractorStyleTrackballCamera::OnChar();
372 }
373 //----------------------------------------------------------------------------
374 void a_interactor::PrintSelf(ostream& os, vtkIndent indent)
375 {
376  this->Superclass::PrintSelf(os,indent);
377  os << indent << "P.Smars" << "\n";
378 }
a generic interactor
Definition: a_interactor.h:29
virtual void OnLeftButtonDown() override
int magnification_
Definition: a_interactor.h:51
virtual void OnRightButtonUp() override
bool keep_color_
Definition: a_interactor.h:50
std::string name_
Definition: a_interactor.h:52
static a_interactor * New()
virtual void OnMiddleButtonDown() override
virtual void OnRightButtonDown() override
virtual void OnMiddleButtonUp() override
void PrintSelf(ostream &os, vtkIndent indent) override
virtual void OnChar() override
std::string name() const
Definition: a_interactor.h:40
a_point dir(int i, int l)
Definition: rib0.cxx:60
vtkRenderer * ren
Definition: view_li.cxx:81