booleanOperations icon indicating copy to clipboard operation
booleanOperations copied to clipboard

Port to VectorBoolean

Open behdad opened this issue 8 years ago • 40 comments

We should port this module to use VectorBoolean instead of Clipper.

https://bitbucket.org/andyfinnell/vectorboolean http://losingfight.com/blog/category/graphics/

Main obstacle is that it's a Objective-C library. We should port to C++ (or ask Andy to do; I'm contacting him separately), but might be worth trying it out first. I managed to get it compiled on Linux using:

clang++ -fblocks *.m  -I /usr/include/GNUstep -I /usr/lib/gcc/x86_64-nux-gnu/4.8/include/objc -lobjc -lgnustep-base -lgnustep-gui -lBlocksRuntime

I had issues with blocks_runtime.h missing from my system, which I hacked around. The app doesn't run for me, but I imagine the operations work. It should be even easier to get it running on OS X. It's probably a weekend's work to hook this up to booleanOperations for someone comfortable with Objective-C, or a week for someone who isn't.

behdad avatar Nov 08 '16 22:11 behdad

Skia does have vector boolean ops: https://www.youtube.com/watch?v=OmfliNQsk88

Maybe we can use it or extract the relevant code? cc @khaledhosny

adrientetar avatar Sep 16 '17 08:09 adrientetar

Boths seems promissing... but python bindings are really necessary in any case...

typemytype avatar Sep 16 '17 13:09 typemytype

So we need someone who's comfortable with C++ and Objective-C to make a bridge.

behdad avatar Oct 31 '17 18:10 behdad

or Swift... https://github.com/lrtitze/Swift-VectorBoolean

anthrotype avatar Oct 31 '17 18:10 anthrotype

The Objective-C version at least I can compile on Linux using clang / gcc.

behdad avatar Oct 31 '17 18:10 behdad

yes, sure. I just mentioned the swift version because it seems more recent, has a few tests, and the code may be easier to read than Objective-C.

EDIT: at least for me... :smile:

anthrotype avatar Oct 31 '17 18:10 anthrotype

Behdad, do you dislike the Skia option?

It's battle-tested with fuzzing etc. probably the most robust vector boolean code out there. One has to extract the relevant source files from "core" and "pathops" subfolders and work from there.

adrientetar avatar Oct 31 '17 21:10 adrientetar

I definitely don't like pulling all of Skia. I have not watched that video yet. Will do. I think extracting it would be much more work than porting the other project. I'll watch the video today; I'm also taking a look at the Swift version to see if I can just port it straight.

behdad avatar Oct 31 '17 21:10 behdad

In general I'm skeptical of Skia getting this right. A lot of Skia vector code I've checked was just not there...

behdad avatar Oct 31 '17 21:10 behdad

I think extracting it would be much more work than porting the other project.

I looked at it briefly and it seems relatively self-contained... mostly the vector ops depend on primitive types like SkRect/SkMatrix etc. and other ground stuff like SkArenaAlloc/SkBuffer.

A lot of Skia vector code I've checked was just not there...

idk, but in the video he says he first wrote boolean ops for QuickDraw GX in the 80s. I would think he has it right if he's been working on it for 30 years...

adrientetar avatar Oct 31 '17 21:10 adrientetar

idk, but in the video he says he first wrote boolean ops for QuickDraw GX in the 80s. I would think he has it right if he's been working on it for 30 years...

Sure. I'll watch the video.

behdad avatar Oct 31 '17 21:10 behdad

Yeah, I like the video.

behdad avatar Oct 31 '17 22:10 behdad

Why this is needed/wanted?

khaledhosny avatar Nov 06 '17 20:11 khaledhosny

Why this is needed/wanted?

Because converting to lines, processing, and back, is slow and error-prone, and the Perfectionist's nightmare! :D

behdad avatar Nov 06 '17 20:11 behdad

I can give the extracting from Skia a try, but I’d know what to do with that code after that.

khaledhosny avatar Nov 06 '17 21:11 khaledhosny

In the end, we want a point- or segment-pen that removes overlaps.

behdad avatar Nov 06 '17 21:11 behdad

Thanks Khaled. I'll also talk to the Skia developer to see what his thoughts are.

behdad avatar Nov 06 '17 21:11 behdad

I haven’t looked at the code, but from the video it looks like the skia shape ops converts cubic into quadratic to compute the intersections. We want to retain the cubics.

anthrotype avatar Nov 06 '17 21:11 anthrotype

I can give the extracting from Skia a try, but I’d know what to do with that code after that.

I think basically we need a pen that builds an SkPath, then call the Op() function defined in SkPathOps.h, then the inverse pen.

adrientetar avatar Nov 06 '17 21:11 adrientetar

I haven’t looked at the code, but from the video it looks like the skia shape ops converts cubic into quadratic to compute the intersections. We want to retain the cubics.

My understanding of the video was that it converts to quadratics to get a feel of the t, then works that back onto the cubic. I'm sure it doesn't convert to quadratics in the output.

behdad avatar Nov 06 '17 21:11 behdad

OK, I now have something that builds and runs, can take paths and return their union (or any path operation supported by Skia). All C++, no Python interface yet.

The bad thing is this is some 60000 lines of code, the good thing is that all is copied verbatim from Skia without modifications so should be easy to keep in sync.

khaledhosny avatar Nov 07 '17 12:11 khaledhosny

https://github.com/khaledhosny/skia-pathops, there is a simple Makefile and the code probably does not build on Windows/Mac.

khaledhosny avatar Nov 07 '17 12:11 khaledhosny

Great! That was quick! :D I would like to keep the same python interface that booleanOperations has: that is, one function for each operation, which takes a list of UFO contours and a pointPen to draw the result on. Internally, we take the input contours, extract the segments using a pointPen, and get the resulting array of array of arrays, that we then feed to our C++ library and then we take the output and draw it on a the output pointpen.

If you have a Makefile that builds a shared library, I can make a quick wrapper for it using cffi or ctypes.

anthrotype avatar Nov 07 '17 12:11 anthrotype

nice, the skia path objects have a pen-like interface very similar to our own! Maybe the python API can expose that directly.

anthrotype avatar Nov 07 '17 12:11 anthrotype

Awesome. Thanks Khaled!

behdad avatar Nov 07 '17 14:11 behdad

super!

as far as I understand only the pathOps are extracted from the Skia project?

FYI: it does not build on mac 10.10 do you want the error message?

+1 for keeping the booleanOperation api

typemytype avatar Nov 07 '17 15:11 typemytype

as far as I understand only the pathOps are extracted from the Skia project?

the whole skia tree is cloned as a git submodule, then we only build the part we need, which is pathops and some parts of the core.

We'll make it build eventually on all platforms. Please file an issue over at https://github.com/khaledhosny/skia-pathops

anthrotype avatar Nov 07 '17 15:11 anthrotype

Also, did anyone consider AFDKO's impl? https://github.com/adobe-type-tools/afdko/blob/develop/c/public/lib/source/absfont/absfont_path.c

behdad avatar Jul 26 '18 22:07 behdad

I didn't know that. what does that do?

anthrotype avatar Jul 26 '18 22:07 anthrotype

Remove overlaps...

On Thu, Jul 26, 2018 at 3:35 PM, Cosimo Lupo [email protected] wrote:

I didn't know that. what does that do?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/typemytype/booleanOperations/issues/40#issuecomment-408256079, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIuK-qc5liVJR58RivPd_8uScl9uAbfks5uKkQ1gaJpZM4Ks_OU .

-- behdad http://behdad.org/

behdad avatar Jul 26 '18 22:07 behdad