Pixel-Composer/scripts/__polygon/__polygon.gml

223 lines
5.5 KiB
Text
Raw Normal View History

2024-04-11 15:43:03 +02:00
function polygon_simplify(points, tolerance = 4) { #region
2024-06-08 09:38:41 +02:00
// Delete duplicated points
for( var i = array_length(points) - 1; i >= 1; i-- ) {
if(points[i].equal(points[i - 1]))
array_delete(points, i, 1);
}
if(points[0].equal(points[array_length(points) - 1]))
array_pop(points);
2023-02-14 02:48:33 +01:00
var remSt = ds_stack_create();
2024-06-08 09:38:41 +02:00
var len = array_length(points);
2023-02-14 02:48:33 +01:00
for( var i = 0; i < len; i++ ) {
2024-06-08 09:38:41 +02:00
var _px0 = points[(i - 1 + len) % len].x;
var _py0 = points[(i - 1 + len) % len].y;
var _px1 = points[i].x;
var _py1 = points[i].y;
var _px2 = points[(i + 1 + len) % len].x;
var _py2 = points[(i + 1 + len) % len].y;
2023-02-14 02:48:33 +01:00
var dir0 = point_direction(_px0, _py0, _px1, _py1);
var dir1 = point_direction(_px1, _py1, _px2, _py2);
2024-06-08 09:38:41 +02:00
if((_px0 == _px1 && _py0 == _py1) || abs(dir0 - dir1) <= tolerance)
2024-06-08 09:38:41 +02:00
ds_stack_push(remSt, i);
2023-02-14 02:48:33 +01:00
}
2024-06-08 09:38:41 +02:00
2023-02-14 02:48:33 +01:00
while(!ds_stack_empty(remSt)) {
var ind = ds_stack_pop(remSt);
array_delete(points, ind, 1);
}
ds_stack_destroy(remSt);
return points;
2024-04-11 15:43:03 +02:00
} #endregion
2023-02-14 02:48:33 +01:00
2024-04-11 15:43:03 +02:00
function polygon_points_classify(points) { #region
2023-02-14 02:48:33 +01:00
var len = array_length(points);
var maxx = -99999;
var maxindex = 0;
for( var i = 0; i < len; i++ ) {
2023-03-19 09:17:39 +01:00
var _x = points[i].x;
2023-02-14 02:48:33 +01:00
if(_x > maxx) {
maxx = _x;
maxindex = i;
}
}
var side, _side = 0;
2023-03-19 09:17:39 +01:00
var convexs = [];
var reflects = [];
var startindex = (maxindex - 1 + len) % len;
2023-02-14 02:48:33 +01:00
for( var i = 0; i < len; i++ ) {
var index = (startindex + i) % len;
2023-03-19 09:17:39 +01:00
var _px0 = points[index].x;
var _py0 = points[index].y;
var _px1 = points[(index + 1) % len].x;
var _py1 = points[(index + 1) % len].y;
var _px2 = points[(index + 2) % len].x;
var _py2 = points[(index + 2) % len].y;
2023-02-14 02:48:33 +01:00
var side = cross_product(_px0, _py0, _px1, _py1, _px2, _py2);
2023-02-14 02:48:33 +01:00
if(_side != 0 && sign(_side) != sign(side))
array_push(reflects, (index + 1) % len);
2023-02-14 02:48:33 +01:00
else {
array_push(convexs, (index + 1) % len);
2023-02-14 02:48:33 +01:00
_side = sign(side);
}
}
return [ convexs, reflects, _side ];
2024-04-11 15:43:03 +02:00
} #endregion
2023-02-14 02:48:33 +01:00
2024-04-11 15:43:03 +02:00
function polygon_triangulate_convex(points) { #region
2023-02-14 02:48:33 +01:00
var triangles = [];
var len = array_length(points);
var c0 = points[0];
for( var i = 0; i < len - 2; i++ ) {
var c1 = points[safe_mod(i + 1, len)];
var c2 = points[safe_mod(i + 2, len)];
array_push(triangles, [c0, c1, c2]);
}
return triangles;
2024-04-11 15:43:03 +02:00
} #endregion
2023-02-14 02:48:33 +01:00
function polygon_triangulate(points, tolerance = 4) { #region // ear clipping
2024-06-08 06:31:21 +02:00
if(array_length(points) < 3) return [ [], points, 1 ];
if(tolerance > 0) points = polygon_simplify(points, tolerance);
2024-06-08 06:31:21 +02:00
if(array_length(points) < 3) return [ [], points, 1 ];
2023-02-14 02:48:33 +01:00
var classes = polygon_points_classify(points);
var convexes = classes[0];
var reflected = classes[1];
var checkSide = classes[2];
if(array_length(reflected) == 0)
2024-06-08 06:31:21 +02:00
return [ polygon_triangulate_convex(points), points, checkSide ];
2023-02-14 02:48:33 +01:00
var pointInd = array_create_ext(array_length(points), function(i) /*=>*/ { return i; });
2023-02-14 02:48:33 +01:00
var triangles = [];
var repeated = 0;
2024-06-08 09:38:41 +02:00
// print($"Ear cutting : {points}");
2023-02-14 02:48:33 +01:00
while(array_length(pointInd) > 3) {
2024-06-08 06:31:21 +02:00
if(array_length(convexes) == 0) return [ triangles, points, checkSide ];
2023-02-14 02:48:33 +01:00
var len = array_length(pointInd);
2024-04-11 15:43:03 +02:00
var c0 = convexes[0];
2023-02-14 02:48:33 +01:00
var c0i = array_find(pointInd, c0);
2024-04-11 15:43:03 +02:00
var c1 = pointInd[safe_mod(c0i - 1 + len, len)];
var c2 = pointInd[safe_mod(c0i + 1, len)];
2023-02-14 02:48:33 +01:00
var p0 = points[c0];
var p1 = points[c1];
var p2 = points[c2];
//check if point is ear
var isEar = true;
for( var i = 0; i < len; i++ ) {
var ind = pointInd[i];
if(ind == c0 || ind == c1 || ind == c2) continue;
2023-02-14 02:48:33 +01:00
var p = points[ind];
2023-03-19 09:17:39 +01:00
if(point_in_triangle(p.x, p.y, p0.x, p0.y, p1.x, p1.y, p2.x, p2.y)) {
2023-02-14 02:48:33 +01:00
isEar = false;
break;
}
}
if(isEar) {
array_remove(convexes, c0);
array_remove(pointInd, c0);
array_push(triangles, [p0, p1, p2]);
len--;
if(array_exists(reflected, c1)) {
var c1i = array_find(pointInd, c1);
2023-02-20 10:16:31 +01:00
var c1b = safe_mod(c1i - 1 + len, len);
var c1a = safe_mod(c1i + 1, len);
2023-02-14 02:48:33 +01:00
var p1b = points[pointInd[c1b]];
var p1a = points[pointInd[c1a]];
2023-03-19 09:17:39 +01:00
var side = cross_product(p1b.x, p1b.y, p1.x, p1.y, p1a.x, p1a.y);
2023-02-14 02:48:33 +01:00
if(sign(side) == checkSide) {
array_remove(reflected, c1);
array_push(convexes, c1);
}
}
if(array_exists(reflected, c2)) {
var c2i = array_find(pointInd, c2);
2023-02-20 10:16:31 +01:00
var c2b = safe_mod(c2i - 1 + len, len);
var c2a = safe_mod(c2i + 1, len);
2023-02-14 02:48:33 +01:00
var p2b = points[pointInd[c2b]];
var p2a = points[pointInd[c2a]];
2023-03-19 09:17:39 +01:00
var side = cross_product(p2b.x, p2b.y, p2.x, p2.y, p2a.x, p2a.y);
2023-02-14 02:48:33 +01:00
if(sign(side) == checkSide) {
array_remove(reflected, c2);
array_push(convexes, c2);
}
}
repeated = 0;
} else {
array_remove(convexes, c0);
array_push(convexes, c0);
if(repeated++ > len) {
2024-06-08 09:38:41 +02:00
// print($"point: {points}");
// print($"index: {pointInd}");
// print($"convx: {convexes}");
// for (var i = 0, n = array_length(pointInd); i < n; i++)
// print(points[pointInd[i]]);
noti_warning("Mesh error");
2023-02-14 02:48:33 +01:00
break;
}
}
}
if(array_length(pointInd) == 3)
array_push(triangles, [ points[pointInd[0]], points[pointInd[1]], points[pointInd[2]] ]);
2023-02-14 02:48:33 +01:00
2024-06-08 06:31:21 +02:00
return [ triangles, points, checkSide ];
2024-04-11 15:43:03 +02:00
} #endregion
2023-02-14 02:48:33 +01:00
2024-04-11 15:43:03 +02:00
function polygon_triangulate_convex_fan(points) { #region
2023-03-19 09:17:39 +01:00
var triangles = [];
var amo = array_length(points);
var cx = 0;
var cy = 0;
for( var i = 0; i < amo; i++ ) {
cx += points[i].x;
cy += points[i].y;
}
cx /= amo;
cy /= amo;
var pc = new __vec2(cx, cy);
2023-03-19 09:17:39 +01:00
for( var i = 0; i < amo; i++ ) {
array_push(triangles, [ points[i], points[(i + 1) % amo], pc ]);
}
return triangles;
2024-04-11 15:43:03 +02:00
} #endregion