r3mi/poly2tri.js

Integration into ExtendScript

Closed this issue · 14 comments

Is it possible to integrate this code into ExtendScript, the js implementation by Adobe systems for their software.

they have #include directive that includes external code, but when I do this for poly2tri, no poly2tri object is created.

r3mi commented

Why not, although I have no knowledge of ExtendScript and no SDK, so I will need some help.
What Adobe environment are you using, which version ?
Can you give an example of this include ?
What is the version of poly2tri ? Minified or uncompressed ?

I'm trying to implement poly2tri into Adobe After Effects via ExtendScript Toolkit.
You may read documentation here: http://www.adobe.com/devnet/scripting/estk.html

Example:

test.jsx file contatins:

  var polygon = function(vertices){
    this.vertices = vertices;
  }

main.jsx file goes like this:

  #include ".../test.jsx"
  new polygon([[1,2],[2,3],[4,5]])

So it's really similar to C syntax.

If it helps, this implementation of Delauney works perfectly with #include directive
https://github.com/dokluch/delaunay/blob/master/delaunay.js

r3mi commented

Which version of poly2tri are you using ? Minified or uncompressed ?

Sorry. It's uncompressed one

r3mi commented

ok, I downloaded the ExtendScript Toolkit and did some tests.
The issue seems to be in the UMD wrapper (Universal Module Definition) I am using, who doesn't seem compatible with ExtendScript. As I use another package to generate this wrapper (browserify), I don't know if I can fix it quickly.
As a workaround, "faking" a Browser or Node environment seems to work for me:

#strict on

var global = {};
#include "poly2tri.js"
var poly2tri = global.poly2tri;

var contour = [
    new poly2tri.Point(100, 100), 
    new poly2tri.Point(100, 300), 
    new poly2tri.Point(300, 300), 
    new poly2tri.Point(300, 100)
];
var swctx = new poly2tri.SweepContext(contour);

swctx.triangulate();
var triangles = swctx.getTriangles();

print (triangles);

or another variant:

#strict on

var poly2tri;
function define(e) {
    poly2tri = e();
}
define.amd = true;
#include "poly2tri.js"

var contour = [
    new poly2tri.Point(100, 100), 
    new poly2tri.Point(100, 300), 
    new poly2tri.Point(300, 300), 
    new poly2tri.Point(300, 100)
];
var swctx = new poly2tri.SweepContext(contour);

swctx.triangulate();
var triangles = swctx.getTriangles();

print (triangles);

I am not an ExtendScript expert though, so I don't know about possible side effects.
What do you think ? Can you try ?

Thanks so much!
This works perfectly.

r3mi commented

Glad it works.
Which workaround are you using ? I am not sure the 1st one (declaring global) is 100% safe with regard to ExtendScript's $.global. Seems to work tough ...

I will leave the issue open, to see if I can get a permanent fix.

I'm using first one, and see no problems now, though I will continue with deeper testing later.
Btw, is there a way to get output in proper array of array and not text, I have to use regex matching which seems unlogical waste of processing time.

r3mi commented

output is not text, it is an array of Triangle objects, each containing 3 Points {x,y}
use triangles[0].getPoint(0).x and so on.

I was going here to remove my comment. Sorry, didn't notice at first.

r3mi commented

that's fine, I will add an example in the README.

r3mi commented

After thinking about it, the ExtendScript syntax ($.global for the global object) is too specific, I don't know that it will ever be supported in the module handlers (browserify and umd). I think the "proper" fix is for ExtendScript to use one of the prevalent JavaScript module syntax. For example, to use CommonJS :

// Add CommonJS syntax to ExtendScript : 
// see "require.jsx" at https://github.com/theiviaxx/PSLib
#include "require.jsx"
var poly2tri = require('poly2tri');

Note that this works only using the latest poly2tri 1.3.3 (released today), because of a bug in the ExtendScript parser on nested ternary operators ?:?: http://forums.adobe.com/thread/1147245 ...

So the proper way: import poly2tri using a normalised JavaScript syntax (eg CommonJS). Or use one of the above workaround.

Thanks, I will try to do ti