Skip to content

Commit

Permalink
Update PolyPartition / Triangulator library
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Jan 12, 2021
1 parent bd07c5b commit ddd6fb3
Show file tree
Hide file tree
Showing 13 changed files with 3,087 additions and 1,896 deletions.
12 changes: 6 additions & 6 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ Comment: Minimal PCG32 implementation
Copyright: 2014, M.E. O'Neill
License: Apache-2.0

Files: ./thirdparty/misc/polypartition.cpp
./thirdparty/misc/polypartition.h
Comment: PolyPartition / Triangulator
Copyright: 2011-2021, Ivan Fratric and contributors
License: Expat

Files: ./thirdparty/misc/r128.c
./thirdparty/misc/r128.h
Comment: r128 library
Expand All @@ -338,12 +344,6 @@ Comment: stb libraries
Copyright: Sean Barrett
License: public-domain or Unlicense or Expat

Files: ./thirdparty/misc/triangulator.cpp
./thirdparty/misc/triangulator.h
Comment: PolyPartition
Copyright: 2011, Ivan Fratric
License: Expat

Files: ./thirdparty/misc/yuv2rgb.h
Comment: YUV2RGB
Copyright: 2008-2011, Robin Watts
Expand Down
2 changes: 1 addition & 1 deletion core/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ thirdparty_misc_sources = [
"smaz.c",
# C++ sources
"pcg.cpp",
"triangulator.cpp",
"polypartition.cpp",
"clipper.cpp",
]
thirdparty_misc_sources = [thirdparty_misc_dir + file for file in thirdparty_misc_sources]
Expand Down
14 changes: 7 additions & 7 deletions core/math/geometry_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,33 @@
#include "geometry_2d.h"

#include "thirdparty/misc/clipper.hpp"
#include "thirdparty/misc/triangulator.h"
#include "thirdparty/misc/polypartition.h"
#define STB_RECT_PACK_IMPLEMENTATION
#include "thirdparty/misc/stb_rect_pack.h"

#define SCALE_FACTOR 100000.0 // Based on CMP_EPSILON.

Vector<Vector<Vector2>> Geometry2D::decompose_polygon_in_convex(Vector<Point2> polygon) {
Vector<Vector<Vector2>> decomp;
List<TriangulatorPoly> in_poly, out_poly;
List<TPPLPoly> in_poly, out_poly;

TriangulatorPoly inp;
TPPLPoly inp;
inp.Init(polygon.size());
for (int i = 0; i < polygon.size(); i++) {
inp.GetPoint(i) = polygon[i];
}
inp.SetOrientation(TRIANGULATOR_CCW);
inp.SetOrientation(TPPL_ORIENTATION_CCW);
in_poly.push_back(inp);
TriangulatorPartition tpart;
TPPLPartition tpart;
if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { // Failed.
ERR_PRINT("Convex decomposing failed!");
return decomp;
}

decomp.resize(out_poly.size());
int idx = 0;
for (List<TriangulatorPoly>::Element *I = out_poly.front(); I; I = I->next()) {
TriangulatorPoly &tp = I->get();
for (List<TPPLPoly>::Element *I = out_poly.front(); I; I = I->next()) {
TPPLPoly &tp = I->get();

decomp.write[idx].resize(tp.GetNumPoints());

Expand Down
2 changes: 1 addition & 1 deletion core/math/geometry_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "core/string/print_string.h"

#include "thirdparty/misc/clipper.hpp"
#include "thirdparty/misc/triangulator.h"
#include "thirdparty/misc/polypartition.h"

void Geometry3D::MeshData::optimize_vertices() {
Map<int, int> vtx_remap;
Expand Down
24 changes: 12 additions & 12 deletions modules/fbx/data/fbx_mesh_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "scene/resources/mesh.h"
#include "scene/resources/surface_tool.h"

#include "thirdparty/misc/triangulator.h"
#include "thirdparty/misc/polypartition.h"

template <class T>
T collect_first(const Vector<VertexData<T>> *p_data, T p_fall_back) {
Expand Down Expand Up @@ -930,30 +930,30 @@ void FBXMeshData::triangulate_polygon(Ref<SurfaceTool> st, Vector<int> p_polygon
}
}

TriangulatorPoly triangulator_poly;
triangulator_poly.Init(polygon_vertex_count);
TPPLPoly tppl_poly;
tppl_poly.Init(polygon_vertex_count);
std::vector<Vector2> projected_vertices(polygon_vertex_count);
for (int i = 0; i < polygon_vertex_count; i += 1) {
const Vector2 pv(poly_vertices[i][axis_1_coord], poly_vertices[i][axis_2_coord]);
projected_vertices[i] = pv;
triangulator_poly.GetPoint(i) = pv;
tppl_poly.GetPoint(i) = pv;
}
triangulator_poly.SetOrientation(TRIANGULATOR_CCW);
tppl_poly.SetOrientation(TPPL_ORIENTATION_CCW);

List<TriangulatorPoly> out_poly;
List<TPPLPoly> out_poly;

TriangulatorPartition triangulator_partition;
if (triangulator_partition.Triangulate_OPT(&triangulator_poly, &out_poly) == 0) { // Good result.
if (triangulator_partition.Triangulate_EC(&triangulator_poly, &out_poly) == 0) { // Medium result.
if (triangulator_partition.Triangulate_MONO(&triangulator_poly, &out_poly) == 0) { // Really poor result.
TPPLPartition tppl_partition;
if (tppl_partition.Triangulate_OPT(&tppl_poly, &out_poly) == 0) { // Good result.
if (tppl_partition.Triangulate_EC(&tppl_poly, &out_poly) == 0) { // Medium result.
if (tppl_partition.Triangulate_MONO(&tppl_poly, &out_poly) == 0) { // Really poor result.
ERR_FAIL_MSG("The triangulation of this polygon failed, please try to triangulate your mesh or check if it has broken polygons.");
}
}
}

std::vector<Vector2> tris(out_poly.size());
for (List<TriangulatorPoly>::Element *I = out_poly.front(); I; I = I->next()) {
TriangulatorPoly &tp = I->get();
for (List<TPPLPoly>::Element *I = out_poly.front(); I; I = I->next()) {
TPPLPoly &tp = I->get();

ERR_FAIL_COND_MSG(tp.GetNumPoints() != 3, "The triangulator retuned more points, how this is possible?");
// Find Index
Expand Down
2 changes: 1 addition & 1 deletion scene/2d/collision_polygon_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "scene/resources/concave_polygon_shape_2d.h"
#include "scene/resources/convex_polygon_shape_2d.h"

#include "thirdparty/misc/triangulator.h"
#include "thirdparty/misc/polypartition.h"

void CollisionPolygon2D::_build_polygon() {
parent->shape_owner_clear_shapes(owner_id);
Expand Down
16 changes: 8 additions & 8 deletions scene/2d/navigation_region_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "navigation_2d.h"
#include "servers/navigation_server_2d.h"

#include "thirdparty/misc/triangulator.h"
#include "thirdparty/misc/polypartition.h"

#ifdef TOOLS_ENABLED
Rect2 NavigationPolygon::_edit_get_rect() const {
Expand Down Expand Up @@ -228,7 +228,7 @@ void NavigationPolygon::make_polygons_from_outlines() {
MutexLock lock(navmesh_generation);
navmesh.unref();
}
List<TriangulatorPoly> in_poly, out_poly;
List<TPPLPoly> in_poly, out_poly;

Vector2 outside_point(-1e10, -1e10);

Expand Down Expand Up @@ -278,23 +278,23 @@ void NavigationPolygon::make_polygons_from_outlines() {

bool outer = (interscount % 2) == 0;

TriangulatorPoly tp;
TPPLPoly tp;
tp.Init(olsize);
for (int j = 0; j < olsize; j++) {
tp[j] = r[j];
}

if (outer) {
tp.SetOrientation(TRIANGULATOR_CCW);
tp.SetOrientation(TPPL_ORIENTATION_CCW);
} else {
tp.SetOrientation(TRIANGULATOR_CW);
tp.SetOrientation(TPPL_ORIENTATION_CW);
tp.SetHole(true);
}

in_poly.push_back(tp);
}

TriangulatorPartition tpart;
TPPLPartition tpart;
if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { //failed!
ERR_PRINT("NavigationPolygon: Convex partition failed!");
return;
Expand All @@ -304,8 +304,8 @@ void NavigationPolygon::make_polygons_from_outlines() {
vertices.resize(0);

Map<Vector2, int> points;
for (List<TriangulatorPoly>::Element *I = out_poly.front(); I; I = I->next()) {
TriangulatorPoly &tp = I->get();
for (List<TPPLPoly>::Element *I = out_poly.front(); I; I = I->next()) {
TPPLPoly &tp = I->get();

struct Polygon p;

Expand Down
9 changes: 5 additions & 4 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ Collection of single-file libraries used in Godot components.
* Upstream: http://www.pcg-random.org
* Version: minimal C implementation, http://www.pcg-random.org/download.html
* License: Apache 2.0
- `polypartition.{cpp,h}`
* Upstream: https://github.com/ivanfratric/polypartition (`src/polypartition.{cpp,h}`)
* Version: git (7bdffb428b2b19ad1c43aa44c714dcc104177e84, 2021)
* Modifications: Change from STL to Godot types (see provided patch).
* License: MIT
- `r128.h`
* Upstream: https://github.com/fahickman/r128
* Version: 1.4.4 (cf2e88fc3e7d7dfe99189686f914874cd0bda15e, 2020)
Expand All @@ -441,10 +446,6 @@ Collection of single-file libraries used in Godot components.
* Upstream: https://github.com/nothings/stb
* Version: 1.20 (314d0a6f9af5af27e585336eecea333e95c5a2d8, 2020)
* License: Public Domain or Unlicense or MIT
- `triangulator.{cpp,h}`
* Upstream: https://github.com/ivanfratric/polypartition (`src/polypartition.cpp`)
* Version: TBD, class was renamed
* License: MIT
- `yuv2rgb.h`
* Upstream: http://wss.co.uk/pinknoise/yuv2rgb/ (to check)
* Version: ?
Expand Down
Loading

0 comments on commit ddd6fb3

Please sign in to comment.