Points&Forces (survey)
Software tools facilitating the task of surveying architecture
a_pointcloud.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2002-2022 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_pointcloud.h"
17 #include "vtkPoints.h"
18 #include "vtkCellArray.h"
19 #include "vtkPolyDataMapper.h"
20 #include "vtkProperty.h"
21 #include "vtkMatrix4x4.h"
22 
23 //---------------------------------------------------------------------------
25 {
26  int pt[2];
27  vtkPoints * points = vtkPoints::New();
28  polydata_->SetPoints(points);
29  points->Delete();
30  has_points_ = false;
31  this->logname();
32 }
33 //---------------------------------------------------------------------------
35 {
36 }
37 //---------------------------------------------------------------------------
38 const std::string a_pointcloud::help()
39 {
40  std::ostringstream o;
41  o << "************" << std::endl;
42  o << "a_pointcloud" << std::endl;
43  o << "************" << std::endl;
44  o << "The class derives from a_element." << std::endl;
45  o << "An object of this class is meant to store, manipulate and display a point cloud in a_canvas object" << std::endl;
46  o << "Commands:" << std::endl;
47  o << "--------" << std::endl;
48  o << "np: get number of points" << std::endl;
49  o << "clear: empty the object" << std::endl;
50  o << "eraselastpoint: erase the last drawn point" << std::endl;
51  o << "point 'x''y' 'z': add a point" << std::endl;
52  o << "point 'a_point': add a point" << std::endl;
53  o << "append 'a_pointcloud': add points from another a_pointcloud" << std::endl;
54  o << "dxfout 'file': export a dxf file with the points" << std::endl;
55  o << a_element::help();
56  return o.str();
57 }
58 //---------------------------------------------------------------------------
59 int a_pointcloud::np() const
60 {
61  return polydata_->GetVerts()->GetNumberOfCells();
62 }
63 //---------------------------------------------------------------------------
64 void a_pointcloud::point(double x, double y, double z)
65 {
66  vtkPoints * points = polydata_->GetPoints();
67  vtkIdType pt = points->InsertNextPoint(x,y,z);
68  polydata_->GetVerts()->InsertNextCell(1,&pt);
69  points->Modified();
70  empty_ = false;
71  has_points_ = true;
72 }
73 //---------------------------------------------------------------------------
74 void a_pointcloud::point(double x[3])
75 {
76  this->point(x[0],x[1],x[2]);
77 }
78 //---------------------------------------------------------------------------
79 void a_pointcloud::point(const a_point& p)
80 {
81  this->point(p.x(),p.y(),p.z());
82 }
83 //---------------------------------------------------------------------------
85 {
86  int pt[1];
87  vtkCellArray * old_pts = polydata_->GetVerts();
88  vtkCellArray * new_pts = vtkCellArray::New();
89  old_pts->InitTraversal();
90  for (int i = 0; i < old_pts->GetNumberOfCells()-1; i++)
91  {
92  vtkIdType npts;
93  const vtkIdType * pts = nullptr;
94  old_pts->GetNextCell(npts, pts);
95  new_pts->InsertNextCell(npts, pts);
96  }
97  polydata_->SetVerts(new_pts);
98  new_pts->Modified();
99  new_pts->Delete();
100  this->render();
101 }
102 //---------------------------------------------------------------------------
103 void a_pointcloud::dxfout(std::ostream& o) const
104 {
105  vtkMatrix4x4 * mat = actor_->GetMatrix();
106  vtkCellArray * cells;
107  cells = polydata_->GetVerts();
108  int n_pts = polydata_->GetNumberOfVerts();
109  cells->InitTraversal();
110  for (int k = 0; k < n_pts; k++)
111  {
112  vtkIdType npt;
113  const vtkIdType * ref_pt = nullptr;
114  double pt[4];
115  pt[3] = 1.;
116  cells->GetNextCell(npt,ref_pt);
117  polydata_->GetPoint(ref_pt[0],pt);
118  mat->MultiplyPoint(pt,pt);
119  o << " 0" << std::endl << "POINT" << std::endl;
120  o << " 8" << std::endl << name_ << std::endl;
121  o << " 10" << std::endl << pt[0] << std::endl;
122  o << " 20" << std::endl << pt[1] << std::endl;
123  o << " 30" << std::endl << pt[2] << std::endl;
124  }
125 }
126 //---------------------------------------------------------------------------
127 std::vector<a_point> a_pointcloud::cloud() const
128 {
129  vtkMatrix4x4 * mat = actor_->GetMatrix();
130  vtkCellArray * cells;
131  cells = polydata_->GetVerts();
132  int n_pts = polydata_->GetNumberOfVerts();
133  std::vector<a_point> cloud;
134  cells->InitTraversal();
135  for (int k = 0; k < n_pts; k++)
136  {
137  vtkIdType npt;
138  const vtkIdType * ref_pt = nullptr;
139  double pt[4];
140  pt[3] = 1.;
141  cells->GetNextCell(npt,ref_pt);
142  polydata_->GetPoint(ref_pt[0],pt);
143  mat->MultiplyPoint(pt,pt);
144  a_point point(pt[0],pt[1],pt[2]);
145  cloud.push_back(point);
146  }
147  return cloud;
148 }
149 //---------------------------------------------------------------------------
150 void a_pointcloud::read(std::istream& in)
151 {
152  int n_pts;
153  double x[3];
154  in >> n_pts;
155  for (int i=0; i<n_pts; i++)
156  {
157  in >> x[0] >> x[1] >> x[2];
158  this->point(x);
159  }
160 }
161 //---------------------------------------------------------------------------
162 void a_pointcloud::write(std::ostream& o) const
163 {
164  vtkMatrix4x4 * mat = actor_->GetMatrix();
165  vtkCellArray * cells = polydata_->GetVerts();
166  int n_pts = polydata_->GetNumberOfVerts();
167  o << n_pts << std::endl;
168  cells->InitTraversal();
169  for (int k = 0; k < n_pts; k++)
170  {
171  vtkIdType npt;
172  const vtkIdType * ref_pt = nullptr;
173  double pt[4];
174  pt[3] = 1.;
175  cells->GetNextCell(npt,ref_pt);
176  polydata_->GetPoint(ref_pt[0],pt);
177  mat->MultiplyPoint(pt,pt);
178  o << pt[0] << " " << pt[1] << " " << pt[2] << std::endl;
179  }
180 }
181 //---------------------------------------------------------------------------
183 {
184  name_ = l.name();
185  color(l.R(),l.G(),l.B());
186  thickness(l.thickness());
187  vtkCellArray * cells = l.polydata_->GetVerts();
188  int n_pts = l.polydata_->GetNumberOfVerts();
189  cells->InitTraversal();
190  for (int k = 0; k < n_pts; k++)
191  {
192  vtkIdType npt;
193  const vtkIdType * ref_pt = nullptr;
194  double pt[4];
195  pt[3] = 1.;
196  cells->GetNextCell(npt,ref_pt);
197  l.polydata_->GetPoint(ref_pt[0],pt);
198  this->point(pt[0],pt[1],pt[2]);
199  }
200 }
201 //---------------------------------------------------------------------------
203 {
204  polydata_->GetVerts()->Reset();
205  polydata_->GetVerts()->Squeeze();
206  polydata_->GetPoints()->Reset();
207  polydata_->GetPoints()->Squeeze();
208  has_points_ = false;
209 }
210 //---------------------------------------------------------------------------
212 {
213  polydata_->GetPoints()->Modified();
215 }
layer used by screen to draw vector graphics
Definition: a_element.h:39
vtkPolyData * polydata_
Definition: a_element.h:125
void logname()
Definition: a_element.cxx:109
void name(std::string &aname)
Definition: a_element.h:49
static const std::string help()
Definition: a_element.cxx:66
void thickness(const int)
Definition: a_element.cxx:227
void reset_mapper_clipping()
Definition: a_element.cxx:484
bool empty_
Definition: a_element.h:132
void R(double v)
Definition: a_element.cxx:194
std::string name_
Definition: a_element.h:123
void G(double v)
Definition: a_element.cxx:205
void color(double r, double g, double b)
Definition: a_element.cxx:180
vtkActor * actor_
Definition: a_element.h:124
void render()
Definition: a_element.cxx:127
int thickness() const
Definition: a_element.h:69
void B(double v)
Definition: a_element.cxx:216
layer used by screen to draw vector graphics
Definition: a_pointcloud.h:32
virtual void read(std::istream &in)
void append(const a_pointcloud &)
int np() const
std::vector< a_point > cloud() const
bool has_points_
Definition: a_pointcloud.h:52
void point(double x, double y, double z)
void reset_mapper_clipping()
void dxfout(std::ostream &o) const
void eraselastpoint()
static const std::string help()
virtual void write(std::ostream &o) const
std::istringstream in
Definition: ply2tri.cxx:32
Definition: stlb2stla.cxx:21