add emscripten compiled libtess

This commit is contained in:
Hannes Janetzek 2013-06-30 09:06:13 +02:00
parent ae2c930e90
commit 54af3862c2
4 changed files with 242 additions and 0 deletions

View File

@ -0,0 +1,37 @@
Copyright notice and license for the libtess files (all source files besides
tessellate.[ch] and main.c):
SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice including the dates of first publication and
either this permission notice or a reference to
http://oss.sgi.com/projects/FreeB/
shall be included in all copies or substantial portions of the Software.
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 AND NONINFRINGEMENT. IN NO EVENT SHALL
SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Except as contained in this notice, the name of Silicon Graphics, Inc.
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization from
Silicon Graphics, Inc.
--------------------------------------------------------------------------------
Copyright notice for the other files:
SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
Copyright (C) 2013 AT&T Intellectual Property. All Rights Reserved.

View File

@ -0,0 +1,18 @@
# A minimal, self-contained port of SGI's GLU libtess
Polygon tessellation is a major pain in the neck. Have you ever tried
writing fast and robust code for it? libtess is, to my knowledge, the
only GPL-compatible, liberally-licensed, high-quality polygon
triangulator out there.
This repository includes a self-contained function (tessellate, in
tessellate.c) that you can call to triangulate a polygon that is
potentially self-intersecting, with holes, or with duplicate
vertices. Simple examples of calling the tessellate function directly
are located in main.c.
More interestingly, this repository also includes an
Emscripten-compiled module, _tessellate.js, and a Javascript-friendly
wrapper, in tessellate.js. Simple examples are available under
index.html.

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,92 @@
tessellate = (function() {
Module.TOTAL_MEMORY = 1024 * 1024;
var c_tessellate = Module.cwrap('tessellate', 'void', ['number', 'number', 'number',
'number', 'number', 'number']);
// special tessellator for extrusion layer
var tessellate = function(vertices, v_start, v_end, boundaries, b_start, b_end) {
var i;
var v_len = (v_end - v_start);
var b_len = (b_end - b_start);
var p = Module._malloc(v_len * 8);
for (i=0; i< v_len; ++i)
Module.setValue(p+i*8, vertices[v_start + i], 'double');
var contours = Module._malloc((b_len + 1) * 4);
// pointer to first contour
Module.setValue(contours + 0, p + 0, 'i32');
var offset = p;
// pointer to further contours + end
for (i = 0; i<b_len; ++i) {
offset += 8 * boundaries[b_start + i];
Module.setValue(contours + 4 * (i + 1), offset, 'i32');
}
var ppcoordinates_out = Module._malloc(4);
var pptris_out = Module._malloc(4);
var pnverts = Module._malloc(4);
var pntris = Module._malloc(4);
c_tessellate(ppcoordinates_out, pnverts, pptris_out, pntris,
contours, contours+4*(b_len + 1));
var pcoordinates_out = Module.getValue(ppcoordinates_out, 'i32');
var ptris_out = Module.getValue(pptris_out, 'i32');
var nverts = Module.getValue(pnverts, 'i32');
var ntris = Module.getValue(pntris, 'i32');
var result_vertices = new Float32Array(nverts * 2);
var result_triangles = new Int32Array(ntris * 3);
for (i=0; i<2*nverts; ++i) {
result_vertices[i] = Module.getValue(pcoordinates_out + i*8, 'double');
if (result_vertices[i] != vertices[v_start + i])
console.log("i:" + i + " " + result_vertices[i] + " " + vertices[v_start + i]);
}
for (i=0; i<3*ntris; ++i) {
result_triangles[i] = Module.getValue(ptris_out + i*4, 'i32') * 2;
}
// when a ring has an odd number of points one (or rather two)
// additional vertices will be added. so the following rings
// needs extra offset...
var start = 0;
for (var j = 0, m = b_len - 1; j < m; j++) {
start += boundaries[b_start + j];
// even number of points?
if (!((boundaries[b_start + j] >> 1) & 1))
continue;
console.log("shift " + boundaries[b_start + j]);
for (var n = ntris * 3, tri = 0; tri < n; tri++)
if (result_triangles[tri] >= start)
result_triangles[tri] += 2;
start += 2;
}
Module._free(pnverts);
Module._free(pntris);
Module._free(ppcoordinates_out);
Module._free(pptris_out);
Module._free(pcoordinates_out);
Module._free(ptris_out);
Module._free(p);
Module._free(contours);
return {
vertices: result_vertices,
triangles: result_triangles
};
};
return tessellate;
})();