2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* triangulate.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "triangulate.h"
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t Triangulate::get_area(const Vector<Vector2> &contour) {
|
2014-02-10 02:10:30 +01:00
|
|
|
int n = contour.size();
|
|
|
|
const Vector2 *c = &contour[0];
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t A = 0.0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
for (int p = n - 1, q = 0; q < n; p = q++) {
|
|
|
|
A += c[p].cross(c[q]);
|
|
|
|
}
|
2022-02-06 12:14:58 +01:00
|
|
|
return A * 0.5f;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:43:36 +02:00
|
|
|
/* `is_inside_triangle` decides if a point P is inside the triangle
|
|
|
|
* defined by A, B, C. */
|
2017-01-14 21:35:39 +01:00
|
|
|
bool Triangulate::is_inside_triangle(real_t Ax, real_t Ay,
|
|
|
|
real_t Bx, real_t By,
|
|
|
|
real_t Cx, real_t Cy,
|
2018-03-01 04:46:56 +01:00
|
|
|
real_t Px, real_t Py,
|
2021-10-28 15:43:36 +02:00
|
|
|
bool include_edges) {
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
|
|
|
|
real_t cCROSSap, bCROSScp, aCROSSbp;
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
ax = Cx - Bx;
|
|
|
|
ay = Cy - By;
|
|
|
|
bx = Ax - Cx;
|
|
|
|
by = Ay - Cy;
|
|
|
|
cx = Bx - Ax;
|
|
|
|
cy = By - Ay;
|
|
|
|
apx = Px - Ax;
|
|
|
|
apy = Py - Ay;
|
|
|
|
bpx = Px - Bx;
|
|
|
|
bpy = Py - By;
|
|
|
|
cpx = Px - Cx;
|
|
|
|
cpy = Py - Cy;
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
aCROSSbp = ax * bpy - ay * bpx;
|
|
|
|
cCROSSap = cx * apy - cy * apx;
|
|
|
|
bCROSScp = bx * cpy - by * cpx;
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2018-03-01 04:46:56 +01:00
|
|
|
if (include_edges) {
|
2022-02-06 12:14:58 +01:00
|
|
|
return ((aCROSSbp > 0.0f) && (bCROSScp > 0.0f) && (cCROSSap > 0.0f));
|
2018-03-01 04:46:56 +01:00
|
|
|
} else {
|
2022-02-06 12:14:58 +01:00
|
|
|
return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
|
2018-03-01 04:46:56 +01:00
|
|
|
}
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-03-01 04:46:56 +01:00
|
|
|
bool Triangulate::snip(const Vector<Vector2> &p_contour, int u, int v, int w, int n, const Vector<int> &V, bool relaxed) {
|
2014-02-10 02:10:30 +01:00
|
|
|
int p;
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t Ax, Ay, Bx, By, Cx, Cy, Px, Py;
|
2014-02-10 02:10:30 +01:00
|
|
|
const Vector2 *contour = &p_contour[0];
|
|
|
|
|
|
|
|
Ax = contour[V[u]].x;
|
|
|
|
Ay = contour[V[u]].y;
|
|
|
|
|
|
|
|
Bx = contour[V[v]].x;
|
|
|
|
By = contour[V[v]].y;
|
|
|
|
|
|
|
|
Cx = contour[V[w]].x;
|
|
|
|
Cy = contour[V[w]].y;
|
|
|
|
|
2018-03-01 04:46:56 +01:00
|
|
|
// It can happen that the triangulation ends up with three aligned vertices to deal with.
|
|
|
|
// In this scenario, making the check below strict may reject the possibility of
|
2021-03-12 14:35:16 +01:00
|
|
|
// forming a last triangle with these aligned vertices, preventing the triangulation
|
2018-03-01 04:46:56 +01:00
|
|
|
// from completing.
|
|
|
|
// To avoid that we allow zero-area triangles if all else failed.
|
|
|
|
float threshold = relaxed ? -CMP_EPSILON : CMP_EPSILON;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (threshold > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax)))) {
|
2018-03-01 04:46:56 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
for (p = 0; p < n; p++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if ((p == u) || (p == v) || (p == w)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
Px = contour[V[p]].x;
|
|
|
|
Py = contour[V[p]].y;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_inside_triangle(Ax, Ay, Bx, By, Cx, Cy, Px, Py, relaxed)) {
|
2018-03-01 04:46:56 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Triangulate::triangulate(const Vector<Vector2> &contour, Vector<int> &result) {
|
|
|
|
/* allocate and initialize list of Vertices in polygon */
|
|
|
|
|
|
|
|
int n = contour.size();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (n < 3) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-11-04 12:55:08 +01:00
|
|
|
Vector<int> V;
|
|
|
|
V.resize(n);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* we want a counter-clockwise polygon in V */
|
|
|
|
|
2022-02-06 12:14:58 +01:00
|
|
|
if (0.0f < get_area(contour)) {
|
2020-05-14 16:41:43 +02:00
|
|
|
for (int v = 0; v < n; v++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
V.write[v] = v;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int v = 0; v < n; v++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
V.write[v] = (n - 1) - v;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-03-01 04:46:56 +01:00
|
|
|
bool relaxed = false;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
int nv = n;
|
|
|
|
|
|
|
|
/* remove nv-2 Vertices, creating 1 triangle every time */
|
|
|
|
int count = 2 * nv; /* error detection */
|
|
|
|
|
2016-11-04 12:55:08 +01:00
|
|
|
for (int v = nv - 1; nv > 2;) {
|
2014-02-10 02:10:30 +01:00
|
|
|
/* if we loop, it is probably a non-simple polygon */
|
|
|
|
if (0 >= (count--)) {
|
2018-03-01 04:46:56 +01:00
|
|
|
if (relaxed) {
|
|
|
|
//** Triangulate: ERROR - probable bad polygon!
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// There may be aligned vertices that the strict
|
|
|
|
// checks prevent from triangulating. In this situation
|
|
|
|
// we are better off adding flat triangles than
|
|
|
|
// failing, so we relax the checks and try one last
|
|
|
|
// round.
|
|
|
|
// Only relaxing the constraints as a last resort avoids
|
|
|
|
// degenerate triangles when they aren't necessary.
|
|
|
|
count = 2 * nv;
|
|
|
|
relaxed = true;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* three consecutive vertices in current polygon, <u,v,w> */
|
|
|
|
int u = v;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (nv <= u) {
|
2014-02-10 02:10:30 +01:00
|
|
|
u = 0; /* previous */
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
v = u + 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (nv <= v) {
|
2014-02-10 02:10:30 +01:00
|
|
|
v = 0; /* new v */
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
int w = v + 1;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (nv <= w) {
|
2014-02-10 02:10:30 +01:00
|
|
|
w = 0; /* next */
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-03-01 04:46:56 +01:00
|
|
|
if (snip(contour, u, v, w, nv, V, relaxed)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
int a, b, c, s, t;
|
|
|
|
|
|
|
|
/* true names of the vertices */
|
|
|
|
a = V[u];
|
|
|
|
b = V[v];
|
|
|
|
c = V[w];
|
|
|
|
|
|
|
|
/* output Triangle */
|
|
|
|
result.push_back(a);
|
|
|
|
result.push_back(b);
|
|
|
|
result.push_back(c);
|
|
|
|
|
|
|
|
/* remove v from remaining polygon */
|
2020-05-14 16:41:43 +02:00
|
|
|
for (s = v, t = v + 1; t < nv; s++, t++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
V.write[s] = V[t];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-07-26 15:04:54 +02:00
|
|
|
|
|
|
|
nv--;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-09-13 03:38:39 +02:00
|
|
|
/* reset error detection counter */
|
2014-02-10 02:10:30 +01:00
|
|
|
count = 2 * nv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|