Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSom committed Dec 29, 2023
0 parents commit c97b965
Show file tree
Hide file tree
Showing 40 changed files with 2,259 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.
50 changes: 50 additions & 0 deletions README.dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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 ../ \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_FLAGS_RELEASE="-O3 -DNDEBUG -flto -finline-functions -ftree-vectorize" \
-DCLIPPER2_HI_PRECISION=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.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[![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/clipper2-wasm/examples/es/basic.html)
- [SVG-Tools](https://eriksom.github.io/Clipper2-WASM/clipper2-wasm/examples/es/svg-tools.html)
- [Rect Clip](https://eriksom.github.io/Clipper2-WASM/clipper2-wasm/examples/es/rectclip.html)
- [Offset](https://eriksom.github.io/Clipper2-WASM/clipper2-wasm/examples/es/offset.html)
- [Minkowski](https://eriksom.github.io/Clipper2-WASM/clipper2-wasm/examples/es/minkowski.html)
- [Rect Clip Lines](https://eriksom.github.io/Clipper2-WASM/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

## Typescript example:
```javascript
import { MainModule } from 'clipper2-wasm';
import * as _Clipper2ZFactory from 'clipper2-wasm/dist/umd/clipper2z';

const Clipper2ZFactory = _Clipper2ZFactory as any;

Clipper2ZFactory({
locateFile: () => {
return './clipper2z.wasm'
},
}).then((Clipper2Z: MainModule) => {
console.log('Clipper2Z', Clipper2Z);
});
```

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
}
Loading

0 comments on commit c97b965

Please sign in to comment.