Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Clipper2 (beta) wrapper implementation #23559

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions editor/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ if env['tools']:
env.CommandNoCache('#editor/builtin_fonts.gen.h', flist, run_in_subprocess(editor_builders.make_fonts_header))

env.add_source_files(env.editor_sources, "*.cpp")
env_thirdparty = env.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.add_source_files(env.editor_sources, ["#thirdparty/misc/clipper.cpp"])

SConscript('collada/SCsub')
SConscript('doc/SCsub')
Expand Down
1 change: 0 additions & 1 deletion editor/plugins/skeleton_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "canvas_item_editor_plugin.h"
#include "scene/2d/mesh_instance_2d.h"
#include "scene/gui/box_container.h"
#include "thirdparty/misc/clipper.hpp"

void Skeleton2DEditor::_node_removed(Node *p_node) {

Expand Down
67 changes: 31 additions & 36 deletions editor/plugins/sprite_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
#include "canvas_item_editor_plugin.h"
#include "scene/2d/mesh_instance_2d.h"
#include "scene/gui/box_container.h"
#include "thirdparty/misc/clipper.hpp"
#include "thirdparty/misc/clipper.h"
#include "thirdparty/misc/clipper_offset.h"

void SpriteEditor::_node_removed(Node *p_node) {

Expand All @@ -51,57 +52,51 @@ void SpriteEditor::edit(Sprite *p_sprite) {
#define PRECISION 10.0

Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) {

using namespace clipperlib;

int size = points.size();
ERR_FAIL_COND_V(size < 2, Vector<Vector2>());

ClipperLib::Path subj;
ClipperLib::PolyTree solution;
ClipperLib::PolyTree out;
Path subj;
Paths solution;
PolyPath out_closed;
Paths out_open; // ignored

for (int i = 0; i < points.size(); i++) {

subj << ClipperLib::IntPoint(points[i].x * PRECISION, points[i].y * PRECISION);
subj << Point64(points[i].x * PRECISION, points[i].y * PRECISION);
}
ClipperLib::ClipperOffset co;
co.AddPath(subj, ClipperLib::jtMiter, ClipperLib::etClosedPolygon);
ClipperOffset co;
co.AddPath(subj, kMiter, kPolygon);
co.Execute(solution, epsilon * PRECISION);

ClipperLib::PolyNode *p = solution.GetFirst();

ERR_FAIL_COND_V(!p, points);
// Clamp into the specified rect
Clipper cl;
cl.AddPaths(solution, ptSubject);

while (p->IsHole()) {
p = p->GetNext();
}
// Create the clipping rect
Path clamp;
clamp << Point64(0, 0);
clamp << Point64(rect.size.width * PRECISION, 0);
clamp << Point64(rect.size.width * PRECISION, rect.size.height * PRECISION);
clamp << Point64(0, rect.size.height * PRECISION);

//turn the result into simply polygon (AKA, fix overlap)

//clamp into the specified rect
ClipperLib::Clipper cl;
cl.StrictlySimple(true);
cl.AddPath(p->Contour, ClipperLib::ptSubject, true);
//create the clipping rect
ClipperLib::Path clamp;
clamp.push_back(ClipperLib::IntPoint(0, 0));
clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, 0));
clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, rect.size.height * PRECISION));
clamp.push_back(ClipperLib::IntPoint(0, rect.size.height * PRECISION));
cl.AddPath(clamp, ClipperLib::ptClip, true);
cl.Execute(ClipperLib::ctIntersection, out);
cl.AddPath(clamp, ptClip);
cl.Execute(ctIntersection, out_closed, out_open);

Vector<Vector2> outPoints;
ClipperLib::PolyNode *p2 = out.GetFirst();
ERR_FAIL_COND_V(!p2, points);

while (p2->IsHole()) {
p2 = p2->GetNext();
}
ERR_FAIL_COND_V(out_closed.ChildCount() == 0, points);

// Root's children are always boundaries, not holes
const Path &outer = out_closed.GetChild(0).GetPath();

int lasti = p2->Contour.size() - 1;
Vector2 prev = Vector2(p2->Contour[lasti].X / PRECISION, p2->Contour[lasti].Y / PRECISION);
for (unsigned int i = 0; i < p2->Contour.size(); i++) {
int lasti = outer.size() - 1;
Vector2 prev = Vector2(outer[lasti].x / PRECISION, outer[lasti].y / PRECISION);
for (unsigned int i = 0; i < outer.size(); i++) {

Vector2 cur = Vector2(p2->Contour[i].X / PRECISION, p2->Contour[i].Y / PRECISION);
Vector2 cur = Vector2(outer[i].x / PRECISION, outer[i].y / PRECISION);
if (cur.distance_to(prev) > 0.5) {
outPoints.push_back(cur);
prev = cur;
Expand Down
24 changes: 24 additions & 0 deletions modules/clipper/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python

Import('env')
Import('env_modules')

env_clipper = env_modules.Clone()

# Thirdparty source files
thirdparty_dir = "#thirdparty/misc/"
thirdparty_sources = [
"clipper.cpp",
"clipper_offset.cpp",
"clipper_triangulation.cpp",
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]

env_clipper.Append(CPPPATH=[thirdparty_dir])

env_thirdparty = env_clipper.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.add_source_files(env.modules_sources, thirdparty_sources)

# Godot's own source files
env_clipper.add_source_files(env.modules_sources, "*.cpp")
Loading