Points&Forces (survey)
Software tools facilitating the task of surveying architecture
view_li.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2008-2019 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 #include "vtkRenderer.h"
17 #include "vtkRenderWindow.h"
18 #include "vtkInteractorStyleTrackballCamera.h"
19 #include "vtkRenderWindowInteractor.h"
20 #include "vtkPolyData.h"
21 #include "vtkPoints.h"
22 #include "vtkProperty.h"
23 #include "vtkCellData.h"
24 #include "vtkCellArray.h"
25 #include "vtkDoubleArray.h"
26 #include "vtkElevationFilter.h"
27 #include "vtkPolyDataMapper.h"
28 #include "vtkPolyDataNormals.h"
29 #include "vtkLookupTable.h"
30 #include "vtkDataSetMapper.h"
31 #include "vtkActor.h"
32 #include "vtkLODActor.h"
33 #include "vtkAxes.h"
34 #include "vtkPointData.h"
35 
36 #include "a_interactor.h"
37 #include "a_pmat.h"
38 
39 #include <iostream>
40 #include <sstream>
41 
42 #include "SimpleOpt.h"
43 
45 
46 CSimpleOpt::SOption g_rgOptions[] = {
47  { OPT_DIR, _T("-x"), SO_NONE },
48  { OPT_DIR, _T("-y"), SO_NONE },
49  { OPT_DIR, _T("-z"), SO_NONE },
50  { OPT_DIR, _T("-0"), SO_NONE },
51  { OPT_SMOOTH, _T("-s"), SO_NONE },
52  { OPT_SMOOTH, _T("--smooth"), SO_NONE },
53  { OPT_GREY, _T("-g"), SO_NONE },
54  { OPT_GREY, _T("--greyscale"), SO_NONE },
55  { OPT_STEPS, _T("-b"), SO_REQ_SEP },
56  { OPT_STEPS, _T("--bands"), SO_REQ_SEP },
57  { OPT_TRA,_T("-tr"), SO_NONE },
58  { OPT_MAG, _T("-m"), SO_REQ_SEP },
59  { OPT_MAG, _T("--magnification"), SO_REQ_SEP },
60  { OPT_TRI, _T("-t"), SO_REQ_SEP },
61  { OPT_TRI, _T("--triangle"), SO_REQ_SEP },
62  { OPT_CAMERA, _T("-c"), SO_REQ_SEP },
63  { OPT_CAMERA, _T("--camera"), SO_REQ_SEP },
64  { OPT_IMAGE, _T("-i"), SO_REQ_SEP },
65  { OPT_IMAGE, _T("--image"), SO_REQ_SEP },
66  { OPT_SCALAR, _T("-sf"), SO_REQ_SEP },
67  { OPT_SCALAR, _T("--scalar-file"), SO_REQ_SEP },
68  { OPT_SCALARD, _T("-si"), SO_NONE },
69  { OPT_SCALARD, _T("--scalar-invert"), SO_NONE },
70  { OPT_SCALARL, _T("-sl"), SO_NONE },
71  { OPT_SCALARL, _T("--scalar-lines"), SO_NONE },
72  { OPT_PTSIZE, _T("-p"), SO_REQ_SEP },
73  { OPT_AXES,_T("-a"), SO_NONE },
74  { OPT_AXES,_T("--axes"), SO_NONE },
75  { OPT_HELP, _T("-?"), SO_NONE },
76  { OPT_HELP, _T("-h"), SO_NONE },
77  { OPT_HELP, _T("--help"), SO_NONE },
78  SO_END_OF_OPTIONS
79 };
80 
81 vtkRenderer * ren;
82 bool smooth = false;
83 bool with_camera_file = false;
84 a_pmat pmat;
85 std::string image_file("");
86 int * bb;
87 bool is_textured = false;
88 bool is_transparent = false;
89 
90 //---------------------------------------------------------------------------
91 int error(int val)
92 {
93 #include "view_li.help"
94  return val;
95 }
96 
97 #include "view_pt_li_common.cxx"
98 
99 //---------------------------------------------------------------------------
100 int main(int argc, char ** argv)
101 {
102  std::string tri_file;
103  bool with_scalars = false;
104  int li_width = 3;
105  int magnification = 1;
106  bool is_greyscale = false;
107  double number_steps = 256;
108  double m_scalar = 1.e30;
109  double M_scalar = -1.e30;
110  bool invert_scalar = false;
111  bool line_scalar = false;
112  bool with_axes = false;
113  std::string code("z");
114  std::string scalar_file;
115  CSimpleOpt args(argc, argv, g_rgOptions);
116  while (args.Next())
117  {
118  if (args.LastError() == SO_SUCCESS)
119  {
120  if (args.OptionId() == OPT_HELP)
121  return error(0);
122  else if (args.OptionId() == OPT_DIR)
123  {
124  std::string code0(args.OptionText());
125  code = code0.substr(1,1);
126  }
127  else if (args.OptionId() == OPT_SMOOTH)
128  smooth = true;
129  else if (args.OptionId() == OPT_TRA)
130  is_transparent = true;
131  else if (args.OptionId() == OPT_GREY)
132  is_greyscale = true;
133  else if (args.OptionId() == OPT_SCALARD)
134  invert_scalar = true;
135  else if (args.OptionId() == OPT_SCALARL)
136  line_scalar = true;
137  else if (args.OptionId() == OPT_SCALAR)
138  {
139  with_scalars = true;
140  code = "s";
141  scalar_file = args.OptionArg();
142  }
143  else if (args.OptionId() == OPT_TRI)
144  {
145  tri_file = args.OptionArg();
146  }
147  else if (args.OptionId() == OPT_PTSIZE)
148  {
149  std::ostringstream o;
150  o << args.OptionArg();
151  std::istringstream in(o.str().c_str());
152  int size;
153  in >> size;
154  if ((size > 0)&&(size < 100))
155  li_width = size;
156  }
157  else if (args.OptionId() == OPT_STEPS)
158  {
159  std::ostringstream o;
160  o << args.OptionArg();
161  std::istringstream in(o.str().c_str());
162  int size;
163  in >> size;
164  if ((size > 0)&&(size < 256))
165  number_steps = size;
166  }
167  else if (args.OptionId() == OPT_MAG)
168  {
169  std::ostringstream o;
170  o << args.OptionArg();
171  std::istringstream in(o.str().c_str());
172  int size;
173  in >> size;
174  if ((size > 1)&&(size < 100))
175  magnification = size;
176  } else if (args.OptionId() == OPT_CAMERA)
177  {
178  std::ifstream in(args.OptionArg());
179  if (!in)
180  {
181  std::cerr << "cannot open file: '" << args.OptionArg() << "'" << std::endl;
182  return error(-3);
183  }
184  in >> pmat;
185  with_camera_file = true;
186  } else if (args.OptionId() == OPT_IMAGE)
187  image_file = args.OptionArg();
188  else if (args.OptionId() == OPT_AXES)
189  with_axes = true;
190  else
191  return error(-1);
192  // handle option, using OptionId(), OptionText() and OptionArg()
193  } else {
194  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
195  return error(args.LastError());
196  // handle error, one of: SO_OPT_INVALID, SO_OPT_MULTIPLE,
197  // SO_ARG_INVALID, SO_ARG_INVALID_TYPE, SO_ARG_MISSING
198  }
199  }
200 
201  bool flat = false;
202  if ((with_camera_file)&&(image_file != "")) is_textured = true;
203  vtkPoints * pts = vtkPoints::New();
204  vtkDoubleArray * scalars;
205  if (with_scalars)
206  {
207  std::ifstream in(scalar_file.c_str());
208  if (!in)
209  {
210  std::cerr << "Cannot open file '" << scalar_file << "'" << std::endl;
211  return error(-2);
212  }
213  scalars = vtkDoubleArray::New();
214  scalars->SetNumberOfComponents(1);
215  int nlis;
216  in >> nlis;
217  short nj=2;
218  if (line_scalar)
219  nj=1;
220  for (unsigned int i=0; i<nlis; i++)
221  {
222  for (short j=0; j<nj; j++)
223  {
224  double val;
225  in >> val;
226  if (val<m_scalar)
227  m_scalar = val;
228  if (val>M_scalar)
229  M_scalar = val;
230  scalars->InsertNextValue(val);
231  }
232  }
233  }
234  int nlis;
235  std::cin >> nlis;
236  for (unsigned int k = 0; k < nlis; k++)
237  {
238  for (short j=0; j<2; j++)
239  {
240  double x,y,z;
241  std::cin >> x >> y >> z;
242  pts->InsertNextPoint(x,y,z);
243  }
244  }
245  ren = vtkRenderer::New();
246  vtkRenderWindow * renWin = vtkRenderWindow::New();
247  renWin->AddRenderer(ren);
248  renWin->SetStereoTypeToAnaglyph();
249  ren->Delete();
250  a_interactor * style = a_interactor::New();
251  style->name("view_li");
252  if (magnification != 1)
253  style->magnification(magnification);
254  vtkRenderWindowInteractor * iren = vtkRenderWindowInteractor::New();
255  iren->SetRenderWindow(renWin);
256  renWin->Delete();
257  iren->SetInteractorStyle(style);
258  iren->LightFollowCameraOff();
259  vtkCellArray * lines = vtkCellArray::New();
260  for (unsigned int k = 0; k < nlis; k++)
261  {
262  vtkIdType co[] = {2*k,2*k+1};
263  lines->InsertNextCell(2,co);
264  }
265  vtkPolyData * data = vtkPolyData::New();
266  data->SetPoints(pts);
267  pts->Delete();
268  data->SetLines(lines);
269  lines->Delete();
270  if (with_scalars)
271  {
272  if (line_scalar)
273  data->GetCellData()->SetScalars(scalars);
274  else
275  data->GetPointData()->SetScalars(scalars);
276  }
277  double bd[6];
278  pts->GetBounds(bd);
279 
280  vtkElevationFilter * elevation = vtkElevationFilter::New();
281  if (code == "0")
282  flat = true;
283  else if (code != "s")
284  {
285  elevation = vtkElevationFilter::New();
286  elevation->SetInputData(data);
287  data->Delete();
288  elevation->SetScalarRange(0,20);
289  }
290  if (code == "x")
291  {
292  if (bd[0]==bd[1])
293  flat = true;
294  else
295  {
296  elevation->SetLowPoint(bd[0],0,0);
297  elevation->SetHighPoint(bd[1],0,0);
298  }
299  }
300  else if (code == "y")
301  {
302  if (bd[2]==bd[3])
303  flat = true;
304  else
305  {
306  elevation->SetLowPoint(0,bd[2],0);
307  elevation->SetHighPoint(0,bd[3],0);
308  }
309  }
310  else if (code == "z")
311  {
312  if (bd[4]==bd[5])
313  flat = true;
314  else
315  {
316  elevation->SetLowPoint(0,0,bd[4]);
317  elevation->SetHighPoint(0,0,bd[5]);
318  }
319  }
320  vtkDataSetMapper * map = vtkDataSetMapper::New();
321  map->SetResolveCoincidentTopologyToShiftZBuffer();
322  if ((!flat)||(with_scalars))
323  {
324  vtkLookupTable * lut = vtkLookupTable::New();
325  lut->SetNumberOfTableValues(number_steps);
326  if (is_greyscale)
327  {
328  lut->SetHueRange(0,0);
329  lut->SetSaturationRange(0,0);
330  if (invert_scalar)
331  lut->SetValueRange(1,0);
332  else
333  lut->SetValueRange(0,1);
334  } else
335  {
336  if (invert_scalar)
337  lut->SetHueRange(0,0.6);
338  else
339  lut->SetHueRange(0.6,0);
340  lut->SetSaturationRange(1,1);
341  lut->SetValueRange(1,1);
342  }
343  map->SetLookupTable(lut);
344  lut->Delete();
345  if (with_scalars)
346  {
347  map->SetScalarRange(m_scalar,M_scalar);
348  map->SetInputData(data);
349  data->Delete();
350  }
351  else
352  {
353  map->SetInputConnection(elevation->GetOutputPort());
354  elevation->Delete();
355  map->SetScalarRange(0,20);
356  }
357  }
358  else
359  {
360  map->SetInputData(data);
361  data->Delete();
362  }
363  vtkActor * actor;
364  if (nlis>20000)
365  {
366  actor = vtkLODActor::New();
367  ((vtkLODActor *)(actor))->SetNumberOfCloudPoints(5000);//set more than default to give a nice impression
368  }
369  else
370  actor = vtkActor::New();
371  actor->SetMapper(map);
372  map->Delete();
373  actor->GetProperty()->SetLineWidth(li_width);
374  actor->GetProperty()->SetInterpolationToFlat();
375  ren->AddActor(actor);
376  actor->Delete();
377  if (tri_file != "")
378  view_triangles(tri_file,flat);
379  renWin->SetSize(640,480);
380  renWin->SetWindowName("view_pt (central)");
381  if (with_axes)
382  {
383  vtkAxes * axes = vtkAxes::New();
384  axes->SetOrigin(0.,0.,0.);
385  axes->SetScaleFactor(1.);
386  vtkLookupTable * lut = vtkLookupTable::New();
387  lut->SetHueRange(0,1);
388  lut->SetSaturationRange(1,1);
389  lut->SetValueRange(1,1);
390  vtkPolyDataMapper * map = vtkPolyDataMapper::New();
391  map->SetInputConnection(axes->GetOutputPort());
392  axes->Delete();
393  map->SetLookupTable(lut);
394  lut->Delete();
395  actor = vtkActor::New();
396  actor->SetMapper(map);
397  map->Delete();
398  actor->GetProperty()->SetLineWidth(3);
399  actor->GetProperty()->SetInterpolationToFlat();
400  ren->AddActor(actor);
401  }
402  renWin->SetSize(renWin->GetScreenSize());
403  renWin->Render();
404  iren->Start();
405  return 0;
406 }
a generic interactor
Definition: a_interactor.h:29
void name(const std::string &name)
Definition: a_interactor.h:39
static a_interactor * New()
void magnification(int val)
Definition: a_interactor.h:37
std::istringstream in
Definition: ply2tri.cxx:32
bool with_camera_file
Definition: view_li.cxx:83
@ OPT_PTSIZE
Definition: view_li.cxx:44
@ OPT_GREY
Definition: view_li.cxx:44
@ OPT_SCALARL
Definition: view_li.cxx:44
@ OPT_MAG
Definition: view_li.cxx:44
@ OPT_CAMERA
Definition: view_li.cxx:44
@ OPT_TRA
Definition: view_li.cxx:44
@ OPT_IMAGE
Definition: view_li.cxx:44
@ OPT_DIR
Definition: view_li.cxx:44
@ OPT_SMOOTH
Definition: view_li.cxx:44
@ OPT_HELP
Definition: view_li.cxx:44
@ OPT_SCALARD
Definition: view_li.cxx:44
@ OPT_TRI
Definition: view_li.cxx:44
@ OPT_SCALAR
Definition: view_li.cxx:44
@ OPT_STEPS
Definition: view_li.cxx:44
@ OPT_AXES
Definition: view_li.cxx:44
int error(int val)
Definition: view_li.cxx:91
CSimpleOpt::SOption g_rgOptions[]
Definition: view_li.cxx:46
bool smooth
Definition: view_li.cxx:82
int main(int argc, char **argv)
Definition: view_li.cxx:100
int * bb
Definition: view_li.cxx:86
bool is_transparent
Definition: view_li.cxx:88
std::string image_file("")
a_pmat pmat
Definition: view_li.cxx:84
bool is_textured
Definition: view_li.cxx:87
vtkRenderer * ren
Definition: view_li.cxx:81
int view_triangles(const std::string &name, bool flat)