Points&Forces (survey)
Software tools facilitating the task of surveying architecture
a_linecloud.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_linecloud.h"
17 #include "vtkCell.h"
18 #include "vtkCleanPolyData.h"
19 #include "vtkPoints.h"
20 #include "vtkCellArray.h"
21 #include "vtkPolyData.h"
22 #include "vtkPolyDataMapper.h"
23 #include "vtkProperty.h"
24 #include "vtkMatrix4x4.h"
25 
26 //---------------------------------------------------------------------------
28 {
29  int pt[2];
30  vtkPoints * points = vtkPoints::New();
31  vtkCellArray * lines = vtkCellArray::New();
32  polydata_->SetPoints(points);
33  points->Delete();
34  polydata_->SetLines(lines);
35  lines->Delete();
36  has_lines_ = false;
37  this->logname();
38 }
39 //---------------------------------------------------------------------------
41 {
42  int pt[2];
43  vtkPoints * points = vtkPoints::New();
44  vtkCellArray * lines = vtkCellArray::New();
45  polydata_->SetPoints(points);
46  points->Delete();
47  polydata_->SetLines(lines);
48  lines->Delete();
49  has_lines_ = false;
50  polydata_->DeepCopy(c.polydata_);
51  this->logname();
52 }
53 //---------------------------------------------------------------------------
55 {
56  this->clear();
57 }
58 //---------------------------------------------------------------------------
59 const std::string a_linecloud::help()
60 {
61  std::ostringstream o;
62  o << "***********" << std::endl;
63  o << "a_linecloud" << std::endl;
64  o << "***********" << std::endl;
65  o << "The class derives from a_element." << std::endl;
66  o << "An object of this class is meant to store, manipulate and display a line cloud in a_canvas object" << std::endl;
67  o << "Commands:" << std::endl;
68  o << "--------" << std::endl;
69  o << "nl: get number of lines" << std::endl;
70  o << "clear: empty the object" << std::endl;
71  // o << "eraselastline: erase the last drawn line" << std::endl;
72  o << "line 'x1' 'y1' 'z2' 'x1' 'y2' 'z': add a line" << std::endl;
73  o << "line 'a_point' 'a_point'" << std::endl;
74  o << "append 'a_linecloud': add lines from another a_linecloud" << std::endl;
75  o << "dxfout 'file': export a dxf file with the points" << std::endl;
76  o << a_element::help();
77  return o.str();
78 }
79 //---------------------------------------------------------------------------
81 {
82  polydata_->GetLines()->Reset();
83  polydata_->GetLines()->Squeeze();
84  polydata_->GetPoints()->Reset();
85  polydata_->GetPoints()->Squeeze();
86  has_lines_ = false;
87 }
88 //---------------------------------------------------------------------------
90 {
91  for (int i=0; i<c.nl(); i++)
92  {
93  a_point p1,p2;
94  c.getline(i,p1,p2);
95  this->addline(p1,p2);
96  }
97  return (*this);
98 }
99 //---------------------------------------------------------------------------
100 void a_linecloud::dxfout(std::ostream& o) const
101 {
102 }
103 //---------------------------------------------------------------------------
104 int a_linecloud::nl() const
105 {
106  return polydata_->GetLines()->GetNumberOfCells();
107 }
108 //---------------------------------------------------------------------------
109 void a_linecloud::read(std::istream& in)
110 {
111  int n_pts;
112  double x[3];
113  in >> n_pts;
114  vtkPoints * points = polydata_->GetPoints();
115  vtkCellArray * lines = polydata_->GetLines();
116  for (int i=0; i<n_pts; i++)
117  {
118  vtkIdType r[] = {0,0};
119  for (int j=0; j<2; j++)
120  {
121  in >> x[0] >> x[1] >> x[2];
122  r[j] = points->InsertNextPoint(x);
123  }
124  int ref = lines->InsertNextCell(2,r);
125  }
126  vtkCleanPolyData * clean = vtkCleanPolyData::New();
127  clean->SetInputData(polydata_);
128  clean->SetTolerance(0.);
129  clean->PointMergingOn();
130  clean->Update();
131  polydata_->DeepCopy(clean->GetOutput());
132 }
133 //---------------------------------------------------------------------------
134 void a_linecloud::write(std::ostream& o) const
135 {
136  vtkMatrix4x4 * mat = actor_->GetMatrix();
137  vtkCellArray * lines_n = polydata_->GetLines();
138  int n_lis = polydata_->GetNumberOfLines();
139  o << n_lis << std::endl;
140  lines_n->InitTraversal();
141  for (int k = 0; k < n_lis; k++)
142  {
143  vtkIdType npt;
144  const vtkIdType * ref_pt = nullptr;
145  double pt1[4];
146  double pt2[4];
147  pt2[3] = pt1[3] = 1.;
148  lines_n->GetNextCell(npt,ref_pt);
149  polydata_->GetPoint(ref_pt[0],pt1);
150  polydata_->GetPoint(ref_pt[1],pt2);
151  mat->MultiplyPoint(pt1,pt1);
152  mat->MultiplyPoint(pt2,pt2);
153  o << pt1[0] << ' ' << pt1[1] << ' ' << pt1[2] << ' ';
154  o << pt2[0] << ' ' << pt2[1] << ' ' << pt2[2] << std::endl;
155  }
156 }
157 //---------------------------------------------------------------------------
158 void a_linecloud::getline(const int ref, a_point& p1, a_point& p2) const
159 {
160  if ((ref<0)||(ref>this->nl()))
161  return;
162  vtkMatrix4x4 * mat = actor_->GetMatrix();
163  vtkIdType npt;
164  const vtkIdType * ref_pt = nullptr;
165  vtkCell * cell = polydata_->GetCell(ref);
166  vtkPoints * points = polydata_->GetPoints();
167  double x[4];
168  x[3] = 1.;
169  points->GetPoint(cell->GetPointId(0),x);
170  mat->MultiplyPoint(x,x);
171  p1.set(x[0],x[1],x[2]);
172  points->GetPoint(cell->GetPointId(1),x);
173  mat->MultiplyPoint(x,x);
174  p2.set(x[0],x[1],x[2]);
175 }
176 //---------------------------------------------------------------------------
177 int a_linecloud::addline(const a_point& p1, const a_point& p2)
178 {
179  vtkCellArray * lines = polydata_->GetLines();
180  vtkIdType p[2];
181  p[0] = this->addpoint(p1);
182  p[1] = this->addpoint(p2);
183  int ref = lines->InsertNextCell(2,p);
184  polydata_->GetPoints()->Modified();
185  return ref;
186 }
187 //---------------------------------------------------------------------------
189 {
190  (*this) += l;
191 }
192 
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
int addpoint(const a_point &p)
Definition: a_element.cxx:643
static const std::string help()
Definition: a_element.cxx:66
vtkActor * actor_
Definition: a_element.h:124
layer used by screen to draw vector graphics
Definition: a_linecloud.h:30
a_linecloud & operator+=(const a_linecloud &c)
Definition: a_linecloud.cxx:89
int nl() const
void clear()
Definition: a_linecloud.cxx:80
int addline(const a_point &p1, const a_point &p2)
bool has_lines_
Definition: a_linecloud.h:48
static const std::string help()
Definition: a_linecloud.cxx:59
void append(const a_linecloud &)
void write(std::ostream &o) const
void dxfout(std::ostream &o) const
void read(std::istream &in)
void getline(const int ref, a_point &p1, a_point &p2) const
std::istringstream in
Definition: ply2tri.cxx:32
Definition: stlb2stla.cxx:21