Skip to content

Instantly share code, notes, and snippets.

@yoloroy
Created October 15, 2023 10:04
Show Gist options
  • Select an option

  • Save yoloroy/d518d6ca6fe2ccdd797b978aa9daefdc to your computer and use it in GitHub Desktop.

Select an option

Save yoloroy/d518d6ca6fe2ccdd797b978aa9daefdc to your computer and use it in GitHub Desktop.
GraphicsLab stl to my json format convertation
import java.io.File
import kotlin.io.path.Path
fun main(args: Array<String>) {
println("Write stl path file to convert: ")
val stlPath = readln()
val triangles = STLParser.parseSTLFile(Path(stlPath))
val (points, connections) = trianglesToPointsAndConnections(triangles)
val resultPath = "$stlPath.converted.json"
val state = """
{
"points":[${points.joinToString(",") { "[${it.x},${it.y},${it.z},1.0]" }}],
"connections":[${connections.joinToString(",") { (a, b) -> "$a,$b" }}]
}
""".trimIndent()
File(resultPath).apply {
createNewFile()
writeText(state)
}
}
private fun trianglesToPointsAndConnections(triangles: List<Triangle>): Pair<List<XYZ>, List<Pair<Int, Int>>> {
val points = mutableListOf<XYZ>()
val connections = mutableListOf<Pair<Int, Int>>()
for (triangle in triangles) {
points += triangle.a
points += triangle.b
points += triangle.c
connections += listOf(0 to +1, +1 to +2, +2 to 0).mapBoth { it + connections.size }
}
return points to connections
}
private fun <E, T> List<Pair<E, E>>.mapBoth(transform: (E) -> T) = map { (a, b) -> transform(a) to transform(b) }
/*
The MIT License (MIT)
Copyright (c) 2014 CCHall (aka Cyanobacterium aka cyanobacteruim), 2017 Andrew Goh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class is a parser for STL files. Currently, normals specified in the
* file are ignored and recalculated under the assumption that the coordinates
* are provided in right-handed coordinate space (counter-clockwise).
* @author CCHall
* @author Andrew Goh
*
* * -reversion: mar 2017 Andrew
* updated logic to handle binary STL files with "solid" in the header
*/
public class STLParser {
/**
* Parses an STL file, attempting to automatically detect whether the file
* is an ASCII or binary STL file
* @param filepath The file to parse
* @return A list of triangles representing all of the triangles in the STL
* file.
* @throws IOException Thrown if there was a problem reading the file
* (typically means the file does not exist or is not a file).
* @throws IllegalArgumentException Thrown if the STL is not properly
* formatted
*/
public static List<Triangle> parseSTLFile(Path filepath) throws IOException{
byte[] allBytes = Files.readAllBytes(filepath);
// determine if it is ASCII or binary STL
//some binary STL files has "solid" in the first 80 chars
//this breaks logic that determines if a file is ascii based on it
//simply beginning with "solid"
boolean isASCIISTL = false;
//read the first 512 chars or less
String buf = readblock(allBytes, 0, 512);
StringBuffer sb = new StringBuffer();
int inl = readline(buf, sb, 0);
String line = sb.toString();
StringTokenizer st = new StringTokenizer(line);
String token = st.nextToken();
if(token.equals("solid")) { //start with "solid"
if(inl>-1) { //found new line for next line
sb = new StringBuffer();
inl = readline(buf, sb, inl+1); //read next line
line = sb.toString();
st = new StringTokenizer(line);
token = st.nextToken();
if(token.equals("endsolid"))
isASCIISTL = true; //empty ascii file
else if(token.equals("facet")) {
isASCIISTL = true; //ascii file
} else if (isbinaryfile(allBytes))
isASCIISTL = false; //binary file
} else { //no linefeed
if (isbinaryfile(allBytes))
isASCIISTL = false; //binary file
}
} else {//does not starts with "solid"
if (isbinaryfile(allBytes))
isASCIISTL = false; //binary file
}
// read file to array of triangles
List<Triangle> mesh;
if(isASCIISTL){
Charset charset = Charset.forName("UTF-8");
mesh = readASCII(charset.decode(ByteBuffer.wrap(allBytes)).toString().toLowerCase());
} else {
mesh = readBinary(allBytes);
}
return mesh;
}
public static String readblock(byte[] allBytes, int offset, int length) {
if(allBytes.length-offset<length) length = allBytes.length-offset;
Charset charset = Charset.forName("UTF-8");
CharBuffer decode = charset.decode(ByteBuffer.wrap(allBytes, offset, length));
return decode.toString().toLowerCase();
}
public static int readline(String buf, StringBuffer sb, int offset) {
int il = buf.indexOf('\n', offset);
if(il>-1)
sb.append(buf.substring(offset, il-1));
else
sb.append(buf.substring(offset));
return il;
}
public static boolean isbinaryfile(byte[] allBytes) throws IllegalArgumentException {
if (allBytes.length<84)
throw new IllegalArgumentException("invalid binary file, length<84");
int numtriangles = byteatoint(Arrays.copyOfRange(allBytes, 80, 84));
if (allBytes.length >= 84 + numtriangles * 50)
return true; //is binary file
else {
String msg = "invalid binary file, num triangles does not match length specs";
throw new IllegalArgumentException(msg);
}
}
// little endian
public static int byteatoint(byte[] bytes) {
assert (bytes.length == 4);
int r = 0;
r = bytes[0] & 0xff;
r |= (bytes[1] & 0xff) << 8;
r |= (bytes[2] & 0xff) << 16;
r |= (bytes[3] & 0xff) << 24 ;
return r;
}
/**
* Reads an STL ASCII file content provided as a String
* @param content ASCII STL
* @return A list of triangles representing all of the triangles in the STL
* file.
* @throws IllegalArgumentException Thrown if the STL is not properly
* formatted
*/
public static List<Triangle> readASCII(String content) {
Logger.getLogger(STLParser.class.getName()).log(Level.FINEST,"Parsing ASCII STL format");
// string is lowercase
ArrayList<Triangle> triangles = new ArrayList<>();
int position = 0;
scan:
{
while (position < content.length() & position >= 0) {
position = content.indexOf("facet", position);
if (position < 0) {
break scan;
}
try {
XYZ[] vertices = new XYZ[3];
for (int v = 0; v < vertices.length; v++) {
position = content.indexOf("vertex", position) + "vertex".length();
while (Character.isWhitespace(content.charAt(position))) {
position++;
}
int nextSpace;
double[] vals = new double[3];
for (int d = 0; d < vals.length; d++) {
nextSpace = position + 1;
while (!Character.isWhitespace(content.charAt(nextSpace))) {
nextSpace++;
}
String value = content.substring(position, nextSpace);
vals[d] = Double.parseDouble(value);
position = nextSpace;
while (Character.isWhitespace(content.charAt(position))) {
position++;
}
}
vertices[v] = XYZ.fromDoubles(vals[0], vals[1], vals[2]);
}
position = content.indexOf("endfacet", position)+"endfacet".length();
triangles.add(new Triangle(vertices[0], vertices[1], vertices[2]));
} catch (Exception ex) {
int back = position - 128;
if (back < 0) {
back = 0;
}
int forward = position + 128;
if (position > content.length()) {
forward = content.length();
}
throw new IllegalArgumentException("Malformed STL syntax near \"" + content.substring(back, forward) + "\"", ex);
}
}
}
return triangles;
}
/**
* Parses binary STL file content provided as a byte array
* @param allBytes binary STL
* @return A list of triangles representing all of the triangles in the STL
* file.
* @throws IllegalArgumentException Thrown if the STL is not properly
* formatted
*/
public static List<Triangle> readBinary(byte[] allBytes) {
Logger.getLogger(STLParser.class.getName()).log(Level.FINEST,"Parsing binary STL format");
DataInputStream in = new DataInputStream(new ByteArrayInputStream(allBytes));
ArrayList<Triangle> triangles = new ArrayList<>();
try{
// skip the header
byte[] header = new byte[80];
in.read(header);
// get number triangles (not really needed)
// WARNING: STL FILES ARE SMALL-ENDIAN
int numberTriangles = Integer.reverseBytes(in.readInt());
triangles.ensureCapacity(numberTriangles);
// read triangles
try{
while(in.available() > 0 ){
float[] nvec = new float[3];
for(int i = 0; i < nvec.length; i++){
nvec[i] = Float.intBitsToFloat(Integer.reverseBytes(in.readInt()));
}
XYZ normal = new XYZ(nvec[0],nvec[1],nvec[2]); // not used (yet)
XYZ[] vertices = new XYZ[3];
for (int v = 0; v < vertices.length; v++) {
float[] vals = new float[3];
for (int d = 0; d < vals.length; d++) {
vals[d] = Float.intBitsToFloat(Integer.reverseBytes(in.readInt()));
}
vertices[v] = new XYZ(vals[0], vals[1], vals[2]);
}
short attribute = Short.reverseBytes(in.readShort()); // not used (yet)
triangles.add(new Triangle(vertices[0], vertices[1], vertices[2]));
}
}catch(Exception ex){
throw new IllegalArgumentException("Malformed STL binary at triangle number " + (triangles.size()+1), ex);
}
}catch(IOException ex){
// IO exceptions are impossible with byte array input streams,
// but still need to be caught
Logger.getLogger(STLParser.class.getName()).log(Level.SEVERE, "HOLY SHIT! A ByteArrayInputStream threw an exception!", ex);
}
return triangles;
}
}
record Triangle(XYZ a, XYZ b, XYZ c) {}
package util
fun <T1, T2, R> Iterable<T1>.zipAsSequence(other: Iterable<T2>, transform: (T1, T2) -> R) = asSequence().zip(other.asSequence(), transform)
package util
import kotlin.math.*
// this is bad file that should be moved to separate class
typealias Matrix = List<List<Float>>
fun xyRotationMatrix(radians: Float): Matrix = listOf(
listOf(cos(radians), -sin(radians), 0F, 0F),
listOf(sin(radians), cos(radians), 0F, 0F),
listOf(0F, 0F, 1F, 0F),
listOf(0F, 0F, 0F, 1F)
)
fun yzRotationMatrix(radians: Float): Matrix = listOf(
listOf(1F, 0F, 0F, 0F),
listOf(0F, cos(radians), -sin(radians), 0F),
listOf(0F, sin(radians), cos(radians), 0F),
listOf(0F, 0F, 0F, 1F)
)
fun zxRotationMatrix(radians: Float): Matrix = listOf(
listOf(cos(radians), 0F, sin(radians), 0F),
listOf(0F, 1F, 0F, 0F),
listOf(-sin(radians), 0F, cos(radians), 0F),
listOf(0F, 0F, 0F, 1F)
)
operator fun Matrix.times(other: Matrix) = indices.map { y ->
other.first().indices.map { x ->
this[y].zipAsSequence(other) { a, bRow -> a * bRow[x] }.sum()
}
}
import util.times
import util.xyRotationMatrix
import util.yzRotationMatrix
import util.zxRotationMatrix
class XYZ(val list: List<Float>) {
init {
require(list.size == 4)
}
val x: Float get() = list[0]
val y: Float get() = list[1]
val z: Float get() = list[2]
constructor(x: Float, y: Float) : this(listOf(x, y, 1F, 1F))
constructor(x: Float, y: Float, z: Float = 1F) : this(listOf(x, y, z, 1F))
companion object {
@JvmStatic
fun fromDoubles(x: Double, y: Double, z: Double) = XYZ(listOf(x, y, z, 1.0).map { it.toFloat() })
val ZERO = XYZ(0F, 0F, 0F)
val ONE = XYZ(1F, 1F, 1F)
}
fun toSpaceDelimitedString() = "$x $y $z"
private fun toMatrix() = list.map { listOf(it) } // matrix of size 4 with one column
private fun toOtherMatrix() = listOf(list) // matrix with one row of size 4
operator fun unaryMinus() = XYZ(list.map { -it })
operator fun plus(other: XYZ) = XYZ(list.zip(other.list) { a, b -> a + b })
operator fun minus(other: XYZ) = this + (-other)
operator fun times(other: XYZ) = XYZ((toMatrix() * other.toOtherMatrix()).map { it[0] })
operator fun times(factor: Float) = XYZ(x * factor, y * factor, z * factor)
operator fun div(factor: Float) = times(1 / factor)
infix fun scaled(other: XYZ) = this * other
infix fun unscaled(other: XYZ) = this * XYZ(other.list.map { 1 / it })
infix fun offset(other: XYZ) = this + other
infix fun `๐Ÿ”„Z`(radians: Float) = XYZ((toOtherMatrix() * xyRotationMatrix(radians))[0])
infix fun `๐Ÿ”„Y`(radians: Float) = XYZ((toOtherMatrix() * zxRotationMatrix(radians))[0])
infix fun `๐Ÿ”„X`(radians: Float) = XYZ((toOtherMatrix() * yzRotationMatrix(radians))[0])
fun copy(x: Float = this.x, y: Float = this.y, z: Float = this.z) = XYZ(x, y, z)
}
fun List<XYZ>.sum() = reduce(XYZ::plus)
fun List<XYZ>.average() = sum() / size.toFloat()
@yoloroy
Copy link
Author

yoloroy commented Oct 15, 2023

STLParser.java is taken from here

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