Points&Forces (survey)
Software tools facilitating the task of surveying architecture
a_subpixel_interactor.h
Go to the documentation of this file.
1 /*
2  Copyright 2010-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 #ifndef _A_SUBPIXEL_INTERACTOR_H_
17 #define _A_SUBPIXEL_INTERACTOR_H_
18 
19 #include "pixelpos_points.h"
20 
21 #include "vtkInteractorStyleTrackballCamera.h"
22 #include "vtkRenderWindow.h"
23 #include "vtkRenderWindowInteractor.h"
24 #include "vtkRendererCollection.h"
25 #include "vtkRenderer.h"
26 #include "vtkPolyDataMapper2D.h"
27 #include "vtkObjectFactory.h"
28 #include "vtkCellPicker.h"
29 #include "vtkPoints.h"
30 #include "vtkImageReader2.h"
31 #include "vtkImageClip.h"
32 #include "vtkImageData.h"
33 #include "vtkPointData.h"
34 #include "vtkTransform.h"
35 #include "vtkImageShiftScale.h"
36 #include <iostream>
37 #include <iomanip>
38 #include <vector>
39 
45 class VTKINTERACTIONSTYLE_EXPORT a_subpixel_interactor : public vtkInteractorStyleTrackballCamera
46  //class VTK_RENDERING_EXPORT a_subpixel_interactor : public vtkInteractorStyleTrackballCamera
47 {
48  public:
49  static a_subpixel_interactor *New();
51 
53  void set(vtkPoints * points, vtkImageClip * clip, int maxu, int maxv)
54  {
55  points_ = points; clip_ = clip;
56  maxu_ = maxu; maxv_ = maxv;
57  }
58  void setpos(pixelpos_points * pos) {pos_ = pos;}
59  void setscale(vtkImageShiftScale * scale, double scale0) {scale_ = scale; scale0_ = scale0;}
60  virtual ~a_subpixel_interactor() {/*this->InteractionPicker->Delete();*/}
61  void SetImage(vtkImageReader2 * image) {image_ = image;}
62  virtual void OnLeftButtonDown() override;
63  virtual void OnLeftButtonUp() override;
64  virtual void OnRightButtonDown() override;
65  virtual void OnRightButtonUp() override;
66  virtual void OnChar() override;
67  protected:
69  vtkRenderer * ren_;
70  vtkImageReader2 * image_;
72  bool started_;
75  bool selection_;
76  vtkImageClip * clip_;
77  vtkPoints * points_;
78  vtkImageShiftScale * scale_;
79  double scale0_;
80  int maxu_;
81  int maxv_;
82  int fxmin_;
83  int fymin_;
84  void reset();
85  void resetlight();
86  void clipmove(int u, int v);
87 };
88 //------------------------------------------------------------------------------
90 {
91  // First try to create the object from the vtkObjectFactory
92  vtkObject* ret = vtkObjectFactory::CreateInstance("a_subpixel_interactor");
93  if(ret)
94  return (a_subpixel_interactor*)ret;
95  // If the factory was unable to create the object, then create it here.
96  return new a_subpixel_interactor;
97 }
98 //---------------------------------------------------------------------------
100 {
101  vtkRenderWindow * renWin = this->GetInteractor()->GetRenderWindow();
102  vtkRendererCollection * rens = renWin->GetRenderers();
103  rens->InitTraversal();
104  ren_ = rens->GetNextItem();
105  initialised_ = true;
106 }
107 //---------------------------------------------------------------------------
109 {
110  initialised_ = false;
111  started_ = false;
112  window_started_ = false;
113  change_pos_ = false;
114  fxmin_ = -1;
115 }
116 //---------------------------------------------------------------------------
118 {
119  vtkRenderWindowInteractor *rwi = this->Interactor;
120  int ctrl = rwi->GetControlKey();
121  int shift = rwi->GetShiftKey();
122  if ((ctrl == 0 )&&(shift == 0))
123  {
124  started_ = true;
125  change_pos_ = false;
126  }
127  else if ((ctrl == 1)&&(shift == 1))
128  {
129  started_ = true;
130  change_pos_ = true;
131  }
132  else
133  vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
134 }
135 //---------------------------------------------------------------------------
137 {
138  if (started_)
139  {
140  vtkRenderWindowInteractor *rwi = this->Interactor;
141  int X,Y;
142  rwi->GetEventPosition(X,Y);
143  started_ = false;
144  if (!initialised_) reset();
145  float Z = ren_->GetZ(X,Y);
146  if ((Z!=0)&&(Z!=1)) //point on the image
147  {
148  ren_->SetDisplayPoint(X,Y,Z);
149  ren_->DisplayToWorld();
150  double * dp = ren_->GetWorldPoint();
151  for (int k = 0; k < 3; k++) dp[k] /= dp[3];
152  if (change_pos_)
153  this->clipmove(int(dp[0]),int(dp[1]));
154  else
155  pos_->add_or_moa_point(dp[0],dp[1]);
156  }
157  }
158  else
159  vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
160 }
161 //---------------------------------------------------------------------------
163 {
164  vtkRenderWindowInteractor *rwi = this->Interactor;
165  int ctrl = rwi->GetControlKey();
166  int shift = rwi->GetShiftKey();
167  if ((ctrl == 0)&&(shift == 0))
168  selection_ = true;
169  if ((ctrl > 1)&&(shift < 1))
170  window_started_ = true;
171  else
172  vtkInteractorStyleTrackballCamera::OnRightButtonDown();
173 }
174 //---------------------------------------------------------------------------
176 {
177  if (selection_)
178  {
179  vtkRenderWindowInteractor *rwi = this->Interactor;
180  int X,Y;
181  rwi->GetEventPosition(X,Y);
182  if (!initialised_) reset();
183  float Z = ren_->GetZ(X,Y);
184  if ((Z!=0)&&(Z!=1)) //point on the image
185  {
186  ren_->SetDisplayPoint(X,Y,Z);
187  ren_->DisplayToWorld();
188  double * dp = ren_->GetWorldPoint();
189  for (int k = 0; k < 3; k++) dp[k] /= dp[3];
190  pos_->select_point(dp[0],dp[1]);
191  }
192  selection_ = false;
193  }
194  if (window_started_)
195  {
196  vtkRenderWindowInteractor *rwi = this->Interactor;
197  int X,Y;
198  rwi->GetEventPosition(X,Y);
199  window_started_ = false;
200  if (!initialised_) reset();
201  float Z = ren_->GetZ(X,Y);
202  if ((Z!=0)&&(Z!=1)) //point on the image
203  {
204  ren_->SetDisplayPoint(X,Y,Z);
205  ren_->DisplayToWorld();
206  double * dp = ren_->GetWorldPoint();
207  for (int k = 0; k < 3; k++) dp[k] /= dp[3];
208  int u = int(dp[0]+.5);
209  int v = int(dp[1]+.5);
210  if (fxmin_==-1)
211  {
212  fxmin_ = u;
213  fymin_ = v;
214  }
215  else
216  {
217  int fxmax, fymax;
218  if (u<fxmin_)
219  {
220  fxmax = fxmin_;
221  fxmin_ = u;
222  }
223  else
224  fxmax = u;
225  if (v<fymin_)
226  {
227  fymax = fymin_;
228  fymin_ = v;
229  }
230  else
231  fymax = v;
232  vtkDataArray * scal = image_->GetOutput()->GetPointData()->GetScalars();
233  int extent[6];
234  image_->GetOutput()->GetExtent(extent);
235  int maxx = extent[1]-extent[0]+1;
236  double xmoy = 0;
237  double ymoy = 0;
238  double p = 0;
239  for (int y = fymin_; y <= fymax; y++)
240  {
241  for (int x = fxmin_; x <= fxmax; x++)
242  {
243  double val[3];
244  scal->GetTuple(x+y*maxx,val);
245  double val2 = 255.-(val[0]+val[0]+val[0])/3.;
246  p += val2;
247  xmoy += x*val2;
248  ymoy += y*val2;
249  }
250  }
251  xmoy /= p;
252  ymoy /= p;
253  pos_->add_or_moa_point(xmoy,ymoy);
254  fxmin_ = -1;
255  }
256  }
257  }
258  else
259  vtkInteractorStyleTrackballCamera::OnRightButtonUp();
260 }
261 //---------------------------------------------------------------------------
263 {
264  vtkRenderWindowInteractor *rwi = this->GetInteractor();
265  char c = rwi->GetKeyCode();
266  if ((c=='e')||(c=='E'))
268  else if ((c=='x')||(c=='X'))
270  else if (c=='0')
271  this->resetlight();
272  else if (c=='2')
273  {
274  double scale = scale_->GetScale();
275  if (scale < scale0_*10)
276  {
277  scale_->SetScale(scale*1.05);
278  if (!initialised_) reset();
279  ren_->GetRenderWindow()->Render();
280  }
281  }
282  else if (c=='1')
283  {
284  double scale = scale_->GetScale();
285  if (scale > scale0_/10)
286  {
287  scale_->SetScale(scale/1.05);
288  if (!initialised_) reset();
289  ren_->GetRenderWindow()->Render();
290  }
291  }
292  else if (c=='5')
293  {
294  double shift = scale_->GetShift();
295  if (shift < 256./scale0_)
296  {
297  scale_->SetShift(shift+10./scale0_);
298  if (!initialised_) reset();
299  ren_->GetRenderWindow()->Render();
300  }
301  }
302  else if (c=='4')
303  {
304  double shift = scale_->GetShift();
305  if (shift > -256./scale0_)
306  {
307  scale_->SetShift(shift-10./scale0_);
308  if (!initialised_) reset();
309  ren_->GetRenderWindow()->Render();
310  }
311  }
312 
313  // else if ((c!='o')&&(c!='q')&&(c!='O')&&(c!='Q')&&(c!='r')&&(c!='R'))
314  else if ((c!='o')&&(c!='O')&&(c!='r')&&(c!='R')&&(c!='f')&&(c!='F'))
315  vtkInteractorStyleTrackballCamera::OnChar();
316 }
317 //---------------------------------------------------------------------------
319 {
320  scale_->SetShift(0.);
321  scale_->SetScale(scale0_);
322  ren_->GetRenderWindow()->Render();
323 }
324 //---------------------------------------------------------------------------
326 {
327  int ext[6];
328  clip_->GetOutputWholeExtent(ext);
329  clip_->Modified();
330  int dimu = (int)((ext[1]-ext[0]+1.)/2.);
331  int dimv = (int)((ext[3]-ext[2]+1.)/2.);
332  if (u<dimu) u = dimu;
333  if (v<dimv) v = dimv;
334  if (u>maxu_-dimu) u = maxu_-dimu;
335  if (v>maxv_-dimv) v = maxv_-dimv;
336  int du = (int)(u-(ext[0]+ext[1]+1.)/2);
337  int dv = (int)(v-(ext[2]+ext[3]+1.)/2);
338  ext[0] += du;
339  ext[1] += du;
340  ext[2] += dv;
341  ext[3] += dv;
342  clip_->SetOutputWholeExtent(ext);
343  float x[4][3]={{static_cast<float>(ext[0]),static_cast<float>(ext[2]),0.f},
344  {static_cast<float>(ext[1]),static_cast<float>(ext[2]),0.f},
345  {static_cast<float>(ext[1]),static_cast<float>(ext[3]),0.f},
346  {static_cast<float>(ext[0]),static_cast<float>(ext[3]),0.f}};
347  for (int i=0; i<4; i++) points_->SetPoint(i,x[i]);
348  points_->Modified();
349  ren_->GetRenderWindow()->Render();
350 }
351 
352 #endif
a_subpixel_interactor
vtkImageShiftScale * scale_
virtual void OnLeftButtonUp() override
void clipmove(int u, int v)
static a_subpixel_interactor * New()
virtual void OnLeftButtonDown() override
vtkTypeMacro(a_subpixel_interactor, vtkInteractorStyleTrackballCamera)
virtual void OnRightButtonUp() override
void setpos(pixelpos_points *pos)
virtual void OnChar() override
virtual void OnRightButtonDown() override
void setscale(vtkImageShiftScale *scale, double scale0)
void SetImage(vtkImageReader2 *image)
void set(vtkPoints *points, vtkImageClip *clip, int maxu, int maxv)
void add_or_moa_point(double u, double v)
int select_point(double u, double v)
double v(const uint32_t step, const uint32_t n)
Definition: generate.cxx:42