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