Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript module for polygon boolean functions (union, intersection, difference and XOR) [closed]

I have polygon set. In this set some are exterior polygons and some may (or may not) be holes. I don't know at this stage that which are polygon holes. I want to calculate the final polygon combining all the polygons including holes.

I thought of this approach:

//'SomeLib'  that has polygon boolean fucntions
var polygonSet = [poly1,poly2,...polyn];

var union, intersection;
var combinedPoly = SomeLib.XOR(polygonSet[0], polygonSet[1]);
for( var i=2; i<polygonSet.length ; i++) {
  combinedPoly = SomeLib.XOR(combinedPoly, polygonSet[i]);
  //or if XOR is not available  
  union = SomeLib.union(combinedPoly, polygonSet[i]);
  intersection = SomeLib.intersection(combinedPoly, polygonSet[i]);
  combinedPoly = union - intersection;
}

So my requirement for module is

  1. just four polygon boolean functions
  2. if npm module is available its good
  3. light weight as polygon combined would be one of many function. I mean as application size is already more, so looking for light weight library.
  4. efficiency: in my case number of polygons in set may not be high but points in polygon are in large numbers so looking for O(k.log(n)) not O(k.n)

List of the libraries that I came across and some points as I understood:

  • JSCLipper efficient, no npm module, is the github project in sync with soureforge JSClipper ?
  • kld-intersections polygon intersection present but union not present( or I am not able to find) , has many other functions (other than polygon boolean). This is javscript port of this project
  • polygon.js dependent on jQuery, efficiency? , no npm module, dont confuse with polygon.js
  • boolean in paper.js its add-on of paper.js lib, not an independent module
  • raphael-boolean its add-on of raphael lib, not an independent module
  • tess2.js GLU tesselator ported to Javascript, contains many functions for tesselation - does tesselation is required for polygon boolean functions? I could not found the boolean functions, no documentation, there is some error on test html page, no npm module
  • turf-donuts dependent on another big library JSTS
  • JSTS Topology Suite big library not modular

In some cases these functions are part of big libraries that are overloaded with other functions or libraries work with SVG or Geo-spatial environement or many are add on to existing library.

Please suggest for my requirement which library is appropriate? Are there any more javscript modular libraries for polygon boolean functions?

like image 889
Gagan Avatar asked Jun 06 '14 15:06

Gagan


People also ask

What are the Boolean operations on polygon and multipolygon geometry?

Boolean operations on polygons (union, intersection, difference, xor) Boolean polygon clipping/overlay operations (union, intersection, difference, xor) on Polygon and MultiPolygon geometry. Multisegments, polygons & multipolygons clipping based on algorithm by F. Martinez et al.

What is the best C++ library for Boolean operation on polygons?

Martinez-Rueda polygon clipping algorithm, does boolean operation on polygons (multipolygons, polygons with holes etc): intersection, union, difference, xor Cython wrapper for the C++ translation of the Angus Johnson's Clipper library (ver. 6.4.2) This is a fairly straightforward port of the polybooljs library to the C# programming language.

What is a Boolean in JavaScript?

A JavaScript Boolean represents one of two values: true or false. Very often, in programming, you will need a data type that can only have one of two values, like For this, JavaScript has a Boolean data type.

How to get the shape of a zone using Boolean operations?

Four different boolean shape operations are available for this purpose. The result images are shown below: You can use Zone2::getTriangles () to conveniently extract the triangles of a zone. However, if you are rather interested in its boundary, use Zone2::getBorderEdges () to retrieve a vector of Edge2 objects.


2 Answers

I use my own methods that operate on SVG convex polygons:

  1. Set Convex Polygons Counter-Clockwise Points

  2. Polygons - Fix for Convex/CCW

  3. Point Inside Convex Polygon: Jordan Curve Theorem

  4. Line Intersect Polygon - vector analysis

  5. Intersecting Polygons - vectors analysis

  6. Composite Intersecting Polygon - Jordan Curve Theorem

  7. Convex Polygons - Trim, Uses Sutherland-Hodgman clipping algorithm

These are shown here.

Also, you may want to check out this.

like image 87
Francis Hemsher Avatar answered Oct 13 '22 12:10

Francis Hemsher


I had started implementing Vatti polygon clipping algorithm, here is the code repository. But I found Greiner Hormann polygon clipping algorithm is much better. So I am not maintaining the code now.

I recommend Greiner Hormann implementation in JavaScript for polygon clipping or other boolean operations.

like image 27
Gagan Avatar answered Oct 13 '22 12:10

Gagan