Skip to content

Instantly share code, notes, and snippets.

@noahhl
Created February 21, 2014 21:43
Show Gist options
  • Select an option

  • Save noahhl/9144144 to your computer and use it in GitHub Desktop.

Select an option

Save noahhl/9144144 to your computer and use it in GitHub Desktop.
/**
* Convert file of cartesian points to an STL file
*/
import java.io.*;
import java.util.*;
public class cart2stl {
//EDIT THE NEXT TWO LINES WITH THE APPROPRIATE INPUT/OUTPUT FILE NAMES
public static final String fileIn = "face.txt";
public static final String outputFile = "face.stl";
public static int pointsRead = 0;
public static int facetsMade = 0;
public static int totalPoints =0;
public static PrintWriter fileOut = null;
public static void main(String[] args) throws IOException {
System.out.println("Cartesian Points to STL File Convertor");
System.out.println("Developed by Noah Lorang, Fall 2006");
System.out.println("\n\nNow beginning conversion on " + fileIn);
double[] i = new double[3];
double[] j = new double[3];
double[] k = new double[3];
//Open file, read first line and set totalPoints
BufferedReader fileReader = new BufferedReader(new FileReader (fileIn));
StringTokenizer st = new StringTokenizer(fileReader.readLine());
totalPoints = Integer.parseInt(st.nextToken())*Integer.parseInt(st.nextToken());
//Now, read the next three lines and make the right arrays to hold them
//First line
st = new StringTokenizer(fileReader.readLine());
for (int a = 0; a<3; a++){
i[a]=Double.parseDouble(st.nextToken());
}
//second line
st = new StringTokenizer(fileReader.readLine());
for (int a = 0; a<3; a++){
j[a]=Double.parseDouble(st.nextToken());
}
//third line
st = new StringTokenizer(fileReader.readLine());
for (int a = 0; a<3; a++){
k[a]=Double.parseDouble(st.nextToken());
}
pointsRead += 3;
//Start file to write to
fileOut = new PrintWriter (new FileWriter(outputFile));
fileOut.println("solid");
fileOut.println();
for(int b = 1; b <totalPoints-2; b++) { //temp solution
makeFacet(i,j,k);
//move up old points
i[0] = j[0];
i[1] = j[1];
i[2] = j[2];
j[0] = k[0];
j[1] = k[1];
j[2] = k[2];
//Read new point
st = new StringTokenizer(fileReader.readLine());
for (int a = 0; a<3; a++){
k[a]=Double.parseDouble(st.nextToken());
}
pointsRead++;
facetsMade++;
}
fileOut.println("endsolid");
fileOut.close();
System.out.println("Total points read: " + pointsRead);
System.out.println("Total facets made: " + facetsMade);
System.out.println("File written to " + outputFile);
}
public static void makeFacet(double[] i, double[] j, double[] k) {
//construct vectors between points
double[] ij = new double[3];
double[] jk = new double[3];
for (int a = 0; a<3; a++){
ij[a] = j[a]-i[a];
jk[a] = k[a]-j[a];
}
//calculate normal ij x jk
double[] normal = new double[3];
normal[0] = ij[1]*jk[2] - ij[2]*jk[1];
normal[1] = ij[2]*jk[0] - ij[0]*jk[2];
normal[2] = ij[0]*jk[1] - ij[1]*jk[0];
//add to file
fileOut.println(" facet normal " + normal[0] + " " + normal[1] + " " + normal[2]);
fileOut.println(" outer loop");
fileOut.println(" vertex " + i[0] + " " + i[1] + " " + i[2]);
fileOut.println(" vertex " + j[0] + " " + j[1] + " " + j[2]);
fileOut.println(" vertex " + k[0] + " " + k[1] + " " + k[2]);
fileOut.println(" endloop");
fileOut.println(" endfacet");
fileOut.println();
}
}
@migreyes
Copy link
Copy Markdown

This is cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment