Class TextFacetDefinitionReader

  • All Implemented Interfaces:
    java.lang.AutoCloseable, FacetDefinitionReader

    public class TextFacetDefinitionReader
    extends java.lang.Object
    implements FacetDefinitionReader
    Facet definition reader implementation that reads an extremely simple text format. The format simply consists of sequences of decimal numbers defining the vertices of each facet, with one facet defined per line. Facet vertices are defined by listing their x, y, and z components in that order. The format can be described as follows:

    p1x p1y p1z p2x p2y p2z p3x p3y p3z ...

    where the p1 elements contain the coordinates of the first facet vertex, p2 those of the second, and so on. At least 3 vertices are required for each facet but more can be specified as long as all x, y, z components are provided for each vertex. The facet normal is defined implicitly from the facet vertices using the right-hand rule (i.e. vertices are arranged counter-clockwise).

    Delimiters

    Vertex coordinate values may be separated by any character that is not a digit, alphabetic, '-' (minus), or '+' (plus). The character does not need to be consistent between (or even within) lines and does not need to be configured in the reader. This design provides configuration-free support for common formats such as CSV as well as other formats designed for human readability.

    Comments

    Comments are supported through use of the comment token property. Characters from the comment token through the end of the current line are discarded. Setting the comment token to null or the empty string disables comment parsing. The default comment token is "#"

    Examples

    The following examples demonstrate the definition of two facets, one with 3 vertices and one with 4 vertices, in different formats.

    CSV

      0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0
      1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0
     

    Whitespace and semicolons

      # line comment
      0 0 0; 1 0 0; 1 1 0 # 3 vertices
      1 0 0; 1 1 0; 1 1 1; 1 0 1 # 4 vertices
     
    See Also:
    TextFacetDefinitionWriter
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int commentStartChar
      First character of the comment token.
      private java.lang.String commentToken
      Comment token string; may be null.
      static java.lang.String DEFAULT_COMMENT_TOKEN
      Default comment token string.
      private boolean hasCommentToken
      True if the instance has a non-null, non-empty comment token.
      private SimpleTextParser parser
      Parser used to parse text content.
      private java.io.Reader reader
      Reader for accessing the character stream.
    • Constructor Summary

      Constructors 
      Constructor Description
      TextFacetDefinitionReader​(java.io.Reader reader)
      Construct a new instance that reads characters from the argument and uses the default comment token value of "#".
      TextFacetDefinitionReader​(java.io.Reader reader, java.lang.String commentToken)
      Construct a new instance with the given reader and comment token.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close this instance and release all associated resources.
      private static boolean containsWhitespace​(java.lang.String str)
      Return true if the given string contains any whitespace characters.
      private void discardNonData()
      Discard a sequence of non-data characters on the current line starting from the current parser position.
      private void discardNonDataLines()
      Discard lines that do not contain any data.
      private boolean foundComment()
      Return true if the parser is positioned at the start of the comment token.
      java.lang.String getCommentToken()
      Get the comment token string.
      private static boolean isDataTokenPart​(int ch)
      Return true if the given character is considered as part of a data token for this reader.
      private double readDouble()
      Read a double starting from the current parser position.
      FacetDefinition readFacet()
      Return the next facet definition from the input source or null if no more facets are available.
      private FacetDefinition readFacetInternal()
      Internal method to read a facet definition starting from the current parser position.
      private Vector3D readVector()
      Read a vector starting from the current parser position.
      void setCommentToken​(java.lang.String commentToken)
      Set the comment token string.
      private void setCommentTokenInternal​(java.lang.String commentTokenStr)
      Internal method called to set the comment token state.
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_COMMENT_TOKEN

        public static final java.lang.String DEFAULT_COMMENT_TOKEN
        Default comment token string.
        See Also:
        Constant Field Values
      • reader

        private final java.io.Reader reader
        Reader for accessing the character stream.
      • parser

        private final SimpleTextParser parser
        Parser used to parse text content.
      • commentToken

        private java.lang.String commentToken
        Comment token string; may be null.
      • hasCommentToken

        private boolean hasCommentToken
        True if the instance has a non-null, non-empty comment token.
      • commentStartChar

        private int commentStartChar
        First character of the comment token.
    • Constructor Detail

      • TextFacetDefinitionReader

        public TextFacetDefinitionReader​(java.io.Reader reader)
        Construct a new instance that reads characters from the argument and uses the default comment token value of "#".
        Parameters:
        reader - reader to read characters from
      • TextFacetDefinitionReader

        public TextFacetDefinitionReader​(java.io.Reader reader,
                                         java.lang.String commentToken)
        Construct a new instance with the given reader and comment token.
        Parameters:
        reader - reader to read characters from
        commentToken - comment token string; set to null to disable comment parsing
        Throws:
        java.lang.IllegalArgumentException - if commentToken is non-null and contains whitespace
    • Method Detail

      • getCommentToken

        public java.lang.String getCommentToken()
        Get the comment token string. If not null or empty, any characters from this token to the end of the current line are discarded during parsing.
        Returns:
        comment token string; may be null
      • setCommentToken

        public void setCommentToken​(java.lang.String commentToken)
        Set the comment token string. If not null or empty, any characters from this token to the end of the current line are discarded during parsing. Set to null or the empty string to disable comment parsing. Comment tokens may not contain whitespace.
        Parameters:
        commentToken - token to set
        Throws:
        java.lang.IllegalArgumentException - if the argument is non-null and contains whitespace
      • readFacet

        public FacetDefinition readFacet()
        Return the next facet definition from the input source or null if no more facets are available.
        Specified by:
        readFacet in interface FacetDefinitionReader
        Returns:
        the next facet definition or null if no more facets are available
      • close

        public void close()
        Close this instance and release all associated resources.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface FacetDefinitionReader
      • readFacetInternal

        private FacetDefinition readFacetInternal()
        Internal method to read a facet definition starting from the current parser position. Empty lines (including lines containing only comments) are discarded.
        Returns:
        facet definition or null if the end of input is reached
        Throws:
        java.lang.IllegalStateException - if a data format error occurs
        java.io.UncheckedIOException - if an I/O error occurs
      • readVector

        private Vector3D readVector()
        Read a vector starting from the current parser position.
        Returns:
        vector read from the parser
        Throws:
        java.lang.IllegalStateException - if a data format error occurs
        java.io.UncheckedIOException - if an I/O error occurs
      • readDouble

        private double readDouble()
        Read a double starting from the current parser position.
        Returns:
        double value read from the parser
        Throws:
        java.lang.IllegalStateException - if a data format error occurs
        java.io.UncheckedIOException - if an I/O error occurs
      • discardNonDataLines

        private void discardNonDataLines()
        Discard lines that do not contain any data. This includes empty lines and lines that only contain comments.
        Throws:
        java.lang.IllegalStateException - if a data format error occurs
        java.io.UncheckedIOException - if an I/O error occurs
      • discardNonData

        private void discardNonData()
        Discard a sequence of non-data characters on the current line starting from the current parser position.
        Throws:
        java.lang.IllegalStateException - if a data format error occurs
        java.io.UncheckedIOException - if an I/O error occurs
      • foundComment

        private boolean foundComment()
        Return true if the parser is positioned at the start of the comment token.
        Returns:
        true if the parser is positioned at the start of the comment token.
        Throws:
        java.lang.IllegalStateException - if a data format error occurs
        java.io.UncheckedIOException - if an I/O error occurs
      • setCommentTokenInternal

        private void setCommentTokenInternal​(java.lang.String commentTokenStr)
        Internal method called to set the comment token state.
        Parameters:
        commentTokenStr - comment token to set
        Throws:
        java.lang.IllegalArgumentException - if the argument is non-null and contains whitespace
      • isDataTokenPart

        private static boolean isDataTokenPart​(int ch)
        Return true if the given character is considered as part of a data token for this reader.
        Parameters:
        ch - character to test
        Returns:
        true if ch is part of a data token
      • containsWhitespace

        private static boolean containsWhitespace​(java.lang.String str)
        Return true if the given string contains any whitespace characters.
        Parameters:
        str - string to test
        Returns:
        true if str contains any whitespace characters