VertexListConvexPolygon3D.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.euclidean.threed;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.stream.Collectors;

  22. import org.apache.commons.geometry.core.Transform;
  23. import org.apache.commons.geometry.euclidean.internal.EuclideanUtils;

  24. /** Internal {@link ConvexPolygon3D} implementation class that uses a list of vertices
  25.  * to represent the plane subset.
  26.  */
  27. final class VertexListConvexPolygon3D extends AbstractConvexPolygon3D {

  28.     /** Vertex loop defining the convex polygon. */
  29.     private final List<Vector3D> vertices;

  30.     /** Construct a new instance with the given plane and list of vertices. Callers are responsible
  31.      * for ensuring that the given vertices form a convex subset lying in {@code plane}. The list of
  32.      * vertices should not contain the duplicated first endpoint. No validation is performed.
  33.      * @param plane plane containing convex polygon
  34.      * @param vertices vertices defining the convex polygon
  35.      * @throws IllegalArgumentException if fewer than 3 vertices are given
  36.      */
  37.     VertexListConvexPolygon3D(final Plane plane, final List<Vector3D> vertices) {
  38.         super(plane);

  39.         // sanity check
  40.         if (vertices.size() < EuclideanUtils.TRIANGLE_VERTEX_COUNT) {
  41.             throw new IllegalArgumentException("Convex polygon requires at least " +
  42.                     EuclideanUtils.TRIANGLE_VERTEX_COUNT + " points; found " + vertices.size());
  43.         }

  44.         this.vertices = Collections.unmodifiableList(vertices);
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public List<Vector3D> getVertices() {
  49.         return vertices;
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public List<Triangle3D> toTriangles() {
  54.         return Planes.convexPolygonToTriangleFan(getPlane(), vertices);
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public VertexListConvexPolygon3D transform(final Transform<Vector3D> transform) {
  59.         final Plane tPlane = getPlane().transform(transform);
  60.         final List<Vector3D> tVertices = vertices.stream()
  61.                 .map(transform)
  62.                 .collect(Collectors.toList());

  63.         return new VertexListConvexPolygon3D(tPlane, tVertices);
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public VertexListConvexPolygon3D reverse() {
  68.         final Plane rPlane = getPlane().reverse();
  69.         final List<Vector3D> rVertices = new ArrayList<>(vertices);
  70.         Collections.reverse(rVertices);

  71.         return new VertexListConvexPolygon3D(rPlane, rVertices);
  72.     }
  73. }