ready for submission
This commit is contained in:
259
Lab1/ReadSTL.cpp
259
Lab1/ReadSTL.cpp
@@ -1,4 +1,15 @@
|
||||
#include <iostream>
|
||||
/**
|
||||
* @file ReadSTL.cpp
|
||||
* @author Trevor Barnes (barnestr@msoe.edu)
|
||||
* @brief A C++ file containing the functionality need to read in a .stl file.
|
||||
* The ReadSTL class allows the min/max coordinates and total count of the STL
|
||||
* facets be stored. The appropriate getters are also included.
|
||||
* @version 1.0
|
||||
* @date 2022-03-15
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
*/
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
@@ -6,141 +17,171 @@ using namespace std;
|
||||
|
||||
class ReadSTL {
|
||||
private:
|
||||
int numFacet = 0;
|
||||
double Xmin = 0;
|
||||
double Xmax = 0;
|
||||
double Ymin = 0;
|
||||
double Ymax = 0;
|
||||
double Zmin = 0;
|
||||
double Zmax = 0;
|
||||
int numFacet;
|
||||
double Xmin;
|
||||
double Xmax;
|
||||
double Ymin;
|
||||
double Ymax;
|
||||
double Zmin;
|
||||
double Zmax;
|
||||
public:
|
||||
ReadSTL(string filename) {
|
||||
// Input Stream setup
|
||||
ifstream inputfile(filename, ifstream::in);
|
||||
string line;
|
||||
string token;
|
||||
string solidName;
|
||||
// Solid header line
|
||||
getline(inputfile, line);
|
||||
// Input string stream for each line
|
||||
istringstream lineStream(line);
|
||||
// Iterate stream past "solid"
|
||||
lineStream >> token;
|
||||
// Get solid name
|
||||
lineStream >> solidName;
|
||||
do {
|
||||
// Process entire solid, looping through each facet
|
||||
// Get next line
|
||||
ReadSTL(string filename) {
|
||||
// Instantiate variables
|
||||
numFacet = 0;
|
||||
// Input Stream setup
|
||||
ifstream inputfile(filename, ifstream::in);
|
||||
string line;
|
||||
string token;
|
||||
string solidName;
|
||||
// Solid header line
|
||||
getline(inputfile, line);
|
||||
// Set string stream to new line
|
||||
lineStream.str(line);
|
||||
// Clear sstream error state
|
||||
lineStream.clear();
|
||||
// Get first word in line
|
||||
// Input string stream for each line
|
||||
istringstream lineStream(line);
|
||||
// Iterate stream past "solid"
|
||||
lineStream >> token;
|
||||
if(!token.compare("facet")) {
|
||||
// Process facet
|
||||
// Facet header
|
||||
// Iterate past "normal"
|
||||
// Get solid name
|
||||
lineStream >> solidName;
|
||||
do {
|
||||
// Process entire solid, looping through each facet
|
||||
// Get next line
|
||||
getline(inputfile, line);
|
||||
// Set string stream to new line
|
||||
lineStream.str(line);
|
||||
// Clear sstream error state
|
||||
lineStream.clear();
|
||||
// Get first word in line
|
||||
lineStream >> token;
|
||||
// Coord double value
|
||||
double coord;
|
||||
// Convert coord string to double
|
||||
// X Coord
|
||||
lineStream >> token;
|
||||
coord = stod(token);
|
||||
// X Min
|
||||
if((coord < Xmin) || numFacet == 0) {
|
||||
Xmin = coord;
|
||||
}
|
||||
// X Max
|
||||
if((coord > Xmax) || numFacet == 0) {
|
||||
Xmax = coord;
|
||||
}
|
||||
// Y Coord
|
||||
lineStream >> token;
|
||||
coord = stod(token);
|
||||
// Min
|
||||
if((coord < Ymin) || numFacet == 0) {
|
||||
Ymin = coord;
|
||||
}
|
||||
// Max
|
||||
if((coord > Ymax) || numFacet == 0) {
|
||||
Ymax = coord;
|
||||
}
|
||||
// Z Coord
|
||||
lineStream >> token;
|
||||
coord = stod(token);
|
||||
// Min
|
||||
if((coord < Zmin) || numFacet == 0) {
|
||||
Zmin = coord;
|
||||
}
|
||||
// Max
|
||||
if((coord > Zmax) || numFacet == 0) {
|
||||
Zmax = coord;
|
||||
}
|
||||
// End facet header
|
||||
|
||||
do {
|
||||
// Get next line
|
||||
getline(inputfile, line);
|
||||
// Set string stream to new line
|
||||
lineStream.str(line);
|
||||
// Clear sstream error state
|
||||
lineStream.clear();
|
||||
if(!token.compare("facet")) {
|
||||
// Facet header
|
||||
// Iterate past "normal"
|
||||
lineStream >> token;
|
||||
if (!token.compare("outer")) {
|
||||
do {
|
||||
// Get next line
|
||||
getline(inputfile, line);
|
||||
// Set string stream to new line
|
||||
lineStream.str(line);
|
||||
// Clear sstream error state
|
||||
lineStream.clear();
|
||||
lineStream >> token;
|
||||
if(!token.compare("vertex")) {
|
||||
//Process vertex
|
||||
// No functionality needed
|
||||
// Does nothing but iterate through each coord
|
||||
// X coord
|
||||
// Coord double value
|
||||
double coord;
|
||||
// X Coord
|
||||
lineStream >> token;
|
||||
coord = stod(token);
|
||||
if((coord < Xmin) || numFacet == 0) {
|
||||
Xmin = coord;
|
||||
}
|
||||
if((coord > Xmax) || numFacet == 0) {
|
||||
Xmax = coord;
|
||||
}
|
||||
// Y Coord
|
||||
lineStream >> token;
|
||||
coord = stod(token);
|
||||
if((coord < Ymin) || numFacet == 0) {
|
||||
Ymin = coord;
|
||||
}
|
||||
if((coord > Ymax) || numFacet == 0) {
|
||||
Ymax = coord;
|
||||
}
|
||||
// Z Coord
|
||||
lineStream >> token;
|
||||
coord = stod(token);
|
||||
if((coord < Zmin) || numFacet == 0) {
|
||||
Zmin = coord;
|
||||
}
|
||||
if((coord > Zmax) || numFacet == 0) {
|
||||
Zmax = coord;
|
||||
}
|
||||
// End facet header
|
||||
do {
|
||||
// Get next line
|
||||
getline(inputfile, line);
|
||||
// Set string stream to new line
|
||||
lineStream.str(line);
|
||||
// Clear sstream error state
|
||||
lineStream.clear();
|
||||
lineStream >> token;
|
||||
if (!token.compare("outer")) {
|
||||
do {
|
||||
// Get next line
|
||||
getline(inputfile, line);
|
||||
// Set string stream to new line
|
||||
lineStream.str(line);
|
||||
// Clear sstream error state
|
||||
lineStream.clear();
|
||||
lineStream >> token;
|
||||
// Y coord
|
||||
lineStream >> token;
|
||||
// Z coord
|
||||
lineStream >> token;
|
||||
} // End vertex line
|
||||
} while(token.compare("endloop")); // Endloop
|
||||
}
|
||||
}while(token.compare("endfacet")); // Endfacet
|
||||
// Iterate facet counter
|
||||
numFacet++;
|
||||
}
|
||||
}while(token.compare("endsolid"));
|
||||
// Close file
|
||||
inputfile.close();
|
||||
}
|
||||
if(!token.compare("vertex")) {
|
||||
// Process vertex
|
||||
// No additional functionality required
|
||||
// X coord
|
||||
lineStream >> token;
|
||||
// Y coord
|
||||
lineStream >> token;
|
||||
// Z coord
|
||||
lineStream >> token;
|
||||
} // End vertex line
|
||||
} while(token.compare("endloop")); // Endloop
|
||||
}
|
||||
}while(token.compare("endfacet")); // Endfacet
|
||||
// Iterate facet counter
|
||||
numFacet++;
|
||||
}
|
||||
}while(token.compare("endsolid"));
|
||||
// Close file
|
||||
inputfile.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter for the number of facets
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int getNumFacets(){
|
||||
return numFacet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Getter for Xmin
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
double getXmin() {
|
||||
return Xmin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter for Xmax
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
double getXmax() {
|
||||
return Xmax;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter for Ymin
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
double getYmin() {
|
||||
return Ymin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter for Ymax
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
double getYmax() {
|
||||
return Ymax;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter for Zmin
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
double getZmin() {
|
||||
return Zmin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter for Zmax
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
double getZmax() {
|
||||
return Zmax;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user