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;
018
019import java.util.Collections;
020import java.util.List;
021import java.util.Objects;
022
023import org.apache.commons.geometry.euclidean.internal.EuclideanUtils;
024import org.apache.commons.geometry.euclidean.threed.Vector3D;
025
026/** Simple {@link FacetDefinition} implementation that stores a list of vertices and
027 * optional normal.
028 */
029public class SimpleFacetDefinition implements FacetDefinition {
030
031    /** Facet vertices. */
032    private final List<Vector3D> vertices;
033
034    /** Facet normal; may be null. */
035    private final Vector3D normal;
036
037    /** Construct a new instance with the given vertices and no defined normal.
038     * @param vertices facet vertices
039     * @throws IllegalArgumentException if {@code vertices} contains fewer than 3 elements
040     */
041    public SimpleFacetDefinition(final List<Vector3D> vertices) {
042        this(vertices, null);
043    }
044
045    /** Construct a new instance with the given vertices and normal.
046     * @param vertices facet vertices
047     * @param normal facet normal; may be null
048     * @throws IllegalArgumentException if {@code vertices} contains fewer than 3 elements
049     */
050    public SimpleFacetDefinition(final List<Vector3D> vertices, final Vector3D normal) {
051        Objects.requireNonNull(vertices, "Facet vertex list cannot be null");
052        if (vertices.size() < EuclideanUtils.TRIANGLE_VERTEX_COUNT) {
053            throw new IllegalArgumentException("Facet vertex list must contain at least " +
054                    EuclideanUtils.TRIANGLE_VERTEX_COUNT + " points; found " + vertices.size());
055        }
056
057        this.vertices = Collections.unmodifiableList(vertices);
058        this.normal = normal;
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    public List<Vector3D> getVertices() {
064        return vertices;
065    }
066
067    /** {@inheritDoc} */
068    @Override
069    public Vector3D getNormal() {
070        return normal;
071    }
072
073    /** {@inheritDoc} */
074    @Override
075    public String toString() {
076        final StringBuilder sb = new StringBuilder();
077        sb.append(getClass().getSimpleName())
078            .append("[vertices= ")
079            .append(getVertices())
080            .append(", normal= ")
081            .append(getNormal())
082            .append(']');
083
084        return sb.toString();
085    }
086}