1 /*
2 * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.geometry;
17
18 import java.util.List;
19
20 /**
21 * This class defines a triangulator for 2D polygons. Triangulators divide
22 * polygons into triangles, which are the simplest geometric figure.
23 * Criteria for triangulation depends on each triangulator implementation.
24 */
25 public abstract class Triangulator2D {
26
27 /**
28 * Constant defining minimum vertices allowed in a polygon.
29 */
30 public static final int MIN_VERTICES = 3;
31
32 /**
33 * Constant defining default triangulator method.
34 */
35 public static final TriangulatorMethod DEFAULT_TRIANGULATOR_METHOD = TriangulatorMethod.VAN_GOGH_TRIANGULATOR;
36
37 /**
38 * Empty constructor.
39 */
40 protected Triangulator2D() {
41 }
42
43 /**
44 * Returns triangulator method.
45 * Each method implementation will divide polygons into triangles using
46 * different techniques.
47 *
48 * @return Triangulator method.
49 */
50 public abstract TriangulatorMethod getMethod();
51
52 /**
53 * Triangulates provided polygon by dividing it into a set of triangles.
54 *
55 * @param polygon Polygon to be triangulated.
56 * @return List of triangles forming the polygon that has been triangulated.
57 * @throws TriangulatorException Raised if triangulation cannot be done.
58 * Usually this indicates numerical instability or polygon degeneracy.
59 */
60 public abstract List<Triangle2D> triangulate(final Polygon2D polygon) throws TriangulatorException;
61
62 /**
63 * Triangulates a polygon formed by provided vertices.
64 *
65 * @param vertices List of points considered as vertices of a polygon.
66 * @return List of triangles forming the polygon that has been triangulated.
67 * @throws TriangulatorException Raised if triangulation cannot be done.
68 * Usually this indicates numerical instability or polygon degeneracy.
69 */
70 public abstract List<Triangle2D> triangulate(final List<Point2D> vertices) throws TriangulatorException;
71
72 /**
73 * Triangulates a polygon formed by provided vertices.
74 *
75 * @param vertices List of points considered as vertices of a polygon.
76 * @param indices List where indices of original vertices will be stored.
77 * This list can be used to refer to the original order of vertices. Notice
78 * that vertices indices might be repeated because vertices might appear in
79 * more than one triangle after triangulation. If this parameter is null,
80 * indices won't be stored in this list.
81 * @return List of triangles forming the polygon that has been triangulated.
82 * @throws TriangulatorException Raised if triangulation cannot be done.
83 * Usually this indicates numerical instability or polygon degeneracy.
84 */
85 public abstract List<Triangle2D> triangulate(final List<Point2D> vertices, final List<int[]> indices)
86 throws TriangulatorException;
87
88 /**
89 * Instantiates a triangulator for 2D polygons using default method.
90 *
91 * @return A triangulator for 2D polygons.
92 */
93 public static Triangulator2D create() {
94 return create(DEFAULT_TRIANGULATOR_METHOD);
95 }
96
97 /**
98 * Instantiates a triangulator for 2D polygons using provided method.
99 *
100 * @param method A triangulator method.
101 * @return A triangulator for 2D polygons.
102 */
103 public static Triangulator2D create(final TriangulatorMethod method) {
104 //noinspection SwitchStatementWithTooFewBranches
105 return switch (method) {
106 default -> new VanGoghTriangulator2D();
107 };
108 }
109 }