java.lang.Object
org.apache.commons.geometry.io.core.utils.AbstractTextFormatWriter
org.apache.commons.geometry.io.euclidean.threed.stl.TextStlWriter
All Implemented Interfaces:
Closeable, AutoCloseable

public class TextStlWriter extends AbstractTextFormatWriter
Class for writing the text-based (i.e., "ASCII") STL format.
See Also:
  • Field Details

    • SPACE

      private static final char SPACE
      Space character.
      See Also:
    • name

      private String name
      Name of the current STL solid.
    • started

      private boolean started
      True if an STL solid definition has been written.
  • Constructor Details

    • TextStlWriter

      public TextStlWriter(Writer writer)
      Construct a new instance for writing STL content to the given writer.
      Parameters:
      writer - writer to write to
  • Method Details

    • startSolid

      public void startSolid()
      Write the start of an unnamed STL solid definition. This method is equivalent to calling stlWriter.startSolid(null);
      Throws:
      UncheckedIOException - if an I/O error occurs
    • startSolid

      public void startSolid(String solidName)
      Write the start of an STL solid definition with the given name.
      Parameters:
      solidName - the name of the solid; may be null
      Throws:
      IllegalArgumentException - if solidName contains new line characters
      IllegalStateException - if a solid definition has already been started
      UncheckedIOException - if an I/O error occurs
    • endSolid

      public void endSolid()
      Write the end of the current STL solid definition. This method is called automatically on close() if needed.
      Throws:
      IllegalStateException - if no solid definition has been started
      UncheckedIOException - if an I/O error occurs
    • writeTriangles

      public void writeTriangles(PlaneConvexSubset boundary)
      Write the given boundary to the output as triangles.
      Parameters:
      boundary - boundary to write
      Throws:
      IllegalStateException - if no solid has been started yet
      UncheckedIOException - if an I/O error occurs
      See Also:
    • writeTriangles

      public void writeTriangles(FacetDefinition facet)
      Write the given facet definition to the output as triangles.
      Parameters:
      facet - facet definition to write
      Throws:
      IllegalStateException - if no solid has been started yet
      UncheckedIOException - if an I/O error occurs
      See Also:
    • writeTriangles

      public void writeTriangles(List<Vector3D> vertices, Vector3D normal)
      Write the facet defined by the given vertices and normal to the output as triangles. If the the given list of vertices contains more than 3 vertices, it is converted to triangles using a triangle fan. Callers are responsible for ensuring that the given vertices represent a valid convex polygon.

      If a non-zero normal is given, the vertices are ordered using the right-hand rule, meaning that they will be in a counter-clockwise orientation when looking down the normal. If no normal is given, or the given value cannot be normalized, a normal is computed from the triangle vertices, also using the right-hand rule. If this also fails (for example, if the triangle vertices do not define a plane), then the zero vector is used.

      Parameters:
      vertices - vertices defining the facet
      normal - facet normal; may be null
      Throws:
      IllegalStateException - if no solid has been started yet or fewer than 3 vertices are given
      UncheckedIOException - if an I/O error occurs
    • writeTriangle

      public void writeTriangle(Vector3D p1, Vector3D p2, Vector3D p3, Vector3D normal)
      Write a triangle to the output.

      If a non-zero normal is given, the vertices are ordered using the right-hand rule, meaning that they will be in a counter-clockwise orientation when looking down the normal. If no normal is given, or the given value cannot be normalized, a normal is computed from the triangle vertices, also using the right-hand rule. If this also fails (for example, if the triangle vertices do not define a plane), then the zero vector is used.

      Parameters:
      p1 - first point
      p2 - second point
      p3 - third point
      normal - facet normal; may be null
      Throws:
      IllegalStateException - if no solid has been started yet
      UncheckedIOException - if an I/O error occurs
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class AbstractTextFormatWriter
    • writeTriangleVertex

      private void writeTriangleVertex(Vector3D vertex)
      Write a triangle vertex to the output.
      Parameters:
      vertex - triangle vertex
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeVector

      private void writeVector(Vector3D vec)
      Write a vector to the output.
      Parameters:
      vec - vector to write
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeBeginOrEndLine

      private void writeBeginOrEndLine(String keyword)
      Write the beginning or ending line of the solid definition.
      Parameters:
      keyword - keyword at the start of the line
      Throws:
      UncheckedIOException - if an I/O error occurs