Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSom committed Dec 27, 2023
0 parents commit 7d7e9e0
Show file tree
Hide file tree
Showing 33 changed files with 1,291 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "clipper2"]
path = clipper2
url = https://github.com/AngusJohnson/Clipper2
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
47 changes: 47 additions & 0 deletions README.dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Clipper2 WASM Development Guide

## Introduction
This guide provides step-by-step instructions for setting up and building the Clipper2 WebAssembly (WASM) module.

## Prerequisites
Before you begin, ensure you have the following prerequisites installed on your system:
- Emscripten: Follow the installation steps detailed [here](https://emscripten.org/docs/getting_started/downloads.html).
- **Important**: Remember to install any dependencies and source the environment variables as described in the guide.

## Building Clipper2 WASM

### Step 1: Prepare the Environment
Change into the C++ directory:
```bash
cd clipper2/CPP
```

### Step 2: Create a Build Directory
Set up a build directory for out-of-source builds:
```bash
mkdir build
cd build
```

### Step 3: Generate Build Files
Use `emcmake` with `cmake` to generate the necessary build files:
```bash
emcmake cmake ../ -DCLIPPER2_TESTS=OFF
```

### Step 4: Build the Library
Compile the library files using `emmake`:
```bash
emmake make
```
This step will create `libClipper2.a` and `libClipper2Z.a` library files.

### Step 5: Compile WebAssembly Module
Return to the root directory and run the WebAssembly compilation script:
```bash
cd ..
sh clipper2-wasm/compile-wasm.sh prod
```

### Completion
After completing these steps, you will find the WASM builds in the `clipper2-wasm/dist` directory.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[![npm version](https://badge.fury.io/js/clipper2-wasm.svg)](https://badge.fury.io/js/clipper2-wasm)
# Clipper2 WASM

The <b>Clipper2</b> library performs **intersection**, **union**, **difference** and **XOR** boolean operations on both simple and complex polygons. It also performs polygon offsetting.

## Documentation:
For Clipper2 documentation please see the [Extensive HTML documentation](http://www.angusj.com/clipper2/Docs/Overview.htm)

## Live examples:
- [Basic](https://eriksom.github.io/Clipper2-WASM/CPP/clipper2-wasm/examples/es/basic.html)
- [SVG-Tools](https://eriksom.github.io/Clipper2-WASM/CPP/clipper2-wasm/examples/es/svg-tools.html)
- [Rect Clip](https://eriksom.github.io/Clipper2-WASM/CPP/clipper2-wasm/examples/es/rectclip.html)
- [Offset](https://eriksom.github.io/Clipper2-WASM/CPP/clipper2-wasm/examples/es/offset.html)
- [Minkowski](https://eriksom.github.io/Clipper2-WASM/CPP/clipper2-wasm/examples/es/minkowski.html)
- [Rect Clip Lines](https://eriksom.github.io/Clipper2-WASM/CPP/clipper2-wasm/examples/es/rectclip-lines.html)

## Download lib
ES6: https://github.com/ErikSom/Clipper2-WASM/tree/main/CPP/clipper2-wasm/dist/es/

UMD: https://github.com/ErikSom/Clipper2-WASM/tree/main/CPP/clipper2-wasm/dist/umd/

## Usage:
For ES6 see the examples here:
https://github.com/ErikSom/Clipper2-WASM/tree/main/CPP/clipper2-wasm/examples/es

For UMD see the examples here:
https://github.com/ErikSom/Clipper2-WASM/tree/main/CPP/clipper2-wasm/examples/umd

Made possible by:
https://github.com/AngusJohnson/Clipper2

Huge thanks to [@Birch-san](https://www.github.com/Birch-san) with an outstanding wasm port example:
https://github.com/Birch-san/box2d-wasm/
1 change: 1 addition & 0 deletions clipper2
Submodule clipper2 added at 31acd3
73 changes: 73 additions & 0 deletions clipper2-wasm/clipper-tools.bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "clipper2/clipper.core.h"
#include "clipper.svg.h"
#include "clipper.svg.utils.h"
#include <emscripten/bind.h>

using namespace emscripten;
using namespace Clipper2Lib;

EMSCRIPTEN_BINDINGS(clipper_module) {
enum_<FillRule>("FillRule")
.value("EvenOdd", FillRule::EvenOdd)
.value("NonZero", FillRule::NonZero)
.value("Positive", FillRule::Positive)
.value("Negative", FillRule::Negative);

class_<SvgReader>("SvgReader")
.constructor<>()
.function("LoadFromFile", &SvgReader::LoadFromFile)
.function("Clear", &SvgReader::Clear)
.function("GetPaths", &SvgReader::GetPaths);

class_<SvgWriter>("SvgWriter")
.constructor<>()
.function("Clear", &SvgWriter::Clear)
.function("FillRule", &SvgWriter::Fill_Rule)
.function("SaveToFile", &SvgWriter::SaveToFile);

// SvgAddSolution
function("SvgAddSolution", select_overload<void(SvgWriter&, const PathsD&, FillRule, bool)>(&SvgAddSolution));

#ifdef USINGZ
class_<PointD>("PointD")
.constructor<double, double, double>()
.property("x", &PointD::x)
.property("y", &PointD::y)
.property("z", &PointD::z)
.function("SetZ", &PointD::SetZ);
#else
class_<PointD>("PointD")
.constructor<double, double>()
.property("x", &PointD::x)
.property("y", &PointD::y);
#endif

class_<PathD>("PathD")
.constructor<>()
.function("size", &PathD::size)
.function("clear", &PathD::clear)
.function("push_back", select_overload<void(const PointD&)>(&PathD::push_back))
.function("get", select_overload<PointD&(size_t)>(&PathD::operator[]), allow_raw_pointers());

class_<PathsD>("PathsD")
.constructor<>()
.function("size", &PathsD::size)
.function("clear", &PathsD::clear)
.function("push_back", select_overload<void(const PathD&)>(&PathsD::push_back))
.function("get", select_overload<PathD&(size_t)>(&PathsD::operator[]), allow_raw_pointers());

// Point64 bindings (for now only support USINGZ=ON)
#ifdef USINGZ
class_<Point64>("Point64")
.constructor<int64_t, int64_t, int64_t>()
.property("x", &Point64::x)
.property("y", &Point64::y)
.property("z", &Point64::z)
.function("SetZ", &Point64::SetZ);
#else
class_<Point64>("Point64")
.constructor<int64_t, int64_t>()
.property("x", &Point64::x)
.property("y", &Point64::y);
#endif
}
133 changes: 133 additions & 0 deletions clipper2-wasm/clipper.bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "clipper2/clipper.core.h"
#include "clipper2/clipper.h"
#include <emscripten/bind.h>

using namespace emscripten;
using namespace Clipper2Lib;

template <typename T>
void ReversePath(Path<T>& path) {
std::reverse(path.begin(), path.end());
}

EMSCRIPTEN_BINDINGS(clipper_module) {
enum_<FillRule>("FillRule")
.value("EvenOdd", FillRule::EvenOdd)
.value("NonZero", FillRule::NonZero)
.value("Positive", FillRule::Positive)
.value("Negative", FillRule::Negative);

enum_<ClipType>("ClipType")
.value("Intersection", ClipType::Intersection)
.value("Union", ClipType::Union)
.value("Difference", ClipType::Difference)
.value("Xor", ClipType::Xor);

enum_<PathType>("PathType")
.value("Subject", PathType::Subject)
.value("Clip", PathType::Clip);

enum_<JoinType>("JoinType")
.value("Square", JoinType::Square)
.value("Round", JoinType::Round)
.value("Miter", JoinType::Miter);

enum_<EndType>("EndType")
.value("Polygon", EndType::Polygon)
.value("Joined", EndType::Joined)
.value("Butt", EndType::Butt)
.value("Square", EndType::Square)
.value("Round", EndType::Round);

enum_<PointInPolygonResult>("PointInPolygonResult")
.value("IsOn", PointInPolygonResult::IsOn)
.value("IsInside", PointInPolygonResult::IsInside)
.value("IsOutside", PointInPolygonResult::IsOutside);

// Point64 bindings (for now only support USINGZ=ON)
#ifdef USINGZ
class_<Point64>("Point64")
.constructor<int64_t, int64_t, int64_t>()
.property("x", &Point64::x)
.property("y", &Point64::y)
.property("z", &Point64::z)
.function("SetZ", &Point64::SetZ);
#endif

// Path64
class_<Path64>("Path64")
.constructor<>()
.function("size", &Path64::size)
.function("clear", &Path64::clear)
.function("push_back", select_overload<void(const Point64&)>(&Path64::push_back))
.function("get", select_overload<Point64&(size_t)>(&Path64::operator[]), allow_raw_pointers());

// Paths64
class_<Paths64>("Paths64")
.constructor<>()
.function("size", &Paths64::size)
.function("clear", &Paths64::clear)
.function("push_back", select_overload<void(const Path64&)>(&Paths64::push_back))
.function("get", select_overload<Path64&(size_t)>(&Paths64::operator[]), allow_raw_pointers());

// Misc64
function("AreaPath64", select_overload<double(const Path64&)>(&Area), allow_raw_pointers());
function("AreaPaths64", select_overload<double(const Paths64&)>(&Area), allow_raw_pointers());
function("IsPositive64", select_overload<bool(const Path64&)>(&IsPositive), allow_raw_pointers());
function("PointInPolygon64", select_overload<PointInPolygonResult(const Point64&, const Path64&)>(&PointInPolygon), allow_raw_pointers());
function("ReversePath64", &ReversePath<int64_t>, allow_raw_pointers());

// Geometry64
class_<Rect64>("Rect64")
.constructor<>()
.constructor<int64_t, int64_t, int64_t, int64_t>()
.property("left", &Rect64::left)
.property("top", &Rect64::top)
.property("right", &Rect64::right)
.property("bottom", &Rect64::bottom)
.function("IsValid", &Rect64::IsValid)
.function("Width", select_overload<int64_t() const>(&Rect<int64_t>::Width))
.function("Height", select_overload<int64_t() const>(&Rect<int64_t>::Height))
.function("MidPoint", &Rect64::MidPoint)
.function("AsPath", &Rect64::AsPath)
.function("ContainsPoint", select_overload<bool(const Point<int64_t>&) const>(&Rect<int64_t>::Contains))
.function("ContainsRect", select_overload<bool(const Rect<int64_t>&) const>(&Rect<int64_t>::Contains))
.function("Scale", &Rect64::Scale)
.function("IsEmpty", &Rect64::IsEmpty)
.function("Intersects", &Rect64::Intersects)
.function("Equals", &Rect64::operator==);

function("Ellipse64", select_overload<Path64(const Point64&, double, double, int)>(&Ellipse), allow_raw_pointers());
function("EllipseFromRect64", select_overload<Path64(const Rect64&, int)>(&Ellipse), allow_raw_pointers());

// Translate64
function("TranslatePath64", select_overload<Path64(const Path64&, int64_t, int64_t)>(&TranslatePath), allow_raw_pointers());
function("TranslatePaths64", select_overload<Paths64(const Paths64&, int64_t, int64_t)>(&TranslatePaths), allow_raw_pointers());

// RectClip64
function("RectClipPaths64", select_overload<Paths64(const Rect64&, const Paths64&)>(&RectClip), allow_raw_pointers());
function("RectClipPath64", select_overload<Paths64(const Rect64&, const Path64&)>(&RectClip), allow_raw_pointers());
function("RectClipLinesPaths64", select_overload<Paths64(const Rect64&, const Paths64&)>(&RectClipLines), allow_raw_pointers());
function("RectClipLinesPath64", select_overload<Paths64(const Rect64&, const Path64&)>(&RectClipLines), allow_raw_pointers());

// Minkowski
function("MinkowskiSum64", select_overload<Paths64(const Path64&, const Path64&, bool)>(&MinkowskiSum), allow_raw_pointers());
function("MinkowskiDiff64", select_overload<Paths64(const Path64&, const Path64&, bool)>(&MinkowskiDiff), allow_raw_pointers());

// BooleanOps
function("BooleanOp64", select_overload<Paths64(ClipType, FillRule, const Paths64&, const Paths64&)>(&BooleanOp), allow_raw_pointers());
function("BooleanOpOut64", select_overload<void(ClipType, FillRule, const Paths64&, const Paths64&, PolyTree64&)>(&BooleanOp), allow_raw_pointers());
function("Intersect64", select_overload<Paths64(const Paths64&, const Paths64&, FillRule)>(&Intersect), allow_raw_pointers());
function("Union64", select_overload<Paths64(const Paths64&, const Paths64&, FillRule)>(&Union), allow_raw_pointers());
function("UnionSelf64", select_overload<Paths64(const Paths64&, FillRule)>(&Union), allow_raw_pointers());
function("Difference64", select_overload<Paths64(const Paths64&, const Paths64&, FillRule)>(&Difference), allow_raw_pointers());
function("Xor64", select_overload<Paths64(const Paths64&, const Paths64&, FillRule)>(&Xor), allow_raw_pointers());

// Offset
function("InflatePaths64", select_overload<Paths64(const Paths64&, double, JoinType, EndType, double, double)>(&InflatePaths), allow_raw_pointers());

// Simplify
function("SimplifyPath64", select_overload<Path64(const Path64&, double, bool)>(&SimplifyPath), allow_raw_pointers());
function("SimplifyPaths64", select_overload<Paths64(const Paths64&, double, bool)>(&SimplifyPaths), allow_raw_pointers());
function("TrimCollinear64", select_overload<Path64(const Path64&, bool)>(&TrimCollinear), allow_raw_pointers());
}
41 changes: 41 additions & 0 deletions clipper2-wasm/compile-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

# Set default build type to 'dev' if not specified
BUILD_TYPE=${1:-dev}

# Create necessary directories
mkdir -p clipper2-wasm/dist
mkdir -p clipper2-wasm/dist/es
mkdir -p clipper2-wasm/dist/umd

# Common flags
COMMON_FLAGS="-Iclipper2/CPP/Clipper2Lib/include -DUSINGZ --bind -s MODULARIZE=1 -s WASM_BIGINT"

# Development build flags
DEV_FLAGS="-g3 -s ASSERTIONS=2 --source-map-base http://localhost:11009/ -s DISABLE_EXCEPTION_CATCHING=0 -s DEMANGLE_SUPPORT=1 -s SAFE_HEAP=1 -O0 -s ALLOW_MEMORY_GROWTH=1"

# Production build flags
PROD_FLAGS="-O3 -s DISABLE_EXCEPTION_CATCHING=1 -DNDEBUG"

# Select flags based on build type
if [ "$BUILD_TYPE" = "dev" ]; then
FLAGS="$COMMON_FLAGS $DEV_FLAGS"
echo "Building Development Version"
else
FLAGS="$COMMON_FLAGS $PROD_FLAGS"
echo "Building Production Version"
fi

# build ES6
echo "Building ES6"
em++ $FLAGS -s EXPORT_ES6=1 clipper2-wasm/clipper.bindings.cpp clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/es/clipper2z.js -s EXPORT_NAME="Clipper2Z" --post-js clipper2-wasm/glue-stub-z.js

# build Tools ES6
em++ $FLAGS -Iclipper2/CPP/Utils -s EXPORT_ES6=1 clipper2-wasm/clipper-tools.bindings.cpp clipper2/CPP/build/libClipper2Zutils.a clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/es/clipper2z-utils.js -s EXPORT_NAME="Clipper2ZUtils" -s EXPORTED_FUNCTIONS=[FS] --post-js clipper2-wasm/glue-stub-tools-z.js

# build UMD
echo "Building UMD"
em++ $FLAGS clipper2-wasm/clipper.bindings.cpp clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/umd/clipper2z.js -s EXPORT_NAME="Clipper2ZFactory" --post-js clipper2-wasm/glue-stub-z.js

# build Tools UMD
em++ $FLAGS -Iclipper2/CPP/Utils clipper2-wasm/clipper-tools.bindings.cpp clipper2/CPP/build/libClipper2Zutils.a clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/umd/clipper2z-utils.js -s EXPORT_NAME="Clipper2ZUtilsFactory" -s EXPORTED_FUNCTIONS=[FS] --post-js clipper2-wasm/glue-stub-tools-z.js
16 changes: 16 additions & 0 deletions clipper2-wasm/dist/es/clipper2z-utils.js

Large diffs are not rendered by default.

Binary file added clipper2-wasm/dist/es/clipper2z-utils.wasm
Binary file not shown.
16 changes: 16 additions & 0 deletions clipper2-wasm/dist/es/clipper2z.js

Large diffs are not rendered by default.

Binary file added clipper2-wasm/dist/es/clipper2z.wasm
Binary file not shown.
19 changes: 19 additions & 0 deletions clipper2-wasm/dist/umd/clipper2z-utils.js

Large diffs are not rendered by default.

Binary file added clipper2-wasm/dist/umd/clipper2z-utils.wasm
Binary file not shown.
19 changes: 19 additions & 0 deletions clipper2-wasm/dist/umd/clipper2z.js

Large diffs are not rendered by default.

Binary file added clipper2-wasm/dist/umd/clipper2z.wasm
Binary file not shown.
Loading

0 comments on commit 7d7e9e0

Please sign in to comment.