void rcFilterWalkableLowHeightSpans(int walkableHeight, rcHeightfield& solid) { rcTimeVal startTime = rcGetPerformanceTimer(); const int w = solid.width; const int h = solid.height; const int MAX_HEIGHT = 0xffff; // Remove walkable flag from spans which do not have enough // space above them for the agent to stand there. for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { for (rcSpan* s = solid.spans[x + y*w]; s; s = s->next) { const int bot = (int)(s->smax); const int top = s->next ? (int)(s->next->smin) : MAX_HEIGHT; if ((top - bot) <= walkableHeight) s->flags &= ~RC_WALKABLE; } } } rcTimeVal endTime = rcGetPerformanceTimer(); // if (rcGetLog()) // rcGetLog()->log(RC_LOG_PROGRESS, "Filter walkable: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f); if (rcGetBuildTimes()) rcGetBuildTimes()->filterWalkable += rcGetDeltaTimeUsec(startTime, endTime); }
void rcRasterizeTriangle(const float* v0, const float* v1, const float* v2, const unsigned char area, rcHeightfield& solid, const int flagMergeThr) { rcTimeVal startTime = rcGetPerformanceTimer(); const float ics = 1.0f/solid.cs; const float ich = 1.0f/solid.ch; rasterizeTri(v0, v1, v2, area, solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr); rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) rcGetBuildTimes()->rasterizeTriangles += rcGetDeltaTimeUsec(startTime, endTime); }
void rcRasterizeTriangles(const float* verts, const unsigned char* areas, const int nt, rcHeightfield& solid, const int flagMergeThr) { rcTimeVal startTime = rcGetPerformanceTimer(); const float ics = 1.0f/solid.cs; const float ich = 1.0f/solid.ch; // Rasterize triangles. for (int i = 0; i < nt; ++i) { const float* v0 = &verts[(i*3+0)*3]; const float* v1 = &verts[(i*3+1)*3]; const float* v2 = &verts[(i*3+2)*3]; // Rasterize. rasterizeTri(v0, v1, v2, areas[i], solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr); } rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) rcGetBuildTimes()->rasterizeTriangles += rcGetDeltaTimeUsec(startTime, endTime); }
bool rcBuildCompactHeightfield(const int walkableHeight, const int walkableClimb, unsigned char flags, rcHeightfield& hf, rcCompactHeightfield& chf) { rcTimeVal startTime = rcGetPerformanceTimer(); const int w = hf.width; const int h = hf.height; const int spanCount = getSpanCount(flags, hf); // Fill in header. chf.width = w; chf.height = h; chf.spanCount = spanCount; chf.walkableHeight = walkableHeight; chf.walkableClimb = walkableClimb; chf.maxRegions = 0; rcVcopy(chf.bmin, hf.bmin); rcVcopy(chf.bmax, hf.bmax); chf.bmax[1] += walkableHeight*hf.ch; chf.cs = hf.cs; chf.ch = hf.ch; chf.cells = new rcCompactCell[w*h]; if (!chf.cells) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.cells' (%d)", w*h); return false; } memset(chf.cells, 0, sizeof(rcCompactCell)*w*h); chf.spans = new rcCompactSpan[spanCount]; if (!chf.spans) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.spans' (%d)", spanCount); return false; } memset(chf.spans, 0, sizeof(rcCompactSpan)*spanCount); chf.areas = new unsigned char[spanCount]; if (!chf.areas) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.areas' (%d)", spanCount); return false; } memset(chf.areas, RC_WALKABLE_AREA, sizeof(unsigned char)*spanCount); const int MAX_HEIGHT = 0xffff; // Fill in cells and spans. int idx = 0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { const rcSpan* s = hf.spans[x + y*w]; // If there are no spans at this cell, just leave the data to index=0, count=0. if (!s) continue; rcCompactCell& c = chf.cells[x+y*w]; c.index = idx; c.count = 0; while (s) { if (s->flags == flags) { const int bot = (int)s->smax; const int top = s->next ? (int)s->next->smin : MAX_HEIGHT; chf.spans[idx].y = (unsigned short)rcClamp(bot, 0, 0xffff); chf.spans[idx].h = (unsigned char)rcClamp(top - bot, 0, 0xff); idx++; c.count++; } s = s->next; } } } // Find neighbour connections. const float MAX_LAYERS = RC_NOT_CONNECTED-1; int tooHighNeighbour = 0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { rcCompactSpan& s = chf.spans[i]; for (int dir = 0; dir < 4; ++dir) { rcSetCon(s, dir, RC_NOT_CONNECTED); const int nx = x + rcGetDirOffsetX(dir); const int ny = y + rcGetDirOffsetY(dir); // First check that the neighbour cell is in bounds. if (nx < 0 || ny < 0 || nx >= w || ny >= h) continue; // Iterate over all neighbour spans and check if any of the is // accessible from current cell. const rcCompactCell& nc = chf.cells[nx+ny*w]; for (int k = (int)nc.index, nk = (int)(nc.index+nc.count); k < nk; ++k) { const rcCompactSpan& ns = chf.spans[k]; const int bot = rcMax(s.y, ns.y); const int top = rcMin(s.y+s.h, ns.y+ns.h); // Check that the gap between the spans is walkable, // and that the climb height between the gaps is not too high. if ((top - bot) >= walkableHeight && rcAbs((int)ns.y - (int)s.y) <= walkableClimb) { // Mark direction as walkable. const int idx = k - (int)nc.index; if (idx < 0 || idx > MAX_LAYERS) { tooHighNeighbour = rcMax(tooHighNeighbour, idx); continue; } rcSetCon(s, dir, idx); break; } } } } } } if (tooHighNeighbour > MAX_LAYERS) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Heighfield has too many layers %d (max: %d)", tooHighNeighbour, MAX_LAYERS); } rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) rcGetBuildTimes()->buildCompact += rcGetDeltaTimeUsec(startTime, endTime); return true; }
bool rcMarkReachableSpans(const int walkableHeight, const int walkableClimb, rcHeightfield& solid) { const int w = solid.width; const int h = solid.height; const int MAX_HEIGHT = 0xffff; rcTimeVal startTime = rcGetPerformanceTimer(); // Build navigable space. const int MAX_SEEDS = w*h; rcReachableSeed* stack = new rcReachableSeed[MAX_SEEDS]; if (!stack) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcMarkReachableSpans: Out of memory 'stack' (%d).", MAX_SEEDS); return false; } int stackSize = 0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { rcSpan* topSpan = solid.spans[x + y*w]; if (!topSpan) continue; while (topSpan->next) topSpan = topSpan->next; // If the span is not walkable, skip it. if ((topSpan->flags & RC_WALKABLE) == 0) continue; // If the span has been visited already, skip it. if (topSpan->flags & RC_REACHABLE) continue; // Start flood fill. topSpan->flags |= RC_REACHABLE; stackSize = 0; stack[stackSize].set(x, y, topSpan); stackSize++; while (stackSize) { // Pop a seed from the stack. stackSize--; rcReachableSeed cur = stack[stackSize]; const int bot = (int)cur.s->smax; const int top = (int)cur.s->next ? (int)cur.s->next->smin : MAX_HEIGHT; // Visit neighbours in all 4 directions. for (int dir = 0; dir < 4; ++dir) { int dx = (int)cur.x + rcGetDirOffsetX(dir); int dy = (int)cur.y + rcGetDirOffsetY(dir); // Skip neighbour which are out of bounds. if (dx < 0 || dy < 0 || dx >= w || dy >= h) continue; for (rcSpan* ns = solid.spans[dx + dy*w]; ns; ns = ns->next) { // Skip neighbour if it is not walkable. if ((ns->flags & RC_WALKABLE) == 0) continue; // Skip the neighbour if it has been visited already. if (ns->flags & RC_REACHABLE) continue; const int nbot = (int)ns->smax; const int ntop = (int)ns->next ? (int)ns->next->smin : MAX_HEIGHT; // Skip neightbour if the gap between the spans is too small. if (rcMin(top,ntop) - rcMax(bot,nbot) < walkableHeight) continue; // Skip neightbour if the climb height to the neighbour is too high. if (rcAbs(nbot - bot) >= walkableClimb) continue; // This neighbour has not been visited yet. // Mark it as reachable and add it to the seed stack. ns->flags |= RC_REACHABLE; if (stackSize < MAX_SEEDS) { stack[stackSize].set(dx, dy, ns); stackSize++; } } } } } } delete [] stack; rcTimeVal endTime = rcGetPerformanceTimer(); // if (rcGetLog()) // rcGetLog()->log(RC_LOG_PROGRESS, "Mark reachable: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f); if (rcGetBuildTimes()) rcGetBuildTimes()->filterMarkReachable += rcGetDeltaTimeUsec(startTime, endTime); return true; }
bool rcErodeArea(unsigned char areaId, int radius, rcCompactHeightfield& chf) { const int w = chf.width; const int h = chf.height; rcTimeVal startTime = rcGetPerformanceTimer(); unsigned char* dist = new unsigned char[chf.spanCount]; if (!dist) return false; // Init distance. memset(dist, 0xff, sizeof(unsigned char)*chf.spanCount); // Mark boundary cells. for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { if (chf.areas[i] != RC_NULL_AREA) { const rcCompactSpan& s = chf.spans[i]; int nc = 0; for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(s, dir) != 0xf) { const int ax = x + rcGetDirOffsetX(dir); const int ay = y + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir); if (chf.areas[ai] == areaId) nc++; } } // At least one missing neighbour. if (nc != 4) dist[i] = 0; } } } } unsigned char nd; // Pass 1 for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { const rcCompactSpan& s = chf.spans[i]; if (rcGetCon(s, 0) != 0xf) { // (-1,0) const int ax = x + rcGetDirOffsetX(0); const int ay = y + rcGetDirOffsetY(0); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0); const rcCompactSpan& as = chf.spans[ai]; nd = (unsigned char)rcMin((int)dist[ai]+2, 255); if (nd < dist[i]) dist[i] = nd; // (-1,-1) if (rcGetCon(as, 3) != 0xf) { const int aax = ax + rcGetDirOffsetX(3); const int aay = ay + rcGetDirOffsetY(3); const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 3); nd = (unsigned char)rcMin((int)dist[aai]+3, 255); if (nd < dist[i]) dist[i] = nd; } } if (rcGetCon(s, 3) != 0xf) { // (0,-1) const int ax = x + rcGetDirOffsetX(3); const int ay = y + rcGetDirOffsetY(3); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3); const rcCompactSpan& as = chf.spans[ai]; nd = (unsigned char)rcMin((int)dist[ai]+2, 255); if (nd < dist[i]) dist[i] = nd; // (1,-1) if (rcGetCon(as, 2) != 0xf) { const int aax = ax + rcGetDirOffsetX(2); const int aay = ay + rcGetDirOffsetY(2); const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 2); nd = (unsigned char)rcMin((int)dist[aai]+3, 255); if (nd < dist[i]) dist[i] = nd; } } } } } // Pass 2 for (int y = h-1; y >= 0; --y) { for (int x = w-1; x >= 0; --x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { const rcCompactSpan& s = chf.spans[i]; if (rcGetCon(s, 2) != 0xf) { // (1,0) const int ax = x + rcGetDirOffsetX(2); const int ay = y + rcGetDirOffsetY(2); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 2); const rcCompactSpan& as = chf.spans[ai]; nd = (unsigned char)rcMin((int)dist[ai]+2, 255); if (nd < dist[i]) dist[i] = nd; // (1,1) if (rcGetCon(as, 1) != 0xf) { const int aax = ax + rcGetDirOffsetX(1); const int aay = ay + rcGetDirOffsetY(1); const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 1); nd = (unsigned char)rcMin((int)dist[aai]+3, 255); if (nd < dist[i]) dist[i] = nd; } } if (rcGetCon(s, 1) != 0xf) { // (0,1) const int ax = x + rcGetDirOffsetX(1); const int ay = y + rcGetDirOffsetY(1); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 1); const rcCompactSpan& as = chf.spans[ai]; nd = (unsigned char)rcMin((int)dist[ai]+2, 255); if (nd < dist[i]) dist[i] = nd; // (-1,1) if (rcGetCon(as, 0) != 0xf) { const int aax = ax + rcGetDirOffsetX(0); const int aay = ay + rcGetDirOffsetY(0); const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 0); nd = (unsigned char)rcMin((int)dist[aai]+3, 255); if (nd < dist[i]) dist[i] = nd; } } } } } const unsigned char thr = (unsigned char)(radius*2); for (int i = 0; i < chf.spanCount; ++i) if (dist[i] < thr) chf.areas[i] = 0; delete [] dist; rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) { rcGetBuildTimes()->erodeArea += rcGetDeltaTimeUsec(startTime, endTime); } return true; }
bool rcBuildRegionsMonotone(rcCompactHeightfield& chf, int borderSize, int minRegionSize, int mergeRegionSize) { rcTimeVal startTime = rcGetPerformanceTimer(); const int w = chf.width; const int h = chf.height; unsigned short id = 1; if (chf.regs) { delete [] chf.regs; chf.regs = 0; } rcScopedDelete<unsigned short> srcReg = new unsigned short[chf.spanCount]; if (!srcReg) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildRegionsMonotone: Out of memory 'src' (%d).", chf.spanCount); return false; } memset(srcReg,0,sizeof(unsigned short)*chf.spanCount); rcScopedDelete<rcSweepSpan> sweeps = new rcSweepSpan[rcMax(chf.width,chf.height)]; if (!sweeps) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildRegionsMonotone: Out of memory 'sweeps' (%d).", chf.width); return false; } // Mark border regions. if (borderSize) { paintRectRegion(0, borderSize, 0, h, id|RC_BORDER_REG, chf, srcReg); id++; paintRectRegion(w-borderSize, w, 0, h, id|RC_BORDER_REG, chf, srcReg); id++; paintRectRegion(0, w, 0, borderSize, id|RC_BORDER_REG, chf, srcReg); id++; paintRectRegion(0, w, h-borderSize, h, id|RC_BORDER_REG, chf, srcReg); id++; } rcIntArray prev(256); // Sweep one line at a time. for (int y = borderSize; y < h-borderSize; ++y) { // Collect spans from this row. prev.resize(id+1); memset(&prev[0],0,sizeof(int)*id); unsigned short rid = 1; for (int x = borderSize; x < w-borderSize; ++x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { const rcCompactSpan& s = chf.spans[i]; if (chf.areas[i] == RC_NULL_AREA) continue; // -x unsigned short previd = 0; if (rcGetCon(s, 0) != RC_NOT_CONNECTED) { const int ax = x + rcGetDirOffsetX(0); const int ay = y + rcGetDirOffsetY(0); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0); if ((srcReg[ai] & RC_BORDER_REG) == 0 && chf.areas[i] == chf.areas[ai]) previd = srcReg[ai]; } if (!previd) { previd = rid++; sweeps[previd].rid = previd; sweeps[previd].ns = 0; sweeps[previd].nei = 0; } // -y if (rcGetCon(s,3) != RC_NOT_CONNECTED) { const int ax = x + rcGetDirOffsetX(3); const int ay = y + rcGetDirOffsetY(3); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3); if (srcReg[ai] && (srcReg[ai] & RC_BORDER_REG) == 0 && chf.areas[i] == chf.areas[ai]) { unsigned short nr = srcReg[ai]; if (!sweeps[previd].nei || sweeps[previd].nei == nr) { sweeps[previd].nei = nr; sweeps[previd].ns++; prev[nr]++; } else { sweeps[previd].nei = RC_NULL_NEI; } } } srcReg[i] = previd; } } // Create unique ID. for (int i = 1; i < rid; ++i) { if (sweeps[i].nei != RC_NULL_NEI && sweeps[i].nei != 0 && prev[sweeps[i].nei] == (int)sweeps[i].ns) { sweeps[i].id = sweeps[i].nei; } else { sweeps[i].id = id++; } } // Remap IDs for (int x = borderSize; x < w-borderSize; ++x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { if (srcReg[i] > 0 && srcReg[i] < rid) srcReg[i] = sweeps[srcReg[i]].id; } } } rcTimeVal filterStartTime = rcGetPerformanceTimer(); // Filter out small regions. chf.maxRegions = id; if (!filterSmallRegions(minRegionSize, mergeRegionSize, chf.maxRegions, chf, srcReg)) return false; rcTimeVal filterEndTime = rcGetPerformanceTimer(); // Store the result out. chf.regs = srcReg; srcReg = 0; rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) { rcGetBuildTimes()->buildRegions += rcGetDeltaTimeUsec(startTime, endTime); rcGetBuildTimes()->buildRegionsFilter += rcGetDeltaTimeUsec(filterStartTime, filterEndTime); } return true; }
bool rcBuildRegions(rcCompactHeightfield& chf, int borderSize, int minRegionSize, int mergeRegionSize) { rcTimeVal startTime = rcGetPerformanceTimer(); const int w = chf.width; const int h = chf.height; if (!chf.regs) { chf.regs = new unsigned short[chf.spanCount]; if (!chf.regs) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildRegions: Out of memory 'chf.reg' (%d).", chf.spanCount); return false; } } rcScopedDelete<unsigned short> tmp = new unsigned short[chf.spanCount*4]; if (!tmp) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildRegions: Out of memory 'tmp' (%d).", chf.spanCount*4); return false; } rcTimeVal regStartTime = rcGetPerformanceTimer(); rcIntArray stack(1024); rcIntArray visited(1024); unsigned short* srcReg = tmp; unsigned short* srcDist = tmp+chf.spanCount; unsigned short* dstReg = tmp+chf.spanCount*2; unsigned short* dstDist = tmp+chf.spanCount*3; memset(srcReg, 0, sizeof(unsigned short)*chf.spanCount); memset(srcDist, 0, sizeof(unsigned short)*chf.spanCount); unsigned short regionId = 1; unsigned short level = (chf.maxDistance+1) & ~1; // TODO: Figure better formula, expandIters defines how much the // watershed "overflows" and simplifies the regions. Tying it to // agent radius was usually good indication how greedy it could be. // const int expandIters = 4 + walkableRadius * 2; const int expandIters = 8; // Mark border regions. paintRectRegion(0, borderSize, 0, h, regionId|RC_BORDER_REG, chf, srcReg); regionId++; paintRectRegion(w-borderSize, w, 0, h, regionId|RC_BORDER_REG, chf, srcReg); regionId++; paintRectRegion(0, w, 0, borderSize, regionId|RC_BORDER_REG, chf, srcReg); regionId++; paintRectRegion(0, w, h-borderSize, h, regionId|RC_BORDER_REG, chf, srcReg); regionId++; rcTimeVal expTime = 0; rcTimeVal floodTime = 0; while (level > 0) { level = level >= 2 ? level-2 : 0; rcTimeVal expStartTime = rcGetPerformanceTimer(); // Expand current regions until no empty connected cells found. if (expandRegions(expandIters, level, chf, srcReg, srcDist, dstReg, dstDist, stack) != srcReg) { rcSwap(srcReg, dstReg); rcSwap(srcDist, dstDist); } expTime += rcGetPerformanceTimer() - expStartTime; rcTimeVal floodStartTime = rcGetPerformanceTimer(); // Mark new regions with IDs. for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { if (chf.dist[i] < level || srcReg[i] != 0 || chf.areas[i] == RC_NULL_AREA) continue; if (floodRegion(x, y, i, level, regionId, chf, srcReg, srcDist, stack)) regionId++; } } } floodTime += rcGetPerformanceTimer() - floodStartTime; } // Expand current regions until no empty connected cells found. if (expandRegions(expandIters*8, 0, chf, srcReg, srcDist, dstReg, dstDist, stack) != srcReg) { rcSwap(srcReg, dstReg); rcSwap(srcDist, dstDist); } rcTimeVal regEndTime = rcGetPerformanceTimer(); rcTimeVal filterStartTime = rcGetPerformanceTimer(); // Filter out small regions. chf.maxRegions = regionId; if (!filterSmallRegions(minRegionSize, mergeRegionSize, chf.maxRegions, chf, srcReg)) return false; rcTimeVal filterEndTime = rcGetPerformanceTimer(); // Write the result out. memcpy(chf.regs, srcReg, sizeof(unsigned short)*chf.spanCount); rcTimeVal endTime = rcGetPerformanceTimer(); /* if (rcGetLog()) { rcGetLog()->log(RC_LOG_PROGRESS, "Build regions: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - reg: %.3f ms", rcGetDeltaTimeUsec(regStartTime, regEndTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - exp: %.3f ms", rcGetDeltaTimeUsec(0, expTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - flood: %.3f ms", rcGetDeltaTimeUsec(0, floodTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - filter: %.3f ms", rcGetDeltaTimeUsec(filterStartTime, filterEndTime)/1000.0f); } */ if (rcGetBuildTimes()) { rcGetBuildTimes()->buildRegions += rcGetDeltaTimeUsec(startTime, endTime); rcGetBuildTimes()->buildRegionsReg += rcGetDeltaTimeUsec(regStartTime, regEndTime); rcGetBuildTimes()->buildRegionsExp += rcGetDeltaTimeUsec(0, expTime); rcGetBuildTimes()->buildRegionsFlood += rcGetDeltaTimeUsec(0, floodTime); rcGetBuildTimes()->buildRegionsFilter += rcGetDeltaTimeUsec(filterStartTime, filterEndTime); } return true; }
bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh) { if (!nmeshes || !meshes) return true; rcTimeVal startTime = rcGetPerformanceTimer(); int* nextVert = 0; int* firstVert = 0; unsigned short* vremap = 0; mesh.nvp = meshes[0]->nvp; mesh.cs = meshes[0]->cs; mesh.ch = meshes[0]->ch; vcopy(mesh.bmin, meshes[0]->bmin); vcopy(mesh.bmax, meshes[0]->bmax); int maxVerts = 0; int maxPolys = 0; int maxVertsPerMesh = 0; for (int i = 0; i < nmeshes; ++i) { vmin(mesh.bmin, meshes[i]->bmin); vmax(mesh.bmax, meshes[i]->bmax); maxVertsPerMesh = rcMax(maxVertsPerMesh, meshes[i]->nverts); maxVerts += meshes[i]->nverts; maxPolys += meshes[i]->npolys; } mesh.nverts = 0; mesh.verts = new unsigned short[maxVerts*3]; if (!mesh.verts) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.verts' (%d).", maxVerts*3); return false; } mesh.npolys = 0; mesh.polys = new unsigned short[maxPolys*2*mesh.nvp]; if (!mesh.polys) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.polys' (%d).", maxPolys*2*mesh.nvp); return false; } memset(mesh.polys, 0xff, sizeof(unsigned short)*maxPolys*2*mesh.nvp); mesh.regs = new unsigned short[maxPolys]; if (!mesh.regs) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.regs' (%d).", maxPolys); return false; } memset(mesh.regs, 0, sizeof(unsigned short)*maxPolys); nextVert = new int[maxVerts]; if (!nextVert) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'nextVert' (%d).", maxVerts); goto failure; } memset(nextVert, 0, sizeof(int)*maxVerts); firstVert = new int[VERTEX_BUCKET_COUNT]; if (!firstVert) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'firstVert' (%d).", VERTEX_BUCKET_COUNT); goto failure; } for (int i = 0; i < VERTEX_BUCKET_COUNT; ++i) firstVert[i] = -1; vremap = new unsigned short[maxVertsPerMesh]; if (!vremap) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'vremap' (%d).", maxVertsPerMesh); goto failure; } memset(nextVert, 0, sizeof(int)*maxVerts); for (int i = 0; i < nmeshes; ++i) { const rcPolyMesh* pmesh = meshes[i]; const unsigned short ox = (unsigned short)floorf((pmesh->bmin[0]-mesh.bmin[0])/mesh.cs+0.5f); const unsigned short oz = (unsigned short)floorf((pmesh->bmin[2]-mesh.bmin[2])/mesh.cs+0.5f); for (int j = 0; j < pmesh->nverts; ++j) { unsigned short* v = &pmesh->verts[j*3]; vremap[j] = addVertex(v[0]+ox, v[1], v[2]+oz, mesh.verts, firstVert, nextVert, mesh.nverts); } for (int j = 0; j < pmesh->npolys; ++j) { unsigned short* tgt = &mesh.polys[mesh.npolys*2*mesh.nvp]; unsigned short* src = &pmesh->polys[j*2*mesh.nvp]; mesh.regs[mesh.npolys] = pmesh->regs[j]; mesh.npolys++; for (int k = 0; k < mesh.nvp; ++k) { if (src[k] == 0xffff) break; tgt[k] = vremap[src[k]]; } } } // Calculate adjacency. if (!buildMeshAdjacency(mesh.polys, mesh.npolys, mesh.nverts, mesh.nvp)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Adjacency failed."); return false; } delete [] firstVert; delete [] nextVert; delete [] vremap; rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) rcGetBuildTimes()->mergePolyMesh += rcGetDeltaTimeUsec(startTime, endTime); return true; failure: delete [] firstVert; delete [] nextVert; delete [] vremap; return false; }
bool rcMergePolyMeshDetails(rcPolyMeshDetail** meshes, const int nmeshes, rcPolyMeshDetail& mesh) { rcTimeVal startTime = rcGetPerformanceTimer(); int maxVerts = 0; int maxTris = 0; int maxMeshes = 0; for (int i = 0; i < nmeshes; ++i) { if (!meshes[i]) continue; maxVerts += meshes[i]->nverts; maxTris += meshes[i]->ntris; maxMeshes += meshes[i]->nmeshes; } mesh.nmeshes = 0; mesh.meshes = new unsigned short[maxMeshes*4]; if (!mesh.meshes) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'pmdtl.meshes' (%d).", maxMeshes*4); return false; } mesh.ntris = 0; mesh.tris = new unsigned char[maxTris*4]; if (!mesh.tris) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.tris' (%d).", maxTris*4); return false; } mesh.nverts = 0; mesh.verts = new float[maxVerts*3]; if (!mesh.verts) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.verts' (%d).", maxVerts*3); return false; } // Merge datas. for (int i = 0; i < nmeshes; ++i) { rcPolyMeshDetail* dm = meshes[i]; if (!dm) continue; for (int j = 0; j < dm->nmeshes; ++j) { unsigned short* dst = &mesh.meshes[mesh.nmeshes*4]; unsigned short* src = &dm->meshes[j*4]; dst[0] = mesh.nverts+src[0]; dst[1] = src[1]; dst[2] = mesh.ntris+src[2]; dst[3] = src[3]; mesh.nmeshes++; } for (int k = 0; k < dm->nverts; ++k) { vcopy(&mesh.verts[mesh.nverts*3], &dm->verts[k*3]); mesh.nverts++; } for (int k = 0; k < dm->ntris; ++k) { mesh.tris[mesh.ntris*4+0] = dm->tris[k*4+0]; mesh.tris[mesh.ntris*4+1] = dm->tris[k*4+1]; mesh.tris[mesh.ntris*4+2] = dm->tris[k*4+2]; mesh.tris[mesh.ntris*4+3] = dm->tris[k*4+3]; mesh.ntris++; } } rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) rcGetBuildTimes()->mergePolyMeshDetail += rcGetDeltaTimeUsec(startTime, endTime); return true; }
bool rcBuildPolyMeshDetail(const rcPolyMesh& mesh, const rcCompactHeightfield& chf, const float sampleDist, const float sampleMaxError, rcPolyMeshDetail& dmesh) { rcTimeVal startTime = rcGetPerformanceTimer(); if (mesh.nverts == 0 || mesh.npolys == 0) return true; const int nvp = mesh.nvp; const float cs = mesh.cs; const float ch = mesh.ch; const float* orig = mesh.bmin; rcIntArray edges(64); rcIntArray tris(512); rcIntArray idx(512); rcIntArray stack(512); rcIntArray samples(512); float verts[256*3]; float* poly = 0; int* bounds = 0; rcHeightPatch hp; int nPolyVerts = 0; int maxhw = 0, maxhh = 0; bounds = new int[mesh.npolys*4]; if (!bounds) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'bounds' (%d).", mesh.npolys*4); goto failure; } poly = new float[nvp*3]; if (!bounds) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'poly' (%d).", nvp*3); goto failure; } // Find max size for a polygon area. for (int i = 0; i < mesh.npolys; ++i) { const unsigned short* p = &mesh.polys[i*nvp*2]; int& xmin = bounds[i*4+0]; int& xmax = bounds[i*4+1]; int& ymin = bounds[i*4+2]; int& ymax = bounds[i*4+3]; xmin = chf.width; xmax = 0; ymin = chf.height; ymax = 0; for (int j = 0; j < nvp; ++j) { if(p[j] == 0xffff) break; const unsigned short* v = &mesh.verts[p[j]*3]; xmin = rcMin(xmin, (int)v[0]); xmax = rcMax(xmax, (int)v[0]); ymin = rcMin(ymin, (int)v[2]); ymax = rcMax(ymax, (int)v[2]); nPolyVerts++; } xmin = rcMax(0,xmin-1); xmax = rcMin(chf.width,xmax+1); ymin = rcMax(0,ymin-1); ymax = rcMin(chf.height,ymax+1); if (xmin >= xmax || ymin >= ymax) continue; maxhw = rcMax(maxhw, xmax-xmin); maxhh = rcMax(maxhh, ymax-ymin); } hp.data = new unsigned short[maxhw*maxhh]; if (!hp.data) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'hp.data' (%d).", maxhw*maxhh); goto failure; } dmesh.nmeshes = mesh.npolys; dmesh.nverts = 0; dmesh.ntris = 0; dmesh.meshes = new unsigned short[dmesh.nmeshes*4]; if (!dmesh.meshes) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.meshes' (%d).", dmesh.nmeshes*4); goto failure; } int vcap = nPolyVerts+nPolyVerts/2; int tcap = vcap*2; dmesh.nverts = 0; dmesh.verts = new float[vcap*3]; if (!dmesh.verts) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.verts' (%d).", vcap*3); goto failure; } dmesh.ntris = 0; dmesh.tris = new unsigned char[tcap*4]; if (!dmesh.tris) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.tris' (%d).", tcap*4); goto failure; } for (int i = 0; i < mesh.npolys; ++i) { const unsigned short* p = &mesh.polys[i*nvp*2]; // Find polygon bounding box. int npoly = 0; for (int j = 0; j < nvp; ++j) { if(p[j] == 0xffff) break; const unsigned short* v = &mesh.verts[p[j]*3]; poly[j*3+0] = orig[0] + v[0]*cs; poly[j*3+1] = orig[1] + v[1]*ch; poly[j*3+2] = orig[2] + v[2]*cs; npoly++; } // Get the height data from the area of the polygon. hp.xmin = bounds[i*4+0]; hp.ymin = bounds[i*4+2]; hp.width = bounds[i*4+1]-bounds[i*4+0]; hp.height = bounds[i*4+3]-bounds[i*4+2]; getHeightData(chf, p, npoly, mesh.verts, hp, stack); // Build detail mesh. int nverts = 0; if (!buildPolyDetail(poly, npoly, mesh.regs[i], sampleDist, sampleMaxError, chf, hp, verts, nverts, tris, edges, idx, samples)) { goto failure; } // Offset detail vertices, unnecassary? for (int j = 0; j < nverts; ++j) verts[j*3+1] += chf.ch; // Store detail submesh. const int ntris = tris.size()/4; dmesh.meshes[i*4+0] = dmesh.nverts; dmesh.meshes[i*4+1] = (unsigned short)nverts; dmesh.meshes[i*4+2] = dmesh.ntris; dmesh.meshes[i*4+3] = (unsigned short)ntris; // Store vertices, allocate more memory if necessary. if (dmesh.nverts+nverts > vcap) { while (dmesh.nverts+nverts > vcap) vcap += 256; float* newv = new float[vcap*3]; if (!newv) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'newv' (%d).", vcap*3); goto failure; } if (dmesh.nverts) memcpy(newv, dmesh.verts, sizeof(float)*3*dmesh.nverts); delete [] dmesh.verts; dmesh.verts = newv; } for (int j = 0; j < nverts; ++j) { dmesh.verts[dmesh.nverts*3+0] = verts[j*3+0]; dmesh.verts[dmesh.nverts*3+1] = verts[j*3+1]; dmesh.verts[dmesh.nverts*3+2] = verts[j*3+2]; dmesh.nverts++; } // Store triangles, allocate more memory if necessary. if (dmesh.ntris+ntris > tcap) { while (dmesh.ntris+ntris > tcap) tcap += 256; unsigned char* newt = new unsigned char[tcap*4]; if (!newt) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'newt' (%d).", tcap*4); goto failure; } if (dmesh.ntris) memcpy(newt, dmesh.tris, sizeof(unsigned char)*4*dmesh.ntris); delete [] dmesh.tris; dmesh.tris = newt; } for (int j = 0; j < ntris; ++j) { const int* t = &tris[j*4]; dmesh.tris[dmesh.ntris*4+0] = (unsigned char)t[0]; dmesh.tris[dmesh.ntris*4+1] = (unsigned char)t[1]; dmesh.tris[dmesh.ntris*4+2] = (unsigned char)t[2]; dmesh.tris[dmesh.ntris*4+3] = getTriFlags(&verts[t[0]*3], &verts[t[1]*3], &verts[t[2]*3], poly, npoly); dmesh.ntris++; } } delete [] bounds; delete [] poly; rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) rcGetBuildTimes()->buildDetailMesh += rcGetDeltaTimeUsec(startTime, endTime); return true; failure: delete [] bounds; delete [] poly; return false; }
PDT_NAV_MESH gkRecast::createNavMesh(PMESHDATA meshData, const Config& config) { if (!meshData.get()) return PDT_NAV_MESH(0); rcConfig cfg; cfg.cs = config.CELL_SIZE; cfg.ch = config.CELL_HEIGHT; GK_ASSERT(cfg.ch && "cfg.ch cannot be zero"); GK_ASSERT(cfg.ch && "cfg.ch cannot be zero"); cfg.walkableSlopeAngle = config.AGENT_MAX_SLOPE; cfg.walkableHeight = (int)ceilf(config.AGENT_HEIGHT / cfg.ch); cfg.walkableClimb = (int)ceilf(config.AGENT_MAX_CLIMB / cfg.ch); cfg.walkableRadius = (int)ceilf(config.AGENT_RADIUS / cfg.cs); cfg.maxEdgeLen = (int)(config.EDGE_MAX_LEN / cfg.cs); cfg.maxSimplificationError = config.EDGE_MAX_ERROR; cfg.minRegionSize = (int)rcSqr(config.REGION_MIN_SIZE); cfg.mergeRegionSize = (int)rcSqr(config.REGION_MERGE_SIZE); cfg.maxVertsPerPoly = gkMin(config.VERTS_PER_POLY, DT_VERTS_PER_POLYGON); cfg.tileSize = config.TILE_SIZE; cfg.borderSize = cfg.walkableRadius + 4; // Reserve enough padding. cfg.detailSampleDist = config.DETAIL_SAMPLE_DIST < 0.9f ? 0 : cfg.cs * config.DETAIL_SAMPLE_DIST; cfg.detailSampleMaxError = cfg.ch * config.DETAIL_SAMPLE_ERROR; if (!meshData->getVertCount()) return PDT_NAV_MESH(0); gkScalar bmin[3], bmax[3]; const gkScalar* verts = meshData->getVerts(); int nverts = meshData->getVertCount(); const int* tris = meshData->getTris(); const gkScalar* trinorms = meshData->getNormals(); int ntris = meshData->getTriCount(); rcCalcBounds(verts, nverts, bmin, bmax); // // Step 1. Initialize build config. // // Set the area where the navigation will be build. // Here the bounds of the input mesh are used, but the // area could be specified by an user defined box, etc. rcVcopy(cfg.bmin, bmin); rcVcopy(cfg.bmax, bmax); rcCalcGridSize(cfg.bmin, cfg.bmax, cfg.cs, &cfg.width, &cfg.height); rcBuildTimes m_buildTimes; // Reset build times gathering. memset(&m_buildTimes, 0, sizeof(m_buildTimes)); rcSetBuildTimes(&m_buildTimes); // Start the build process. rcTimeVal totStartTime = rcGetPerformanceTimer(); //gkPrintf("Building navigation:"); //gkPrintf(" - %d x %d cells", cfg.width, cfg.height); //gkPrintf(" - %.1fK verts, %.1fK tris", nverts/1000.0f, ntris/1000.0f); // // Step 2. Rasterize input polygon soup. // // Allocate voxel heighfield where we rasterize our input data to. rcHeightfield heightField; if (!rcCreateHeightfield(heightField, cfg.width, cfg.height, cfg.bmin, cfg.bmax, cfg.cs, cfg.ch)) { gkPrintf("buildNavigation: Could not create solid heightfield."); return PDT_NAV_MESH(0); } { // Allocate array that can hold triangle flags. // If you have multiple meshes you need to process, allocate // and array which can hold the max number of triangles you need to process. utArray<unsigned char> triflags; triflags.resize(ntris); // Find triangles which are walkable based on their slope and rasterize them. // If your input data is multiple meshes, you can transform them here, calculate // the flags for each of the meshes and rasterize them. memset(triflags.ptr(), 0, ntris * sizeof(unsigned char)); rcMarkWalkableTriangles(cfg.walkableSlopeAngle, verts, nverts, tris, ntris, triflags.ptr()); rcRasterizeTriangles(verts, nverts, tris, triflags.ptr(), ntris, heightField); } // // Step 3. Filter walkables surfaces. // // Once all geoemtry is rasterized, we do initial pass of filtering to // remove unwanted overhangs caused by the conservative rasterization // as well as filter spans where the character cannot possibly stand. rcFilterLedgeSpans(cfg.walkableHeight, cfg.walkableClimb, heightField); rcFilterWalkableLowHeightSpans(cfg.walkableHeight, heightField); // // Step 4. Partition walkable surface to simple regions. // // Compact the heightfield so that it is faster to handle from now on. // This will result more cache coherent data as well as the neighbours // between walkable cells will be calculated. rcCompactHeightfield chf; if (!rcBuildCompactHeightfield(cfg.walkableHeight, cfg.walkableClimb, RC_WALKABLE, heightField, chf)) { gkPrintf("buildNavigation: Could not build compact data."); return PDT_NAV_MESH(0); } // Erode the walkable area by agent radius. if (!rcErodeArea(RC_WALKABLE_AREA, cfg.walkableRadius, chf)) { gkPrintf("buildNavigation: Could not erode."); return PDT_NAV_MESH(0); } // // Mark areas from objects // gkScene* scene = gkEngine::getSingleton().getActiveScene(); gkGameObjectSet& objects = scene->getInstancedObjects(); gkGameObjectSet::Iterator it = objects.iterator(); while (it.hasMoreElements()) { gkGameObject* obj = it.getNext(); if (!obj->getNavData().isEmpty()) { size_t tBaseIndex = obj->getNavData().triangleBaseIndex; size_t vBaseIndex = tBaseIndex / 2; const float* v = verts + vBaseIndex; const int nVerts = obj->getNavData().nIndex / 3; const gkGameObjectProperties& prop = obj->getProperties(); rcMarkConvexPolyArea(v, nVerts, obj->getNavData().hmin, obj->getNavData().hmax, prop.m_findPathFlag, chf); } } // Prepare for region partitioning, by calculating distance field along the walkable surface. if (!rcBuildDistanceField(chf)) { gkPrintf("buildNavigation: Could not build distance field."); return PDT_NAV_MESH(0); } // Partition the walkable surface into simple regions without holes. if (!rcBuildRegions(chf, cfg.borderSize, cfg.minRegionSize, cfg.mergeRegionSize)) { gkPrintf("buildNavigation: Could not build regions."); return PDT_NAV_MESH(0); } // // Step 5. Trace and simplify region contours. // // Create contours. rcContourSet cset; if (!rcBuildContours(chf, cfg.maxSimplificationError, cfg.maxEdgeLen, cset)) { gkPrintf("buildNavigation: Could not create contours."); return PDT_NAV_MESH(0); } // // Step 6. Build polygons mesh from contours. // // Build polygon navmesh from the contours. rcPolyMesh pmesh; if (!rcBuildPolyMesh(cset, cfg.maxVertsPerPoly, pmesh)) { gkPrintf("buildNavigation: Could not triangulate contours."); return PDT_NAV_MESH(0); } // // Step 7. Create detail mesh which allows to access approximate height on each polygon. // rcPolyMeshDetail dmesh; if (!rcBuildPolyMeshDetail(pmesh, chf, cfg.detailSampleDist, cfg.detailSampleMaxError, dmesh)) { gkPrintf("buildNavigation: Could not build detail mesh."); return PDT_NAV_MESH(0); } // At this point the navigation mesh data is ready, you can access it from pmesh. // See rcDebugDrawPolyMesh or dtCreateNavMeshData as examples how to access the data. // // Step 8. Create Detour data from Recast poly mesh. // PDT_NAV_MESH navMesh; // Update poly flags from areas. for (int i = 0; i < pmesh.npolys; ++i) pmesh.flags[i] = 0xFFFF & pmesh.areas[i]; dtNavMeshCreateParams params; memset(¶ms, 0, sizeof(params)); params.verts = pmesh.verts; params.vertCount = pmesh.nverts; params.polys = pmesh.polys; params.polyAreas = pmesh.areas; params.polyFlags = pmesh.flags; params.polyCount = pmesh.npolys; params.nvp = pmesh.nvp; params.detailMeshes = dmesh.meshes; params.detailVerts = dmesh.verts; params.detailVertsCount = dmesh.nverts; params.detailTris = dmesh.tris; params.detailTriCount = dmesh.ntris; /* params.offMeshConVerts = m_geom->getOffMeshConnectionVerts(); params.offMeshConRad = m_geom->getOffMeshConnectionRads(); params.offMeshConDir = m_geom->getOffMeshConnectionDirs(); params.offMeshConAreas = m_geom->getOffMeshConnectionAreas(); params.offMeshConFlags = m_geom->getOffMeshConnectionFlags(); params.offMeshConCount = m_geom->getOffMeshConnectionCount(); */ params.walkableHeight = cfg.walkableHeight * cfg.ch; params.walkableRadius = cfg.walkableRadius * cfg.cs;; params.walkableClimb = cfg.walkableClimb * cfg.ch; rcVcopy(params.bmin, pmesh.bmin); rcVcopy(params.bmax, pmesh.bmax); params.cs = cfg.cs; params.ch = cfg.ch; unsigned char* navData = 0; int navDataSize = 0; if (!dtCreateNavMeshData(¶ms, &navData, &navDataSize)) { gkPrintf("Could not build Detour navmesh."); return PDT_NAV_MESH(0); } navMesh = PDT_NAV_MESH(new gkDetourNavMesh(new dtNavMesh)); if (!navMesh->m_p->init(navData, navDataSize, DT_TILE_FREE_DATA, 2048)) { delete [] navData; gkPrintf("Could not init Detour navmesh"); return PDT_NAV_MESH(0); } rcTimeVal totEndTime = rcGetPerformanceTimer(); gkPrintf("Navigation mesh created: %.1fms", rcGetDeltaTimeUsec(totStartTime, totEndTime) / 1000.0f); return navMesh; }
bool rcBuildPolyMeshDetail(const rcPolyMesh& mesh, const rcCompactHeightfield& chf, const float sampleDist, const float sampleMaxError, rcPolyMeshDetail& dmesh) { rcTimeVal startTime = rcGetPerformanceTimer(); if (mesh.nverts == 0 || mesh.npolys == 0) return true; const int nvp = mesh.nvp; const float cs = mesh.cs; const float ch = mesh.ch; const float* orig = mesh.bmin; rcIntArray edges(64); rcIntArray tris(512); rcIntArray stack(512); rcIntArray samples(512); float verts[256*3]; rcHeightPatch hp; int nPolyVerts = 0; int maxhw = 0, maxhh = 0; rcScopedDelete<int> bounds = (int*)rcAlloc(sizeof(int)*mesh.npolys*4, RC_ALLOC_TEMP); if (!bounds) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'bounds' (%d).", mesh.npolys*4); return false; } rcScopedDelete<float> poly = (float*)rcAlloc(sizeof(float)*nvp*3, RC_ALLOC_TEMP); if (!poly) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'poly' (%d).", nvp*3); return false; } // Find max size for a polygon area. for (int i = 0; i < mesh.npolys; ++i) { const unsigned short* p = &mesh.polys[i*nvp*2]; int& xmin = bounds[i*4+0]; int& xmax = bounds[i*4+1]; int& ymin = bounds[i*4+2]; int& ymax = bounds[i*4+3]; xmin = chf.width; xmax = 0; ymin = chf.height; ymax = 0; for (int j = 0; j < nvp; ++j) { if(p[j] == RC_MESH_NULL_IDX) break; const unsigned short* v = &mesh.verts[p[j]*3]; xmin = rcMin(xmin, (int)v[0]); xmax = rcMax(xmax, (int)v[0]); ymin = rcMin(ymin, (int)v[2]); ymax = rcMax(ymax, (int)v[2]); nPolyVerts++; } xmin = rcMax(0,xmin-1); xmax = rcMin(chf.width,xmax+1); ymin = rcMax(0,ymin-1); ymax = rcMin(chf.height,ymax+1); if (xmin >= xmax || ymin >= ymax) continue; maxhw = rcMax(maxhw, xmax-xmin); maxhh = rcMax(maxhh, ymax-ymin); } hp.data = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxhw*maxhh, RC_ALLOC_TEMP); if (!hp.data) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'hp.data' (%d).", maxhw*maxhh); return false; } dmesh.nmeshes = mesh.npolys; dmesh.nverts = 0; dmesh.ntris = 0; dmesh.meshes = (unsigned short*)rcAlloc(sizeof(unsigned short)*dmesh.nmeshes*4, RC_ALLOC_PERM); if (!dmesh.meshes) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.meshes' (%d).", dmesh.nmeshes*4); return false; } int vcap = nPolyVerts+nPolyVerts/2; int tcap = vcap*2; dmesh.nverts = 0; dmesh.verts = (float*)rcAlloc(sizeof(float)*vcap*3, RC_ALLOC_PERM); if (!dmesh.verts) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.verts' (%d).", vcap*3); return false; } dmesh.ntris = 0; dmesh.tris = (unsigned char*)rcAlloc(sizeof(unsigned char*)*tcap*4, RC_ALLOC_PERM); if (!dmesh.tris) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.tris' (%d).", tcap*4); return false; } for (int i = 0; i < mesh.npolys; ++i) { const unsigned short* p = &mesh.polys[i*nvp*2]; // Store polygon vertices for processing. int npoly = 0; for (int j = 0; j < nvp; ++j) { if(p[j] == RC_MESH_NULL_IDX) break; const unsigned short* v = &mesh.verts[p[j]*3]; poly[j*3+0] = v[0]*cs; poly[j*3+1] = v[1]*ch; poly[j*3+2] = v[2]*cs; npoly++; } // Get the height data from the area of the polygon. hp.xmin = bounds[i*4+0]; hp.ymin = bounds[i*4+2]; hp.width = bounds[i*4+1]-bounds[i*4+0]; hp.height = bounds[i*4+3]-bounds[i*4+2]; getHeightData(chf, p, npoly, mesh.verts, hp, stack); // Build detail mesh. int nverts = 0; if (!buildPolyDetail(poly, npoly, sampleDist, sampleMaxError, chf, hp, verts, nverts, tris, edges, samples)) { return false; } // Move detail verts to world space. for (int j = 0; j < nverts; ++j) { verts[j*3+0] += orig[0]; verts[j*3+1] += orig[1] + chf.ch; // Is this offset necessary? verts[j*3+2] += orig[2]; } // Offset poly too, will be used to flag checking. for (int j = 0; j < npoly; ++j) { poly[j*3+0] += orig[0]; poly[j*3+1] += orig[1]; poly[j*3+2] += orig[2]; } // Store detail submesh. const int ntris = tris.size()/4; dmesh.meshes[i*4+0] = (unsigned short)dmesh.nverts; dmesh.meshes[i*4+1] = (unsigned short)nverts; dmesh.meshes[i*4+2] = (unsigned short)dmesh.ntris; dmesh.meshes[i*4+3] = (unsigned short)ntris; // Store vertices, allocate more memory if necessary. if (dmesh.nverts+nverts > vcap) { while (dmesh.nverts+nverts > vcap) vcap += 256; float* newv = (float*)rcAlloc(sizeof(float)*vcap*3, RC_ALLOC_PERM); if (!newv) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'newv' (%d).", vcap*3); return false; } if (dmesh.nverts) memcpy(newv, dmesh.verts, sizeof(float)*3*dmesh.nverts); rcFree(dmesh.verts); dmesh.verts = newv; } for (int j = 0; j < nverts; ++j) { dmesh.verts[dmesh.nverts*3+0] = verts[j*3+0]; dmesh.verts[dmesh.nverts*3+1] = verts[j*3+1]; dmesh.verts[dmesh.nverts*3+2] = verts[j*3+2]; dmesh.nverts++; } // Store triangles, allocate more memory if necessary. if (dmesh.ntris+ntris > tcap) { while (dmesh.ntris+ntris > tcap) tcap += 256; unsigned char* newt = (unsigned char*)rcAlloc(sizeof(unsigned char)*tcap*4, RC_ALLOC_PERM); if (!newt) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'newt' (%d).", tcap*4); return false; } if (dmesh.ntris) memcpy(newt, dmesh.tris, sizeof(unsigned char)*4*dmesh.ntris); rcFree(dmesh.tris); dmesh.tris = newt; } for (int j = 0; j < ntris; ++j) { const int* t = &tris[j*4]; dmesh.tris[dmesh.ntris*4+0] = (unsigned char)t[0]; dmesh.tris[dmesh.ntris*4+1] = (unsigned char)t[1]; dmesh.tris[dmesh.ntris*4+2] = (unsigned char)t[2]; dmesh.tris[dmesh.ntris*4+3] = getTriFlags(&verts[t[0]*3], &verts[t[1]*3], &verts[t[2]*3], poly, npoly); dmesh.ntris++; } } rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) rcGetBuildTimes()->buildDetailMesh += rcGetDeltaTimeUsec(startTime, endTime); return true; }
bool rcBuildDistanceField(rcCompactHeightfield& chf) { rcTimeVal startTime = rcGetPerformanceTimer(); unsigned short* dist0 = new unsigned short[chf.spanCount]; if (!dist0) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'dist0' (%d).", chf.spanCount); return false; } unsigned short* dist1 = new unsigned short[chf.spanCount]; if (!dist1) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'dist1' (%d).", chf.spanCount); delete [] dist0; return false; } unsigned short* src = dist0; unsigned short* dst = dist1; unsigned short maxDist = 0; rcTimeVal distStartTime = rcGetPerformanceTimer(); if (calculateDistanceField(chf, src, dst, maxDist) != src) rcSwap(src, dst); chf.maxDistance = maxDist; rcTimeVal distEndTime = rcGetPerformanceTimer(); rcTimeVal blurStartTime = rcGetPerformanceTimer(); // Blur if (boxBlur(chf, 1, src, dst) != src) rcSwap(src, dst); // Store distance. for (int i = 0; i < chf.spanCount; ++i) chf.spans[i].dist = src[i]; rcTimeVal blurEndTime = rcGetPerformanceTimer(); delete [] dist0; delete [] dist1; rcTimeVal endTime = rcGetPerformanceTimer(); /* if (rcGetLog()) { rcGetLog()->log(RC_LOG_PROGRESS, "Build distance field: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - dist: %.3f ms", rcGetDeltaTimeUsec(distStartTime, distEndTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - blur: %.3f ms", rcGetDeltaTimeUsec(blurStartTime, blurEndTime)/1000.0f); }*/ if (rcGetBuildTimes()) { rcGetBuildTimes()->buildDistanceField += rcGetDeltaTimeUsec(startTime, endTime); rcGetBuildTimes()->buildDistanceFieldDist += rcGetDeltaTimeUsec(distStartTime, distEndTime); rcGetBuildTimes()->buildDistanceFieldBlur += rcGetDeltaTimeUsec(blurStartTime, blurEndTime); } return true; }
bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh) { rcTimeVal startTime = rcGetPerformanceTimer(); vcopy(mesh.bmin, cset.bmin); vcopy(mesh.bmax, cset.bmax); mesh.cs = cset.cs; mesh.ch = cset.ch; int maxVertices = 0; int maxTris = 0; int maxVertsPerCont = 0; for (int i = 0; i < cset.nconts; ++i) { maxVertices += cset.conts[i].nverts; maxTris += cset.conts[i].nverts - 2; maxVertsPerCont = rcMax(maxVertsPerCont, cset.conts[i].nverts); } if (maxVertices >= 0xfffe) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Too many vertices %d.", maxVertices); return false; } unsigned char* vflags = 0; int* nextVert = 0; int* firstVert = 0; int* indices = 0; int* tris = 0; unsigned short* polys = 0; vflags = new unsigned char[maxVertices]; if (!vflags) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.verts' (%d).", maxVertices); goto failure; } memset(vflags, 0, maxVertices); mesh.verts = new unsigned short[maxVertices*3]; if (!mesh.verts) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.verts' (%d).", maxVertices); goto failure; } mesh.polys = new unsigned short[maxTris*nvp*2]; if (!mesh.polys) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.polys' (%d).", maxTris*nvp*2); goto failure; } mesh.regs = new unsigned short[maxTris]; if (!mesh.regs) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.regs' (%d).", maxTris); goto failure; } mesh.nverts = 0; mesh.npolys = 0; mesh.nvp = nvp; memset(mesh.verts, 0, sizeof(unsigned short)*maxVertices*3); memset(mesh.polys, 0xff, sizeof(unsigned short)*maxTris*nvp*2); memset(mesh.regs, 0, sizeof(unsigned short)*maxTris); nextVert = new int[maxVertices]; if (!nextVert) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'nextVert' (%d).", maxVertices); goto failure; } memset(nextVert, 0, sizeof(int)*maxVertices); firstVert = new int[VERTEX_BUCKET_COUNT]; if (!firstVert) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'firstVert' (%d).", VERTEX_BUCKET_COUNT); goto failure; } for (int i = 0; i < VERTEX_BUCKET_COUNT; ++i) firstVert[i] = -1; indices = new int[maxVertsPerCont]; if (!indices) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'indices' (%d).", maxVertsPerCont); goto failure; } tris = new int[maxVertsPerCont*3]; if (!tris) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'tris' (%d).", maxVertsPerCont*3); goto failure; } polys = new unsigned short[(maxVertsPerCont+1)*nvp]; if (!polys) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'polys' (%d).", maxVertsPerCont*nvp); goto failure; } unsigned short* tmpPoly = &polys[maxVertsPerCont*nvp]; for (int i = 0; i < cset.nconts; ++i) { rcContour& cont = cset.conts[i]; // Skip empty contours. if (cont.nverts < 3) continue; // Triangulate contour for (int j = 0; j < cont.nverts; ++j) indices[j] = j; int ntris = triangulate(cont.nverts, cont.verts, &indices[0], &tris[0]); if (ntris <= 0) { // Bad triangulation, should not happen. /* for (int k = 0; k < cont.nverts; ++k) { const int* v = &cont.verts[k*4]; printf("\t\t%d,%d,%d,%d,\n", v[0], v[1], v[2], v[3]); if (nBadPos < 100) { badPos[nBadPos*3+0] = v[0]; badPos[nBadPos*3+1] = v[1]; badPos[nBadPos*3+2] = v[2]; nBadPos++; } }*/ ntris = -ntris; } // Add and merge vertices. for (int j = 0; j < cont.nverts; ++j) { const int* v = &cont.verts[j*4]; indices[j] = addVertex((unsigned short)v[0], (unsigned short)v[1], (unsigned short)v[2], mesh.verts, firstVert, nextVert, mesh.nverts); if (v[3] & RC_BORDER_VERTEX) { // This vertex should be removed. vflags[indices[j]] = 1; } } // Build initial polygons. int npolys = 0; memset(polys, 0xff, maxVertsPerCont*nvp*sizeof(unsigned short)); for (int j = 0; j < ntris; ++j) { int* t = &tris[j*3]; if (t[0] != t[1] && t[0] != t[2] && t[1] != t[2]) { polys[npolys*nvp+0] = (unsigned short)indices[t[0]]; polys[npolys*nvp+1] = (unsigned short)indices[t[1]]; polys[npolys*nvp+2] = (unsigned short)indices[t[2]]; npolys++; } } if (!npolys) continue; // Merge polygons. if (nvp > 3) { while (true) { // Find best polygons to merge. int bestMergeVal = 0; int bestPa, bestPb, bestEa, bestEb; for (int j = 0; j < npolys-1; ++j) { unsigned short* pj = &polys[j*nvp]; for (int k = j+1; k < npolys; ++k) { unsigned short* pk = &polys[k*nvp]; int ea, eb; int v = getPolyMergeValue(pj, pk, mesh.verts, ea, eb, nvp); if (v > bestMergeVal) { bestMergeVal = v; bestPa = j; bestPb = k; bestEa = ea; bestEb = eb; } } } if (bestMergeVal > 0) { // Found best, merge. unsigned short* pa = &polys[bestPa*nvp]; unsigned short* pb = &polys[bestPb*nvp]; mergePolys(pa, pb, mesh.verts, bestEa, bestEb, tmpPoly, nvp); memcpy(pb, &polys[(npolys-1)*nvp], sizeof(unsigned short)*nvp); npolys--; } else { // Could not merge any polygons, stop. break; } } } // Store polygons. for (int j = 0; j < npolys; ++j) { unsigned short* p = &mesh.polys[mesh.npolys*nvp*2]; unsigned short* q = &polys[j*nvp]; for (int k = 0; k < nvp; ++k) p[k] = q[k]; mesh.regs[mesh.npolys] = cont.reg; mesh.npolys++; } } // Remove edge vertices. for (int i = 0; i < mesh.nverts; ++i) { if (vflags[i]) { if (!removeVertex(mesh, i, maxTris)) goto failure; for (int j = i; j < mesh.nverts-1; ++j) vflags[j] = vflags[j+1]; --i; } } delete [] vflags; delete [] firstVert; delete [] nextVert; delete [] indices; delete [] tris; // Calculate adjacency. if (!buildMeshAdjacency(mesh.polys, mesh.npolys, mesh.nverts, nvp)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Adjacency failed."); return false; } rcTimeVal endTime = rcGetPerformanceTimer(); // if (rcGetLog()) // rcGetLog()->log(RC_LOG_PROGRESS, "Build polymesh: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f); if (rcGetBuildTimes()) rcGetBuildTimes()->buildPolymesh += rcGetDeltaTimeUsec(startTime, endTime); return true; failure: delete [] vflags; delete [] tmpPoly; delete [] firstVert; delete [] nextVert; delete [] indices; delete [] tris; return false; }
bool rcBuildRegions(rcCompactHeightfield& chf, int walkableRadius, int borderSize, int minRegionSize, int mergeRegionSize) { rcTimeVal startTime = rcGetPerformanceTimer(); const int w = chf.width; const int h = chf.height; unsigned short* tmp1 = new unsigned short[chf.spanCount*2]; if (!tmp1) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'tmp1' (%d).", chf.spanCount*2); return false; } unsigned short* tmp2 = new unsigned short[chf.spanCount*2]; if (!tmp2) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'tmp2' (%d).", chf.spanCount*2); delete [] tmp1; return false; } rcTimeVal regStartTime = rcGetPerformanceTimer(); rcIntArray stack(1024); rcIntArray visited(1024); unsigned short* src = tmp1; unsigned short* dst = tmp2; memset(src, 0, sizeof(unsigned short) * chf.spanCount*2); unsigned short regionId = 1; unsigned short level = (chf.maxDistance+1) & ~1; unsigned short minLevel = (unsigned short)(walkableRadius*2); const int expandIters = 4 + walkableRadius * 2; // Mark border regions. paintRectRegion(0, borderSize, 0, h, regionId|RC_BORDER_REG, minLevel, chf, src); regionId++; paintRectRegion(w-borderSize, w, 0, h, regionId|RC_BORDER_REG, minLevel, chf, src); regionId++; paintRectRegion(0, w, 0, borderSize, regionId|RC_BORDER_REG, minLevel, chf, src); regionId++; paintRectRegion(0, w, h-borderSize, h, regionId|RC_BORDER_REG, minLevel, chf, src); regionId++; rcTimeVal expTime = 0; rcTimeVal floodTime = 0; while (level > minLevel) { level = level >= 2 ? level-2 : 0; rcTimeVal expStartTime = rcGetPerformanceTimer(); // Expand current regions until no empty connected cells found. if (expandRegions(expandIters, level, chf, src, dst, stack) != src) rcSwap(src, dst); expTime += rcGetPerformanceTimer() - expStartTime; rcTimeVal floodStartTime = rcGetPerformanceTimer(); // Mark new regions with IDs. for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { if (chf.spans[i].dist < level || src[i*2] != 0) continue; if (floodRegion(x, y, i, minLevel, level, regionId, chf, src, stack)) regionId++; } } } floodTime += rcGetPerformanceTimer() - floodStartTime; } // Expand current regions until no empty connected cells found. if (expandRegions(expandIters*8, minLevel, chf, src, dst, stack) != src) rcSwap(src, dst); rcTimeVal regEndTime = rcGetPerformanceTimer(); rcTimeVal filterStartTime = rcGetPerformanceTimer(); // Filter out small regions. chf.maxRegions = regionId; if (!filterSmallRegions(minRegionSize, mergeRegionSize, chf.maxRegions, chf, src)) return false; rcTimeVal filterEndTime = rcGetPerformanceTimer(); // Write the result out. for (int i = 0; i < chf.spanCount; ++i) chf.spans[i].reg = src[i*2]; delete [] tmp1; delete [] tmp2; rcTimeVal endTime = rcGetPerformanceTimer(); /* if (rcGetLog()) { rcGetLog()->log(RC_LOG_PROGRESS, "Build regions: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - reg: %.3f ms", rcGetDeltaTimeUsec(regStartTime, regEndTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - exp: %.3f ms", rcGetDeltaTimeUsec(0, expTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - flood: %.3f ms", rcGetDeltaTimeUsec(0, floodTime)/1000.0f); rcGetLog()->log(RC_LOG_PROGRESS, " - filter: %.3f ms", rcGetDeltaTimeUsec(filterStartTime, filterEndTime)/1000.0f); } */ if (rcGetBuildTimes()) { rcGetBuildTimes()->buildRegions += rcGetDeltaTimeUsec(startTime, endTime); rcGetBuildTimes()->buildRegionsReg += rcGetDeltaTimeUsec(regStartTime, regEndTime); rcGetBuildTimes()->buildRegionsExp += rcGetDeltaTimeUsec(0, expTime); rcGetBuildTimes()->buildRegionsFlood += rcGetDeltaTimeUsec(0, floodTime); rcGetBuildTimes()->buildRegionsFilter += rcGetDeltaTimeUsec(filterStartTime, filterEndTime); } return true; }
void TestCase::doTests(dtNavMesh* navmesh) { if (!navmesh) return; resetTimes(); static const int MAX_POLYS = 256; dtPolyRef polys[MAX_POLYS]; float straight[MAX_POLYS*3]; const float polyPickExt[3] = {2,4,2}; for (Test* iter = m_tests; iter; iter = iter->next) { delete [] iter->polys; iter->polys = 0; iter->npolys = 0; delete [] iter->straight; iter->straight = 0; iter->nstraight = 0; dtQueryFilter filter; filter.includeFlags = (unsigned short)iter->includeFlags; filter.excludeFlags = (unsigned short)iter->excludeFlags; // Find start points rcTimeVal findNearestPolyStart = rcGetPerformanceTimer(); dtPolyRef startRef = navmesh->findNearestPoly(iter->spos, polyPickExt, &filter, 0); dtPolyRef endRef = navmesh->findNearestPoly(iter->epos, polyPickExt, &filter, 0); rcTimeVal findNearestPolyEnd = rcGetPerformanceTimer(); iter->findNearestPolyTime += rcGetDeltaTimeUsec(findNearestPolyStart, findNearestPolyEnd); if (!startRef || ! endRef) continue; // Find path rcTimeVal findPathStart = rcGetPerformanceTimer(); iter->npolys = navmesh->findPath(startRef, endRef, iter->spos, iter->epos, &filter, polys, MAX_POLYS); rcTimeVal findPathEnd = rcGetPerformanceTimer(); iter->findPathTime += rcGetDeltaTimeUsec(findPathStart, findPathEnd); // Find straight path if (iter->npolys) { rcTimeVal findStraightPathStart = rcGetPerformanceTimer(); iter->nstraight = navmesh->findStraightPath(iter->spos, iter->epos, polys, iter->npolys, straight, 0, 0, MAX_POLYS); rcTimeVal findStraightPathEnd = rcGetPerformanceTimer(); iter->findStraightPathTime += rcGetDeltaTimeUsec(findStraightPathStart, findStraightPathEnd); } // Copy results if (iter->npolys) { iter->polys = new dtPolyRef[iter->npolys]; memcpy(iter->polys, polys, sizeof(dtPolyRef)*iter->npolys); } if (iter->nstraight) { iter->straight = new float[iter->nstraight*3]; memcpy(iter->straight, straight, sizeof(float)*3*iter->nstraight); } } }
void rcFilterLedgeSpans(const int walkableHeight, const int walkableClimb, rcHeightfield& solid) { rcTimeVal startTime = rcGetPerformanceTimer(); const int w = solid.width; const int h = solid.height; const int MAX_HEIGHT = 0xffff; // Mark border spans. for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { for (rcSpan* s = solid.spans[x + y*w]; s; s = s->next) { // Skip non walkable spans. if ((s->flags & RC_WALKABLE) == 0) continue; const int bot = (int)(s->smax); const int top = s->next ? (int)(s->next->smin) : MAX_HEIGHT; // Find neighbours minimum height. int minh = MAX_HEIGHT; // Min and max height of accessible neighbours. int asmin = s->smax; int asmax = s->smax; for (int dir = 0; dir < 4; ++dir) { int dx = x + rcGetDirOffsetX(dir); int dy = y + rcGetDirOffsetY(dir); // Skip neighbours which are out of bounds. if (dx < 0 || dy < 0 || dx >= w || dy >= h) { minh = rcMin(minh, -walkableClimb - bot); continue; } // From minus infinity to the first span. rcSpan* ns = solid.spans[dx + dy*w]; int nbot = -walkableClimb; int ntop = ns ? (int)ns->smin : MAX_HEIGHT; // Skip neightbour if the gap between the spans is too small. if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight) minh = rcMin(minh, nbot - bot); // Rest of the spans. for (ns = solid.spans[dx + dy*w]; ns; ns = ns->next) { nbot = (int)ns->smax; ntop = ns->next ? (int)ns->next->smin : MAX_HEIGHT; // Skip neightbour if the gap between the spans is too small. if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight) { minh = rcMin(minh, nbot - bot); // Find min/max accessible neighbour height. if (rcAbs(nbot - bot) <= walkableClimb) { if (nbot < asmin) asmin = nbot; if (nbot > asmax) asmax = nbot; } } } } // The current span is close to a ledge if the drop to any // neighbour span is less than the walkableClimb. if (minh < -walkableClimb) s->flags |= RC_LEDGE; // If the difference between all neighbours is too large, // we are at steep slope, mark the span as ledge. if ((asmax - asmin) > walkableClimb) { s->flags |= RC_LEDGE; } } } } rcTimeVal endTime = rcGetPerformanceTimer(); // if (rcGetLog()) // rcGetLog()->log(RC_LOG_PROGRESS, "Filter border: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f); if (rcGetBuildTimes()) rcGetBuildTimes()->filterBorder += rcGetDeltaTimeUsec(startTime, endTime); }
bool rcMedianFilterWalkableArea(rcCompactHeightfield& chf) { const int w = chf.width; const int h = chf.height; rcTimeVal startTime = rcGetPerformanceTimer(); unsigned char* areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP); if (!areas) return false; // Init distance. memset(areas, 0xff, sizeof(unsigned char)*chf.spanCount); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { const rcCompactCell& c = chf.cells[x+y*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { const rcCompactSpan& s = chf.spans[i]; if (chf.areas[i] == RC_NULL_AREA) { areas[i] = chf.areas[i]; continue; } unsigned char nei[9]; for (int j = 0; j < 9; ++j) nei[j] = chf.areas[i]; for (int dir = 0; dir < 4; ++dir) { 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*w].index + rcGetCon(s, dir); if (chf.areas[ai] != RC_NULL_AREA) nei[dir*2+0] = chf.areas[ai]; 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] != RC_NULL_AREA) nei[dir*2+1] = chf.areas[ai2]; } } } insertSort(nei, 9); areas[i] = nei[4]; } } } memcpy(chf.areas, areas, sizeof(unsigned char)*chf.spanCount); rcFree(areas); rcTimeVal endTime = rcGetPerformanceTimer(); if (rcGetBuildTimes()) { rcGetBuildTimes()->filterMedian += rcGetDeltaTimeUsec(startTime, endTime); } return true; }