Class BinaryStlWriter

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

    public class BinaryStlWriter
    extends java.lang.Object
    implements java.io.Closeable
    Low-level class for writing binary STL content.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.io.OutputStream out
      Output stream to write to.
      private java.nio.ByteBuffer triangleBuffer
      Buffer used to construct triangle definitions.
    • Constructor Summary

      Constructors 
      Constructor Description
      BinaryStlWriter​(java.io.OutputStream out)
      Construct a new instance for writing to the given output.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      private void putVector​(Vector3D vec)
      Put all double components of vec into the internal buffer.
      void writeHeader​(byte[] headerContent, int triangleCount)
      Write binary STL header content.
      (package private) static void writeHeader​(byte[] headerContent, int triangleCount, java.io.OutputStream out)
      Write binary STL header content to the given output stream.
      void writeTriangle​(Vector3D p1, Vector3D p2, Vector3D p3, Vector3D normal)
      Write a triangle to the output using a default attribute value of 0.
      void writeTriangle​(Vector3D p1, Vector3D p2, Vector3D p3, Vector3D normal, int attributeValue)
      Write a triangle to the output.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • out

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

        private final java.nio.ByteBuffer triangleBuffer
        Buffer used to construct triangle definitions.
    • Constructor Detail

      • BinaryStlWriter

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

      • 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:
        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 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:
        java.io.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:
        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
      • 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,
                                java.io.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:
        java.io.UncheckedIOException - if an I/O error occurs