java.lang.Object
org.apache.commons.geometry.io.euclidean.threed.stl.BinaryStlWriter
All Implemented Interfaces:
Closeable, AutoCloseable

public class BinaryStlWriter extends Object implements Closeable
Low-level class for writing binary STL content.
  • Field Details

    • out

      private final OutputStream out
      Output stream to write to.
    • triangleBuffer

      private final ByteBuffer triangleBuffer
      Buffer used to construct triangle definitions.
  • Constructor Details

    • BinaryStlWriter

      public BinaryStlWriter(OutputStream out)
      Construct a new instance for writing to the given output.
      Parameters:
      out - output stream to write to
  • Method Details

    • writeHeader

      public void writeHeader(byte[] headerContent, int triangleCount)
      Write binary STL header content. If headerContent is null, the written header will consist entirely of zeros. Otherwise, up to 80 bytes from headerContent are written to the header, with any remaining bytes of the header filled with zeros.
      Parameters:
      headerContent - bytes to include in the header; may be null
      triangleCount - number of triangles to be included in the content
      Throws:
      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 using a default attribute value of 0. Callers are responsible for ensuring that the number of triangles written matches the number given in the header.

      If a 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. Thus, the given point ordering may not be the ordering used in the written content.

      Parameters:
      p1 - first point
      p2 - second point
      p3 - third point
      normal - triangle normal; may be null
      Throws:
      UncheckedIOException - if an I/O error occurs
    • writeTriangle

      public void writeTriangle(Vector3D p1, Vector3D p2, Vector3D p3, Vector3D normal, int attributeValue)
      Write a triangle to the output. Callers are responsible for ensuring that the number of triangles written matches the number given in the header.

      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 - triangle normal; may be null
      attributeValue - 2-byte STL triangle attribute value
      Throws:
      UncheckedIOException - if an I/O error occurs
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • putVector

      private void putVector(Vector3D vec)
      Put all double components of vec into the internal buffer.
      Parameters:
      vec - vector to place into the buffer
    • writeHeader

      static void writeHeader(byte[] headerContent, int triangleCount, OutputStream out)
      Write binary STL header content to the given output stream. If headerContent is null, the written header will consist entirely of zeros. Otherwise, up to 80 bytes from headerContent are written to the header, with any remaining bytes of the header filled with zeros.
      Parameters:
      headerContent -
      triangleCount -
      out -
      Throws:
      UncheckedIOException - if an I/O error occurs