Points&Forces (survey)
Software tools facilitating the task of surveying architecture
stlb2stla.cxx
Go to the documentation of this file.
1 /*
2  Copyright 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 <fstream>
18 #include "SimpleOpt.h"
19 
20 //---------------------------------------------------------------------------
21 struct pt {
22  float x;
23  float y;
24  float z;
25 };
26 //---------------------------------------------------------------------------
27 enum { OPT_HELP, OPT_NAME };
28 //---------------------------------------------------------------------------
29 CSimpleOpt::SOption g_rgOptions[] = {
30  { OPT_NAME, _T("-n"), SO_REQ_SEP },
31  { OPT_NAME, _T("--name"), SO_REQ_SEP },
32  { OPT_HELP, _T("-?"), SO_NONE },
33  { OPT_HELP, _T("-h"), SO_NONE },
34  { OPT_HELP, _T("--help"), SO_NONE },
35  SO_END_OF_OPTIONS
36 };
37 //---------------------------------------------------------------------------
38 int error(int val)
39 {
40 #include "stlb2stla.help"
41  return val;
42 }
43 //---------------------------------------------------------------------------
44 int main( int argc, char *argv[] )
45 {
46  std::string name("noname");
47  CSimpleOpt args(argc, argv, g_rgOptions);
48  while (args.Next())
49  {
50  if (args.LastError() == SO_SUCCESS)
51  {
52  if (args.OptionId() == OPT_HELP)
53  return error(0);
54  else if (args.OptionId() == OPT_NAME)
55  name = args.OptionArg();
56  else
57  return error(-1);
58  } else {
59  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
60  return error(args.LastError());
61  }
62  }
63  if (args.FileCount() < 1) return error(-2);
64  std::ifstream in(args.File(0), std::ios::in | std::ios::binary);
65  if (!in) {std::cerr << "file '" << args.File(0) << "' does not exist" << std::endl; return -3;}
66  char buffer[80];
67  in.read(buffer,80);
68  int32_t npts;
69  in.read((char *) &npts, sizeof(npts));
70  std::cout << "solid " << name << std::endl;
71  for (unsigned int i=0; i<npts; i++)
72  {
73  pt n;
74  in.read((char *)&n, sizeof(n));
75  std::cout << " facet normal ";
76  std::cout << n.x << " " << n.y << " " << n.z << std::endl;
77  std::cout << " outer loop" << std::endl;
78  for (short j=0; j<3; j++)
79  {
80  pt p;
81  in.read((char *)&p, sizeof(p));
82  std::cout << " vertex " << p.x << " " << p.y << " " << p.z << std::endl;
83  }
84  int16_t code;
85  in.read((char *)&code, sizeof(code));
86  std::cout << " endloop" << std::endl;
87  std::cout << " endfacet" << std::endl;
88  }
89  in.close();
90  std::cout << "endsolid " << name << std::endl;
91  return 0;
92 }
char buffer[BLOCK_SIZE]
Definition: copy.h:18
uint32_t n[]
Definition: generate.cxx:34
std::string name
Definition: pixelpos.cxx:77
std::istringstream in
Definition: ply2tri.cxx:32
int main(int argc, char *argv[])
Definition: stlb2stla.cxx:44
int error(int val)
Definition: stlb2stla.cxx:38
CSimpleOpt::SOption g_rgOptions[]
Definition: stlb2stla.cxx:29
@ OPT_HELP
Definition: stlb2stla.cxx:27
@ OPT_NAME
Definition: stlb2stla.cxx:27
Definition: stlb2stla.cxx:21
float y
Definition: stlb2stla.cxx:23
float z
Definition: stlb2stla.cxx:24
float x
Definition: stlb2stla.cxx:22