Points&Forces (survey)
Software tools facilitating the task of surveying architecture
a_profile_edit.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2002-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 "a_profile_edit.h"
17 
18 #include "vtkCamera.h"
19 #include "vtkRenderWindow.h"
20 #include "vtkRenderWindowInteractor.h"
21 #include "vtkPolyData.h"
22 #include "vtkPoints.h"
23 #include "vtkProperty.h"
24 #include "vtkCellArray.h"
25 #include "vtkPolyDataMapper.h"
26 #include "vtkActor.h"
27 #include "a_profile_edit_interactor.h"
28 
29 // .PORTABILITY : ansi C++
30 
31 //***************************************************************************
32 //---------------------------------------------------------------------------
34 {
35 }
36 //---------------------------------------------------------------------------
37 void a_profile_edit::draw_prof(a_curve_lin * prof, vtkRenderer * ren)
38 {
39  int npts = prof->size();
40  vtkPoints * pts = vtkPoints::New();
41  for (int i = 0; i < npts; i++)
42  pts->InsertNextPoint((*prof)[i]->x(),(*prof)[i]->y(),(*prof)[i]->z());
43  vtkCellArray * vertices = vtkCellArray::New();
44  vtkCellArray * lines = vtkCellArray::New();
45  for (int i = 0; i < npts-1; i++)
46  {
47  vertices->InsertNextCell(1,&i);
48  int p[] = {i, i+1};
49  lines->InsertNextCell(2,p);
50  }
51  vtkPolyData * data = vtkPolyData::New();
52  data->SetPoints(pts);
53  pts->Delete();
54  data->SetVerts(vertices);
55  vertices->Delete();
56  data->SetLines(lines);
57  lines->Delete();
58  vtkPolyDataMapper * map = vtkPolyDataMapper::New();
59  map->SetInput(data);
60  data->Delete();
61  vtkActor * actor = vtkActor::New();
62  actor->SetMapper(map);
63  map->Delete();
64  actor->GetProperty()->SetPointSize(3);
65  actor->PickableOff();
66  actor->DragableOff();
67  ren->AddActor(actor);
68  actor->Delete();
69  pts->GetBounds(bd_);
70 }
71 //---------------------------------------------------------------------------
72 void a_profile_edit::draw_plan(vtkRenderer * ren)
73 {
74  double bx = (bd_[1]-bd_[0])/2.;
75  double by = (bd_[3]-bd_[2])/2.;
76  vtkPoints * pts = vtkPoints::New();
77  pts->InsertNextPoint(bd_[0]-bx,bd_[2]-by,0.);
78  pts->InsertNextPoint(bd_[1]+bx,bd_[2]-by,0.);
79  pts->InsertNextPoint(bd_[0]-bx,bd_[3]+by,0.);
80  pts->InsertNextPoint(bd_[1]+bx,bd_[3]+by,0.);
81  vtkCellArray * tri = vtkCellArray::New();
82  int pp[] = {0,1,2};
83  tri->InsertNextCell(3,pp);
84  pp[0] = 1; pp[1] = 3; pp[2] = 2;
85  tri->InsertNextCell(3,pp);
86  vtkPolyData * data = vtkPolyData::New();
87  data->SetPoints(pts);
88  pts->Delete();
89  data->SetPolys(tri);
90  tri->Delete();
91  vtkPolyDataMapper * map = vtkPolyDataMapper::New();
92  map->SetInput(data);
93  data->Delete();
94  map->SetResolveCoincidentTopologyToPolygonOffset();
95  // map->SetResolveCoincidentTopologyZShift(.01);
96  vtkActor * actor = vtkActor::New();
97  actor->SetMapper(map);
98  actor->GetProperty()->SetColor(.1,.1,.1);
99  actor->PickableOff();
100  actor->DragableOff();
101  map->Delete();
102  ren->AddActor(actor);
103  actor->Delete();
104 }
105 //---------------------------------------------------------------------------
106 void getcolor(int ref, double * RGB)
107 {
108  switch (ref)
109  {
110  case 0:
111  RGB[0] = 1; RGB[1] = 0; RGB[2] = 0;
112  break;
113  case 1:
114  RGB[0] = 0; RGB[1] = 1; RGB[2] = 0;
115  break;
116  case 2:
117  RGB[0] = 0; RGB[1] = 0; RGB[2] = 1;
118  break;
119  case 3:
120  RGB[0] = 1; RGB[1] = 1; RGB[2] = 0;
121  break;
122  case 4:
123  RGB[0] = 0; RGB[1] = 1; RGB[2] = 1;
124  break;
125  default:
126  RGB[0] = 1; RGB[1] = 0; RGB[2] = 1;
127  }
128 }
129 //---------------------------------------------------------------------------
130 void a_profile_edit::draw_refprof(a_curve_lin * prof, vtkRenderer * ren)
131 {
132  static int ref = -1;
133  ref++;
134  int npts = prof->size();
135  vtkPoints * pts = vtkPoints::New();
136  for (int i = 0; i < npts; i++)
137  pts->InsertNextPoint((*prof)[i]->x(),(*prof)[i]->y(),(*prof)[i]->z());
138  double bx = (bd_[1]-bd_[0])/50.;
139  double by = (bd_[3]-bd_[2])/50.;
140  pts->InsertNextPoint(0.,0.,0.);
141  pts->InsertNextPoint(bx,0.,0.);
142  pts->InsertNextPoint(0.,by,0.);
143  pts->InsertNextPoint(bx,by,0.);
144  vtkCellArray * vertices = vtkCellArray::New();
145  for (int i = 0; i < npts; i++)
146  vertices->InsertNextCell(1,&i);
147  vtkCellArray * handle = vtkCellArray::New();
148  int p[] = {npts,npts+1,npts+2};
149  handle->InsertNextCell(3,p);
150  p[0] = npts+1; p[1] = npts+3; p[2] = npts+2;
151  handle->InsertNextCell(3,p);
152  vtkPolyData * data = vtkPolyData::New();
153  data->SetPoints(pts);
154  pts->Delete();
155  data->SetVerts(vertices);
156  vertices->Delete();
157  data->SetPolys(handle);
158  handle->Delete();
159  vtkPolyDataMapper * map = vtkPolyDataMapper::New();
160  map->SetInput(data);
161  data->Delete();
162  vtkActor * actor = vtkActor::New();
163  actor->SetMapper(map);
164  map->Delete();
165  actor->GetProperty()->SetPointSize(2);
166  double RGB[3];
167  getcolor(ref,RGB);
168  actor->GetProperty()->SetColor(RGB);
169  ren->AddActor(actor);
170  actor->Delete();
171 }
172 //---------------------------------------------------------------------------
173 void a_profile_edit::show(int precision)
174 {
175  a_point x_axis = profile_.x_axis();
176  a_point y_axis = profile_.y_axis();
177  a_point origin = *profile_[0];
178  std::vector<a_point> newpoints;
179 
181  vtkRenderer * ren = vtkRenderer::New();
182  vtkRenderWindow * renWin = vtkRenderWindow::New();
183  renWin->AddRenderer(ren);
184  ren->Delete();
185  a_profile_edit_interactor * style = a_profile_edit_interactor::New();
186  style->setpoints(&newpoints);
187  style->SetPickColor(.5,1.,1.);
188  vtkRenderWindowInteractor * iren = vtkRenderWindowInteractor::New();
189  iren->SetRenderWindow(renWin);
190  renWin->Delete();
191  iren->SetInteractorStyle(style);
192 
193  this->draw_prof(&profile_,ren);
194  this->draw_plan(ren);
195 
196  for (auto prof:profiles_ref_)
197  this->draw_refprof(prof,ren);
198 
199  ren->GetActiveCamera()->ParallelProjectionOn();
200  renWin->SetSize(640,480);
201  renWin->Render();
202 
203  renWin->SetWindowName("profile_edit");
204  iren->Start();
205 
206  if (newpoints.size()>0)
207  {
208  a_curve_lin newprofile;
209  for (auto pt:newpoints)
210  {
211  auto x = new a_point(pt);
212  newprofile.addpoint(x);
213  }
214  newprofile.place3D(origin,x_axis,y_axis);
215  std::cout << std::fixed << std::setprecision(precision);
216  std::cout << newprofile;
217  }
218 }
219 //***************************************************************************
220 //---------------------------------------------------------------------------
221 std::istream& operator>> (std::istream& in, a_profile_edit & p)
222 {
223  in >> p.profile_;
224  return in;
225 }
226 //---------------------------------------------------------------------------
227 std::ostream& operator<< (std::ostream& o, const a_profile_edit & p)
228 {
229  o << p.profile_;
230  return o;
231 }
std::ostream & operator<<(std::ostream &o, const a_profile_edit &p)
std::istream & operator>>(std::istream &in, a_profile_edit &p)
void getcolor(int ref, double *RGB)
a curve with linear interpolation
Definition: a_curve_lin.h:28
void addpoint(a_point *pt)
Definition: a_curve.h:44
a_curve & flatten2D()
Definition: a_curve.cxx:207
int size() const
Definition: a_curve.h:47
a_point y_axis() const
Definition: a_curve.h:76
a_curve & place3D(a_point &origin, a_point &x_axis, a_point &y_axis)
Definition: a_curve.cxx:219
a_point x_axis() const
Definition: a_curve.h:75
void show(int precision)
std::vector< a_curve_lin * > profiles_ref_
void draw_refprof(a_curve_lin *prof, vtkRenderer *ren)
a_curve_lin profile_
void draw_plan(vtkRenderer *ren)
void draw_prof(a_curve_lin *prof, vtkRenderer *ren)
void tri(const uint32_t i, const uint32_t j)
Definition: generate.cxx:50
std::istringstream in
Definition: ply2tri.cxx:32
Definition: stlb2stla.cxx:21
vtkRenderer * ren
Definition: view_li.cxx:81