Points&Forces (survey)
Software tools facilitating the task of surveying architecture
ply2tri.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2013-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 <iostream>
17 #include <sstream>
18 #include <string>
19 #include <vector>
20 
21 #include "vtkCellArray.h"
22 #include "vtkCleanPolyData.h"
23 #include "vtkPLYReader.h"
24 #include "vtkPoints.h"
25 #include "vtkPolyData.h"
26 #include "vtkTriangleFilter.h"
27 
28 #include "SimpleOpt.h"
29 
31 
32 std::istringstream in;
33 int n_pt;
34 int n_tri;
35 //prepare output streams: 'on' (to stdout) and 'off' (to sink)
36 std::streambuf* on = std::cout.rdbuf();
37 std::ofstream fout("/dev/null");
38 std::streambuf* off = fout.rdbuf();
39 
40 //---------------------------------------------------------------------------
41 CSimpleOpt::SOption g_rgOptions[] = {
42  { OPT_PRECISION, _T("-p"), SO_REQ_SEP },
43  { OPT_PRECISION, _T("--precision"), SO_REQ_SEP },
44  { OPT_HELP, _T("-?"), SO_NONE },
45  { OPT_HELP, _T("-h"), SO_NONE },
46  { OPT_HELP, _T("--help"), SO_NONE },
47  SO_END_OF_OPTIONS
48 };
49 //---------------------------------------------------------------------------
50 int error(int val)
51 {
52 #include "ply2tri.help"
53  return val;
54 }
55 //---------------------------------------------------------------------------
56 void set_cout(std::streambuf* buf)
57 {
58  std::cout.rdbuf(buf);
59 }
60 //---------------------------------------------------------------------------
61 int main( int argc, char *argv[])
62 {
63  int pre = 3;
64  CSimpleOpt args(argc, argv, g_rgOptions);
65  while (args.Next())
66  {
67  if (args.LastError() == SO_SUCCESS)
68  {
69  if (args.OptionId() == OPT_HELP)
70  return error(0);
71  else if (args.OptionId() == OPT_PRECISION)
72  {
73  std::ostringstream o;
74  o << args.OptionArg();
75  std::istringstream in(o.str().c_str());
76  int val;
77  in >> val;
78  if ((val>-1)&&(val<10))
79  pre = val;
80  }
81  else
82  return error(-1);
83  } else {
84  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
85  return error(args.LastError());
86  }
87  }
88  if (args.FileCount() != 0) return error(-3);
89  vtkPLYReader * reader = vtkPLYReader::New();
90  reader->SetFileName("/dev/stdin");
91  //don't output anything while reading
92  set_cout(off);
93  reader->Update();
94  set_cout(on);
95  vtkPolyData * output = reader->GetOutput();
96  vtkCellArray * triangles_n = output->GetPolys();
97  int n_tri = output->GetNumberOfPolys();
98  if (n_tri > 0)
99  {
100  vtkCleanPolyData * clean = vtkCleanPolyData::New();
101  clean->SetInputConnection(reader->GetOutputPort());
102  reader->Delete();
103  clean->ConvertPolysToLinesOff();
104  vtkTriangleFilter * onlytri = vtkTriangleFilter::New();
105  onlytri->SetInputConnection(clean->GetOutputPort());
106  clean->Delete();
107  onlytri->Update();
108  output = onlytri->GetOutput();
109  }
110  vtkPoints * pts_n = output->GetPoints();
111  int n_pts = pts_n->GetNumberOfPoints();
112  std::cout << n_pts << std::endl;
113  std::cout << std::fixed << std::setprecision(pre);
114  for (unsigned int k = 0; k < n_pts; k++)
115  {
116  double x[3];
117  pts_n->GetPoint(k,x);
118  std::cout << x[0] << ' ' << x[1] << ' ' << x[2] << std::endl;
119  }
120  triangles_n = output->GetPolys();
121  n_tri = output->GetNumberOfPolys();
122  std::cout << n_tri << std::endl;
123  triangles_n->InitTraversal();
124  for (unsigned int k = 0; k < n_tri; k++)
125  {
126  const vtkIdType * ref_pt = nullptr;
127  vtkIdType npt = 3;
128  triangles_n->GetNextCell(npt,ref_pt);
129  std::cout << ref_pt[0] << ' ' << ref_pt[1] << ' ' << ref_pt[2] << std::endl;
130  }
131  return 0;
132 }
int pre
Definition: bspline.cxx:49
int n_pt
Definition: ply2tri.cxx:33
int main(int argc, char *argv[])
Definition: ply2tri.cxx:61
int error(int val)
Definition: ply2tri.cxx:50
CSimpleOpt::SOption g_rgOptions[]
Definition: ply2tri.cxx:41
std::streambuf * on
Definition: ply2tri.cxx:36
void set_cout(std::streambuf *buf)
Definition: ply2tri.cxx:56
std::istringstream in
Definition: ply2tri.cxx:32
std::streambuf * off
Definition: ply2tri.cxx:38
int n_tri
Definition: ply2tri.cxx:34
std::ofstream fout("/dev/null")
@ OPT_PRECISION
Definition: ply2tri.cxx:30
@ OPT_HELP
Definition: ply2tri.cxx:30