Class TextStlWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

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

      • name

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

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

      • TextStlWriter

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

      • startSolid

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

        public void startSolid​(java.lang.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:
        java.lang.IllegalArgumentException - if solidName contains new line characters
        java.lang.IllegalStateException - if a solid definition has already been started
        java.io.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:
        java.lang.IllegalStateException - if no solid definition has been started
        java.io.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:
        java.lang.IllegalStateException - if no solid has been started yet
        java.io.UncheckedIOException - if an I/O error occurs
        See Also:
        PlaneSubset.toTriangles()
      • writeTriangles

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

        public void writeTriangles​(java.util.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:
        java.lang.IllegalStateException - if no solid has been started yet or fewer than 3 vertices are given
        java.io.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:
        java.lang.IllegalStateException - if no solid has been started yet
        java.io.UncheckedIOException - if an I/O error occurs
      • close

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

        private void writeTriangleVertex​(Vector3D vertex)
        Write a triangle vertex to the output.
        Parameters:
        vertex - triangle vertex
        Throws:
        java.io.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:
        java.io.UncheckedIOException - if an I/O error occurs
      • writeBeginOrEndLine

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