Code Monkey home page Code Monkey logo

arrangement-2d-js's Introduction

arrangement-2d-js

build GitHub release Language grade: JavaScript License

Examples

  • Arrangement demo Link

Install

From GitHub

npm i git+https://github.com/LokiResearch/arrangement-2d-js.git

Build

Install and configure Emscripten
# Get the emscripten repo 
git clone [email protected]:emscripten-core/emsdk.git

# Enter folder
cd emsdk

# Download the SDK tool (latest or another version)
./emsdk install latest

# Activate the downloaded SDK
./emsdk activate latest

# Add Emscripten env to PATH
source ./emsdk_env.sh
Install and build arrangement-2d-js
# Get this repo
git clone [email protected]:LokiResearch/arrangement-2d-js.git

# Install CGAL and boost libraries
./install_libraries.sh

# Build
npm run build

Use

Note: This library is a WebAssembly module

import Arrangement2D from 'arrangement-2d-js';

// Init module, wait for the promise with the loaded module
Arrangement2D().then(Arr2D => {

    // Create a list of points
    const points = new Arr2D.PointList();

    // Add six points forming 3 strokes forming a triangle
    points.push_back(new Arr2D.Point(1, 0));
    points.push_back(new Arr2D.Point(1, 4));
    points.push_back(new Arr2D.Point(0, 1));
    points.push_back(new Arr2D.Point(4, 1));
    points.push_back(new Arr2D.Point(4, 0));
    points.push_back(new Arr2D.Point(0, 4));

    // Get the polygons in the arrangement
    const builder = new Arr2D.ArrangementBuilder();
    const polygonList = builder.getPolygons(points);

    // Get the contour points of the polygon
    const poly = polygonList.at(0);
    for (let i=0; i<poly.contour.size(); i++) {
        const point = poly.contour.at(i);
        console.log(point.x, point.y);
    }

    // Clean the initial points
    Arr2D.destroy(points);

    // Clean the polygons : i.e. call C++ Polygon class destructor,
    // and avoid memory leaks
    for (let i=0; i<polygonList.size(); i++) {
        Arr2D.destroy(polygonList.at(i));
    }
    Arr2D.destroy(polygonList);
    Arr2D.destroy(builder);
    
});

See examples code for a more complete use of the library.

Documentation

class ArrangementBuilder {
    constructor();
    getPolygons(points: PointList): PolygonList;
}

class Point {
    constructor();
    constructor(x: number, y: number);
    x: number;
    y: number;
}

class Polygon {
    constructor(contour: Contour, holes: ContourList);
    getInsidePoint(point: Point): boolean;
    contour: Contour;
    holes: ContourList;
}

class PointList {
    constructor();
    push_back(s: Point): void;
    size(): number;
    at(i: number): Point;
    clear(): void;
}

class PolygonList {
    constructor();
    push_back(s: Polygon): void;
    size(): number;
    at(i: number): Polygon;
    clear(): void;
}

class Contour {
    constructor();
    push_back(s: Point): void;
    size(): number;
    at(i: number): Point;
    clear(): void;
}

class ContourList {
    constructor();
    push_back(s: Contour): void;
    size(): number;
    at(i: number): Contour;
    clear(): void;
}

arrangement-2d-js's People

Contributors

minitoine avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

arrangement-2d-js's Issues

Question about CGAL import

Some time ago try to use CGAL arrangement module in my project c++ with export to webassembly, but can find solution.
CGAL try to use gmp lib and as understand this produce a lot of errors when try to compile module to JS, in plane c++ all work well.
Question how to adopt your solution in c++ program with em compiler?
take part from cmake or need some additional classes as gpc.c included?

arrangement-2d-js installed from npm vs arrangement-2d-js local build

Hello,
have two questions:
first is about difference between arrangement-2d-js installed from npm vs arrangement-2d-js local build. They work, but first variant from npm support more segments to intersect. For example 1500 segments this two libs work identically, but if increase number of segmnets to more then 15000 first work, but local build lib send error :
Uncaught RuntimeError RuntimeError: memory access out of bounds
at $func17 (wasm/001a34f2:1624:1)
at $emscripten_bind_ArrangementBuilder_getPolygons_1 (wasm/001a34f2:87228:1)
all flags in cmakelists,txt dont changes, one problem that complile it from ide not from npm script.
version of CGAl and Boost as in your version.
Do you have ideas why?

And second question:
As understand svg render now is not supported. But take main ideas from your project and they works.
Screenshot at 2024-03-28 10-31-42

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.