Skip to content

Commit

Permalink
STL parser application complete
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-edward committed Jun 17, 2016
1 parent bdc9c00 commit 1356ef6
Showing 1 changed file with 76 additions and 13 deletions.
89 changes: 76 additions & 13 deletions src/dev/ezhu/STLParser/AppSTLParser.cpp
Expand Up @@ -2,7 +2,8 @@
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>

using namespace std;

Expand All @@ -22,7 +23,7 @@ int main(int argc, char* argv[]) {
string filename_out = argv[2];
//string out_path = "/home/edward/NTRTsim/src/dev/ezhu/STLParser/";
// Check for valid file extension
if (strstr(filename_in.c_str(), ".stl") == NULL) {
if (filename_in.find(".stl") == string::npos) {
cout << "Incorrect filetype, application for ASCII STL files only" << endl;
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -55,28 +56,90 @@ int main(int argc, char* argv[]) {
}
// file_out << "Test 1 2 3" << endl;
string key = "vertex";
string line_out;
int vertex_index = 0;
int vertex_count = 0;
int triangle_count = 0;
int element_index = 0;
int line_index = 0;
// Parse triangle verticies from .stl file
for (int i = 0; i < 20; i++) {
//for (int i = 0; i < 204; i++) {
while (1) {
if (file_in.good()) {
string line;
getline(file_in, line);
string line_in;
getline(file_in, line_in);
line_index += 1;
cout << "Lines processed: " << line_index << endl;
//cout << line << endl;
if (strstr(line.c_str(), key.c_str()) != NULL) {
cout << line << endl;
size_t found_key = line_in.find(key);
// Look for "vertex" tag
if (found_key != string::npos) {
//cout << line_in << endl;
// Coordinates are space delimited
size_t found_char_last = line_in.find_first_of(" ", found_key);
size_t found_char_curr;
while (found_char_last != string::npos) {
found_char_curr = line_in.find_first_of(" ", found_char_last + 1);
string element;
if (found_char_curr == string::npos) {
element = line_in.substr(found_char_last + 1, line_in.size()-found_char_last);
//cout << element << endl;
}
else {
element = line_in.substr(found_char_last + 1, found_char_curr - 1 - found_char_last);
//cout << element << endl;
}
size_t found_E = element.find_first_of("E");
string coeff = element.substr(0, found_E);
string exp = element.substr(found_E + 1, element.size() - found_E);
//cout << coeff << " " << exp << endl;
double coeff_num = atof(coeff.c_str());
double exp_num = atof(exp.c_str());
//cout << exp_num << endl;
double num = coeff_num * pow(10.0, exp_num);
//cout << num << endl;

switch (element_index) {
case 0:
file_out << "[" << num << ", ";
element_index += 1;
break;
case 1:
file_out << num << ", ";
element_index += 1;
break;
case 2:
file_out << num << "] ";
element_index = 0;
break;
}
found_char_last = found_char_curr;
}
vertex_index += 1;
vertex_count += 1;
if (vertex_index >= 3) {
file_out << endl;
vertex_index = 0;
triangle_count += 1;
}
}

}
else {
// Check for read failures or end of file
if (file_in.fail()) {
cout << "Read failed" << endl;
if (file_in.eof()) {
cout << "Reached end of file, closing files" << endl;
cout << "Number of verticies extracted: " << vertex_count << endl;
cout << "Number of triangles: " << triangle_count << endl;
break;
}
else if (file_in.eof()) {
cout << "Reached end of file, closing input file" << endl;
else if (file_in.fail()) {
cout << "Read failed" << endl;
break;
}

}
}


file_in.close();
file_out.close();
}
Expand Down

0 comments on commit 1356ef6

Please sign in to comment.