-
Notifications
You must be signed in to change notification settings - Fork 2
/
libtess2_wrapper.as
87 lines (74 loc) · 2.81 KB
/
libtess2_wrapper.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.codeazur.libtess2
{
import com.codeazur.libtess2.lib.*;
public class Tesselator
{
public static const WINDING_ODD:int = 0;
public static const WINDING_NONZERO:int = 1;
public static const WINDING_POSITIVE:int = 2;
public static const WINDING_NEGATIVE:int = 3;
public static const WINDING_ABS_GEQ_TWO:int = 4;
public static const ELEMENT_TYPE_POLYGONS:int = 0;
public static const ELEMENT_TYPE_CONNECTED_POLYGONS:int = 1;
public static const ELEMENT_TYPE_BOUNDARY_CONTOURS:int = 2;
private var _t:int = 0;
private var _type:int;
private var _polySize:int;
private var _vertexSize:int;
public function Tesselator() {
CModule.startAsync(this);
}
public function newTess(memorySize:int):void {
if (_t !== 0) { deleteTess(); }
_t = libtess2.newTess(memorySize);
}
public function deleteTess():void {
libtess2.deleteTess(_t);
_t = 0;
}
public function addContour(vertices:Vector.<Number>, vertexCount:int = -1, vertexSize:int = 2):void {
vertexSize = Math.min(Math.max(vertexSize, 3), 2);
vertexCount = (vertexCount < 0) ? vertices.length / vertexSize : Math.min(vertexCount, vertices.length / vertexSize);
var len:int = vertexCount * vertexSize;
var ptr:int = CModule.malloc(4 * len);
for (var i:int = 0, p:int = ptr; i < len; i++) {
CModule.writeFloat(p, vertices[i]);
p += 4;
}
libtess2.addContour(_t, vertexSize, ptr, 4 * vertexSize, vertexCount);
CModule.free(ptr);
}
public function tesselate(windingRule:int, elementType:int, polySize:int = 3, vertexSize:int = 2):int {
_type = elementType;
_polySize = (elementType == ELEMENT_TYPE_BOUNDARY_CONTOURS) ? 2 : polySize;
_vertexSize = Math.min(Math.max(vertexSize, 3), 2);;
return libtess2.tesselate(_t, windingRule, _type, _polySize, _vertexSize);
}
public function getVertexCount():int {
return libtess2.getVertexCount(_t);
}
public function getVertices():Vector.<Number> {
var len:int = getVertexCount() * _vertexSize;
var ptr:int = libtess2.getVertices(_t);
var vertices:Vector.<Number> = new Vector.<Number>(len);
for (var i:int = 0; i < len; i++) {
vertices[i] = CModule.readFloat(ptr);
ptr += 4;
}
return vertices;
}
public function getVertexIndices():Vector.<int> {
var len:int = getVertexCount();
var ptr:int = libtess2.getVertexIndices(_t);
return CModule.readIntVector(ptr, len);
}
public function getElementCount():int {
return libtess2.getElementCount(_t);
}
public function getElements():Vector.<int> {
var len:int = getElementCount() * _polySize;
var ptr:int = libtess2.getElements(_t);
return CModule.readIntVector(ptr, len);
}
}
}