write.fs.surface.stl: Write mesh to file in STL format (ASCII or binary).

View source: R/write_fs_surface.R

write.fs.surface.stlR Documentation

Write mesh to file in STL format (ASCII or binary).

Description

The STL format (stereolithography, the format used for 3D printing) stores a triangular mesh as a list of triangles, each with its vertex coordinates repeated and with a normal vector, instead of storing a vertex list and indices into it. Both the ASCII and the binary version of the format are written by this function, the binary one being the default since it is much smaller and it is what most software uses. The resulting files can be read back with read.fs.surface.stl and are accepted by mesh viewers and slicers.

Usage

write.fs.surface.stl(
  filepath,
  vertex_coords,
  faces,
  ascii = FALSE,
  solid_name = "mesh"
)

Arguments

filepath

character string, the path of the file to write.

vertex_coords

n x 3 matrix of doubles, the vertex coordinates.

faces

n x 3 matrix of integers, the vertex indices of the triangles. The STL format has no support for polygons with more than 3 vertices, so a quad mesh has to be converted first with faces.quad.to.tris.

ascii

logical, whether to write the ASCII version of the format. The default is the binary version, which is smaller by a factor of about 5 and which is what most mesh processing software writes. Use the ASCII version if the file has to be readable by humans or by software that supports only the ASCII variant.

solid_name

character string, the name of the mesh. Only used in the ASCII version, where the format requires the name in the first and the last line of the file.

Value

character string, the format that was written: 'tris'.

Note

The normals of the triangles are computed from the vertex coordinates (the STL format stores them, but no reader has to trust them). A degenerate triangle, i.e., one whose vertices are collinear or identical, has no normal, so a zero vector is written for it.

An indexed mesh is stored as a polygon soup in an STL file: every triangle repeats the coordinates of its vertices. Reading such a file back with read.fs.surface.stl merges the repeated vertices again (using the digits precision of that function), so a round trip through an STL file preserves the geometry of the mesh, but not the order or the count of the vertices in the vertex list.

See Also

Other mesh export functions: write.fs.surface(), write.fs.surface.obj(), write.fs.surface.off(), write.fs.surface.off.ply2(), write.fs.surface.ply(), write.fs.surface.ply2(), write.fs.surface.vtk()

Examples

## Not run: 
# Write a mesh as binary and as ASCII STL:
mesh <- read.fs.surface(system.file("extdata", "cube.stl", package = "freesurferformats"));
write.fs.surface.stl(tempfile(fileext = ".stl"), mesh$vertices, mesh$faces);
write.fs.surface.stl(tempfile(fileext = ".stl"), mesh$vertices, mesh$faces, ascii = TRUE);

# The file format is also chosen by the file name when using the generic
# writer:
write.fs.surface(tempfile(fileext = ".stl"), mesh$vertices, mesh$faces);

## End(Not run)


freesurferformats documentation built on Sept. 25, 2026, 1:07 a.m.