001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.geometry.io.euclidean.threed.obj;
018
019import java.io.Reader;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.commons.geometry.euclidean.threed.Vector3D;
024import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
025import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
026import org.apache.commons.geometry.io.euclidean.threed.SimpleFacetDefinition;
027
028/** Class for reading {@link FacetDefinition facets} from OBJ content.
029 */
030public class ObjFacetDefinitionReader extends AbstractObjPolygonReader
031    implements FacetDefinitionReader {
032
033    /** List of vertices encountered in the input. */
034    private final List<Vector3D> vertices = new ArrayList<>();
035
036    /** List of normals encountered in the input. */
037    private final List<Vector3D> normals = new ArrayList<>();
038
039    /** Construct a new instance that reads OBJ content from the given reader.
040     * @param reader reader to read from
041     */
042    public ObjFacetDefinitionReader(final Reader reader) {
043        super(reader);
044    }
045
046    /** {@inheritDoc} */
047    @Override
048    public FacetDefinition readFacet() {
049        final PolygonObjParser.Face face = readFace();
050        if (face != null) {
051            final List<Vector3D> faceVertices = face.getVertices(vertices::get);
052            final Vector3D definedNormal = face.getDefinedCompositeNormal(normals::get);
053
054            return new SimpleFacetDefinition(faceVertices, definedNormal);
055        }
056
057        return null;
058    }
059
060    /** {@inheritDoc} */
061    @Override
062    protected void handleVertex(final Vector3D vertex) {
063        vertices.add(vertex);
064    }
065
066    /** {@inheritDoc} */
067    @Override
068    protected void handleNormal(final Vector3D normal) {
069        normals.add(normal);
070    }
071}