FacetDefinitions.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;

  18. import java.util.Collection;
  19. import java.util.Objects;

  20. import org.apache.commons.geometry.euclidean.threed.ConvexPolygon3D;
  21. import org.apache.commons.geometry.euclidean.threed.Planes;
  22. import org.apache.commons.geometry.euclidean.threed.Vector3D;
  23. import org.apache.commons.numbers.core.Precision;

  24. /** Class containing static methods that operate on {@link FacetDefinition} instances.
  25.  */
  26. public final class FacetDefinitions {

  27.     /** Utility class; no instantiation. */
  28.     private FacetDefinitions() {}

  29.     /** Construct a {@link ConvexPolygon3D} from the given facet vertices and optional normal.
  30.      * If the normal is non-null, this method attempts to honor it by making the
  31.      * polygon point in a similar (but not necessarily equal) direction, reversing the
  32.      * order of vertices if needed.
  33.      * @param vertices facet vertices
  34.      * @param normal facet normal; may be null
  35.      * @param precision precision context used for floating point comparisons
  36.      * @return convex polygon constructed from the vertices and normal
  37.      * @throws IllegalArgumentException if a valid convex polygon cannot be constructed
  38.      */
  39.     public static ConvexPolygon3D toPolygon(final Collection<Vector3D> vertices, final Vector3D normal,
  40.             final Precision.DoubleEquivalence precision) {
  41.         final ConvexPolygon3D polygon = Planes.convexPolygonFromVertices(vertices, precision);

  42.         // ensure that the polygon normal matches whatever normal was defined, if any
  43.         if (normal != null &&
  44.                 normal.dot(polygon.getPlane().getNormal()) < 0) {
  45.             return polygon.reverse();
  46.         }
  47.         return polygon;
  48.     }

  49.     /** Construct a {@link ConvexPolygon3D} from the vertices of the given facet. This method
  50.      * attempts to honor any normal defined for the facet by making the polygon point in a similar
  51.      * (but not necessarily equal) direction by reversing the order of vertices if needed.
  52.      * @param facet facet to convert to a polygon instance
  53.      * @param precision precision context used for floating point comparisons
  54.      * @return convex polygon constructed from the facet
  55.      * @throws NullPointerException if either argument is null
  56.      * @throws IllegalArgumentException if a valid convex polygon cannot be constructed
  57.      */
  58.     public static ConvexPolygon3D toPolygon(final FacetDefinition facet, final Precision.DoubleEquivalence precision) {
  59.         Objects.requireNonNull(facet, "Facet cannot be null");
  60.         Objects.requireNonNull(precision, "Precision context cannot be null");
  61.         return toPolygon(facet.getVertices(), facet.getNormal(), precision);
  62.     }
  63. }