├── .gitignore
├── LICENSE
├── README.md
├── pom.xml
├── resources
├── delaunator.example.png
└── delaunator.voronoi.example.png
└── src
└── main
└── java
└── org
└── waveware
└── delaunator
├── DEdge.java
├── DPoint.java
├── DTriangle.java
└── Delaunator.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 | /target/
3 | .classpath
4 | .project
5 | /.settings/
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://jitpack.io/#waveware4ai/delaunator-java)
2 |
3 |
4 | # delaunator-java
5 |
6 | This library is Java port of [*Delaunator*](https://github.com/mapbox/delaunator), an incredibly fast and robust JavaScript library for Delaunay triangulation of 2D points.
7 |
8 | The port was produced by referring to the c++ and c# versions of *Delaunator*.
9 |
10 |
11 |
12 |
13 |
14 | ## Example
15 |
16 | ```
17 | List list = new ArrayList<>();
18 | for (int i = 0; i < 10000; i++) {
19 | double x = Math.random() * 1000;
20 | double y = Math.random() * 1000;
21 | DPoint p = new DPoint((int) x, (int) y);
22 | list.add(p);
23 | }
24 |
25 | Delaunator del = new Delaunator(list);
26 | for (DTriangle t : del.getTriangles()) {
27 | DEdge edgeA = t.ab; // edge A of Tri
28 | DEdge edgeB = t.bc; // edge B of Tri
29 | DEdge edgeC = t.ca; // edge C of Tri
30 | DTriangle[] wingA = edgeA.getWing(); // wing A0, A1 of edge A
31 | DTriangle[] wingB = edgeB.getWing(); // wing B0, B1 of edge B
32 | DTriangle[] wingC = edgeC.getWing(); // wing C0, C1 of edge C
33 | }
34 | ```
35 | ## Ports to other languages
36 |
37 | - [delaunator-rs](https://github.com/mourner/delaunator-rs) (Rust)
38 | - [fogleman/delaunay](https://github.com/fogleman/delaunay) (Go)
39 | - [delaunator-cpp](https://github.com/abellgithub/delaunator-cpp) (C++)
40 | - [delaunator-sharp](https://github.com/nol1fe/delaunator-sharp) (C#)
41 | - [delaunator-ruby](https://github.com/hendrixfan/delaunator-ruby) (Ruby)
42 | - [Delaunator-Python](https://github.com/HakanSeven12/Delaunator-Python) (Python)
43 | - [hx-delaunator](https://github.com/dmitryhryppa/hx-delaunator) (Haxe)
44 | - [ricardomatias/delaunator](https://github.com/ricardomatias/delaunator) (Kotlin)
45 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 | org.waveware
6 | delaunator
7 | 0.0.1-SNAPSHOT
8 | delaunator-java
9 | A Java port of Delaunator
10 |
11 |
12 |
13 |
14 | org.apache.maven.plugins
15 | maven-compiler-plugin
16 | 3.8.1
17 |
18 | 8
19 | 8
20 | UTF-8
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/resources/delaunator.example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/waveware4ai/delaunator-java/78b776f435af1cd4d98d64f7abbab6dacc1ca406/resources/delaunator.example.png
--------------------------------------------------------------------------------
/resources/delaunator.voronoi.example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/waveware4ai/delaunator-java/78b776f435af1cd4d98d64f7abbab6dacc1ca406/resources/delaunator.voronoi.example.png
--------------------------------------------------------------------------------
/src/main/java/org/waveware/delaunator/DEdge.java:
--------------------------------------------------------------------------------
1 | package org.waveware.delaunator;
2 |
3 | public class DEdge {
4 |
5 | public DPoint a;
6 | public DPoint b;
7 |
8 | public DEdge(DPoint a, DPoint b) {
9 | boolean swap = 0 < a.compareTo(b);
10 | this.a = swap ? b : a;
11 | this.b = swap ? a : b;
12 | }
13 |
14 | public DTriangle A;
15 | public DTriangle B;
16 |
17 | public DTriangle[] getWing() {
18 | if (A != null && B != null) {
19 | return new DTriangle[] { A, B };
20 | }
21 |
22 | return new DTriangle[] { A };
23 | }
24 |
25 | public void wing(DTriangle t) {
26 | if (false) {
27 | } else if (this.A == null) {
28 | this.A = t;
29 | } else if (this.B == null) {
30 | this.B = t;
31 | } else {
32 | System.err.println("[ERR] error state in edge's wing triangle ...");
33 | }
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "e[" + a + " - " + b + "]";
39 | }
40 |
41 | Integer hash = null;
42 |
43 | @Override
44 | public int hashCode() {
45 | if (hash != null) {
46 | return hash;
47 | }
48 | return hash = hash(a, b);
49 | }
50 |
51 | public static int hash(DPoint a, DPoint b) {
52 | int ahash = a.hashCode();
53 | int bhash = b.hashCode();
54 | return ahash * 31 + bhash;
55 | }
56 |
57 | public boolean equals(DPoint a, DPoint b) {
58 | if (this.a.equals(a) && this.b.equals(b)) {
59 | return true;
60 | }
61 | if (this.a.equals(b) && this.b.equals(a)) {
62 | return true;
63 | }
64 | return false;
65 | }
66 |
67 | @Override
68 | public boolean equals(Object obj) {
69 | if (this == obj) {
70 | return true;
71 | }
72 | if (obj == null) {
73 | return false;
74 | }
75 | if (getClass() != obj.getClass()) {
76 | return false;
77 | }
78 | DEdge A = this;
79 | DEdge B = (DEdge) obj;
80 |
81 | return (A.a.equals(B.a) && A.b.equals(B.b)) || (A.a.equals(B.b) && A.b.equals(B.a));
82 | }
83 | }
--------------------------------------------------------------------------------
/src/main/java/org/waveware/delaunator/DPoint.java:
--------------------------------------------------------------------------------
1 | package org.waveware.delaunator;
2 |
3 | public class DPoint implements Comparable {
4 |
5 | public double x;
6 | public double y;
7 | private Integer hash = null;
8 |
9 | public DPoint(double x, double y) {
10 | this.x = x;
11 | this.y = y;
12 | }
13 |
14 | public double x() {
15 | return x;
16 | }
17 |
18 | public double y() {
19 | return y;
20 | }
21 |
22 | @Override
23 | public int compareTo(DPoint p) {
24 | return this.x != p.x ? Double.compare(this.x, p.x) : Double.compare(this.y, p.y);
25 | }
26 |
27 | @Override
28 | public String toString() {
29 | return "p[" + x + ", " + y + "]";
30 | }
31 |
32 | @Override
33 | public int hashCode() {
34 | if (hash != null) {
35 | return hash;
36 | }
37 | return hash = hash(x, y);
38 | }
39 |
40 | public static int hash(double x, double y) {
41 | final int prime = 31;
42 | int result = 1;
43 | long temp;
44 | temp = Double.doubleToLongBits(x);
45 | result = prime * result + (int) (temp ^ (temp >>> 32));
46 | temp = Double.doubleToLongBits(y);
47 | return prime * result + (int) (temp ^ (temp >>> 32));
48 | }
49 |
50 | @Override
51 | public boolean equals(Object obj) {
52 | if (this == obj) {
53 | return true;
54 | }
55 | if (obj == null) {
56 | return false;
57 | }
58 | if (getClass() != obj.getClass()) {
59 | return false;
60 | }
61 |
62 | DPoint a = this;
63 | DPoint b = (DPoint) obj;
64 | if (a.x != b.x || a.y != b.y) {
65 | return false;
66 | }
67 | return true;
68 | }
69 | }
--------------------------------------------------------------------------------
/src/main/java/org/waveware/delaunator/DTriangle.java:
--------------------------------------------------------------------------------
1 | package org.waveware.delaunator;
2 |
3 | import java.util.Arrays;
4 |
5 | public class DTriangle {
6 |
7 | public DPoint a;
8 | public DPoint b;
9 | public DPoint c;
10 |
11 | public DEdge ab;
12 | public DEdge bc;
13 | public DEdge ca;
14 |
15 | private Integer hash = null;
16 |
17 | public DTriangle(DPoint a, DPoint b, DPoint c) {
18 | DPoint[] tmp = { a, b, c };
19 | Arrays.sort(tmp);
20 | a = tmp[0];
21 | b = tmp[1];
22 | c = tmp[2];
23 |
24 | this.a = a;
25 | this.b = b;
26 | this.c = c;
27 | }
28 |
29 | public void edges(DEdge ab, DEdge bc, DEdge ca) {
30 | this.ab = ab.equals(a, b) ? ab : bc.equals(a, b) ? bc : ca;
31 | this.bc = ab.equals(b, c) ? ab : bc.equals(b, c) ? bc : ca;
32 | this.ca = ab.equals(c, a) ? ab : bc.equals(c, a) ? bc : ca;
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return "t[" + a + " - " + b + " - " + c + "]";
38 | }
39 |
40 | @Override
41 | public int hashCode() {
42 | if (hash != null) {
43 | return hash;
44 | }
45 | return hash = hash(a, b, c);
46 | }
47 |
48 | public static int hash(DPoint a, DPoint b, DPoint c) {
49 | final int prime = 31;
50 | int hash = 1;
51 | hash = prime * hash + a.hashCode();
52 | hash = prime * hash + b.hashCode();
53 | hash = prime * hash + c.hashCode();
54 | return hash;
55 | }
56 |
57 | @Override
58 | public boolean equals(Object obj) {
59 | if (this == obj) {
60 | return true;
61 | }
62 | if (obj == null) {
63 | return false;
64 | }
65 | if (getClass() != obj.getClass()) {
66 | return false;
67 | }
68 | DTriangle A = this;
69 | DTriangle B = (DTriangle) obj;
70 |
71 | if (A.a.equals(B.a)) {
72 | return (A.b.equals(B.b) && A.c.equals(B.c)) || (A.b.equals(B.c) && A.c.equals(B.b));
73 | } else if (A.a.equals(B.b)) {
74 | return (A.b.equals(B.a) && A.c.equals(B.c)) || (A.b.equals(B.c) && A.c.equals(B.a));
75 | } else if (A.a.equals(B.c)) {
76 | return (A.b.equals(B.a) && A.c.equals(B.b)) || (A.b.equals(B.b) && A.c.equals(B.a));
77 | }
78 |
79 | return false;
80 | }
81 | }
--------------------------------------------------------------------------------
/src/main/java/org/waveware/delaunator/Delaunator.java:
--------------------------------------------------------------------------------
1 | package org.waveware.delaunator;
2 |
3 | import java.awt.geom.Point2D;
4 | import java.util.ArrayList;
5 | import java.util.Arrays;
6 | import java.util.HashMap;
7 | import java.util.LinkedHashSet;
8 | import java.util.List;
9 | import java.util.Map;
10 | import java.util.Set;
11 |
12 | /**
13 | * This code is java port of delaunator. Delaunator triangulation algorithm is
14 | * incredibly fast and robust library for point of 2D clouds. For more
15 | * information, go to the original site https://github.com/mapbox/delaunator.
16 | *
17 | * @author 14mhz@hanmail.net
18 | * @author zookim@waveware.co.kr
19 | *
20 | */
21 | public class Delaunator {
22 |
23 | private static final double EPSILON = Math.pow(2, -52);
24 | private int[] EDGE_STACK = new int[512];
25 |
26 | public int[] triangles;
27 | public int[] halfedges;
28 | public DPoint[] points;
29 |
30 | private int hashSize;
31 | private int[] hullPrev;
32 | private int[] hullNext;
33 | private int[] hullTria;
34 | private int[] hullHash;
35 |
36 | private double cx;
37 | private double cy;
38 |
39 | private int trianglesLen;
40 | private double[] coords;
41 | private int hullStart;
42 | private int hullSize;
43 | public int[] hull;
44 |
45 | public Delaunator(List points) {
46 | this(unique(points));
47 | }
48 |
49 | public Delaunator(DPoint[] points) {
50 | if (points.length < 3) {
51 | System.err.println("Need at least 3 points");
52 | return;
53 | }
54 |
55 | points = unique(Arrays.asList(points));
56 |
57 | this.points = points;
58 | this.coords = new double[points.length * 2];
59 |
60 | for (int i = 0; i < points.length; i++) {
61 | DPoint p = points[i];
62 | coords[2 * i] = p.x;
63 | coords[2 * i + 1] = p.y;
64 | }
65 |
66 | int n = coords.length >> 1;
67 | int maxTriangles = 2 * n - 5;
68 |
69 | triangles = new int[maxTriangles * 3];
70 |
71 | halfedges = new int[maxTriangles * 3];
72 | hashSize = (int) Math.ceil(Math.sqrt(n));
73 |
74 | hullPrev = new int[n];
75 | hullNext = new int[n];
76 | hullTria = new int[n];
77 | hullHash = new int[hashSize];
78 |
79 | int[] ids = new int[n];
80 |
81 | double minX = Double.MAX_VALUE;
82 | double minY = Double.MAX_VALUE;
83 | double maxX = Double.MIN_VALUE;
84 | double maxY = Double.MIN_VALUE;
85 |
86 | for (int i = 0; i < n; i++) {
87 | double x = coords[2 * i];
88 | double y = coords[2 * i + 1];
89 | if (x < minX) {
90 | minX = x;
91 | }
92 | if (y < minY) {
93 | minY = y;
94 | }
95 | if (x > maxX) {
96 | maxX = x;
97 | }
98 | if (y > maxY) {
99 | maxY = y;
100 | }
101 | ids[i] = i;
102 | }
103 |
104 | double cx = (minX + maxX) / 2;
105 | double cy = (minY + maxY) / 2;
106 |
107 | double minDist = Double.MAX_VALUE;
108 | int i0 = 0;
109 | int i1 = 0;
110 | int i2 = 0;
111 |
112 | for (int i = 0; i < n; i++) {// pick a seed point close to the center
113 | double d = dist(cx, cy, coords[2 * i], coords[2 * i + 1]);
114 | if (d < minDist) {
115 | i0 = i;
116 | minDist = d;
117 | }
118 | }
119 | double i0x = coords[2 * i0];
120 | double i0y = coords[2 * i0 + 1];
121 |
122 | minDist = Double.MAX_VALUE;
123 |
124 | for (int i = 0; i < n; i++) {// find the point closest to the seed
125 | if (i == i0) {
126 | continue;
127 | }
128 | double d = dist(i0x, i0y, coords[2 * i], coords[2 * i + 1]);
129 | if (d < minDist && d > 0) {
130 | i1 = i;
131 | minDist = d;
132 | }
133 | }
134 |
135 | double i1x = coords[2 * i1];
136 | double i1y = coords[2 * i1 + 1];
137 | double minRadius = Double.MAX_VALUE;
138 |
139 | for (int i = 0; i < n; i++) {// find the third point which forms the smallest circumcircle with the first two
140 | if (i == i0 || i == i1) {
141 | continue;
142 | }
143 | double r = circumradius(i0x, i0y, i1x, i1y, coords[2 * i], coords[2 * i + 1]);
144 | if (r < minRadius) {
145 | i2 = i;
146 | minRadius = r;
147 | }
148 | }
149 | double i2x = coords[2 * i2];
150 | double i2y = coords[2 * i2 + 1];
151 |
152 | if (minRadius == Double.MAX_VALUE) {
153 | System.err.println("No Delaunay triangulation exists for this input.");
154 | return;
155 | }
156 |
157 | if (orient(i0x, i0y, i1x, i1y, i2x, i2y)) {
158 | int i = i1;
159 | double x = i1x;
160 | double y = i1y;
161 | i1 = i2;
162 | i1x = i2x;
163 | i1y = i2y;
164 | i2 = i;
165 | i2x = x;
166 | i2y = y;
167 | }
168 |
169 | DPoint center = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
170 | this.cx = center.x;
171 | this.cy = center.y;
172 |
173 | double[] dists = new double[n];
174 | for (int i = 0; i < n; i++) {
175 | dists[i] = dist(coords[2 * i], coords[2 * i + 1], center.x, center.y);
176 | }
177 |
178 | {// sort the points by distance from the seed triangle circumcenter
179 | quicksort(ids, dists, 0, n - 1);
180 | }
181 |
182 | // set up the seed triangle as the starting hull
183 | hullStart = i0;
184 | hullSize = 3;
185 |
186 | hullNext[i0] = hullPrev[i2] = i1;
187 | hullNext[i1] = hullPrev[i0] = i2;
188 | hullNext[i2] = hullPrev[i1] = i0;
189 |
190 | hullTria[i0] = 0;
191 | hullTria[i1] = 1;
192 | hullTria[i2] = 2;
193 |
194 | hullHash[hashKey(i0x, i0y)] = i0;
195 | hullHash[hashKey(i1x, i1y)] = i1;
196 | hullHash[hashKey(i2x, i2y)] = i2;
197 |
198 | trianglesLen = 0;
199 | addTriangle(i0, i1, i2, -1, -1, -1);
200 |
201 | double xp = 0;
202 | double yp = 0;
203 |
204 | for (int k = 0; k < ids.length; k++) {
205 | int i = ids[k];
206 | double x = coords[2 * i];
207 | double y = coords[2 * i + 1];
208 |
209 | // skip near-duplicate points
210 | if (k > 0 && Math.abs(x - xp) <= EPSILON && Math.abs(y - yp) <= EPSILON) {
211 | continue;
212 | }
213 | xp = x;
214 | yp = y;
215 |
216 | // skip seed triangle points
217 | if (i == i0 || i == i1 || i == i2) {
218 | continue;
219 | }
220 |
221 | // find a visible edge on the convex hull using edge hash
222 | int start = 0;
223 | for (int j = 0; j < hashSize; j++) {
224 | int key = hashKey(x, y);
225 | start = hullHash[(key + j) % hashSize];
226 | if (start != -1 && start != hullNext[start]) {
227 | break;
228 | }
229 | }
230 |
231 | start = hullPrev[start];
232 | int e = start;
233 | int q = hullNext[e];
234 |
235 | while (!orient(x, y, coords[2 * e], coords[2 * e + 1], coords[2 * q], coords[2 * q + 1])) {
236 | e = q;
237 | if (e == start) {
238 | e = Integer.MAX_VALUE;
239 | break;
240 | }
241 |
242 | q = hullNext[e];
243 | }
244 |
245 | if (e == Integer.MAX_VALUE) {
246 | continue; // likely a near-duplicate point; skip it
247 | }
248 |
249 | // add the first triangle from the point
250 | int t = addTriangle(e, i, hullNext[e], -1, -1, hullTria[e]);
251 |
252 | // recursively flip triangles from the point until they satisfy the Delaunay
253 | // condition
254 | hullTria[i] = legalize(t + 2);
255 | hullTria[e] = t; // keep track of boundary triangles on the hull
256 | hullSize++;
257 |
258 | // walk forward through the hull, adding more triangles and flipping recursively
259 | int next = hullNext[e];
260 | q = hullNext[next];
261 |
262 | while (orient(x, y, coords[2 * next], coords[2 * next + 1], coords[2 * q], coords[2 * q + 1])) {
263 | t = addTriangle(next, i, q, hullTria[i], -1, hullTria[next]);
264 | hullTria[i] = legalize(t + 2);
265 | hullNext[next] = next; // mark as removed
266 | hullSize--;
267 | next = q;
268 |
269 | q = hullNext[next];
270 | }
271 |
272 | if (e == start) {// walk backward from the other side, adding more triangles and flipping
273 | q = hullPrev[e];
274 |
275 | while (orient(x, y, coords[2 * q], coords[2 * q + 1], coords[2 * e], coords[2 * e + 1])) {
276 | t = addTriangle(q, i, e, -1, hullTria[e], hullTria[q]);
277 | legalize(t + 2);
278 | hullTria[q] = t;
279 | hullNext[e] = e; // mark as removed
280 | hullSize--;
281 | e = q;
282 | q = hullPrev[e];
283 | }
284 | }
285 |
286 | // update the hull indices
287 | hullStart = hullPrev[i] = e;
288 | hullNext[e] = hullPrev[next] = i;
289 | hullNext[i] = next;
290 |
291 | // save the two new edges in the hash table
292 | hullHash[hashKey(x, y)] = i;
293 | hullHash[hashKey(coords[2 * e], coords[2 * e + 1])] = e;
294 | }
295 |
296 | hull = new int[hullSize];
297 | int s = hullStart;
298 | for (int i = 0; i < hullSize; i++) {
299 | hull[i] = s;
300 | s = hullNext[s];
301 | }
302 |
303 | hullPrev = hullNext = hullTria = null; // get rid of temporary arrays
304 |
305 | // trim typed triangle mesh arrays
306 | int[] tempTriangles = new int[trianglesLen];
307 | System.arraycopy(triangles, 0, tempTriangles, 0, trianglesLen);
308 | triangles = tempTriangles;
309 |
310 | int[] tempHalfedges = new int[trianglesLen];
311 | System.arraycopy(halfedges, 0, tempHalfedges, 0, trianglesLen);
312 | halfedges = tempHalfedges;
313 |
314 | generate();
315 | }
316 |
317 | private List trias = null;
318 | private List edges = null;
319 | private List poinz = null;
320 | private List hulls = null;
321 | private List voron = null;
322 | private List vhull = null;
323 |
324 | public List getTriangles() {
325 | return trias;
326 | }
327 |
328 | public List getEdges() {
329 | return edges;
330 | }
331 |
332 | public List getPoints() {
333 | return poinz;
334 | }
335 |
336 | public List getHullEdges() {
337 | return hulls;
338 | }
339 |
340 | public List getVoronoiEdges() {
341 | return voron;
342 | }
343 |
344 | public List getVoronoiHullEdges() {
345 | return vhull;
346 | }
347 |
348 | public static List convert(List points) {
349 | List lst = new ArrayList<>();
350 | points.forEach(p -> lst.add(new DPoint(p.getX(), p.getY())));
351 | return lst;
352 | }
353 |
354 | public static DPoint[] unique(List points) {
355 | Set unq = new LinkedHashSet<>();
356 | for (int i = 0; i < points.size(); i++) {
357 | DPoint v = points.get(i);
358 | if (unq.contains(v)) {
359 | while (unq.contains(v) == false) {
360 | System.err.printf("[INF] found duplicated point (%f, %f), fix it will be plus +1e-6... \n", (float) v.x, (float) v.y);
361 | v = new DPoint(v.x() + 1e-6, v.y() + 1e-6);
362 | }
363 | }
364 | unq.add(v);
365 | }
366 |
367 | return unq.toArray(new DPoint[] {});
368 | }
369 |
370 | private void generate() {
371 | Map unq_edges = new HashMap<>();
372 | Map unq_trias = new HashMap<>();
373 |
374 | for (int n = 0; triangles != null && n < triangles.length / 3; n++) {
375 | int i = triangles[3 * n + 0];
376 | int j = triangles[3 * n + 1];
377 | int k = triangles[3 * n + 2];
378 | DPoint a = points[i];
379 | DPoint b = points[j];
380 | DPoint c = points[k];
381 |
382 | DPoint[] tmp = { a, b, c };
383 | Arrays.sort(tmp);
384 | a = tmp[0];
385 | b = tmp[1];
386 | c = tmp[2];
387 |
388 | DEdge ab = new DEdge(a, b);
389 | DEdge bc = new DEdge(b, c);
390 | DEdge ca = new DEdge(c, a);
391 |
392 | if (unq_edges.containsKey(ab)) {
393 | ab = unq_edges.get(ab);
394 | } else {
395 | unq_edges.put(ab, ab);
396 | }
397 |
398 | if (unq_edges.containsKey(bc)) {
399 | bc = unq_edges.get(bc);
400 | } else {
401 | unq_edges.put(bc, bc);
402 | }
403 |
404 | if (unq_edges.containsKey(ca)) {
405 | ca = unq_edges.get(ca);
406 | } else {
407 | unq_edges.put(ca, ca);
408 | }
409 |
410 | DTriangle t = new DTriangle(a, b, c);
411 | if (unq_trias.containsKey(t)) {
412 | System.err.println("[ERR] duplicated triangle " + t + " vs " + unq_trias.get(t));
413 | t = unq_trias.get(t);
414 | } else {
415 | unq_trias.put(t, t);
416 | }
417 |
418 | t.edges(ab, bc, ca);
419 | ab.wing(t);
420 | bc.wing(t);
421 | ca.wing(t);
422 | }
423 |
424 | ////
425 |
426 | List hulledges = new ArrayList<>();
427 | for (int i = 0; i < hull.length; i++) {
428 | DPoint a = points[hull[i]];
429 | DPoint b = points[hull[(i + 1) % hull.length]];
430 | DEdge e = new DEdge(a, b);
431 |
432 | if (unq_edges.containsKey(e)) {
433 | e = unq_edges.get(e);
434 | } else {
435 | System.err.println("[ERR] cannot found valid edge " + e);
436 | }
437 | hulledges.add(e);
438 | }
439 |
440 | ////
441 |
442 | List voronoiedges = new ArrayList<>();
443 | List voronoihulledges = new ArrayList<>();
444 | Set tmp = new LinkedHashSet();
445 | for (int i = 0; i < triangles.length; i++) {
446 | int id = triangles[nextHalfEdge(i)];
447 | if (!tmp.contains(id)) {
448 | tmp.add(id);
449 | List edges = edgesAroundPoint(i);
450 | List point = new ArrayList<>();
451 |
452 | for (int j = 0; j < edges.size(); j++) {
453 | DPoint[] pnt = getTrianglePoints(triangleOfEdge(edges.get(j)));
454 | DPoint cen = getCentroid(pnt);
455 | if (cen == null) {
456 | continue;
457 | }
458 | point.add(cen);
459 | }
460 |
461 | for (int j = 0; j < point.size(); j++) {
462 | DPoint a = point.get(j);
463 | DPoint b = point.get((j + 1) % point.size());
464 | DEdge e = new DEdge(a, b);
465 |
466 | voronoiedges.add(e);
467 | }
468 | }
469 | }
470 |
471 | this.trias = new ArrayList(unq_trias.values());
472 | this.edges = new ArrayList(unq_edges.values());
473 | this.poinz = Arrays.asList(points);
474 | this.hulls = hulledges;
475 | this.voron = voronoiedges;
476 | this.vhull = hulledges; // voronoihulledges; //it must be implement !!!
477 | }
478 |
479 | ///////////////////////////////////////////////////////////////////////////
480 |
481 | private int legalize(int a) {
482 | int i = 0;
483 | int ar;
484 |
485 | // recursion eliminated with a fixed-size stack
486 | while (true) {
487 | int b = halfedges[a];
488 |
489 | /*
490 | * if the pair of triangles doesn't satisfy the Delaunay condition (p1 is inside
491 | * the circumcircle of [p0, pl, pr]), flip them, then do the same check/flip
492 | * recursively for the new pair of triangles
493 | *
494 | * pl pl /||\ / \ al/ || \bl al/ \a / || \ / \ / a||b \ flip /___ar___\ p0\ ||
495 | * /p1 => p0\---bl---/p1 \ || / \ / ar\ || /br b\ /br \||/ \ / pr pr
496 | */
497 | int a0 = a - a % 3;
498 | ar = a0 + (a + 2) % 3;
499 |
500 | if (b == -1) { // convex hull edge
501 | if (i == 0) {
502 | break;
503 | }
504 | a = EDGE_STACK[--i];
505 | continue;
506 | }
507 |
508 | int b0 = b - b % 3;
509 | int al = a0 + (a + 1) % 3;
510 | int bl = b0 + (b + 2) % 3;
511 |
512 | int p0 = triangles[ar];
513 | int pr = triangles[a];
514 | int pl = triangles[al];
515 | int p1 = triangles[bl];
516 |
517 | boolean illegal = inCircle(coords[2 * p0], coords[2 * p0 + 1], coords[2 * pr], coords[2 * pr + 1], coords[2 * pl],
518 | coords[2 * pl + 1], coords[2 * p1], coords[2 * p1 + 1]);
519 |
520 | if (illegal) {
521 | triangles[a] = p1;
522 | triangles[b] = p0;
523 |
524 | int hbl = halfedges[bl];
525 |
526 | // edge swapped on the other side of the hull (rare); fix the halfedge reference
527 | if (hbl == -1) {
528 | int e = hullStart;
529 | do {
530 | if (hullTria[e] == bl) {
531 | hullTria[e] = a;
532 | break;
533 | }
534 | e = hullPrev[e];
535 | } while (e != hullStart);
536 | }
537 | link(a, hbl);
538 | link(b, halfedges[ar]);
539 | link(ar, bl);
540 |
541 | int br = b0 + (b + 1) % 3;
542 |
543 | // don't worry about hitting the cap: it can only happen on extremely degenerate
544 | // input
545 | if (i < EDGE_STACK.length) {
546 | EDGE_STACK[i++] = br;
547 | }
548 | } else {
549 | if (i == 0) {
550 | break;
551 | }
552 | a = EDGE_STACK[--i];
553 | }
554 | }
555 |
556 | return ar;
557 | }
558 |
559 | private int addTriangle(int i0, int i1, int i2, int a, int b, int c) {
560 | int t = trianglesLen;
561 |
562 | triangles[t] = i0;
563 | triangles[t + 1] = i1;
564 | triangles[t + 2] = i2;
565 |
566 | link(t, a);
567 | link(t + 1, b);
568 | link(t + 2, c);
569 |
570 | trianglesLen += 3;
571 | return t;
572 | }
573 |
574 | private void link(int a, int b) {
575 | halfedges[a] = b;
576 | if (b != -1) {
577 | halfedges[b] = a;
578 | }
579 | }
580 |
581 | private int hashKey(double x, double y) {
582 | return (int) (Math.floor(pseudoAngle(x - cx, y - cy) * hashSize) % hashSize);
583 | }
584 |
585 | private static boolean inCircle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py) {
586 | double dx = ax - px;
587 | double dy = ay - py;
588 | double ex = bx - px;
589 | double ey = by - py;
590 | double fx = cx - px;
591 | double fy = cy - py;
592 |
593 | double ap = dx * dx + dy * dy;
594 | double bp = ex * ex + ey * ey;
595 | double cp = fx * fx + fy * fy;
596 |
597 | return dx * (ey * cp - bp * fy) - dy * (ex * cp - bp * fx) + ap * (ex * fy - ey * fx) < 0;
598 | }
599 |
600 | private static double pseudoAngle(double dx, double dy) {
601 | double p = dx / (Math.abs(dx) + Math.abs(dy));
602 | return (dy > 0 ? 3 - p : 1 + p) / 4; // [0..1]
603 | }
604 |
605 | private static void quicksort(int[] ids, double[] dists, int left, int right) {
606 | if (right - left <= 20) {
607 | for (int i = left + 1; i <= right; i++) {
608 | int temp = ids[i];
609 | double tempDist = dists[temp];
610 | int j = i - 1;
611 | while (j >= left && dists[ids[j]] > tempDist) {
612 | ids[j + 1] = ids[j--];
613 | }
614 | ids[j + 1] = temp;
615 | }
616 | } else {
617 | int median = (left + right) >> 1;
618 | int i = left + 1;
619 | int j = right;
620 | swap(ids, median, i);
621 | if (dists[ids[left]] > dists[ids[right]]) {
622 | swap(ids, left, right);
623 | }
624 | if (dists[ids[i]] > dists[ids[right]]) {
625 | swap(ids, i, right);
626 | }
627 | if (dists[ids[left]] > dists[ids[i]]) {
628 | swap(ids, left, i);
629 | }
630 |
631 | int temp = ids[i];
632 | double tempDist = dists[temp];
633 | while (true) {
634 | do {
635 | i++;
636 | } while (dists[ids[i]] < tempDist);
637 | do {
638 | j--;
639 | } while (dists[ids[j]] > tempDist);
640 | if (j < i) {
641 | break;
642 | }
643 | swap(ids, i, j);
644 | }
645 | ids[left + 1] = ids[j];
646 | ids[j] = temp;
647 |
648 | if (right - i + 1 >= j - left) {
649 | quicksort(ids, dists, i, right);
650 | quicksort(ids, dists, left, j - 1);
651 | } else {
652 | quicksort(ids, dists, left, j - 1);
653 | quicksort(ids, dists, i, right);
654 | }
655 | }
656 | }
657 |
658 | private static void swap(int[] arr, int i, int j) {
659 | int tmp = arr[i];
660 | arr[i] = arr[j];
661 | arr[j] = tmp;
662 | }
663 |
664 | private static boolean orient(double px, double py, double qx, double qy, double rx, double ry) {
665 | return (qy - py) * (rx - qx) - (qx - px) * (ry - qy) < 0;
666 | }
667 |
668 | private static double circumradius(double ax, double ay, double bx, double by, double cx, double cy) {
669 | double dx = bx - ax;
670 | double dy = by - ay;
671 | double ex = cx - ax;
672 | double ey = cy - ay;
673 | double bl = dx * dx + dy * dy;
674 | double cl = ex * ex + ey * ey;
675 | double d = 0.5 / (dx * ey - dy * ex);
676 | double x = (ey * bl - dy * cl) * d;
677 | double y = (dx * cl - ex * bl) * d;
678 | return x * x + y * y;
679 | }
680 |
681 | protected static DPoint circumcenter(double ax, double ay, double bx, double by, double cx, double cy) {
682 | double dx = bx - ax;
683 | double dy = by - ay;
684 | double ex = cx - ax;
685 | double ey = cy - ay;
686 | double bl = dx * dx + dy * dy;
687 | double cl = ex * ex + ey * ey;
688 | double d = 0.5 / (dx * ey - dy * ex);
689 | double x = ax + (ey * bl - dy * cl) * d;
690 | double y = ay + (dx * cl - ex * bl) * d;
691 |
692 | return new DPoint(x, y);
693 | }
694 |
695 | private static double dist(double ax, double ay, double bx, double by) {
696 | double dx = ax - bx;
697 | double dy = ay - by;
698 | return dx * dx + dy * dy;
699 | }
700 |
701 | ///////////////////////////////////////////////////////////////////////////
702 |
703 | public int[] edgesOfTriangle(int t) {
704 | return new int[] { 3 * t, 3 * t + 1, 3 * t + 2 };
705 | }
706 |
707 | public int triangleOfEdge(int e) {
708 | return (int) Math.floor(e / 3);
709 | }
710 |
711 | public int nextHalfEdge(int e) {
712 | return (e % 3 == 2) ? e - 2 : e + 1;
713 | }
714 |
715 | public int prevHalfEdge(int e) {
716 | return (e % 3 == 0) ? e + 2 : e - 1;
717 | }
718 |
719 | public int[] pointsOfTriangle(int t) {
720 | int[] e = edgesOfTriangle(t);
721 | int a = triangles[e[0]];
722 | int b = triangles[e[1]];
723 | int c = triangles[e[2]];
724 | return new int[] { a, b, c };
725 | }
726 |
727 | public DPoint[] getTrianglePoints(int t) {
728 | int[] p = pointsOfTriangle(t);
729 | DPoint a = points[p[0]];
730 | DPoint b = points[p[1]];
731 | DPoint c = points[p[2]];
732 |
733 | return new DPoint[] { a, b, c };
734 | }
735 |
736 | public DPoint[] getEdgesOfTriangle(int t) {
737 | int[] e = edgesOfTriangle(t);
738 | DPoint a = points[e[0]];
739 | DPoint b = points[e[1]];
740 | DPoint c = points[e[2]];
741 | return new DPoint[] { a, b, c };
742 | }
743 |
744 | public DPoint[] GetHullPoints() {
745 | DPoint[] hullpoint = new DPoint[hull.length];
746 | for (int i = 0; i < hull.length; i++) {
747 | hullpoint[i] = points[hull[i]];
748 | }
749 | return hullpoint;
750 | }
751 |
752 | public DPoint getTriangleCircumcenter(int t) {
753 | DPoint[] vertices = getTrianglePoints(t);
754 | return getCircumcenter(vertices[0], vertices[1], vertices[2]);
755 | }
756 |
757 | public DPoint getCentroid(int t) {
758 | DPoint[] vertices = getTrianglePoints(t);
759 | return getCentroid(vertices);
760 | }
761 |
762 | public DPoint getCircumcenter(DPoint a, DPoint b, DPoint c) {
763 | return circumcenter(a.x, a.y, b.x, b.y, c.x, c.y);
764 | }
765 |
766 | public DPoint getCentroid(DPoint[] points) {
767 | double accumulatedArea = 0.0f;
768 | double centerX = 0.0f;
769 | double centerY = 0.0f;
770 |
771 | for (int i = 0, j = points.length - 1; i < points.length; j = i++) {
772 | double temp = points[i].x * points[j].y - points[j].x * points[i].y;
773 | accumulatedArea += temp;
774 | centerX += (points[i].x + points[j].x) * temp;
775 | centerY += (points[i].y + points[j].y) * temp;
776 | }
777 |
778 | if (Math.abs(accumulatedArea) < 1E-7f) {
779 | return null; // new DPoint(0, 0); // ???
780 | }
781 |
782 | accumulatedArea *= 3f;
783 | return new DPoint(centerX / accumulatedArea, centerY / accumulatedArea);
784 | }
785 |
786 | public List edgesAroundPoint(int start) {
787 | List lst = new ArrayList<>();
788 | int incoming = start;
789 | do {
790 | lst.add(incoming);
791 | int outgoing = nextHalfEdge(incoming);
792 | incoming = halfedges[outgoing];
793 | } while (incoming != -1 && incoming != start);
794 |
795 | return lst;
796 | }
797 |
798 | public List trianglesAdjacentToTriangle(int t) {
799 | List adjacentTriangles = new ArrayList<>();
800 | int[] triangleEdges = edgesOfTriangle(t);
801 |
802 | for (int i = 0; i < triangleEdges.length; i++) {
803 | int e = triangleEdges[i];
804 | int opposite = halfedges[e];
805 | if (opposite >= 0) {
806 | adjacentTriangles.add(triangleOfEdge(opposite));
807 | }
808 | }
809 | return adjacentTriangles;
810 | }
811 | }
812 |
--------------------------------------------------------------------------------