View Javadoc
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  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.stream.Collectors;
23  
24  import org.apache.commons.geometry.core.Transform;
25  import org.apache.commons.geometry.euclidean.internal.EuclideanUtils;
26  
27  /** Internal {@link ConvexPolygon3D} implementation class that uses a list of vertices
28   * to represent the plane subset.
29   */
30  final class VertexListConvexPolygon3D extends AbstractConvexPolygon3D {
31  
32      /** Vertex loop defining the convex polygon. */
33      private final List<Vector3D> vertices;
34  
35      /** Construct a new instance with the given plane and list of vertices. Callers are responsible
36       * for ensuring that the given vertices form a convex subset lying in {@code plane}. The list of
37       * vertices should not contain the duplicated first endpoint. No validation is performed.
38       * @param plane plane containing convex polygon
39       * @param vertices vertices defining the convex polygon
40       * @throws IllegalArgumentException if fewer than 3 vertices are given
41       */
42      VertexListConvexPolygon3D(final Plane plane, final List<Vector3D> vertices) {
43          super(plane);
44  
45          // sanity check
46          if (vertices.size() < EuclideanUtils.TRIANGLE_VERTEX_COUNT) {
47              throw new IllegalArgumentException("Convex polygon requires at least " +
48                      EuclideanUtils.TRIANGLE_VERTEX_COUNT + " points; found " + vertices.size());
49          }
50  
51          this.vertices = Collections.unmodifiableList(vertices);
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public List<Vector3D> getVertices() {
57          return vertices;
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public List<Triangle3D> toTriangles() {
63          return Planes.convexPolygonToTriangleFan(getPlane(), vertices);
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public VertexListConvexPolygon3D transform(final Transform<Vector3D> transform) {
69          final Plane tPlane = getPlane().transform(transform);
70          final List<Vector3D> tVertices = vertices.stream()
71                  .map(transform)
72                  .collect(Collectors.toList());
73  
74          return new VertexListConvexPolygon3D(tPlane, tVertices);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public VertexListConvexPolygon3D reverse() {
80          final Plane rPlane = getPlane().reverse();
81          final List<Vector3D> rVertices = new ArrayList<>(vertices);
82          Collections.reverse(rVertices);
83  
84          return new VertexListConvexPolygon3D(rPlane, rVertices);
85      }
86  }