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.Collection;
020import java.util.Objects;
021
022import org.apache.commons.geometry.euclidean.threed.ConvexPolygon3D;
023import org.apache.commons.geometry.euclidean.threed.Planes;
024import org.apache.commons.geometry.euclidean.threed.Vector3D;
025import org.apache.commons.numbers.core.Precision;
026
027/** Class containing static methods that operate on {@link FacetDefinition} instances.
028 */
029public final class FacetDefinitions {
030
031    /** Utility class; no instantiation. */
032    private FacetDefinitions() {}
033
034    /** Construct a {@link ConvexPolygon3D} from the given facet vertices and optional normal.
035     * If the normal is non-null, this method attempts to honor it by making the
036     * polygon point in a similar (but not necessarily equal) direction, reversing the
037     * order of vertices if needed.
038     * @param vertices facet vertices
039     * @param normal facet normal; may be null
040     * @param precision precision context used for floating point comparisons
041     * @return convex polygon constructed from the vertices and normal
042     * @throws IllegalArgumentException if a valid convex polygon cannot be constructed
043     */
044    public static ConvexPolygon3D toPolygon(final Collection<Vector3D> vertices, final Vector3D normal,
045            final Precision.DoubleEquivalence precision) {
046        final ConvexPolygon3D polygon = Planes.convexPolygonFromVertices(vertices, precision);
047
048        // ensure that the polygon normal matches whatever normal was defined, if any
049        if (normal != null &&
050                normal.dot(polygon.getPlane().getNormal()) < 0) {
051            return polygon.reverse();
052        }
053        return polygon;
054    }
055
056    /** Construct a {@link ConvexPolygon3D} from the vertices of the given facet. This method
057     * attempts to honor any normal defined for the facet by making the polygon point in a similar
058     * (but not necessarily equal) direction by reversing the order of vertices if needed.
059     * @param facet facet to convert to a polygon instance
060     * @param precision precision context used for floating point comparisons
061     * @return convex polygon constructed from the facet
062     * @throws NullPointerException if either argument is null
063     * @throws IllegalArgumentException if a valid convex polygon cannot be constructed
064     */
065    public static ConvexPolygon3D toPolygon(final FacetDefinition facet, final Precision.DoubleEquivalence precision) {
066        Objects.requireNonNull(facet, "Facet cannot be null");
067        Objects.requireNonNull(precision, "Precision context cannot be null");
068        return toPolygon(facet.getVertices(), facet.getNormal(), precision);
069    }
070}