static bool floodRegion(int x, int y, int i, unsigned short level, unsigned short r, rcCompactHeightfield& chf, unsigned short* srcReg, unsigned short* srcDist, rcIntArray& stack) { const int w = chf.width; const unsigned char area = chf.areas[i]; // Flood fill mark region. stack.resize(0); stack.push((int)x); stack.push((int)y); stack.push((int)i); srcReg[i] = r; srcDist[i] = 0; unsigned short lev = level >= 2 ? level-2 : 0; int count = 0; while (stack.size() > 0) { int ci = stack.pop(); int cy = stack.pop(); int cx = stack.pop(); const rcCompactSpan& cs = chf.spans[ci]; // Check if any of the neighbours already have a valid region set. unsigned short ar = 0; for (int dir = 0; dir < 4; ++dir) { // 8 connected if (rcGetCon(cs, dir) != RC_NOT_CONNECTED) { const int ax = cx + rcGetDirOffsetX(dir); const int ay = cy + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(cs, dir); if (chf.areas[ai] != area) continue; unsigned short nr = srcReg[ai]; if (nr & RC_BORDER_REG) // Do not take borders into account. continue; if (nr != 0 && nr != r) ar = nr; const rcCompactSpan& as = chf.spans[ai]; const int dir2 = (dir+1) & 0x3; if (rcGetCon(as, dir2) != RC_NOT_CONNECTED) { const int ax2 = ax + rcGetDirOffsetX(dir2); const int ay2 = ay + rcGetDirOffsetY(dir2); const int ai2 = (int)chf.cells[ax2+ay2*w].index + rcGetCon(as, dir2); if (chf.areas[ai2] != area) continue; unsigned short nr2 = srcReg[ai2]; if (nr2 != 0 && nr2 != r) ar = nr2; } } } if (ar != 0) { srcReg[ci] = 0; continue; } count++; // Expand neighbours. for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(cs, dir) != RC_NOT_CONNECTED) { const int ax = cx + rcGetDirOffsetX(dir); const int ay = cy + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(cs, dir); if (chf.areas[ai] != area) continue; if (chf.dist[ai] >= lev && srcReg[ai] == 0) { srcReg[ai] = r; srcDist[ai] = 0; stack.push(ax); stack.push(ay); stack.push(ai); } } } } return count > 0; }
static void walkContour(int x, int y, int i, int dir, rcCompactHeightfield& chf, unsigned short* srcReg, rcIntArray& cont) { int startDir = dir; int starti = i; const rcCompactSpan& ss = chf.spans[i]; unsigned short curReg = 0; if (rcGetCon(ss, dir) != RC_NOT_CONNECTED) { const int ax = x + rcGetDirOffsetX(dir); const int ay = y + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(ss, dir); curReg = srcReg[ai]; } cont.push(curReg); int iter = 0; while (++iter < 40000) { const rcCompactSpan& s = chf.spans[i]; if (isSolidEdge(chf, srcReg, x, y, i, dir)) { // Choose the edge corner unsigned short r = 0; if (rcGetCon(s, dir) != RC_NOT_CONNECTED) { const int ax = x + rcGetDirOffsetX(dir); const int ay = y + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir); r = srcReg[ai]; } if (r != curReg) { curReg = r; cont.push(curReg); } dir = (dir+1) & 0x3; // Rotate CW } else { int ni = -1; const int nx = x + rcGetDirOffsetX(dir); const int ny = y + rcGetDirOffsetY(dir); if (rcGetCon(s, dir) != RC_NOT_CONNECTED) { const rcCompactCell& nc = chf.cells[nx+ny*chf.width]; ni = (int)nc.index + rcGetCon(s, dir); } if (ni == -1) { // Should not happen. return; } x = nx; y = ny; i = ni; dir = (dir+3) & 0x3; // Rotate CCW } if (starti == i && startDir == dir) { break; } } // Remove adjacent duplicates. if (cont.size() > 1) { for (int j = 0; j < cont.size(); ) { int nj = (j+1) % cont.size(); if (cont[j] == cont[nj]) { for (int k = j; k < cont.size()-1; ++k) cont[k] = cont[k+1]; cont.pop(); } else ++j; } } }
static void getHeightDataSeedsFromVertices(const rcCompactHeightfield& chf, const unsigned short* poly, const int npoly, const unsigned short* verts, const int bs, rcHeightPatch& hp, rcIntArray& stack) { // Floodfill the heightfield to get 2D height data, // starting at vertex locations as seeds. // Note: Reads to the compact heightfield are offset by border size (bs) // since border size offset is already removed from the polymesh vertices. memset(hp.data, 0, sizeof(unsigned short)*hp.width*hp.height); stack.resize(0); static const int offset[9*2] = { 0,0, -1,-1, 0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1, -1,0, }; // Use poly vertices as seed points for the flood fill. for (int j = 0; j < npoly; ++j) { int cx = 0, cz = 0, ci =-1; int dmin = RC_UNSET_HEIGHT; for (int k = 0; k < 9; ++k) { const int ax = (int)verts[poly[j]*3+0] + offset[k*2+0]; const int ay = (int)verts[poly[j]*3+1]; const int az = (int)verts[poly[j]*3+2] + offset[k*2+1]; if (ax < hp.xmin || ax >= hp.xmin+hp.width || az < hp.ymin || az >= hp.ymin+hp.height) continue; const rcCompactCell& c = chf.cells[(ax+bs)+(az+bs)*chf.width]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { const rcCompactSpan& s = chf.spans[i]; int d = rcAbs(ay - (int)s.y); if (d < dmin) { cx = ax; cz = az; ci = i; dmin = d; } } } if (ci != -1) { stack.push(cx); stack.push(cz); stack.push(ci); } } // Find center of the polygon using flood fill. int pcx = 0, pcz = 0; for (int j = 0; j < npoly; ++j) { pcx += (int)verts[poly[j]*3+0]; pcz += (int)verts[poly[j]*3+2]; } pcx /= npoly; pcz /= npoly; for (int i = 0; i < stack.size(); i += 3) { int cx = stack[i+0]; int cy = stack[i+1]; int idx = cx-hp.xmin+(cy-hp.ymin)*hp.width; hp.data[idx] = 1; } while (stack.size() > 0) { int ci = stack.pop(); int cy = stack.pop(); int cx = stack.pop(); // Check if close to center of the polygon. if (rcAbs(cx-pcx) <= 1 && rcAbs(cy-pcz) <= 1) { stack.resize(0); stack.push(cx); stack.push(cy); stack.push(ci); break; } const rcCompactSpan& cs = chf.spans[ci]; for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(cs, dir) == RC_NOT_CONNECTED) continue; const int ax = cx + rcGetDirOffsetX(dir); const int ay = cy + rcGetDirOffsetY(dir); if (ax < hp.xmin || ax >= (hp.xmin+hp.width) || ay < hp.ymin || ay >= (hp.ymin+hp.height)) continue; if (hp.data[ax-hp.xmin+(ay-hp.ymin)*hp.width] != 0) continue; const int ai = (int)chf.cells[(ax+bs)+(ay+bs)*chf.width].index + rcGetCon(cs, dir); int idx = ax-hp.xmin+(ay-hp.ymin)*hp.width; hp.data[idx] = 1; stack.push(ax); stack.push(ay); stack.push(ai); } } memset(hp.data, 0xff, sizeof(unsigned short)*hp.width*hp.height); // Mark start locations. for (int i = 0; i < stack.size(); i += 3) { int cx = stack[i+0]; int cy = stack[i+1]; int ci = stack[i+2]; int idx = cx-hp.xmin+(cy-hp.ymin)*hp.width; const rcCompactSpan& cs = chf.spans[ci]; hp.data[idx] = cs.y; // getHeightData seeds are given in coordinates with borders stack[i+0] += bs; stack[i+1] += bs; } }
static void getHeightData(const rcCompactHeightfield& chf, const unsigned short* poly, const int npoly, const unsigned short* verts, rcHeightPatch& hp, rcIntArray& stack) { // Floodfill the heightfield to get 2D height data, // starting at vertex locations as seeds. memset(hp.data, 0, sizeof(unsigned short)*hp.width*hp.height); stack.resize(0); static const int offset[9*2] = { 0,0, -1,-1, 0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1, -1,0, }; // Use poly vertices as seed points for the flood fill. for (int j = 0; j < npoly; ++j) { int cx = 0, cz = 0, ci =-1; int dmin = RC_UNSET_HEIGHT; for (int k = 0; k < 9; ++k) { const int ax = (int)verts[poly[j]*3+0] + offset[k*2+0]; const int ay = (int)verts[poly[j]*3+1]; const int az = (int)verts[poly[j]*3+2] + offset[k*2+1]; if (ax < hp.xmin || ax >= hp.xmin+hp.width || az < hp.ymin || az >= hp.ymin+hp.height) continue; const rcCompactCell& c = chf.cells[ax+az*chf.width]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { const rcCompactSpan& s = chf.spans[i]; int d = rcAbs(ay - (int)s.y); if (d < dmin) { cx = ax; cz = az; ci = i; dmin = d; } } } if (ci != -1) { stack.push(cx); stack.push(cz); stack.push(ci); } } // Find center of the polygon using flood fill. int pcx = 0, pcz = 0; for (int j = 0; j < npoly; ++j) { pcx += (int)verts[poly[j]*3+0]; pcz += (int)verts[poly[j]*3+2]; } pcx /= npoly; pcz /= npoly; for (int i = 0; i < stack.size(); i += 3) { int cx = stack[i+0]; int cy = stack[i+1]; int idx = cx-hp.xmin+(cy-hp.ymin)*hp.width; hp.data[idx] = 1; } while (stack.size() > 0) { int ci = stack.pop(); int cy = stack.pop(); int cx = stack.pop(); // Check if close to center of the polygon. if (rcAbs(cx-pcx) <= 1 && rcAbs(cy-pcz) <= 1) { stack.resize(0); stack.push(cx); stack.push(cy); stack.push(ci); break; } const rcCompactSpan& cs = chf.spans[ci]; for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(cs, dir) == RC_NOT_CONNECTED) continue; const int ax = cx + rcGetDirOffsetX(dir); const int ay = cy + rcGetDirOffsetY(dir); if (ax < hp.xmin || ax >= (hp.xmin+hp.width) || ay < hp.ymin || ay >= (hp.ymin+hp.height)) continue; if (hp.data[ax-hp.xmin+(ay-hp.ymin)*hp.width] != 0) continue; const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(cs, dir); int idx = ax-hp.xmin+(ay-hp.ymin)*hp.width; hp.data[idx] = 1; stack.push(ax); stack.push(ay); stack.push(ai); } } memset(hp.data, 0xff, sizeof(unsigned short)*hp.width*hp.height); // Mark start locations. for (int i = 0; i < stack.size(); i += 3) { int cx = stack[i+0]; int cy = stack[i+1]; int ci = stack[i+2]; int idx = cx-hp.xmin+(cy-hp.ymin)*hp.width; const rcCompactSpan& cs = chf.spans[ci]; hp.data[idx] = cs.y; } static const int RETRACT_SIZE = 256; int head = 0; while (head*3 < stack.size()) { int cx = stack[head*3+0]; int cy = stack[head*3+1]; int ci = stack[head*3+2]; head++; if (head >= RETRACT_SIZE) { head = 0; if (stack.size() > RETRACT_SIZE*3) memmove(&stack[0], &stack[RETRACT_SIZE*3], sizeof(int)*(stack.size()-RETRACT_SIZE*3)); stack.resize(stack.size()-RETRACT_SIZE*3); } const rcCompactSpan& cs = chf.spans[ci]; for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(cs, dir) == RC_NOT_CONNECTED) continue; const int ax = cx + rcGetDirOffsetX(dir); const int ay = cy + rcGetDirOffsetY(dir); if (ax < hp.xmin || ax >= (hp.xmin+hp.width) || ay < hp.ymin || ay >= (hp.ymin+hp.height)) continue; if (hp.data[ax-hp.xmin+(ay-hp.ymin)*hp.width] != RC_UNSET_HEIGHT) continue; const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(cs, dir); const rcCompactSpan& as = chf.spans[ai]; int idx = ax-hp.xmin+(ay-hp.ymin)*hp.width; hp.data[idx] = as.y; stack.push(ax); stack.push(ay); stack.push(ai); } } }
static bool floodRegion(int x, int y, int i, unsigned short level, unsigned short minLevel, unsigned short r, rcCompactHeightfield& chf, unsigned short* src, rcIntArray& stack) { const int w = chf.width; // Flood fill mark region. stack.resize(0); stack.push((int)x); stack.push((int)y); stack.push((int)i); src[i*2] = r; src[i*2+1] = 0; unsigned short lev = level >= minLevel+2 ? level-2 : minLevel; int count = 0; while (stack.size() > 0) { int ci = stack.pop(); int cy = stack.pop(); int cx = stack.pop(); const rcCompactSpan& cs = chf.spans[ci]; // Check if any of the neighbours already have a valid region set. unsigned short ar = 0; for (int dir = 0; dir < 4; ++dir) { // 8 connected if (rcGetCon(cs, dir) != 0xf) { const int ax = cx + rcGetDirOffsetX(dir); const int ay = cy + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(cs, dir); unsigned short nr = src[ai*2]; if (nr != 0 && nr != r) ar = nr; const rcCompactSpan& as = chf.spans[ai]; const int dir2 = (dir+1) & 0x3; if (rcGetCon(as, dir2) != 0xf) { const int ax2 = ax + rcGetDirOffsetX(dir2); const int ay2 = ay + rcGetDirOffsetY(dir2); const int ai2 = (int)chf.cells[ax2+ay2*w].index + rcGetCon(as, dir2); unsigned short nr = src[ai2*2]; if (nr != 0 && nr != r) ar = nr; } } } if (ar != 0) { src[ci*2] = 0; continue; } count++; // Expand neighbours. for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(cs, dir) != 0xf) { const int ax = cx + rcGetDirOffsetX(dir); const int ay = cy + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(cs, dir); if (chf.spans[ai].dist >= lev) { if (src[ai*2] == 0) { src[ai*2] = r; src[ai*2+1] = 0; stack.push(ax); stack.push(ay); stack.push(ai); } } } } } return count > 0; }
static void getHeightData(const rcCompactHeightfield& chf, const unsigned short* poly, const int npoly, const unsigned short* verts, rcHeightPatch& hp, rcIntArray& stack) { // Floodfill the heightfield to get 2D height data, // starting at vertex locations as seeds. memset(hp.data, 0xff, sizeof(unsigned short)*hp.width*hp.height); stack.resize(0); // Use poly vertices as seed points for the flood fill. for (int j = 0; j < npoly; ++j) { const int ax = (int)verts[poly[j]*3+0]; const int ay = (int)verts[poly[j]*3+1]; const int az = (int)verts[poly[j]*3+2]; if (ax < hp.xmin || ax >= hp.xmin+hp.width || az < hp.ymin || az >= hp.ymin+hp.height) continue; const rcCompactCell& c = chf.cells[ax+az*chf.width]; int dmin = 0xffff; int ai = -1; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { const rcCompactSpan& s = chf.spans[i]; int d = rcAbs(ay - (int)s.y); if (d < dmin) { ai = i; dmin = d; } } if (ai != -1) { stack.push(ax); stack.push(az); stack.push(ai); } } while (stack.size() > 0) { int ci = stack.pop(); int cy = stack.pop(); int cx = stack.pop(); // Skip already visited locations. int idx = cx-hp.xmin+(cy-hp.ymin)*hp.width; if (hp.data[idx] != 0xffff) continue; const rcCompactSpan& cs = chf.spans[ci]; hp.data[idx] = cs.y; for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(cs, dir) == 0xf) continue; const int ax = cx + rcGetDirOffsetX(dir); const int ay = cy + rcGetDirOffsetY(dir); if (ax < hp.xmin || ax >= (hp.xmin+hp.width) || ay < hp.ymin || ay >= (hp.ymin+hp.height)) continue; if (hp.data[ax-hp.xmin+(ay-hp.ymin)*hp.width] != 0xffff) continue; const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(cs, dir); stack.push(ax); stack.push(ay); stack.push(ai); } } }
static void seedArrayWithPolyCenter(rcContext* ctx, const rcCompactHeightfield& chf, const unsigned short* poly, const int npoly, const unsigned short* verts, const int bs, rcHeightPatch& hp, rcIntArray& array) { // Note: Reads to the compact heightfield are offset by border size (bs) // since border size offset is already removed from the polymesh vertices. static const int offset[9*2] = { 0,0, -1,-1, 0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1, -1,0, }; // Find cell closest to a poly vertex int startCellX = 0, startCellY = 0, startSpanIndex = -1; int dmin = RC_UNSET_HEIGHT; for (int j = 0; j < npoly && dmin > 0; ++j) { for (int k = 0; k < 9 && dmin > 0; ++k) { const int ax = (int)verts[poly[j]*3+0] + offset[k*2+0]; const int ay = (int)verts[poly[j]*3+1]; const int az = (int)verts[poly[j]*3+2] + offset[k*2+1]; if (ax < hp.xmin || ax >= hp.xmin+hp.width || az < hp.ymin || az >= hp.ymin+hp.height) continue; const rcCompactCell& c = chf.cells[(ax+bs)+(az+bs)*chf.width]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni && dmin > 0; ++i) { const rcCompactSpan& s = chf.spans[i]; int d = rcAbs(ay - (int)s.y); if (d < dmin) { startCellX = ax; startCellY = az; startSpanIndex = i; dmin = d; } } } } rcAssert(startSpanIndex != -1); // Find center of the polygon int pcx = 0, pcy = 0; for (int j = 0; j < npoly; ++j) { pcx += (int)verts[poly[j]*3+0]; pcy += (int)verts[poly[j]*3+2]; } pcx /= npoly; pcy /= npoly; // Use seeds array as a stack for DFS array.resize(0); array.push(startCellX); array.push(startCellY); array.push(startSpanIndex); int dirs[] = { 0, 1, 2, 3 }; memset(hp.data, 0, sizeof(unsigned short)*hp.width*hp.height); // DFS to move to the center. Note that we need a DFS here and can not just move // directly towards the center without recording intermediate nodes, even though the polygons // are convex. In very rare we can get stuck due to contour simplification if we do not // record nodes. int cx = -1, cy = -1, ci = -1; while (true) { if (array.size() < 3) { ctx->log(RC_LOG_WARNING, "Walk towards polygon center failed to reach center"); break; } ci = array.pop(); cy = array.pop(); cx = array.pop(); if (cx == pcx && cy == pcy) break; // If we are already at the correct X-position, prefer direction // directly towards the center in the Y-axis; otherwise prefer // direction in the X-axis int directDir; if (cx == pcx) directDir = rcGetDirForOffset(0, pcy > cy ? 1 : -1); else directDir = rcGetDirForOffset(pcx > cx ? 1 : -1, 0); // Push the direct dir last so we start with this on next iteration rcSwap(dirs[directDir], dirs[3]); const rcCompactSpan& cs = chf.spans[ci]; for (int i = 0; i < 4; i++) { int dir = dirs[i]; if (rcGetCon(cs, dir) == RC_NOT_CONNECTED) continue; int newX = cx + rcGetDirOffsetX(dir); int newY = cy + rcGetDirOffsetY(dir); int hpx = newX - hp.xmin; int hpy = newY - hp.ymin; if (hpx < 0 || hpx >= hp.width || hpy < 0 || hpy >= hp.height) continue; if (hp.data[hpx+hpy*hp.width] != 0) continue; hp.data[hpx+hpy*hp.width] = 1; array.push(newX); array.push(newY); array.push((int)chf.cells[(newX+bs)+(newY+bs)*chf.width].index + rcGetCon(cs, dir)); } rcSwap(dirs[directDir], dirs[3]); } array.resize(0); // getHeightData seeds are given in coordinates with borders array.push(cx+bs); array.push(cy+bs); array.push(ci); memset(hp.data, 0xff, sizeof(unsigned short)*hp.width*hp.height); const rcCompactSpan& cs = chf.spans[ci]; hp.data[cx-hp.xmin+(cy-hp.ymin)*hp.width] = cs.y; }