NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
AppSTLParser.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string>
6 #include <math.h>
7 
8 using namespace std;
9 
10 int main(int argc, char* argv[]) {
11  // Check for number of arguments
12  if (argc <= 2) {
13  cout << "Not enough arguments, please use ./AppSTLParser [path/filename_in] [path/filename_out]" << endl;
14  exit(EXIT_FAILURE);
15  }
16  else if (argc > 3) {
17  cout << "Too many arguments, please use ./AppSTLParser [path/filename_in] [path/filename_out]" << endl;
18  exit(EXIT_FAILURE);
19  }
20  else {
21  // Get filenames from argv
22  string filename_in = argv[1];
23  string filename_out = argv[2];
24  //string out_path = "/home/edward/NTRTsim/src/dev/ezhu/STLParser/";
25  // Check for valid file extension
26  if (filename_in.find(".stl") == string::npos) {
27  cout << "Incorrect filetype, application for ASCII STL files only" << endl;
28  exit(EXIT_FAILURE);
29  }
30  else {
31  cout << "File to parse: " << filename_in << endl;
32  }
33  fstream file_in;
34  fstream file_out;
35  // Open input file
36  file_in.open(filename_in.c_str(), fstream::in);
37  // Check if input file opened successfully
38  if (!file_in.is_open()) {
39  cout << "Failed to open input file, please check filename" << endl;
40  exit(EXIT_FAILURE);
41  }
42  else {
43  cout << "Input file opened successfully" << endl;
44  }
45  //out_path = out_path + filename_out;
46  cout << "Parsed info writing to: " << filename_out << endl;
47  // Open output file
48  file_out.open(filename_out.c_str(), fstream::out);
49  // Check if output file opened successfully
50  if (!file_out.is_open()) {
51  cout << "Failed to open output file" << endl;
52  exit(EXIT_FAILURE);
53  }
54  else {
55  cout << "Output file opened successfully" << endl;
56  }
57  // file_out << "Test 1 2 3" << endl;
58  string key = "vertex";
59  string line_out;
60  int vertex_index = 0;
61  int vertex_count = 0;
62  int triangle_count = 0;
63  int element_index = 0;
64  int line_index = 0;
65  // Parse triangle verticies from .stl file
66  //for (int i = 0; i < 204; i++) {
67  while (1) {
68  if (file_in.good()) {
69  string line_in;
70  getline(file_in, line_in);
71  line_index += 1;
72  cout << "Lines processed: " << line_index << endl;
73  //cout << line << endl;
74  size_t found_key = line_in.find(key);
75  // Look for "vertex" tag
76  if (found_key != string::npos) {
77  //cout << line_in << endl;
78  // Coordinates are space delimited
79  size_t found_char_last = line_in.find_first_of(" ", found_key);
80  size_t found_char_curr;
81  while (found_char_last != string::npos) {
82  found_char_curr = line_in.find_first_of(" ", found_char_last + 1);
83  string element;
84  if (found_char_curr == string::npos) {
85  element = line_in.substr(found_char_last + 1, line_in.size()-found_char_last);
86  //cout << element << endl;
87  }
88  else {
89  element = line_in.substr(found_char_last + 1, found_char_curr - 1 - found_char_last);
90  //cout << element << endl;
91  }
92  size_t found_E = element.find_first_of("E");
93  string coeff = element.substr(0, found_E);
94  string exp = element.substr(found_E + 1, element.size() - found_E);
95  //cout << coeff << " " << exp << endl;
96  double coeff_num = atof(coeff.c_str());
97  double exp_num = atof(exp.c_str());
98  //cout << exp_num << endl;
99  double num = coeff_num * pow(10.0, exp_num);
100  //cout << num << endl;
101 
102  switch (element_index) {
103  case 0:
104  file_out << "[" << num << ", ";
105  element_index += 1;
106  break;
107  case 1:
108  file_out << num << ", ";
109  element_index += 1;
110  break;
111  case 2:
112  file_out << num << "] ";
113  element_index = 0;
114  break;
115  }
116  found_char_last = found_char_curr;
117  }
118  vertex_index += 1;
119  vertex_count += 1;
120  if (vertex_index >= 3) {
121  file_out << endl;
122  vertex_index = 0;
123  triangle_count += 1;
124  }
125  }
126 
127  }
128  else {
129  // Check for read failures or end of file
130  if (file_in.eof()) {
131  cout << "Reached end of file, closing files" << endl;
132  cout << "Number of verticies extracted: " << vertex_count << endl;
133  cout << "Number of triangles: " << triangle_count << endl;
134  break;
135  }
136  else if (file_in.fail()) {
137  cout << "Read failed" << endl;
138  break;
139  }
140 
141  }
142  }
143  file_in.close();
144  file_out.close();
145  }
146 
147  return 0;
148 }
int main(int argc, char **argv)