AbstractObjPolygonReader.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.obj;

  18. import java.io.Closeable;
  19. import java.io.Reader;

  20. import org.apache.commons.geometry.euclidean.threed.Vector3D;
  21. import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;

  22. /** Abstract base class for types that read OBJ polygon content using
  23.  * {@link PolygonObjParser}.
  24.  */
  25. public abstract class AbstractObjPolygonReader implements Closeable {

  26.     /** Underlying reader. */
  27.     private final Reader reader;

  28.     /** OBJ polygon parser. */
  29.     private final PolygonObjParser parser;

  30.     /** Construct a new instance that reads OBJ content from the given reader.
  31.      * @param reader reader to read characters from
  32.      */
  33.     protected AbstractObjPolygonReader(final Reader reader) {
  34.         this.reader = reader;
  35.         this.parser = new PolygonObjParser(reader);
  36.     }

  37.     /** Get the flag indicating whether or not an {@link IllegalStateException} will be thrown
  38.      * if the OBJ content contains any keywords defining non-polygon geometric content
  39.      * (ex: {@code curv}). If false, non-polygon data is ignored.
  40.      * @return flag indicating whether or not an {@link IllegalStateException} will be thrown
  41.      *      if non-polygon content is encountered
  42.      * @see PolygonObjParser#isFailOnNonPolygonKeywords()
  43.      */
  44.     public boolean isFailOnNonPolygonKeywords() {
  45.         return parser.isFailOnNonPolygonKeywords();
  46.     }

  47.     /** Set the flag indicating whether or not an {@link IllegalStateException} will be thrown
  48.      * if the OBJ content contains any keywords defining non-polygon geometric content
  49.      * (ex: {@code curv}). If set to false, non-polygon data is ignored.
  50.      * @param fail flag indicating whether or not an {@link IllegalStateException} will be thrown
  51.      *      if non-polygon content is encountered
  52.      */
  53.     public void setFailOnNonPolygonKeywords(final boolean fail) {
  54.         parser.setFailOnNonPolygonKeywords(fail);
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public void close() {
  59.         GeometryIOUtils.closeUnchecked(reader);
  60.     }

  61.     /** Return the next face from the OBJ content or null if no face is found.
  62.      * @return the next face from the OBJ content or null if no face is found
  63.      * @throws IllegalStateException if a parsing error occurs
  64.      * @throws java.io.UncheckedIOException if an I/O error occurs
  65.      */
  66.     protected PolygonObjParser.Face readFace() {
  67.         while (parser.nextKeyword()) {
  68.             switch (parser.getCurrentKeyword()) {
  69.             case ObjConstants.VERTEX_KEYWORD:
  70.                 handleVertex(parser.readVector());
  71.                 break;
  72.             case ObjConstants.VERTEX_NORMAL_KEYWORD:
  73.                 handleNormal(parser.readVector());
  74.                 break;
  75.             case ObjConstants.FACE_KEYWORD:
  76.                 return parser.readFace();
  77.             default:
  78.                 break;
  79.             }
  80.         }

  81.         return null;
  82.     }

  83.     /** Method called when a vertex is found in the OBJ content.
  84.      * @param vertex vertex value
  85.      */
  86.     protected abstract void handleVertex(Vector3D vertex);

  87.     /** Method called when a normal is found in the OBJ content.
  88.      * @param normal normal value
  89.      */
  90.     protected abstract void handleNormal(Vector3D normal);
  91. }