View Javadoc
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  
19  import java.io.Reader;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.geometry.euclidean.threed.Vector3D;
24  import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
25  import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
26  import org.apache.commons.geometry.io.euclidean.threed.SimpleFacetDefinition;
27  
28  /** Class for reading {@link FacetDefinition facets} from OBJ content.
29   */
30  public class ObjFacetDefinitionReader extends AbstractObjPolygonReader
31      implements FacetDefinitionReader {
32  
33      /** List of vertices encountered in the input. */
34      private final List<Vector3D> vertices = new ArrayList<>();
35  
36      /** List of normals encountered in the input. */
37      private final List<Vector3D> normals = new ArrayList<>();
38  
39      /** Construct a new instance that reads OBJ content from the given reader.
40       * @param reader reader to read from
41       */
42      public ObjFacetDefinitionReader(final Reader reader) {
43          super(reader);
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public FacetDefinition readFacet() {
49          final PolygonObjParser.Face face = readFace();
50          if (face != null) {
51              final List<Vector3D> faceVertices = face.getVertices(vertices::get);
52              final Vector3D definedNormal = face.getDefinedCompositeNormal(normals::get);
53  
54              return new SimpleFacetDefinition(faceVertices, definedNormal);
55          }
56  
57          return null;
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      protected void handleVertex(final Vector3D vertex) {
63          vertices.add(vertex);
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      protected void handleNormal(final Vector3D normal) {
69          normals.add(normal);
70      }
71  }