BitMap polygon code cleanup
This commit is contained in:
parent
4b4e701e06
commit
28c5250c2d
@ -169,14 +169,6 @@ Dictionary BitMap::_get_data() const {
|
|||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CrossStackEntry {
|
|
||||||
Point2i cross;
|
|
||||||
Vector<int> ranges;
|
|
||||||
|
|
||||||
_FORCE_INLINE_ bool operator==(const CrossStackEntry &p_other) const { return cross == p_other.cross; }
|
|
||||||
_FORCE_INLINE_ bool operator!=(const CrossStackEntry &p_other) const { return cross != p_other.cross; }
|
|
||||||
};
|
|
||||||
|
|
||||||
Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_start) const {
|
Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_start) const {
|
||||||
int stepx = 0;
|
int stepx = 0;
|
||||||
int stepy = 0;
|
int stepy = 0;
|
||||||
@ -188,14 +180,11 @@ Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2
|
|||||||
int cury = starty;
|
int cury = starty;
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
|
|
||||||
Vector<CrossStackEntry> cross_stack;
|
HashMap<Point2i, int> cross_map;
|
||||||
int cross_stack_size = 0;
|
|
||||||
|
|
||||||
// Add starting point to stack as the default entry.
|
Vector<Vector2> _points;
|
||||||
cross_stack.push_back({ Point2i(-1, -1), Vector<int>({ 0 }) });
|
int points_size = 0;
|
||||||
cross_stack_size++;
|
|
||||||
|
|
||||||
Vector<Point2i> _points;
|
|
||||||
Vector<Vector<Vector2>> ret;
|
Vector<Vector<Vector2>> ret;
|
||||||
|
|
||||||
// Add starting entry at start of return.
|
// Add starting entry at start of return.
|
||||||
@ -326,52 +315,25 @@ Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2
|
|||||||
|
|
||||||
// Handle crossing points.
|
// Handle crossing points.
|
||||||
if (sv == 6 || sv == 9) {
|
if (sv == 6 || sv == 9) {
|
||||||
const int new_index = _points.size() - 1;
|
const Point2i cur_pos(curx, cury);
|
||||||
|
|
||||||
// Add previous point to last stack entry.
|
// Find if this point has occured before.
|
||||||
cross_stack.write[cross_stack_size - 1].ranges.push_back(new_index);
|
if (HashMap<Point2i, int>::Iterator found = cross_map.find(cur_pos)) {
|
||||||
|
// Add points after the previous crossing to the result.
|
||||||
|
ret.push_back(_points.slice(found->value + 1, points_size));
|
||||||
|
|
||||||
// Create temporary entry to maybe insert, for searching.
|
// Remove points after crossing point.
|
||||||
const CrossStackEntry new_entry = { _points[new_index], Vector<int>({ new_index }) };
|
points_size = found->value + 1;
|
||||||
|
|
||||||
// Attempt to find matching entry.
|
// Erase trailing map elements.
|
||||||
const int found = cross_stack.rfind(new_entry, cross_stack_size - 1);
|
while (cross_map.last() != found) {
|
||||||
|
cross_map.remove(cross_map.last());
|
||||||
if (found != -1) {
|
|
||||||
Vector<Vector2> tmp;
|
|
||||||
|
|
||||||
// Iterate over entries between end of stack and found, adding ranges to result.
|
|
||||||
for (int i = found; i < cross_stack_size; i++) {
|
|
||||||
const Vector<int> &ranges = cross_stack[i].ranges;
|
|
||||||
|
|
||||||
for (int j = 0; j < ranges.size() / 2; j++) {
|
|
||||||
int first = ranges[2 * j];
|
|
||||||
const int last = ranges[2 * j + 1];
|
|
||||||
|
|
||||||
int new_pos = tmp.size();
|
|
||||||
|
|
||||||
tmp.resize(tmp.size() + (last - first));
|
|
||||||
|
|
||||||
Vector2 *tmp_ptrw = tmp.ptrw();
|
|
||||||
|
|
||||||
for (; first < last; first++, new_pos++) {
|
|
||||||
tmp_ptrw[new_pos].x = (float)(_points[first].x - p_rect.position.x);
|
|
||||||
tmp_ptrw[new_pos].y = (float)(_points[first].y - p_rect.position.y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret.push_back(tmp);
|
cross_map.erase(cur_pos);
|
||||||
|
|
||||||
// Shrink stack.
|
|
||||||
cross_stack_size = found;
|
|
||||||
|
|
||||||
// Add previous point to last stack entry.
|
|
||||||
cross_stack.write[cross_stack_size - 1].ranges.push_back(new_index);
|
|
||||||
} else {
|
} else {
|
||||||
cross_stack.resize(MAX(cross_stack_size + 1, cross_stack.size()));
|
// Add crossing point to map.
|
||||||
cross_stack.set(cross_stack_size, new_entry);
|
cross_map.insert(cur_pos, points_size - 1);
|
||||||
cross_stack_size++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -381,10 +343,11 @@ Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2
|
|||||||
curx += stepx;
|
curx += stepx;
|
||||||
cury += stepy;
|
cury += stepy;
|
||||||
if (stepx == prevx && stepy == prevy) {
|
if (stepx == prevx && stepy == prevy) {
|
||||||
_points.write[_points.size() - 1].x = curx;
|
_points.set(points_size - 1, Vector2(curx, cury) - p_rect.position);
|
||||||
_points.write[_points.size() - 1].y = cury;
|
|
||||||
} else {
|
} else {
|
||||||
_points.push_back(Point2i(curx, cury));
|
_points.resize(MAX(points_size + 1, _points.size()));
|
||||||
|
_points.set(points_size, Vector2(curx, cury) - p_rect.position);
|
||||||
|
points_size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
@ -394,28 +357,10 @@ Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2
|
|||||||
ERR_FAIL_COND_V((int)count > width * height, Vector<Vector<Vector2>>());
|
ERR_FAIL_COND_V((int)count > width * height, Vector<Vector<Vector2>>());
|
||||||
} while (curx != startx || cury != starty);
|
} while (curx != startx || cury != starty);
|
||||||
|
|
||||||
// Add last position to last stack entry.
|
// Add remaining points to result.
|
||||||
cross_stack.write[cross_stack_size - 1].ranges.push_back(_points.size());
|
_points.resize(points_size);
|
||||||
|
|
||||||
for (int i = 0; i < cross_stack_size; i++) {
|
ret.set(0, _points);
|
||||||
const Vector<int> &ranges = cross_stack[i].ranges;
|
|
||||||
|
|
||||||
for (int j = 0; j < ranges.size() / 2; j++) {
|
|
||||||
int first = ranges[2 * j];
|
|
||||||
const int last = ranges[2 * j + 1];
|
|
||||||
|
|
||||||
int new_pos = ret[0].size();
|
|
||||||
|
|
||||||
ret.write[0].resize(ret[0].size() + (last - first));
|
|
||||||
|
|
||||||
Vector2 *tmp_ptrw = ret.write[0].ptrw();
|
|
||||||
|
|
||||||
for (; first < last; first++, new_pos++) {
|
|
||||||
tmp_ptrw[new_pos].x = (float)(_points[first].x - p_rect.position.x);
|
|
||||||
tmp_ptrw[new_pos].y = (float)(_points[first].y - p_rect.position.y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user