ObjFacetDefinitionReader.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.Reader;
  19. import java.util.ArrayList;
  20. import java.util.List;

  21. import org.apache.commons.geometry.euclidean.threed.Vector3D;
  22. import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
  23. import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
  24. import org.apache.commons.geometry.io.euclidean.threed.SimpleFacetDefinition;

  25. /** Class for reading {@link FacetDefinition facets} from OBJ content.
  26.  */
  27. public class ObjFacetDefinitionReader extends AbstractObjPolygonReader
  28.     implements FacetDefinitionReader {

  29.     /** List of vertices encountered in the input. */
  30.     private final List<Vector3D> vertices = new ArrayList<>();

  31.     /** List of normals encountered in the input. */
  32.     private final List<Vector3D> normals = new ArrayList<>();

  33.     /** Construct a new instance that reads OBJ content from the given reader.
  34.      * @param reader reader to read from
  35.      */
  36.     public ObjFacetDefinitionReader(final Reader reader) {
  37.         super(reader);
  38.     }

  39.     /** {@inheritDoc} */
  40.     @Override
  41.     public FacetDefinition readFacet() {
  42.         final PolygonObjParser.Face face = readFace();
  43.         if (face != null) {
  44.             final List<Vector3D> faceVertices = face.getVertices(vertices::get);
  45.             final Vector3D definedNormal = face.getDefinedCompositeNormal(normals::get);

  46.             return new SimpleFacetDefinition(faceVertices, definedNormal);
  47.         }

  48.         return null;
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     protected void handleVertex(final Vector3D vertex) {
  53.         vertices.add(vertex);
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     protected void handleNormal(final Vector3D normal) {
  58.         normals.add(normal);
  59.     }
  60. }