Points&Forces (survey)
Software tools facilitating the task of surveying architecture
a_trianglecloud.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2002-2016 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_trianglecloud.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  vtkCellArray * triangles = vtkCellArray::New();
29  polydata_->SetPoints(points);
30  points->Delete();
31  polydata_->SetPolys(triangles);
32  triangles->Delete();
33  has_triangles_ = false;
34  this->logname();
35 }
36 //---------------------------------------------------------------------------
38 {
39  int pt[2];
40  vtkPoints * points = vtkPoints::New();
41  vtkCellArray * triangles = vtkCellArray::New();
42  polydata_->SetPoints(points);
43  points->Delete();
44  polydata_->SetPolys(triangles);
45  triangles->Delete();
46  has_triangles_ = false;
47  polydata_->DeepCopy(c.polydata_);
48  this->logname();
49 }
50 //---------------------------------------------------------------------------
52 {
53  this->clear();
54 }
55 //---------------------------------------------------------------------------
56 const std::string a_trianglecloud::help()
57 {
58  std::ostringstream o;
59  o << "***************" << std::endl;
60  o << "a_trianglecloud" << std::endl;
61  o << "***************" << std::endl;
62  o << "The class derives from a_element." << std::endl;
63  o << "An object of this class is meant to store, manipulate and display a triangle cloud in a_canvas object" << std::endl;
64  o << "Commands:" << std::endl;
65  o << "--------" << std::endl;
66  o << "nt: get number of triangles" << std::endl;
67  o << "clear: empty the object" << std::endl;
68  // o << "eraselasttriangle: erase the last drawn triangle" << std::endl;
69  o << "triangle 'x1' 'y1' 'z1' 'x2' 'y2' 'z2' 'x3' 'y3' 'z3': add a triangle" << std::endl;
70  o << "triangle 'a_point' 'a_point' 'a_point': add a triangle" << std::endl;
71  o << "append 'a_pointcloud': add triangles from another a_trianglecloud" << std::endl;
72  o << a_element::help();
73  return o.str();
74 }
75 //---------------------------------------------------------------------------
77 {
78  polydata_->GetPolys()->Reset();
79  polydata_->GetPolys()->Squeeze();
80  // std::cout << polydata_->GetPolys()->GetSize() << std::endl;
81  // std::cout << polydata_->GetPoints()->GetActualMemorySize() << std::endl;
82  polydata_->GetPoints()->Reset();
83  polydata_->GetPoints()->Squeeze();
84  // std::cout << polydata_->GetPoints()->GetActualMemorySize() << std::endl;
85  has_triangles_ = false;
86 }
87 //---------------------------------------------------------------------------
89 {
90  for (int i=0; i<c.nt(); i++)
91  {
92  a_point p1,p2,p3;
93  c.gettriangle(i,p1,p2,p3);
94  this->addtriangle(p1,p2,p3);
95  }
96  return (*this);
97 }
98 //---------------------------------------------------------------------------
100 {
101  return polydata_->GetPolys()->GetNumberOfCells();
102 }
103 //---------------------------------------------------------------------------
104 void a_trianglecloud::dxfout(std::ostream& o) const
105 {
106 }
107 //---------------------------------------------------------------------------
108 void a_trianglecloud::read(std::istream& in)
109 {
110  int n_pts;
111  double x[3];
112  in >> n_pts;
113  vtkPoints * points = polydata_->GetPoints();
114  for (int i=0; i<n_pts; i++)
115  {
116  in >> x[0] >> x[1] >> x[2];
117  points->InsertNextPoint(x);
118  }
119  int n_tris;
120  in >> n_tris;
121  vtkCellArray * triangles = polydata_->GetPolys();
122  for (int k = 0; k < n_tris; k++)
123  {
124  vtkIdType p[3];
125  for (int l = 0; l < 3; l++)
126  in >> p[l];
127  triangles->InsertNextCell(3,p);
128  }
129 }
130 //---------------------------------------------------------------------------
131 void a_trianglecloud::write(std::ostream& o) const
132 {
133  vtkMatrix4x4 * mat = actor_->GetMatrix();
134  vtkPoints * pts_n = polydata_->GetPoints();
135  int n_pts = pts_n->GetNumberOfPoints();
136  o << n_pts << std::endl;
137  for (int k = 0; k < n_pts; k++)
138  {
139  double x[4];
140  x[3] = 1.;
141  pts_n->GetPoint(k,x);
142  mat->MultiplyPoint(x,x);
143  o << x[0] << '\t' << x[1] << '\t' << x[2] << std::endl;
144  }
145  vtkCellArray * triangles_n = polydata_->GetPolys();
146  int n_tri = polydata_->GetNumberOfPolys();
147  o << n_tri << std::endl;
148  triangles_n->InitTraversal();
149  for (int k = 0; k < n_tri; k++)
150  {
151  vtkIdType npt;
152  const vtkIdType * ref_pt = nullptr;
153  triangles_n->GetNextCell(npt,ref_pt);
154  o << ref_pt[0] << '\t' << ref_pt[1] << '\t' << ref_pt[2] << std::endl;
155  }
156 }
157 //---------------------------------------------------------------------------
158 /*int a_trianglecloud::addpoint(const a_point& p)
159  {
160  vtkMatrix4x4 * mat = actor_->GetMatrix();
161  vtkPoints * points = polydata_->GetPoints();
162  int n_pts = points->GetNumberOfPoints();
163  for (int k = 0; k < n_pts; k++)
164  {
165  double x[4];
166  x[3] = 1.;
167  points->GetPoint(k,x);
168  mat->MultiplyPoint(x,x);
169  a_point pn(x[0],x[1],x[2]);
170  if ((pn-p).norm() < small_)
171  return k;
172  }
173  double x[] = {p.x(), p.y(), p.z()};
174  points->InsertNextPoint(x);
175  return n_pts;
176  }*/
177 
178 //---------------------------------------------------------------------------
179 void a_trianglecloud::gettriangle(const int ref, a_point& p1, a_point& p2, a_point& p3) const
180 {
181  //in case of problem, watch a_linecloud, maybe better method
182  if ((ref<0)||(ref>this->nt()))
183  return;
184  vtkMatrix4x4 * mat = actor_->GetMatrix();
185  vtkCellArray * triangles = polydata_->GetPolys();
186  vtkIdType npt;
187  const vtkIdType * ref_pt = nullptr;
188  triangles->GetCell(ref, npt, ref_pt);
189  vtkPoints * points = polydata_->GetPoints();
190  double x[4];
191  x[3] = 1.;
192  points->GetPoint(ref_pt[0],x);
193  mat->MultiplyPoint(x,x);
194  p1.set(x[0],x[1],x[2]);
195  points->GetPoint(ref_pt[1],x);
196  mat->MultiplyPoint(x,x);
197  p2.set(x[0],x[1],x[2]);
198  points->GetPoint(ref_pt[2],x);
199  mat->MultiplyPoint(x,x);
200  p3.set(x[0],x[1],x[2]);
201 }
202 //---------------------------------------------------------------------------
203 int a_trianglecloud::addtriangle(const a_point& p1, const a_point& p2, const a_point& p3)
204 {
205  vtkCellArray * triangles = polydata_->GetPolys();
206  vtkIdType p[3];
207  p[0] = this->addpoint(p1);
208  p[1] = this->addpoint(p2);
209  p[2] = this->addpoint(p3);
210  return triangles->InsertNextCell(3,p);
211 }
212 //---------------------------------------------------------------------------
214 {
215  polydata_->BuildLinks();
216  polydata_->DeleteCell(this->nt()-1);
217  polydata_->RemoveDeletedCells();
218  polydata_->Modified();
219  this->render();
220 }
221 //---------------------------------------------------------------------------
223 {
224  (*this) += l;
225 }
226 //---------------------------------------------------------------------------
228 {
229  polydata_->GetPoints()->Modified();
230  polydata_->GetPolys()->Modified();
232 }
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
void reset_mapper_clipping()
Definition: a_element.cxx:484
vtkActor * actor_
Definition: a_element.h:124
void render()
Definition: a_element.cxx:127
layer used by screen to draw vector graphics
void dxfout(std::ostream &o) const
void append(const a_trianglecloud &)
static const std::string help()
void read(std::istream &in)
void gettriangle(const int ref, a_point &p1, a_point &p2, a_point &p3) const
a_trianglecloud & operator+=(const a_trianglecloud &c)
int addtriangle(const a_point &p1, const a_point &p2, const a_point &p3)
void write(std::ostream &o) const
std::istringstream in
Definition: ply2tri.cxx:32
int n_tri
Definition: ply2tri.cxx:34
Definition: stlb2stla.cxx:21