TextStlFacetDefinitionReader.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.geometry.io.euclidean.threed.stl;

  18. import java.io.Reader;
  19. import java.util.Arrays;

  20. import org.apache.commons.geometry.euclidean.threed.Vector3D;
  21. import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
  22. import org.apache.commons.geometry.io.core.internal.SimpleTextParser;
  23. import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
  24. import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
  25. import org.apache.commons.geometry.io.euclidean.threed.SimpleFacetDefinition;

  26. /** {@link FacetDefinitionReader} for reading the text (i.e., "ASCII") version of the STL file format.
  27.  * @see <a href="https://en.wikipedia.org/wiki/STL_%28file_format%29#ASCII_STL">ASCII STL</a>
  28.  */
  29. public class TextStlFacetDefinitionReader implements FacetDefinitionReader {

  30.     /** Underlying reader instance. */
  31.     private Reader reader;

  32.     /** Text parser. */
  33.     private SimpleTextParser parser;

  34.     /** Flag indicating if the start of a solid definition was detected. */
  35.     private boolean foundSolidStart;

  36.     /** Flag indicating if the end of a solid definition was detected. */
  37.     private boolean foundSolidEnd;

  38.     /** The name of the solid being read. */
  39.     private String solidName;

  40.     /** Construct a new instance for reading text STL content from the given reader.
  41.      * @param reader reader to read characters from
  42.      */
  43.     public TextStlFacetDefinitionReader(final Reader reader) {
  44.         this.reader = reader;
  45.         this.parser = new SimpleTextParser(reader);
  46.     }

  47.     /** Get the name of the STL solid being read or null if no name was specified.
  48.      * @return the name of the STL solid being read or null if no name was specified
  49.      * @throws IllegalStateException if a data format error occurs
  50.      * @throws java.io.UncheckedIOException if an I/O error occurs
  51.      */
  52.     public String getSolidName() {
  53.         ensureSolidStarted();

  54.         return solidName;
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public FacetDefinition readFacet() {
  59.         if (!foundSolidEnd && parser.hasMoreCharacters()) {
  60.             ensureSolidStarted();

  61.             nextWord();

  62.             int choice = parser.chooseIgnoreCase(
  63.                     StlConstants.FACET_START_KEYWORD,
  64.                     StlConstants.SOLID_END_KEYWORD);

  65.             if (choice == 0) {
  66.                 return readFacetInternal();
  67.             } else {
  68.                 foundSolidEnd = true;
  69.             }
  70.         }

  71.         return null;
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public void close() {
  76.         GeometryIOUtils.closeUnchecked(reader);
  77.     }

  78.     /** Internal method to read a single facet from the STL content.
  79.      * @return next facet definition
  80.      * @throws IllegalStateException if a data format error occurs
  81.      * @throws java.io.UncheckedIOException if an I/O error occurs
  82.      */
  83.     private FacetDefinition readFacetInternal() {
  84.         matchKeyword(StlConstants.NORMAL_KEYWORD);
  85.         final Vector3D normal = readVector();

  86.         matchKeyword(StlConstants.OUTER_KEYWORD);
  87.         matchKeyword(StlConstants.LOOP_START_KEYWORD);

  88.         matchKeyword(StlConstants.VERTEX_KEYWORD);
  89.         final Vector3D p1 = readVector();

  90.         matchKeyword(StlConstants.VERTEX_KEYWORD);
  91.         final Vector3D p2 = readVector();

  92.         matchKeyword(StlConstants.VERTEX_KEYWORD);
  93.         final Vector3D p3 = readVector();

  94.         matchKeyword(StlConstants.LOOP_END_KEYWORD);
  95.         matchKeyword(StlConstants.FACET_END_KEYWORD);

  96.         return new SimpleFacetDefinition(Arrays.asList(p1, p2, p3), normal);
  97.     }

  98.     /** Ensure that an STL solid definition is in the process of being read. If not, the beginning
  99.      * of a the definition is attempted to be read from the input.
  100.      * @throws IllegalStateException if a data format error occurs
  101.      * @throws java.io.UncheckedIOException if an I/O error occurs
  102.      */
  103.     private void ensureSolidStarted() {
  104.         if (!foundSolidStart) {
  105.             beginSolid();

  106.             foundSolidStart = true;
  107.         }
  108.     }

  109.     /** Begin reading an STL solid definition. The "solid" keyword is read
  110.      * along with the name of the solid.
  111.      * @throws IllegalStateException if a data format error occurs
  112.      * @throws java.io.UncheckedIOException if an I/O error occurs
  113.      */
  114.     private void beginSolid() {
  115.         matchKeyword(StlConstants.SOLID_START_KEYWORD);

  116.         solidName = trimmedOrNull(parser.nextLine()
  117.                 .getCurrentToken());
  118.     }

  119.     /** Read the next word from the content, discarding preceding whitespace.
  120.      * @throws IllegalStateException if a data format error occurs
  121.      * @throws java.io.UncheckedIOException if an I/O error occurs
  122.      */
  123.     private void nextWord() {
  124.         parser.discardWhitespace()
  125.             .nextAlphanumeric();
  126.     }

  127.     /** Read the next word from the content and match it against the given keyword.
  128.      * @param keyword keyword to match against
  129.      * @throws IllegalStateException if the read content does not match the given keyword
  130.      * @throws java.io.UncheckedIOException if an I/O error occurs or
  131.      */
  132.     private void matchKeyword(final String keyword) {
  133.         nextWord();
  134.         parser.matchIgnoreCase(keyword);
  135.     }

  136.     /** Read a vector from the input.
  137.      * @return the vector read from the input
  138.      * @throws IllegalStateException if a data format error occurs
  139.      * @throws java.io.UncheckedIOException if an I/O error occurs
  140.      */
  141.     private Vector3D readVector() {
  142.         final double x = readDouble();
  143.         final double y = readDouble();
  144.         final double z = readDouble();

  145.         return Vector3D.of(x, y, z);
  146.     }

  147.     /** Read a double value from the input.
  148.      * @return double value read from the input
  149.      * @throws IllegalStateException if a data format error occurs
  150.      * @throws java.io.UncheckedIOException if an I/O error occurs
  151.      */
  152.     private double readDouble() {
  153.         return parser
  154.                 .discardWhitespace()
  155.                 .next(SimpleTextParser::isDecimalPart)
  156.                 .getCurrentTokenAsDouble();
  157.     }

  158.     /** Return a trimmed version of the given string or null if the string contains
  159.      * only whitespace.
  160.      * @param str input stream
  161.      * @return a trimmed version of the given string or null if the string contains only
  162.      *      whitespace
  163.      */
  164.     private static String trimmedOrNull(final String str) {
  165.         if (str != null) {
  166.             final String trimmed = str.trim();
  167.             if (!trimmed.isEmpty()) {
  168.                 return trimmed;
  169.             }
  170.         }

  171.         return null;
  172.     }
  173. }