-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: simple binary stl writer added * feat: simple binary stl reader added * feat: simple ascii stl reader added --------- Co-authored-by: dennis.madsen <dennis.madsen@unibas.ch> Co-authored-by: Andreas Morel-Forster <Forster.Andreas@gmail.com>
- Loading branch information
1 parent
7da793f
commit 0c3ac5b
Showing
11 changed files
with
6,500 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package scalismo.io | ||
|
||
import java.io.RandomAccessFile | ||
import java.nio.ByteBuffer | ||
|
||
object FileReader { | ||
def readFileToByteBuffer(file: String): ByteBuffer = { | ||
val raf = new RandomAccessFile(file, "r") | ||
val channel = raf.getChannel | ||
val buffer = ByteBuffer.allocate(channel.size.toInt) | ||
channel.read(buffer) | ||
buffer.rewind() | ||
buffer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2015 University of Basel, Graphics and Vision Research Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package scalismo.io.stl | ||
|
||
import scalismo.geometry.{EuclideanVector3D, Point3D} | ||
import scalismo.mesh.TriangleMesh3D | ||
|
||
import java.io.{BufferedReader, FileReader} | ||
import java.nio.ByteOrder | ||
import scala.util.Try | ||
|
||
object STL { | ||
// Binary numbers are assumed to be little-endian in the STL format. | ||
private[stl] val STL_BYTE_ORDER = ByteOrder.LITTLE_ENDIAN | ||
private[stl] val STL_HEADER_LENGTH = 80 | ||
|
||
def write(mesh: TriangleMesh3D, filename: String): Try[Unit] = { | ||
STLMeshWriter.write(mesh, filename, "Scalismo generated STL File") | ||
} | ||
|
||
def read(filename: String): Try[TriangleMesh3D] = { | ||
val breader = new BufferedReader(new FileReader(filename)) | ||
val fileType = breader.readLine().take(5) | ||
if (fileType == "solid") { | ||
STLMeshReaderAscii.read(filename) | ||
} else { | ||
STLMeshReaderBinary.read(filename) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2015 University of Basel, Graphics and Vision Research Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package scalismo.io.stl | ||
|
||
import scalismo.geometry.{EuclideanVector, EuclideanVector3D, Point3D} | ||
import scalismo.mesh.TriangleMesh3D | ||
|
||
import java.io.{BufferedReader, FileReader, IOException} | ||
import scala.collection.mutable.ArrayBuffer | ||
import scala.util.Try | ||
|
||
object STLMeshReaderAscii { | ||
def read(file: String): Try[TriangleMesh3D] = Try { | ||
val breader = new BufferedReader(new FileReader(file)) | ||
val header = breader.readLine() | ||
|
||
val triangles = ArrayBuffer.empty[STLTriangle] | ||
var line: String = null | ||
def hasLineToProcess: Boolean = { | ||
line = breader.readLine(); | ||
line != null && !line.trim.startsWith("endsolid") | ||
} | ||
while (hasLineToProcess) { | ||
line = line.trim.replaceAll(" +", " ") | ||
val triangleStrings: Array[String] = Array(line) ++ | ||
(0 until 6).map(_ => breader.readLine().trim.replaceAll(" +", " ")).toArray | ||
val triangle = parseTriangleStrings(triangleStrings) | ||
triangles += triangle | ||
} | ||
breader.close() | ||
|
||
STLTriangle.STLTrianglesToTriangleMesh(triangles.toSeq) | ||
} | ||
|
||
private def parseTriangleStrings(data: Seq[String]): STLTriangle = { | ||
if (data.length != 7) { | ||
throw new IOException("Wrong faces description format does not include all 7 descriptors.") | ||
} | ||
|
||
checkTag(data(1), "outer loop") | ||
checkTag(data(5), "endloop") | ||
checkTag(data(6), "endfacet") | ||
|
||
STLTriangle( | ||
parseNormalString(data(0)), | ||
parseVertexString(data(2)), | ||
parseVertexString(data(3)), | ||
parseVertexString(data(4)) | ||
) | ||
} | ||
|
||
private def checkTag(part: String, tag: String): Unit = { | ||
if (!part.startsWith(tag)) { | ||
throw new IOException(f"Wrong start of line, expected ${tag} but start is ${part.take(tag.length)}") | ||
} | ||
} | ||
|
||
private def splitChecked(part: String, tag: String, nElements: Int) = { | ||
if (!part.startsWith(tag)) { | ||
throw new IOException(f"Wrong identifier at beginning, expected ${tag} but start is ${part.take(tag.length)}") | ||
} | ||
|
||
val parts = part.replace(tag, "").trim.split(" ") | ||
if (parts.length != nElements) { | ||
throw new IOException(f"Wrong number of elements for ${tag}. Found ${parts.length}, expected ${nElements}") | ||
} | ||
parts | ||
} | ||
|
||
private def parseNormalString(part: String): EuclideanVector3D = { | ||
val tag = "facet normal" | ||
val expectedValues = 3 | ||
val parts = splitChecked(part, tag, expectedValues).map(_.toDouble) | ||
EuclideanVector3D(parts(0), parts(1), parts(2)) | ||
} | ||
|
||
private def parseVertexString(part: String): Point3D = { | ||
val tag = "vertex" | ||
val expectedValues = 3 | ||
val parts = splitChecked(part, tag, expectedValues).map(_.toDouble) | ||
Point3D(parts(0), parts(1), parts(2)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2015 University of Basel, Graphics and Vision Research Group | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package scalismo.io.stl | ||
|
||
import scalismo.geometry.Point3D | ||
import scalismo.io.FileReader | ||
import scalismo.io.stl.STL.{STL_BYTE_ORDER, STL_HEADER_LENGTH} | ||
import scalismo.io.stl.STLTriangle | ||
import scalismo.mesh.TriangleMesh3D | ||
|
||
import java.nio.ByteBuffer | ||
import scala.util.Try | ||
|
||
object STLMeshReaderBinary { | ||
def read(file: String): Try[TriangleMesh3D] = Try { | ||
val dataBuffer = FileReader.readFileToByteBuffer(file) | ||
dataBuffer.position(STL_HEADER_LENGTH).order(STL_BYTE_ORDER) | ||
|
||
val numTriangles = readInt(dataBuffer) | ||
val trianglesArray = new Array[STLTriangle](numTriangles) | ||
|
||
val triangles = (0 until numTriangles).map { _ => | ||
val n = readVertex(dataBuffer).toVector | ||
val p1 = readVertex(dataBuffer) | ||
val p2 = readVertex(dataBuffer) | ||
val p3 = readVertex(dataBuffer) | ||
readShort(dataBuffer) | ||
STLTriangle(n, p1, p2, p3) | ||
} | ||
STLTriangle.STLTrianglesToTriangleMesh(triangles) | ||
} | ||
|
||
private def readShort(bb: ByteBuffer): Short = { | ||
bb.getShort | ||
} | ||
|
||
private def readInt(bb: ByteBuffer): Int = { | ||
bb.getInt | ||
} | ||
|
||
private def readVertex(bb: ByteBuffer): Point3D = { | ||
Point3D(readFloat(bb), readFloat(bb), readFloat(bb)) | ||
} | ||
|
||
private def readFloat(bb: ByteBuffer): Float = { | ||
bb.getFloat | ||
} | ||
} |
Oops, something went wrong.