DetourNavMeshQuery.cpp 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <math.h>
  19. #include <float.h>
  20. #include <string.h>
  21. #include "DetourNavMeshQuery.h"
  22. #include "DetourNavMesh.h"
  23. #include "DetourNode.h"
  24. #include "DetourCommon.h"
  25. #include "DetourAlloc.h"
  26. #include "DetourAssert.h"
  27. #include <new>
  28. /// @class dtQueryFilter
  29. ///
  30. /// <b>The Default Implementation</b>
  31. ///
  32. /// At construction: All area costs default to 1.0. All flags are included
  33. /// and none are excluded.
  34. ///
  35. /// If a polygon has both an include and an exclude flag, it will be excluded.
  36. ///
  37. /// The way filtering works, a navigation mesh polygon must have at least one flag
  38. /// set to ever be considered by a query. So a polygon with no flags will never
  39. /// be considered.
  40. ///
  41. /// Setting the include flags to 0 will result in all polygons being excluded.
  42. ///
  43. /// <b>Custom Implementations</b>
  44. ///
  45. /// DT_VIRTUAL_QUERYFILTER must be defined in order to extend this class.
  46. ///
  47. /// Implement a custom query filter by overriding the virtual passFilter()
  48. /// and getCost() functions. If this is done, both functions should be as
  49. /// fast as possible. Use cached local copies of data rather than accessing
  50. /// your own objects where possible.
  51. ///
  52. /// Custom implementations do not need to adhere to the flags or cost logic
  53. /// used by the default implementation.
  54. ///
  55. /// In order for A* searches to work properly, the cost should be proportional to
  56. /// the travel distance. Implementing a cost modifier less than 1.0 is likely
  57. /// to lead to problems during pathfinding.
  58. ///
  59. /// @see dtNavMeshQuery
  60. dtQueryFilter::dtQueryFilter() :
  61. m_includeFlags(0xffff),
  62. m_excludeFlags(0)
  63. {
  64. for (int i = 0; i < DT_MAX_AREAS; ++i)
  65. m_areaCost[i] = 1.0f;
  66. }
  67. #ifdef DT_VIRTUAL_QUERYFILTER
  68. bool dtQueryFilter::passFilter(const dtPolyRef /*ref*/,
  69. const dtMeshTile* /*tile*/,
  70. const dtPoly* poly) const
  71. {
  72. return (poly->flags & m_includeFlags) != 0 && (poly->flags & m_excludeFlags) == 0;
  73. }
  74. float dtQueryFilter::getCost(const float* pa, const float* pb,
  75. const dtPolyRef /*prevRef*/, const dtMeshTile* /*prevTile*/, const dtPoly* /*prevPoly*/,
  76. const dtPolyRef /*curRef*/, const dtMeshTile* /*curTile*/, const dtPoly* curPoly,
  77. const dtPolyRef /*nextRef*/, const dtMeshTile* /*nextTile*/, const dtPoly* /*nextPoly*/) const
  78. {
  79. return dtVdist(pa, pb) * m_areaCost[curPoly->getArea()];
  80. }
  81. #else
  82. inline bool dtQueryFilter::passFilter(const dtPolyRef /*ref*/,
  83. const dtMeshTile* /*tile*/,
  84. const dtPoly* poly) const
  85. {
  86. return (poly->flags & m_includeFlags) != 0 && (poly->flags & m_excludeFlags) == 0;
  87. }
  88. inline float dtQueryFilter::getCost(const float* pa, const float* pb,
  89. const dtPolyRef /*prevRef*/, const dtMeshTile* /*prevTile*/, const dtPoly* /*prevPoly*/,
  90. const dtPolyRef /*curRef*/, const dtMeshTile* /*curTile*/, const dtPoly* curPoly,
  91. const dtPolyRef /*nextRef*/, const dtMeshTile* /*nextTile*/, const dtPoly* /*nextPoly*/) const
  92. {
  93. return dtVdist(pa, pb) * m_areaCost[curPoly->getArea()];
  94. }
  95. #endif
  96. static const float H_SCALE = 0.999f; // Search heuristic scale.
  97. dtNavMeshQuery* dtAllocNavMeshQuery()
  98. {
  99. void* mem = dtAlloc(sizeof(dtNavMeshQuery), DT_ALLOC_PERM);
  100. if (!mem) return 0;
  101. return new(mem) dtNavMeshQuery;
  102. }
  103. void dtFreeNavMeshQuery(dtNavMeshQuery* navmesh)
  104. {
  105. if (!navmesh) return;
  106. navmesh->~dtNavMeshQuery();
  107. dtFree(navmesh);
  108. }
  109. //////////////////////////////////////////////////////////////////////////////////////////
  110. /// @class dtNavMeshQuery
  111. ///
  112. /// For methods that support undersized buffers, if the buffer is too small
  113. /// to hold the entire result set the return status of the method will include
  114. /// the #DT_BUFFER_TOO_SMALL flag.
  115. ///
  116. /// Constant member functions can be used by multiple clients without side
  117. /// effects. (E.g. No change to the closed list. No impact on an in-progress
  118. /// sliced path query. Etc.)
  119. ///
  120. /// Walls and portals: A @e wall is a polygon segment that is
  121. /// considered impassable. A @e portal is a passable segment between polygons.
  122. /// A portal may be treated as a wall based on the dtQueryFilter used for a query.
  123. ///
  124. /// @see dtNavMesh, dtQueryFilter, #dtAllocNavMeshQuery(), #dtAllocNavMeshQuery()
  125. dtNavMeshQuery::dtNavMeshQuery() :
  126. m_nav(0),
  127. m_tinyNodePool(0),
  128. m_nodePool(0),
  129. m_openList(0)
  130. {
  131. memset(&m_query, 0, sizeof(dtQueryData));
  132. }
  133. dtNavMeshQuery::~dtNavMeshQuery()
  134. {
  135. if (m_tinyNodePool)
  136. m_tinyNodePool->~dtNodePool();
  137. if (m_nodePool)
  138. m_nodePool->~dtNodePool();
  139. if (m_openList)
  140. m_openList->~dtNodeQueue();
  141. dtFree(m_tinyNodePool);
  142. dtFree(m_nodePool);
  143. dtFree(m_openList);
  144. }
  145. /// @par
  146. ///
  147. /// Must be the first function called after construction, before other
  148. /// functions are used.
  149. ///
  150. /// This function can be used multiple times.
  151. dtStatus dtNavMeshQuery::init(const dtNavMesh* nav, const int maxNodes)
  152. {
  153. m_nav = nav;
  154. if (!m_nodePool || m_nodePool->getMaxNodes() < maxNodes)
  155. {
  156. if (m_nodePool)
  157. {
  158. m_nodePool->~dtNodePool();
  159. dtFree(m_nodePool);
  160. m_nodePool = 0;
  161. }
  162. m_nodePool = new (dtAlloc(sizeof(dtNodePool), DT_ALLOC_PERM)) dtNodePool(maxNodes, dtNextPow2(maxNodes/4));
  163. if (!m_nodePool)
  164. return DT_FAILURE | DT_OUT_OF_MEMORY;
  165. }
  166. else
  167. {
  168. m_nodePool->clear();
  169. }
  170. if (!m_tinyNodePool)
  171. {
  172. m_tinyNodePool = new (dtAlloc(sizeof(dtNodePool), DT_ALLOC_PERM)) dtNodePool(64, 32);
  173. if (!m_tinyNodePool)
  174. return DT_FAILURE | DT_OUT_OF_MEMORY;
  175. }
  176. else
  177. {
  178. m_tinyNodePool->clear();
  179. }
  180. // TODO: check the open list size too.
  181. if (!m_openList || m_openList->getCapacity() < maxNodes)
  182. {
  183. if (m_openList)
  184. {
  185. m_openList->~dtNodeQueue();
  186. dtFree(m_openList);
  187. m_openList = 0;
  188. }
  189. m_openList = new (dtAlloc(sizeof(dtNodeQueue), DT_ALLOC_PERM)) dtNodeQueue(maxNodes);
  190. if (!m_openList)
  191. return DT_FAILURE | DT_OUT_OF_MEMORY;
  192. }
  193. else
  194. {
  195. m_openList->clear();
  196. }
  197. return DT_SUCCESS;
  198. }
  199. dtStatus dtNavMeshQuery::findRandomPoint(const dtQueryFilter* filter, float (*frand)(),
  200. dtPolyRef* randomRef, float* randomPt) const
  201. {
  202. dtAssert(m_nav);
  203. // Randomly pick one tile. Assume that all tiles cover roughly the same area.
  204. const dtMeshTile* tile = 0;
  205. float tsum = 0.0f;
  206. for (int i = 0; i < m_nav->getMaxTiles(); i++)
  207. {
  208. const dtMeshTile* t = m_nav->getTile(i);
  209. if (!t || !t->header) continue;
  210. // Choose random tile using reservoi sampling.
  211. const float area = 1.0f; // Could be tile area too.
  212. tsum += area;
  213. const float u = frand();
  214. if (u*tsum <= area)
  215. tile = t;
  216. }
  217. if (!tile)
  218. return DT_FAILURE;
  219. // Randomly pick one polygon weighted by polygon area.
  220. const dtPoly* poly = 0;
  221. dtPolyRef polyRef = 0;
  222. const dtPolyRef base = m_nav->getPolyRefBase(tile);
  223. float areaSum = 0.0f;
  224. for (int i = 0; i < tile->header->polyCount; ++i)
  225. {
  226. const dtPoly* p = &tile->polys[i];
  227. // Do not return off-mesh connection polygons.
  228. if (p->getType() != DT_POLYTYPE_GROUND)
  229. continue;
  230. // Must pass filter
  231. const dtPolyRef ref = base | (dtPolyRef)i;
  232. if (!filter->passFilter(ref, tile, p))
  233. continue;
  234. // Calc area of the polygon.
  235. float polyArea = 0.0f;
  236. for (int j = 2; j < p->vertCount; ++j)
  237. {
  238. const float* va = &tile->verts[p->verts[0]*3];
  239. const float* vb = &tile->verts[p->verts[j-1]*3];
  240. const float* vc = &tile->verts[p->verts[j]*3];
  241. polyArea += dtTriArea2D(va,vb,vc);
  242. }
  243. // Choose random polygon weighted by area, using reservoi sampling.
  244. areaSum += polyArea;
  245. const float u = frand();
  246. if (u*areaSum <= polyArea)
  247. {
  248. poly = p;
  249. polyRef = ref;
  250. }
  251. }
  252. if (!poly)
  253. return DT_FAILURE;
  254. // Randomly pick point on polygon.
  255. const float* v = &tile->verts[poly->verts[0]*3];
  256. float verts[3*DT_VERTS_PER_POLYGON];
  257. float areas[DT_VERTS_PER_POLYGON];
  258. dtVcopy(&verts[0*3],v);
  259. for (int j = 1; j < poly->vertCount; ++j)
  260. {
  261. v = &tile->verts[poly->verts[j]*3];
  262. dtVcopy(&verts[j*3],v);
  263. }
  264. const float s = frand();
  265. const float t = frand();
  266. float pt[3];
  267. dtRandomPointInConvexPoly(verts, poly->vertCount, areas, s, t, pt);
  268. float h = 0.0f;
  269. dtStatus status = getPolyHeight(polyRef, pt, &h);
  270. if (dtStatusFailed(status))
  271. return status;
  272. pt[1] = h;
  273. dtVcopy(randomPt, pt);
  274. *randomRef = polyRef;
  275. return DT_SUCCESS;
  276. }
  277. dtStatus dtNavMeshQuery::findRandomPointAroundCircle(dtPolyRef startRef, const float* centerPos, const float radius,
  278. const dtQueryFilter* filter, float (*frand)(),
  279. dtPolyRef* randomRef, float* randomPt) const
  280. {
  281. dtAssert(m_nav);
  282. dtAssert(m_nodePool);
  283. dtAssert(m_openList);
  284. // Validate input
  285. if (!startRef || !m_nav->isValidPolyRef(startRef))
  286. return DT_FAILURE | DT_INVALID_PARAM;
  287. const dtMeshTile* startTile = 0;
  288. const dtPoly* startPoly = 0;
  289. m_nav->getTileAndPolyByRefUnsafe(startRef, &startTile, &startPoly);
  290. if (!filter->passFilter(startRef, startTile, startPoly))
  291. return DT_FAILURE | DT_INVALID_PARAM;
  292. m_nodePool->clear();
  293. m_openList->clear();
  294. dtNode* startNode = m_nodePool->getNode(startRef);
  295. dtVcopy(startNode->pos, centerPos);
  296. startNode->pidx = 0;
  297. startNode->cost = 0;
  298. startNode->total = 0;
  299. startNode->id = startRef;
  300. startNode->flags = DT_NODE_OPEN;
  301. m_openList->push(startNode);
  302. dtStatus status = DT_SUCCESS;
  303. const float radiusSqr = dtSqr(radius);
  304. float areaSum = 0.0f;
  305. const dtMeshTile* randomTile = 0;
  306. const dtPoly* randomPoly = 0;
  307. dtPolyRef randomPolyRef = 0;
  308. while (!m_openList->empty())
  309. {
  310. dtNode* bestNode = m_openList->pop();
  311. bestNode->flags &= ~DT_NODE_OPEN;
  312. bestNode->flags |= DT_NODE_CLOSED;
  313. // Get poly and tile.
  314. // The API input has been cheked already, skip checking internal data.
  315. const dtPolyRef bestRef = bestNode->id;
  316. const dtMeshTile* bestTile = 0;
  317. const dtPoly* bestPoly = 0;
  318. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  319. // Place random locations on on ground.
  320. if (bestPoly->getType() == DT_POLYTYPE_GROUND)
  321. {
  322. // Calc area of the polygon.
  323. float polyArea = 0.0f;
  324. for (int j = 2; j < bestPoly->vertCount; ++j)
  325. {
  326. const float* va = &bestTile->verts[bestPoly->verts[0]*3];
  327. const float* vb = &bestTile->verts[bestPoly->verts[j-1]*3];
  328. const float* vc = &bestTile->verts[bestPoly->verts[j]*3];
  329. polyArea += dtTriArea2D(va,vb,vc);
  330. }
  331. // Choose random polygon weighted by area, using reservoi sampling.
  332. areaSum += polyArea;
  333. const float u = frand();
  334. if (u*areaSum <= polyArea)
  335. {
  336. randomTile = bestTile;
  337. randomPoly = bestPoly;
  338. randomPolyRef = bestRef;
  339. }
  340. }
  341. // Get parent poly and tile.
  342. dtPolyRef parentRef = 0;
  343. const dtMeshTile* parentTile = 0;
  344. const dtPoly* parentPoly = 0;
  345. if (bestNode->pidx)
  346. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  347. if (parentRef)
  348. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  349. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  350. {
  351. const dtLink* link = &bestTile->links[i];
  352. dtPolyRef neighbourRef = link->ref;
  353. // Skip invalid neighbours and do not follow back to parent.
  354. if (!neighbourRef || neighbourRef == parentRef)
  355. continue;
  356. // Expand to neighbour
  357. const dtMeshTile* neighbourTile = 0;
  358. const dtPoly* neighbourPoly = 0;
  359. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  360. // Do not advance if the polygon is excluded by the filter.
  361. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  362. continue;
  363. // Find edge and calc distance to the edge.
  364. float va[3], vb[3];
  365. if (!getPortalPoints(bestRef, bestPoly, bestTile, neighbourRef, neighbourPoly, neighbourTile, va, vb))
  366. continue;
  367. // If the circle is not touching the next polygon, skip it.
  368. float tseg;
  369. float distSqr = dtDistancePtSegSqr2D(centerPos, va, vb, tseg);
  370. if (distSqr > radiusSqr)
  371. continue;
  372. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  373. if (!neighbourNode)
  374. {
  375. status |= DT_OUT_OF_NODES;
  376. continue;
  377. }
  378. if (neighbourNode->flags & DT_NODE_CLOSED)
  379. continue;
  380. // Cost
  381. if (neighbourNode->flags == 0)
  382. dtVlerp(neighbourNode->pos, va, vb, 0.5f);
  383. const float total = bestNode->total + dtVdist(bestNode->pos, neighbourNode->pos);
  384. // The node is already in open list and the new result is worse, skip.
  385. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  386. continue;
  387. neighbourNode->id = neighbourRef;
  388. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  389. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  390. neighbourNode->total = total;
  391. if (neighbourNode->flags & DT_NODE_OPEN)
  392. {
  393. m_openList->modify(neighbourNode);
  394. }
  395. else
  396. {
  397. neighbourNode->flags = DT_NODE_OPEN;
  398. m_openList->push(neighbourNode);
  399. }
  400. }
  401. }
  402. if (!randomPoly)
  403. return DT_FAILURE;
  404. // Randomly pick point on polygon.
  405. const float* v = &randomTile->verts[randomPoly->verts[0]*3];
  406. float verts[3*DT_VERTS_PER_POLYGON];
  407. float areas[DT_VERTS_PER_POLYGON];
  408. dtVcopy(&verts[0*3],v);
  409. for (int j = 1; j < randomPoly->vertCount; ++j)
  410. {
  411. v = &randomTile->verts[randomPoly->verts[j]*3];
  412. dtVcopy(&verts[j*3],v);
  413. }
  414. const float s = frand();
  415. const float t = frand();
  416. float pt[3];
  417. dtRandomPointInConvexPoly(verts, randomPoly->vertCount, areas, s, t, pt);
  418. float h = 0.0f;
  419. dtStatus stat = getPolyHeight(randomPolyRef, pt, &h);
  420. if (dtStatusFailed(status))
  421. return stat;
  422. pt[1] = h;
  423. dtVcopy(randomPt, pt);
  424. *randomRef = randomPolyRef;
  425. return DT_SUCCESS;
  426. }
  427. //////////////////////////////////////////////////////////////////////////////////////////
  428. /// @par
  429. ///
  430. /// Uses the detail polygons to find the surface height. (Most accurate.)
  431. ///
  432. /// @p pos does not have to be within the bounds of the polygon or navigation mesh.
  433. ///
  434. /// See closestPointOnPolyBoundary() for a limited but faster option.
  435. ///
  436. dtStatus dtNavMeshQuery::closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest) const
  437. {
  438. dtAssert(m_nav);
  439. const dtMeshTile* tile = 0;
  440. const dtPoly* poly = 0;
  441. if (dtStatusFailed(m_nav->getTileAndPolyByRef(ref, &tile, &poly)))
  442. return DT_FAILURE | DT_INVALID_PARAM;
  443. if (!tile)
  444. return DT_FAILURE | DT_INVALID_PARAM;
  445. closestPointOnPolyInTile(tile, poly, pos, closest);
  446. return DT_SUCCESS;
  447. }
  448. void dtNavMeshQuery::closestPointOnPolyInTile(const dtMeshTile* tile, const dtPoly* poly,
  449. const float* pos, float* closest) const
  450. {
  451. // Off-mesh connections don't have detail polygons.
  452. if (poly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  453. {
  454. const float* v0 = &tile->verts[poly->verts[0]*3];
  455. const float* v1 = &tile->verts[poly->verts[1]*3];
  456. const float d0 = dtVdist(pos, v0);
  457. const float d1 = dtVdist(pos, v1);
  458. const float u = d0 / (d0+d1);
  459. dtVlerp(closest, v0, v1, u);
  460. return;
  461. }
  462. const unsigned int ip = (unsigned int)(poly - tile->polys);
  463. const dtPolyDetail* pd = &tile->detailMeshes[ip];
  464. // Clamp point to be inside the polygon.
  465. float verts[DT_VERTS_PER_POLYGON*3];
  466. float edged[DT_VERTS_PER_POLYGON];
  467. float edget[DT_VERTS_PER_POLYGON];
  468. const int nv = poly->vertCount;
  469. for (int i = 0; i < nv; ++i)
  470. dtVcopy(&verts[i*3], &tile->verts[poly->verts[i]*3]);
  471. dtVcopy(closest, pos);
  472. if (!dtDistancePtPolyEdgesSqr(pos, verts, nv, edged, edget))
  473. {
  474. // Point is outside the polygon, dtClamp to nearest edge.
  475. float dmin = FLT_MAX;
  476. int imin = -1;
  477. for (int i = 0; i < nv; ++i)
  478. {
  479. if (edged[i] < dmin)
  480. {
  481. dmin = edged[i];
  482. imin = i;
  483. }
  484. }
  485. const float* va = &verts[imin*3];
  486. const float* vb = &verts[((imin+1)%nv)*3];
  487. dtVlerp(closest, va, vb, edget[imin]);
  488. }
  489. // Find height at the location.
  490. for (int j = 0; j < pd->triCount; ++j)
  491. {
  492. const unsigned char* t = &tile->detailTris[(pd->triBase+j)*4];
  493. const float* v[3];
  494. for (int k = 0; k < 3; ++k)
  495. {
  496. if (t[k] < poly->vertCount)
  497. v[k] = &tile->verts[poly->verts[t[k]]*3];
  498. else
  499. v[k] = &tile->detailVerts[(pd->vertBase+(t[k]-poly->vertCount))*3];
  500. }
  501. float h;
  502. if (dtClosestHeightPointTriangle(pos, v[0], v[1], v[2], h))
  503. {
  504. closest[1] = h;
  505. break;
  506. }
  507. }
  508. /* float closestDistSqr = FLT_MAX;
  509. for (int j = 0; j < pd->triCount; ++j)
  510. {
  511. const unsigned char* t = &tile->detailTris[(pd->triBase+j)*4];
  512. const float* v[3];
  513. for (int k = 0; k < 3; ++k)
  514. {
  515. if (t[k] < poly->vertCount)
  516. v[k] = &tile->verts[poly->verts[t[k]]*3];
  517. else
  518. v[k] = &tile->detailVerts[(pd->vertBase+(t[k]-poly->vertCount))*3];
  519. }
  520. float pt[3];
  521. dtClosestPtPointTriangle(pt, pos, v[0], v[1], v[2]);
  522. float d = dtVdistSqr(pos, pt);
  523. if (d < closestDistSqr)
  524. {
  525. dtVcopy(closest, pt);
  526. closestDistSqr = d;
  527. }
  528. }*/
  529. }
  530. /// @par
  531. ///
  532. /// Much faster than closestPointOnPoly().
  533. ///
  534. /// If the provided position lies within the polygon's xz-bounds (above or below),
  535. /// then @p pos and @p closest will be equal.
  536. ///
  537. /// The height of @p closest will be the polygon boundary. The height detail is not used.
  538. ///
  539. /// @p pos does not have to be within the bounds of the polybon or the navigation mesh.
  540. ///
  541. dtStatus dtNavMeshQuery::closestPointOnPolyBoundary(dtPolyRef ref, const float* pos, float* closest) const
  542. {
  543. dtAssert(m_nav);
  544. const dtMeshTile* tile = 0;
  545. const dtPoly* poly = 0;
  546. if (dtStatusFailed(m_nav->getTileAndPolyByRef(ref, &tile, &poly)))
  547. return DT_FAILURE | DT_INVALID_PARAM;
  548. // Collect vertices.
  549. float verts[DT_VERTS_PER_POLYGON*3];
  550. float edged[DT_VERTS_PER_POLYGON];
  551. float edget[DT_VERTS_PER_POLYGON];
  552. int nv = 0;
  553. for (int i = 0; i < (int)poly->vertCount; ++i)
  554. {
  555. dtVcopy(&verts[nv*3], &tile->verts[poly->verts[i]*3]);
  556. nv++;
  557. }
  558. bool inside = dtDistancePtPolyEdgesSqr(pos, verts, nv, edged, edget);
  559. if (inside)
  560. {
  561. // Point is inside the polygon, return the point.
  562. dtVcopy(closest, pos);
  563. }
  564. else
  565. {
  566. // Point is outside the polygon, dtClamp to nearest edge.
  567. float dmin = FLT_MAX;
  568. int imin = -1;
  569. for (int i = 0; i < nv; ++i)
  570. {
  571. if (edged[i] < dmin)
  572. {
  573. dmin = edged[i];
  574. imin = i;
  575. }
  576. }
  577. const float* va = &verts[imin*3];
  578. const float* vb = &verts[((imin+1)%nv)*3];
  579. dtVlerp(closest, va, vb, edget[imin]);
  580. }
  581. return DT_SUCCESS;
  582. }
  583. /// @par
  584. ///
  585. /// Will return #DT_FAILURE if the provided position is outside the xz-bounds
  586. /// of the polygon.
  587. ///
  588. dtStatus dtNavMeshQuery::getPolyHeight(dtPolyRef ref, const float* pos, float* height) const
  589. {
  590. dtAssert(m_nav);
  591. const dtMeshTile* tile = 0;
  592. const dtPoly* poly = 0;
  593. if (dtStatusFailed(m_nav->getTileAndPolyByRef(ref, &tile, &poly)))
  594. return DT_FAILURE | DT_INVALID_PARAM;
  595. if (poly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  596. {
  597. const float* v0 = &tile->verts[poly->verts[0]*3];
  598. const float* v1 = &tile->verts[poly->verts[1]*3];
  599. const float d0 = dtVdist(pos, v0);
  600. const float d1 = dtVdist(pos, v1);
  601. const float u = d0 / (d0+d1);
  602. if (height)
  603. *height = v0[1] + (v1[1] - v0[1]) * u;
  604. return DT_SUCCESS;
  605. }
  606. else
  607. {
  608. const unsigned int ip = (unsigned int)(poly - tile->polys);
  609. const dtPolyDetail* pd = &tile->detailMeshes[ip];
  610. for (int j = 0; j < pd->triCount; ++j)
  611. {
  612. const unsigned char* t = &tile->detailTris[(pd->triBase+j)*4];
  613. const float* v[3];
  614. for (int k = 0; k < 3; ++k)
  615. {
  616. if (t[k] < poly->vertCount)
  617. v[k] = &tile->verts[poly->verts[t[k]]*3];
  618. else
  619. v[k] = &tile->detailVerts[(pd->vertBase+(t[k]-poly->vertCount))*3];
  620. }
  621. float h;
  622. if (dtClosestHeightPointTriangle(pos, v[0], v[1], v[2], h))
  623. {
  624. if (height)
  625. *height = h;
  626. return DT_SUCCESS;
  627. }
  628. }
  629. }
  630. return DT_FAILURE | DT_INVALID_PARAM;
  631. }
  632. /// @par
  633. ///
  634. /// @note If the search box does not intersect any polygons the search will
  635. /// return #DT_SUCCESS, but @p nearestRef will be zero. So if in doubt, check
  636. /// @p nearestRef before using @p nearestPt.
  637. ///
  638. /// @warning This function is not suitable for large area searches. If the search
  639. /// extents overlaps more than 128 polygons it may return an invalid result.
  640. ///
  641. dtStatus dtNavMeshQuery::findNearestPoly(const float* center, const float* extents,
  642. const dtQueryFilter* filter,
  643. dtPolyRef* nearestRef, float* nearestPt) const
  644. {
  645. dtAssert(m_nav);
  646. *nearestRef = 0;
  647. // Get nearby polygons from proximity grid.
  648. dtPolyRef polys[128];
  649. int polyCount = 0;
  650. if (dtStatusFailed(queryPolygons(center, extents, filter, polys, &polyCount, 128)))
  651. return DT_FAILURE | DT_INVALID_PARAM;
  652. // Find nearest polygon amongst the nearby polygons.
  653. dtPolyRef nearest = 0;
  654. float nearestDistanceSqr = FLT_MAX;
  655. for (int i = 0; i < polyCount; ++i)
  656. {
  657. dtPolyRef ref = polys[i];
  658. float closestPtPoly[3];
  659. closestPointOnPoly(ref, center, closestPtPoly);
  660. float d = dtVdistSqr(center, closestPtPoly);
  661. if (d < nearestDistanceSqr)
  662. {
  663. if (nearestPt)
  664. dtVcopy(nearestPt, closestPtPoly);
  665. nearestDistanceSqr = d;
  666. nearest = ref;
  667. }
  668. }
  669. if (nearestRef)
  670. *nearestRef = nearest;
  671. return DT_SUCCESS;
  672. }
  673. dtPolyRef dtNavMeshQuery::findNearestPolyInTile(const dtMeshTile* tile, const float* center, const float* extents,
  674. const dtQueryFilter* filter, float* nearestPt) const
  675. {
  676. dtAssert(m_nav);
  677. float bmin[3], bmax[3];
  678. dtVsub(bmin, center, extents);
  679. dtVadd(bmax, center, extents);
  680. // Get nearby polygons from proximity grid.
  681. dtPolyRef polys[128];
  682. int polyCount = queryPolygonsInTile(tile, bmin, bmax, filter, polys, 128);
  683. // Find nearest polygon amongst the nearby polygons.
  684. dtPolyRef nearest = 0;
  685. float nearestDistanceSqr = FLT_MAX;
  686. for (int i = 0; i < polyCount; ++i)
  687. {
  688. dtPolyRef ref = polys[i];
  689. const dtPoly* poly = &tile->polys[m_nav->decodePolyIdPoly(ref)];
  690. float closestPtPoly[3];
  691. closestPointOnPolyInTile(tile, poly, center, closestPtPoly);
  692. float d = dtVdistSqr(center, closestPtPoly);
  693. if (d < nearestDistanceSqr)
  694. {
  695. if (nearestPt)
  696. dtVcopy(nearestPt, closestPtPoly);
  697. nearestDistanceSqr = d;
  698. nearest = ref;
  699. }
  700. }
  701. return nearest;
  702. }
  703. int dtNavMeshQuery::queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax,
  704. const dtQueryFilter* filter,
  705. dtPolyRef* polys, const int maxPolys) const
  706. {
  707. dtAssert(m_nav);
  708. if (tile->bvTree)
  709. {
  710. const dtBVNode* node = &tile->bvTree[0];
  711. const dtBVNode* end = &tile->bvTree[tile->header->bvNodeCount];
  712. const float* tbmin = tile->header->bmin;
  713. const float* tbmax = tile->header->bmax;
  714. const float qfac = tile->header->bvQuantFactor;
  715. // Calculate quantized box
  716. unsigned short bmin[3], bmax[3];
  717. // dtClamp query box to world box.
  718. float minx = dtClamp(qmin[0], tbmin[0], tbmax[0]) - tbmin[0];
  719. float miny = dtClamp(qmin[1], tbmin[1], tbmax[1]) - tbmin[1];
  720. float minz = dtClamp(qmin[2], tbmin[2], tbmax[2]) - tbmin[2];
  721. float maxx = dtClamp(qmax[0], tbmin[0], tbmax[0]) - tbmin[0];
  722. float maxy = dtClamp(qmax[1], tbmin[1], tbmax[1]) - tbmin[1];
  723. float maxz = dtClamp(qmax[2], tbmin[2], tbmax[2]) - tbmin[2];
  724. // Quantize
  725. bmin[0] = (unsigned short)(qfac * minx) & 0xfffe;
  726. bmin[1] = (unsigned short)(qfac * miny) & 0xfffe;
  727. bmin[2] = (unsigned short)(qfac * minz) & 0xfffe;
  728. bmax[0] = (unsigned short)(qfac * maxx + 1) | 1;
  729. bmax[1] = (unsigned short)(qfac * maxy + 1) | 1;
  730. bmax[2] = (unsigned short)(qfac * maxz + 1) | 1;
  731. // Traverse tree
  732. const dtPolyRef base = m_nav->getPolyRefBase(tile);
  733. int n = 0;
  734. while (node < end)
  735. {
  736. const bool overlap = dtOverlapQuantBounds(bmin, bmax, node->bmin, node->bmax);
  737. const bool isLeafNode = node->i >= 0;
  738. if (isLeafNode && overlap)
  739. {
  740. dtPolyRef ref = base | (dtPolyRef)node->i;
  741. if (filter->passFilter(ref, tile, &tile->polys[node->i]))
  742. {
  743. if (n < maxPolys)
  744. polys[n++] = ref;
  745. }
  746. }
  747. if (overlap || isLeafNode)
  748. node++;
  749. else
  750. {
  751. const int escapeIndex = -node->i;
  752. node += escapeIndex;
  753. }
  754. }
  755. return n;
  756. }
  757. else
  758. {
  759. float bmin[3], bmax[3];
  760. int n = 0;
  761. const dtPolyRef base = m_nav->getPolyRefBase(tile);
  762. for (int i = 0; i < tile->header->polyCount; ++i)
  763. {
  764. const dtPoly* p = &tile->polys[i];
  765. // Do not return off-mesh connection polygons.
  766. if (p->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  767. continue;
  768. // Must pass filter
  769. const dtPolyRef ref = base | (dtPolyRef)i;
  770. if (!filter->passFilter(ref, tile, p))
  771. continue;
  772. // Calc polygon bounds.
  773. const float* v = &tile->verts[p->verts[0]*3];
  774. dtVcopy(bmin, v);
  775. dtVcopy(bmax, v);
  776. for (int j = 1; j < p->vertCount; ++j)
  777. {
  778. v = &tile->verts[p->verts[j]*3];
  779. dtVmin(bmin, v);
  780. dtVmax(bmax, v);
  781. }
  782. if (dtOverlapBounds(qmin,qmax, bmin,bmax))
  783. {
  784. if (n < maxPolys)
  785. polys[n++] = ref;
  786. }
  787. }
  788. return n;
  789. }
  790. }
  791. /// @par
  792. ///
  793. /// If no polygons are found, the function will return #DT_SUCCESS with a
  794. /// @p polyCount of zero.
  795. ///
  796. /// If @p polys is too small to hold the entire result set, then the array will
  797. /// be filled to capacity. The method of choosing which polygons from the
  798. /// full set are included in the partial result set is undefined.
  799. ///
  800. dtStatus dtNavMeshQuery::queryPolygons(const float* center, const float* extents,
  801. const dtQueryFilter* filter,
  802. dtPolyRef* polys, int* polyCount, const int maxPolys) const
  803. {
  804. dtAssert(m_nav);
  805. float bmin[3], bmax[3];
  806. dtVsub(bmin, center, extents);
  807. dtVadd(bmax, center, extents);
  808. // Find tiles the query touches.
  809. int minx, miny, maxx, maxy;
  810. m_nav->calcTileLoc(bmin, &minx, &miny);
  811. m_nav->calcTileLoc(bmax, &maxx, &maxy);
  812. static const int MAX_NEIS = 32;
  813. const dtMeshTile* neis[MAX_NEIS];
  814. int n = 0;
  815. for (int y = miny; y <= maxy; ++y)
  816. {
  817. for (int x = minx; x <= maxx; ++x)
  818. {
  819. const int nneis = m_nav->getTilesAt(x,y,neis,MAX_NEIS);
  820. for (int j = 0; j < nneis; ++j)
  821. {
  822. n += queryPolygonsInTile(neis[j], bmin, bmax, filter, polys+n, maxPolys-n);
  823. if (n >= maxPolys)
  824. {
  825. *polyCount = n;
  826. return DT_SUCCESS | DT_BUFFER_TOO_SMALL;
  827. }
  828. }
  829. }
  830. }
  831. *polyCount = n;
  832. return DT_SUCCESS;
  833. }
  834. /// @par
  835. ///
  836. /// If the end polygon cannot be reached through the navigation graph,
  837. /// the last polygon in the path will be the nearest the end polygon.
  838. ///
  839. /// If the path array is to small to hold the full result, it will be filled as
  840. /// far as possible from the start polygon toward the end polygon.
  841. ///
  842. /// The start and end positions are used to calculate traversal costs.
  843. /// (The y-values impact the result.)
  844. ///
  845. dtStatus dtNavMeshQuery::findPath(dtPolyRef startRef, dtPolyRef endRef,
  846. const float* startPos, const float* endPos,
  847. const dtQueryFilter* filter,
  848. dtPolyRef* path, int* pathCount, const int maxPath) const
  849. {
  850. dtAssert(m_nav);
  851. dtAssert(m_nodePool);
  852. dtAssert(m_openList);
  853. *pathCount = 0;
  854. if (!startRef || !endRef)
  855. return DT_FAILURE | DT_INVALID_PARAM;
  856. if (!maxPath)
  857. return DT_FAILURE | DT_INVALID_PARAM;
  858. // Validate input
  859. if (!m_nav->isValidPolyRef(startRef) || !m_nav->isValidPolyRef(endRef))
  860. return DT_FAILURE | DT_INVALID_PARAM;
  861. if (startRef == endRef)
  862. {
  863. path[0] = startRef;
  864. *pathCount = 1;
  865. return DT_SUCCESS;
  866. }
  867. m_nodePool->clear();
  868. m_openList->clear();
  869. dtNode* startNode = m_nodePool->getNode(startRef);
  870. dtVcopy(startNode->pos, startPos);
  871. startNode->pidx = 0;
  872. startNode->cost = 0;
  873. startNode->total = dtVdist(startPos, endPos) * H_SCALE;
  874. startNode->id = startRef;
  875. startNode->flags = DT_NODE_OPEN;
  876. m_openList->push(startNode);
  877. dtNode* lastBestNode = startNode;
  878. float lastBestNodeCost = startNode->total;
  879. dtStatus status = DT_SUCCESS;
  880. while (!m_openList->empty())
  881. {
  882. // Remove node from open list and put it in closed list.
  883. dtNode* bestNode = m_openList->pop();
  884. bestNode->flags &= ~DT_NODE_OPEN;
  885. bestNode->flags |= DT_NODE_CLOSED;
  886. // Reached the goal, stop searching.
  887. if (bestNode->id == endRef)
  888. {
  889. lastBestNode = bestNode;
  890. break;
  891. }
  892. // Get current poly and tile.
  893. // The API input has been cheked already, skip checking internal data.
  894. const dtPolyRef bestRef = bestNode->id;
  895. const dtMeshTile* bestTile = 0;
  896. const dtPoly* bestPoly = 0;
  897. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  898. // Get parent poly and tile.
  899. dtPolyRef parentRef = 0;
  900. const dtMeshTile* parentTile = 0;
  901. const dtPoly* parentPoly = 0;
  902. if (bestNode->pidx)
  903. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  904. if (parentRef)
  905. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  906. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  907. {
  908. dtPolyRef neighbourRef = bestTile->links[i].ref;
  909. // Skip invalid ids and do not expand back to where we came from.
  910. if (!neighbourRef || neighbourRef == parentRef)
  911. continue;
  912. // Get neighbour poly and tile.
  913. // The API input has been cheked already, skip checking internal data.
  914. const dtMeshTile* neighbourTile = 0;
  915. const dtPoly* neighbourPoly = 0;
  916. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  917. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  918. continue;
  919. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  920. if (!neighbourNode)
  921. {
  922. status |= DT_OUT_OF_NODES;
  923. continue;
  924. }
  925. // If the node is visited the first time, calculate node position.
  926. if (neighbourNode->flags == 0)
  927. {
  928. getEdgeMidPoint(bestRef, bestPoly, bestTile,
  929. neighbourRef, neighbourPoly, neighbourTile,
  930. neighbourNode->pos);
  931. }
  932. // Calculate cost and heuristic.
  933. float cost = 0;
  934. float heuristic = 0;
  935. // Special case for last node.
  936. if (neighbourRef == endRef)
  937. {
  938. // Cost
  939. const float curCost = filter->getCost(bestNode->pos, neighbourNode->pos,
  940. parentRef, parentTile, parentPoly,
  941. bestRef, bestTile, bestPoly,
  942. neighbourRef, neighbourTile, neighbourPoly);
  943. const float endCost = filter->getCost(neighbourNode->pos, endPos,
  944. bestRef, bestTile, bestPoly,
  945. neighbourRef, neighbourTile, neighbourPoly,
  946. 0, 0, 0);
  947. cost = bestNode->cost + curCost + endCost;
  948. heuristic = 0;
  949. }
  950. else
  951. {
  952. // Cost
  953. const float curCost = filter->getCost(bestNode->pos, neighbourNode->pos,
  954. parentRef, parentTile, parentPoly,
  955. bestRef, bestTile, bestPoly,
  956. neighbourRef, neighbourTile, neighbourPoly);
  957. cost = bestNode->cost + curCost;
  958. heuristic = dtVdist(neighbourNode->pos, endPos)*H_SCALE;
  959. }
  960. const float total = cost + heuristic;
  961. // The node is already in open list and the new result is worse, skip.
  962. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  963. continue;
  964. // The node is already visited and process, and the new result is worse, skip.
  965. if ((neighbourNode->flags & DT_NODE_CLOSED) && total >= neighbourNode->total)
  966. continue;
  967. // Add or update the node.
  968. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  969. neighbourNode->id = neighbourRef;
  970. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  971. neighbourNode->cost = cost;
  972. neighbourNode->total = total;
  973. if (neighbourNode->flags & DT_NODE_OPEN)
  974. {
  975. // Already in open, update node location.
  976. m_openList->modify(neighbourNode);
  977. }
  978. else
  979. {
  980. // Put the node in open list.
  981. neighbourNode->flags |= DT_NODE_OPEN;
  982. m_openList->push(neighbourNode);
  983. }
  984. // Update nearest node to target so far.
  985. if (heuristic < lastBestNodeCost)
  986. {
  987. lastBestNodeCost = heuristic;
  988. lastBestNode = neighbourNode;
  989. }
  990. }
  991. }
  992. if (lastBestNode->id != endRef)
  993. status |= DT_PARTIAL_RESULT;
  994. // Reverse the path.
  995. dtNode* prev = 0;
  996. dtNode* node = lastBestNode;
  997. do
  998. {
  999. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  1000. node->pidx = m_nodePool->getNodeIdx(prev);
  1001. prev = node;
  1002. node = next;
  1003. }
  1004. while (node);
  1005. // Store path
  1006. node = prev;
  1007. int n = 0;
  1008. do
  1009. {
  1010. path[n++] = node->id;
  1011. if (n >= maxPath)
  1012. {
  1013. status |= DT_BUFFER_TOO_SMALL;
  1014. break;
  1015. }
  1016. node = m_nodePool->getNodeAtIdx(node->pidx);
  1017. }
  1018. while (node);
  1019. *pathCount = n;
  1020. return status;
  1021. }
  1022. /// @par
  1023. ///
  1024. /// @warning Calling any non-slice methods before calling finalizeSlicedFindPath()
  1025. /// or finalizeSlicedFindPathPartial() may result in corrupted data!
  1026. ///
  1027. /// The @p filter pointer is stored and used for the duration of the sliced
  1028. /// path query.
  1029. ///
  1030. dtStatus dtNavMeshQuery::initSlicedFindPath(dtPolyRef startRef, dtPolyRef endRef,
  1031. const float* startPos, const float* endPos,
  1032. const dtQueryFilter* filter)
  1033. {
  1034. dtAssert(m_nav);
  1035. dtAssert(m_nodePool);
  1036. dtAssert(m_openList);
  1037. // Init path state.
  1038. memset(&m_query, 0, sizeof(dtQueryData));
  1039. m_query.status = DT_FAILURE;
  1040. m_query.startRef = startRef;
  1041. m_query.endRef = endRef;
  1042. dtVcopy(m_query.startPos, startPos);
  1043. dtVcopy(m_query.endPos, endPos);
  1044. m_query.filter = filter;
  1045. if (!startRef || !endRef)
  1046. return DT_FAILURE | DT_INVALID_PARAM;
  1047. // Validate input
  1048. if (!m_nav->isValidPolyRef(startRef) || !m_nav->isValidPolyRef(endRef))
  1049. return DT_FAILURE | DT_INVALID_PARAM;
  1050. if (startRef == endRef)
  1051. {
  1052. m_query.status = DT_SUCCESS;
  1053. return DT_SUCCESS;
  1054. }
  1055. m_nodePool->clear();
  1056. m_openList->clear();
  1057. dtNode* startNode = m_nodePool->getNode(startRef);
  1058. dtVcopy(startNode->pos, startPos);
  1059. startNode->pidx = 0;
  1060. startNode->cost = 0;
  1061. startNode->total = dtVdist(startPos, endPos) * H_SCALE;
  1062. startNode->id = startRef;
  1063. startNode->flags = DT_NODE_OPEN;
  1064. m_openList->push(startNode);
  1065. m_query.status = DT_IN_PROGRESS;
  1066. m_query.lastBestNode = startNode;
  1067. m_query.lastBestNodeCost = startNode->total;
  1068. return m_query.status;
  1069. }
  1070. dtStatus dtNavMeshQuery::updateSlicedFindPath(const int maxIter, int* doneIters)
  1071. {
  1072. if (!dtStatusInProgress(m_query.status))
  1073. return m_query.status;
  1074. // Make sure the request is still valid.
  1075. if (!m_nav->isValidPolyRef(m_query.startRef) || !m_nav->isValidPolyRef(m_query.endRef))
  1076. {
  1077. m_query.status = DT_FAILURE;
  1078. return DT_FAILURE;
  1079. }
  1080. int iter = 0;
  1081. while (iter < maxIter && !m_openList->empty())
  1082. {
  1083. iter++;
  1084. // Remove node from open list and put it in closed list.
  1085. dtNode* bestNode = m_openList->pop();
  1086. bestNode->flags &= ~DT_NODE_OPEN;
  1087. bestNode->flags |= DT_NODE_CLOSED;
  1088. // Reached the goal, stop searching.
  1089. if (bestNode->id == m_query.endRef)
  1090. {
  1091. m_query.lastBestNode = bestNode;
  1092. const dtStatus details = m_query.status & DT_STATUS_DETAIL_MASK;
  1093. m_query.status = DT_SUCCESS | details;
  1094. if (doneIters)
  1095. *doneIters = iter;
  1096. return m_query.status;
  1097. }
  1098. // Get current poly and tile.
  1099. // The API input has been cheked already, skip checking internal data.
  1100. const dtPolyRef bestRef = bestNode->id;
  1101. const dtMeshTile* bestTile = 0;
  1102. const dtPoly* bestPoly = 0;
  1103. if (dtStatusFailed(m_nav->getTileAndPolyByRef(bestRef, &bestTile, &bestPoly)))
  1104. {
  1105. // The polygon has disappeared during the sliced query, fail.
  1106. m_query.status = DT_FAILURE;
  1107. if (doneIters)
  1108. *doneIters = iter;
  1109. return m_query.status;
  1110. }
  1111. // Get parent poly and tile.
  1112. dtPolyRef parentRef = 0;
  1113. const dtMeshTile* parentTile = 0;
  1114. const dtPoly* parentPoly = 0;
  1115. if (bestNode->pidx)
  1116. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  1117. if (parentRef)
  1118. {
  1119. if (dtStatusFailed(m_nav->getTileAndPolyByRef(parentRef, &parentTile, &parentPoly)))
  1120. {
  1121. // The polygon has disappeared during the sliced query, fail.
  1122. m_query.status = DT_FAILURE;
  1123. if (doneIters)
  1124. *doneIters = iter;
  1125. return m_query.status;
  1126. }
  1127. }
  1128. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  1129. {
  1130. dtPolyRef neighbourRef = bestTile->links[i].ref;
  1131. // Skip invalid ids and do not expand back to where we came from.
  1132. if (!neighbourRef || neighbourRef == parentRef)
  1133. continue;
  1134. // Get neighbour poly and tile.
  1135. // The API input has been cheked already, skip checking internal data.
  1136. const dtMeshTile* neighbourTile = 0;
  1137. const dtPoly* neighbourPoly = 0;
  1138. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  1139. if (!m_query.filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  1140. continue;
  1141. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  1142. if (!neighbourNode)
  1143. {
  1144. m_query.status |= DT_OUT_OF_NODES;
  1145. continue;
  1146. }
  1147. // If the node is visited the first time, calculate node position.
  1148. if (neighbourNode->flags == 0)
  1149. {
  1150. getEdgeMidPoint(bestRef, bestPoly, bestTile,
  1151. neighbourRef, neighbourPoly, neighbourTile,
  1152. neighbourNode->pos);
  1153. }
  1154. // Calculate cost and heuristic.
  1155. float cost = 0;
  1156. float heuristic = 0;
  1157. // Special case for last node.
  1158. if (neighbourRef == m_query.endRef)
  1159. {
  1160. // Cost
  1161. const float curCost = m_query.filter->getCost(bestNode->pos, neighbourNode->pos,
  1162. parentRef, parentTile, parentPoly,
  1163. bestRef, bestTile, bestPoly,
  1164. neighbourRef, neighbourTile, neighbourPoly);
  1165. const float endCost = m_query.filter->getCost(neighbourNode->pos, m_query.endPos,
  1166. bestRef, bestTile, bestPoly,
  1167. neighbourRef, neighbourTile, neighbourPoly,
  1168. 0, 0, 0);
  1169. cost = bestNode->cost + curCost + endCost;
  1170. heuristic = 0;
  1171. }
  1172. else
  1173. {
  1174. // Cost
  1175. const float curCost = m_query.filter->getCost(bestNode->pos, neighbourNode->pos,
  1176. parentRef, parentTile, parentPoly,
  1177. bestRef, bestTile, bestPoly,
  1178. neighbourRef, neighbourTile, neighbourPoly);
  1179. cost = bestNode->cost + curCost;
  1180. heuristic = dtVdist(neighbourNode->pos, m_query.endPos)*H_SCALE;
  1181. }
  1182. const float total = cost + heuristic;
  1183. // The node is already in open list and the new result is worse, skip.
  1184. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  1185. continue;
  1186. // The node is already visited and process, and the new result is worse, skip.
  1187. if ((neighbourNode->flags & DT_NODE_CLOSED) && total >= neighbourNode->total)
  1188. continue;
  1189. // Add or update the node.
  1190. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  1191. neighbourNode->id = neighbourRef;
  1192. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  1193. neighbourNode->cost = cost;
  1194. neighbourNode->total = total;
  1195. if (neighbourNode->flags & DT_NODE_OPEN)
  1196. {
  1197. // Already in open, update node location.
  1198. m_openList->modify(neighbourNode);
  1199. }
  1200. else
  1201. {
  1202. // Put the node in open list.
  1203. neighbourNode->flags |= DT_NODE_OPEN;
  1204. m_openList->push(neighbourNode);
  1205. }
  1206. // Update nearest node to target so far.
  1207. if (heuristic < m_query.lastBestNodeCost)
  1208. {
  1209. m_query.lastBestNodeCost = heuristic;
  1210. m_query.lastBestNode = neighbourNode;
  1211. }
  1212. }
  1213. }
  1214. // Exhausted all nodes, but could not find path.
  1215. if (m_openList->empty())
  1216. {
  1217. const dtStatus details = m_query.status & DT_STATUS_DETAIL_MASK;
  1218. m_query.status = DT_SUCCESS | details;
  1219. }
  1220. if (doneIters)
  1221. *doneIters = iter;
  1222. return m_query.status;
  1223. }
  1224. dtStatus dtNavMeshQuery::finalizeSlicedFindPath(dtPolyRef* path, int* pathCount, const int maxPath)
  1225. {
  1226. *pathCount = 0;
  1227. if (dtStatusFailed(m_query.status))
  1228. {
  1229. // Reset query.
  1230. memset(&m_query, 0, sizeof(dtQueryData));
  1231. return DT_FAILURE;
  1232. }
  1233. int n = 0;
  1234. if (m_query.startRef == m_query.endRef)
  1235. {
  1236. // Special case: the search starts and ends at same poly.
  1237. path[n++] = m_query.startRef;
  1238. }
  1239. else
  1240. {
  1241. // Reverse the path.
  1242. dtAssert(m_query.lastBestNode);
  1243. if (m_query.lastBestNode->id != m_query.endRef)
  1244. m_query.status |= DT_PARTIAL_RESULT;
  1245. dtNode* prev = 0;
  1246. dtNode* node = m_query.lastBestNode;
  1247. do
  1248. {
  1249. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  1250. node->pidx = m_nodePool->getNodeIdx(prev);
  1251. prev = node;
  1252. node = next;
  1253. }
  1254. while (node);
  1255. // Store path
  1256. node = prev;
  1257. do
  1258. {
  1259. path[n++] = node->id;
  1260. if (n >= maxPath)
  1261. {
  1262. m_query.status |= DT_BUFFER_TOO_SMALL;
  1263. break;
  1264. }
  1265. node = m_nodePool->getNodeAtIdx(node->pidx);
  1266. }
  1267. while (node);
  1268. }
  1269. const dtStatus details = m_query.status & DT_STATUS_DETAIL_MASK;
  1270. // Reset query.
  1271. memset(&m_query, 0, sizeof(dtQueryData));
  1272. *pathCount = n;
  1273. return DT_SUCCESS | details;
  1274. }
  1275. dtStatus dtNavMeshQuery::finalizeSlicedFindPathPartial(const dtPolyRef* existing, const int existingSize,
  1276. dtPolyRef* path, int* pathCount, const int maxPath)
  1277. {
  1278. *pathCount = 0;
  1279. if (existingSize == 0)
  1280. {
  1281. return DT_FAILURE;
  1282. }
  1283. if (dtStatusFailed(m_query.status))
  1284. {
  1285. // Reset query.
  1286. memset(&m_query, 0, sizeof(dtQueryData));
  1287. return DT_FAILURE;
  1288. }
  1289. int n = 0;
  1290. if (m_query.startRef == m_query.endRef)
  1291. {
  1292. // Special case: the search starts and ends at same poly.
  1293. path[n++] = m_query.startRef;
  1294. }
  1295. else
  1296. {
  1297. // Find furthest existing node that was visited.
  1298. dtNode* prev = 0;
  1299. dtNode* node = 0;
  1300. for (int i = existingSize-1; i >= 0; --i)
  1301. {
  1302. node = m_nodePool->findNode(existing[i]);
  1303. if (node)
  1304. break;
  1305. }
  1306. if (!node)
  1307. {
  1308. m_query.status |= DT_PARTIAL_RESULT;
  1309. dtAssert(m_query.lastBestNode);
  1310. node = m_query.lastBestNode;
  1311. }
  1312. // Reverse the path.
  1313. do
  1314. {
  1315. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  1316. node->pidx = m_nodePool->getNodeIdx(prev);
  1317. prev = node;
  1318. node = next;
  1319. }
  1320. while (node);
  1321. // Store path
  1322. node = prev;
  1323. do
  1324. {
  1325. path[n++] = node->id;
  1326. if (n >= maxPath)
  1327. {
  1328. m_query.status |= DT_BUFFER_TOO_SMALL;
  1329. break;
  1330. }
  1331. node = m_nodePool->getNodeAtIdx(node->pidx);
  1332. }
  1333. while (node);
  1334. }
  1335. const dtStatus details = m_query.status & DT_STATUS_DETAIL_MASK;
  1336. // Reset query.
  1337. memset(&m_query, 0, sizeof(dtQueryData));
  1338. *pathCount = n;
  1339. return DT_SUCCESS | details;
  1340. }
  1341. dtStatus dtNavMeshQuery::appendVertex(const float* pos, const unsigned char flags, const dtPolyRef ref,
  1342. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  1343. int* straightPathCount, const int maxStraightPath) const
  1344. {
  1345. if ((*straightPathCount) > 0 && dtVequal(&straightPath[((*straightPathCount)-1)*3], pos))
  1346. {
  1347. // The vertices are equal, update flags and poly.
  1348. if (straightPathFlags)
  1349. straightPathFlags[(*straightPathCount)-1] = flags;
  1350. if (straightPathRefs)
  1351. straightPathRefs[(*straightPathCount)-1] = ref;
  1352. }
  1353. else
  1354. {
  1355. // Append new vertex.
  1356. dtVcopy(&straightPath[(*straightPathCount)*3], pos);
  1357. if (straightPathFlags)
  1358. straightPathFlags[(*straightPathCount)] = flags;
  1359. if (straightPathRefs)
  1360. straightPathRefs[(*straightPathCount)] = ref;
  1361. (*straightPathCount)++;
  1362. // If reached end of path or there is no space to append more vertices, return.
  1363. if (flags == DT_STRAIGHTPATH_END || (*straightPathCount) >= maxStraightPath)
  1364. {
  1365. return DT_SUCCESS | (((*straightPathCount) >= maxStraightPath) ? DT_BUFFER_TOO_SMALL : 0);
  1366. }
  1367. }
  1368. return DT_IN_PROGRESS;
  1369. }
  1370. dtStatus dtNavMeshQuery::appendPortals(const int startIdx, const int endIdx, const float* endPos, const dtPolyRef* path,
  1371. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  1372. int* straightPathCount, const int maxStraightPath, const int options) const
  1373. {
  1374. const float* startPos = &straightPath[(*straightPathCount-1)*3];
  1375. // Append or update last vertex
  1376. dtStatus stat = 0;
  1377. for (int i = startIdx; i < endIdx; i++)
  1378. {
  1379. // Calculate portal
  1380. const dtPolyRef from = path[i];
  1381. const dtMeshTile* fromTile = 0;
  1382. const dtPoly* fromPoly = 0;
  1383. if (dtStatusFailed(m_nav->getTileAndPolyByRef(from, &fromTile, &fromPoly)))
  1384. return DT_FAILURE | DT_INVALID_PARAM;
  1385. const dtPolyRef to = path[i+1];
  1386. const dtMeshTile* toTile = 0;
  1387. const dtPoly* toPoly = 0;
  1388. if (dtStatusFailed(m_nav->getTileAndPolyByRef(to, &toTile, &toPoly)))
  1389. return DT_FAILURE | DT_INVALID_PARAM;
  1390. float left[3], right[3];
  1391. if (dtStatusFailed(getPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, left, right)))
  1392. break;
  1393. if (options & DT_STRAIGHTPATH_AREA_CROSSINGS)
  1394. {
  1395. // Skip intersection if only area crossings are requested.
  1396. if (fromPoly->getArea() == toPoly->getArea())
  1397. continue;
  1398. }
  1399. // Append intersection
  1400. float s,t;
  1401. if (dtIntersectSegSeg2D(startPos, endPos, left, right, s, t))
  1402. {
  1403. float pt[3];
  1404. dtVlerp(pt, left,right, t);
  1405. stat = appendVertex(pt, 0, path[i+1],
  1406. straightPath, straightPathFlags, straightPathRefs,
  1407. straightPathCount, maxStraightPath);
  1408. if (stat != DT_IN_PROGRESS)
  1409. return stat;
  1410. }
  1411. }
  1412. return DT_IN_PROGRESS;
  1413. }
  1414. /// @par
  1415. ///
  1416. /// This method peforms what is often called 'string pulling'.
  1417. ///
  1418. /// The start position is clamped to the first polygon in the path, and the
  1419. /// end position is clamped to the last. So the start and end positions should
  1420. /// normally be within or very near the first and last polygons respectively.
  1421. ///
  1422. /// The returned polygon references represent the reference id of the polygon
  1423. /// that is entered at the associated path position. The reference id associated
  1424. /// with the end point will always be zero. This allows, for example, matching
  1425. /// off-mesh link points to their representative polygons.
  1426. ///
  1427. /// If the provided result buffers are too small for the entire result set,
  1428. /// they will be filled as far as possible from the start toward the end
  1429. /// position.
  1430. ///
  1431. dtStatus dtNavMeshQuery::findStraightPath(const float* startPos, const float* endPos,
  1432. const dtPolyRef* path, const int pathSize,
  1433. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  1434. int* straightPathCount, const int maxStraightPath, const int options) const
  1435. {
  1436. dtAssert(m_nav);
  1437. *straightPathCount = 0;
  1438. if (!maxStraightPath)
  1439. return DT_FAILURE | DT_INVALID_PARAM;
  1440. if (!path[0])
  1441. return DT_FAILURE | DT_INVALID_PARAM;
  1442. dtStatus stat = 0;
  1443. // TODO: Should this be callers responsibility?
  1444. float closestStartPos[3];
  1445. if (dtStatusFailed(closestPointOnPolyBoundary(path[0], startPos, closestStartPos)))
  1446. return DT_FAILURE | DT_INVALID_PARAM;
  1447. float closestEndPos[3];
  1448. if (dtStatusFailed(closestPointOnPolyBoundary(path[pathSize-1], endPos, closestEndPos)))
  1449. return DT_FAILURE | DT_INVALID_PARAM;
  1450. // Add start point.
  1451. stat = appendVertex(closestStartPos, DT_STRAIGHTPATH_START, path[0],
  1452. straightPath, straightPathFlags, straightPathRefs,
  1453. straightPathCount, maxStraightPath);
  1454. if (stat != DT_IN_PROGRESS)
  1455. return stat;
  1456. if (pathSize > 1)
  1457. {
  1458. float portalApex[3], portalLeft[3], portalRight[3];
  1459. dtVcopy(portalApex, closestStartPos);
  1460. dtVcopy(portalLeft, portalApex);
  1461. dtVcopy(portalRight, portalApex);
  1462. int apexIndex = 0;
  1463. int leftIndex = 0;
  1464. int rightIndex = 0;
  1465. unsigned char leftPolyType = 0;
  1466. unsigned char rightPolyType = 0;
  1467. dtPolyRef leftPolyRef = path[0];
  1468. dtPolyRef rightPolyRef = path[0];
  1469. for (int i = 0; i < pathSize; ++i)
  1470. {
  1471. float left[3], right[3];
  1472. unsigned char fromType, toType;
  1473. if (i+1 < pathSize)
  1474. {
  1475. // Next portal.
  1476. if (dtStatusFailed(getPortalPoints(path[i], path[i+1], left, right, fromType, toType)))
  1477. {
  1478. // Failed to get portal points, in practice this means that path[i+1] is invalid polygon.
  1479. // Clamp the end point to path[i], and return the path so far.
  1480. if (dtStatusFailed(closestPointOnPolyBoundary(path[i], endPos, closestEndPos)))
  1481. {
  1482. // This should only happen when the first polygon is invalid.
  1483. return DT_FAILURE | DT_INVALID_PARAM;
  1484. }
  1485. // Apeend portals along the current straight path segment.
  1486. if (options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS))
  1487. {
  1488. stat = appendPortals(apexIndex, i, closestEndPos, path,
  1489. straightPath, straightPathFlags, straightPathRefs,
  1490. straightPathCount, maxStraightPath, options);
  1491. }
  1492. stat = appendVertex(closestEndPos, 0, path[i],
  1493. straightPath, straightPathFlags, straightPathRefs,
  1494. straightPathCount, maxStraightPath);
  1495. return DT_SUCCESS | DT_PARTIAL_RESULT | ((*straightPathCount >= maxStraightPath) ? DT_BUFFER_TOO_SMALL : 0);
  1496. }
  1497. // If starting really close the portal, advance.
  1498. if (i == 0)
  1499. {
  1500. float t;
  1501. if (dtDistancePtSegSqr2D(portalApex, left, right, t) < dtSqr(0.001f))
  1502. continue;
  1503. }
  1504. }
  1505. else
  1506. {
  1507. // End of the path.
  1508. dtVcopy(left, closestEndPos);
  1509. dtVcopy(right, closestEndPos);
  1510. fromType = toType = DT_POLYTYPE_GROUND;
  1511. }
  1512. // Right vertex.
  1513. if (dtTriArea2D(portalApex, portalRight, right) <= 0.0f)
  1514. {
  1515. if (dtVequal(portalApex, portalRight) || dtTriArea2D(portalApex, portalLeft, right) > 0.0f)
  1516. {
  1517. dtVcopy(portalRight, right);
  1518. rightPolyRef = (i+1 < pathSize) ? path[i+1] : 0;
  1519. rightPolyType = toType;
  1520. rightIndex = i;
  1521. }
  1522. else
  1523. {
  1524. // Append portals along the current straight path segment.
  1525. if (options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS))
  1526. {
  1527. stat = appendPortals(apexIndex, leftIndex, portalLeft, path,
  1528. straightPath, straightPathFlags, straightPathRefs,
  1529. straightPathCount, maxStraightPath, options);
  1530. if (stat != DT_IN_PROGRESS)
  1531. return stat;
  1532. }
  1533. dtVcopy(portalApex, portalLeft);
  1534. apexIndex = leftIndex;
  1535. unsigned char flags = 0;
  1536. if (!leftPolyRef)
  1537. flags = DT_STRAIGHTPATH_END;
  1538. else if (leftPolyType == DT_POLYTYPE_OFFMESH_CONNECTION)
  1539. flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION;
  1540. dtPolyRef ref = leftPolyRef;
  1541. // Append or update vertex
  1542. stat = appendVertex(portalApex, flags, ref,
  1543. straightPath, straightPathFlags, straightPathRefs,
  1544. straightPathCount, maxStraightPath);
  1545. if (stat != DT_IN_PROGRESS)
  1546. return stat;
  1547. dtVcopy(portalLeft, portalApex);
  1548. dtVcopy(portalRight, portalApex);
  1549. leftIndex = apexIndex;
  1550. rightIndex = apexIndex;
  1551. // Restart
  1552. i = apexIndex;
  1553. continue;
  1554. }
  1555. }
  1556. // Left vertex.
  1557. if (dtTriArea2D(portalApex, portalLeft, left) >= 0.0f)
  1558. {
  1559. if (dtVequal(portalApex, portalLeft) || dtTriArea2D(portalApex, portalRight, left) < 0.0f)
  1560. {
  1561. dtVcopy(portalLeft, left);
  1562. leftPolyRef = (i+1 < pathSize) ? path[i+1] : 0;
  1563. leftPolyType = toType;
  1564. leftIndex = i;
  1565. }
  1566. else
  1567. {
  1568. // Append portals along the current straight path segment.
  1569. if (options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS))
  1570. {
  1571. stat = appendPortals(apexIndex, rightIndex, portalRight, path,
  1572. straightPath, straightPathFlags, straightPathRefs,
  1573. straightPathCount, maxStraightPath, options);
  1574. if (stat != DT_IN_PROGRESS)
  1575. return stat;
  1576. }
  1577. dtVcopy(portalApex, portalRight);
  1578. apexIndex = rightIndex;
  1579. unsigned char flags = 0;
  1580. if (!rightPolyRef)
  1581. flags = DT_STRAIGHTPATH_END;
  1582. else if (rightPolyType == DT_POLYTYPE_OFFMESH_CONNECTION)
  1583. flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION;
  1584. dtPolyRef ref = rightPolyRef;
  1585. // Append or update vertex
  1586. stat = appendVertex(portalApex, flags, ref,
  1587. straightPath, straightPathFlags, straightPathRefs,
  1588. straightPathCount, maxStraightPath);
  1589. if (stat != DT_IN_PROGRESS)
  1590. return stat;
  1591. dtVcopy(portalLeft, portalApex);
  1592. dtVcopy(portalRight, portalApex);
  1593. leftIndex = apexIndex;
  1594. rightIndex = apexIndex;
  1595. // Restart
  1596. i = apexIndex;
  1597. continue;
  1598. }
  1599. }
  1600. }
  1601. // Append portals along the current straight path segment.
  1602. if (options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS))
  1603. {
  1604. stat = appendPortals(apexIndex, pathSize-1, closestEndPos, path,
  1605. straightPath, straightPathFlags, straightPathRefs,
  1606. straightPathCount, maxStraightPath, options);
  1607. if (stat != DT_IN_PROGRESS)
  1608. return stat;
  1609. }
  1610. }
  1611. stat = appendVertex(closestEndPos, DT_STRAIGHTPATH_END, 0,
  1612. straightPath, straightPathFlags, straightPathRefs,
  1613. straightPathCount, maxStraightPath);
  1614. return DT_SUCCESS | ((*straightPathCount >= maxStraightPath) ? DT_BUFFER_TOO_SMALL : 0);
  1615. }
  1616. /// @par
  1617. ///
  1618. /// This method is optimized for small delta movement and a small number of
  1619. /// polygons. If used for too great a distance, the result set will form an
  1620. /// incomplete path.
  1621. ///
  1622. /// @p resultPos will equal the @p endPos if the end is reached.
  1623. /// Otherwise the closest reachable position will be returned.
  1624. ///
  1625. /// @p resultPos is not projected onto the surface of the navigation
  1626. /// mesh. Use #getPolyHeight if this is needed.
  1627. ///
  1628. /// This method treats the end position in the same manner as
  1629. /// the #raycast method. (As a 2D point.) See that method's documentation
  1630. /// for details.
  1631. ///
  1632. /// If the @p visited array is too small to hold the entire result set, it will
  1633. /// be filled as far as possible from the start position toward the end
  1634. /// position.
  1635. ///
  1636. dtStatus dtNavMeshQuery::moveAlongSurface(dtPolyRef startRef, const float* startPos, const float* endPos,
  1637. const dtQueryFilter* filter,
  1638. float* resultPos, dtPolyRef* visited, int* visitedCount, const int maxVisitedSize) const
  1639. {
  1640. dtAssert(m_nav);
  1641. dtAssert(m_tinyNodePool);
  1642. *visitedCount = 0;
  1643. // Validate input
  1644. if (!startRef)
  1645. return DT_FAILURE | DT_INVALID_PARAM;
  1646. if (!m_nav->isValidPolyRef(startRef))
  1647. return DT_FAILURE | DT_INVALID_PARAM;
  1648. dtStatus status = DT_SUCCESS;
  1649. static const int MAX_STACK = 48;
  1650. dtNode* stack[MAX_STACK];
  1651. int nstack = 0;
  1652. m_tinyNodePool->clear();
  1653. dtNode* startNode = m_tinyNodePool->getNode(startRef);
  1654. startNode->pidx = 0;
  1655. startNode->cost = 0;
  1656. startNode->total = 0;
  1657. startNode->id = startRef;
  1658. startNode->flags = DT_NODE_CLOSED;
  1659. stack[nstack++] = startNode;
  1660. float bestPos[3];
  1661. float bestDist = FLT_MAX;
  1662. dtNode* bestNode = 0;
  1663. dtVcopy(bestPos, startPos);
  1664. // Search constraints
  1665. float searchPos[3], searchRadSqr;
  1666. dtVlerp(searchPos, startPos, endPos, 0.5f);
  1667. searchRadSqr = dtSqr(dtVdist(startPos, endPos)/2.0f + 0.001f);
  1668. float verts[DT_VERTS_PER_POLYGON*3];
  1669. while (nstack)
  1670. {
  1671. // Pop front.
  1672. dtNode* curNode = stack[0];
  1673. for (int i = 0; i < nstack-1; ++i)
  1674. stack[i] = stack[i+1];
  1675. nstack--;
  1676. // Get poly and tile.
  1677. // The API input has been cheked already, skip checking internal data.
  1678. const dtPolyRef curRef = curNode->id;
  1679. const dtMeshTile* curTile = 0;
  1680. const dtPoly* curPoly = 0;
  1681. m_nav->getTileAndPolyByRefUnsafe(curRef, &curTile, &curPoly);
  1682. // Collect vertices.
  1683. const int nverts = curPoly->vertCount;
  1684. for (int i = 0; i < nverts; ++i)
  1685. dtVcopy(&verts[i*3], &curTile->verts[curPoly->verts[i]*3]);
  1686. // If target is inside the poly, stop search.
  1687. if (dtPointInPolygon(endPos, verts, nverts))
  1688. {
  1689. bestNode = curNode;
  1690. dtVcopy(bestPos, endPos);
  1691. break;
  1692. }
  1693. // Find wall edges and find nearest point inside the walls.
  1694. for (int i = 0, j = (int)curPoly->vertCount-1; i < (int)curPoly->vertCount; j = i++)
  1695. {
  1696. // Find links to neighbours.
  1697. static const int MAX_NEIS = 8;
  1698. int nneis = 0;
  1699. dtPolyRef neis[MAX_NEIS];
  1700. if (curPoly->neis[j] & DT_EXT_LINK)
  1701. {
  1702. // Tile border.
  1703. for (unsigned int k = curPoly->firstLink; k != DT_NULL_LINK; k = curTile->links[k].next)
  1704. {
  1705. const dtLink* link = &curTile->links[k];
  1706. if (link->edge == j)
  1707. {
  1708. if (link->ref != 0)
  1709. {
  1710. const dtMeshTile* neiTile = 0;
  1711. const dtPoly* neiPoly = 0;
  1712. m_nav->getTileAndPolyByRefUnsafe(link->ref, &neiTile, &neiPoly);
  1713. if (filter->passFilter(link->ref, neiTile, neiPoly))
  1714. {
  1715. if (nneis < MAX_NEIS)
  1716. neis[nneis++] = link->ref;
  1717. }
  1718. }
  1719. }
  1720. }
  1721. }
  1722. else if (curPoly->neis[j])
  1723. {
  1724. const unsigned int idx = (unsigned int)(curPoly->neis[j]-1);
  1725. const dtPolyRef ref = m_nav->getPolyRefBase(curTile) | idx;
  1726. if (filter->passFilter(ref, curTile, &curTile->polys[idx]))
  1727. {
  1728. // Internal edge, encode id.
  1729. neis[nneis++] = ref;
  1730. }
  1731. }
  1732. if (!nneis)
  1733. {
  1734. // Wall edge, calc distance.
  1735. const float* vj = &verts[j*3];
  1736. const float* vi = &verts[i*3];
  1737. float tseg;
  1738. const float distSqr = dtDistancePtSegSqr2D(endPos, vj, vi, tseg);
  1739. if (distSqr < bestDist)
  1740. {
  1741. // Update nearest distance.
  1742. dtVlerp(bestPos, vj,vi, tseg);
  1743. bestDist = distSqr;
  1744. bestNode = curNode;
  1745. }
  1746. }
  1747. else
  1748. {
  1749. for (int k = 0; k < nneis; ++k)
  1750. {
  1751. // Skip if no node can be allocated.
  1752. dtNode* neighbourNode = m_tinyNodePool->getNode(neis[k]);
  1753. if (!neighbourNode)
  1754. continue;
  1755. // Skip if already visited.
  1756. if (neighbourNode->flags & DT_NODE_CLOSED)
  1757. continue;
  1758. // Skip the link if it is too far from search constraint.
  1759. // TODO: Maybe should use getPortalPoints(), but this one is way faster.
  1760. const float* vj = &verts[j*3];
  1761. const float* vi = &verts[i*3];
  1762. float tseg;
  1763. float distSqr = dtDistancePtSegSqr2D(searchPos, vj, vi, tseg);
  1764. if (distSqr > searchRadSqr)
  1765. continue;
  1766. // Mark as the node as visited and push to queue.
  1767. if (nstack < MAX_STACK)
  1768. {
  1769. neighbourNode->pidx = m_tinyNodePool->getNodeIdx(curNode);
  1770. neighbourNode->flags |= DT_NODE_CLOSED;
  1771. stack[nstack++] = neighbourNode;
  1772. }
  1773. }
  1774. }
  1775. }
  1776. }
  1777. int n = 0;
  1778. if (bestNode)
  1779. {
  1780. // Reverse the path.
  1781. dtNode* prev = 0;
  1782. dtNode* node = bestNode;
  1783. do
  1784. {
  1785. dtNode* next = m_tinyNodePool->getNodeAtIdx(node->pidx);
  1786. node->pidx = m_tinyNodePool->getNodeIdx(prev);
  1787. prev = node;
  1788. node = next;
  1789. }
  1790. while (node);
  1791. // Store result
  1792. node = prev;
  1793. do
  1794. {
  1795. visited[n++] = node->id;
  1796. if (n >= maxVisitedSize)
  1797. {
  1798. status |= DT_BUFFER_TOO_SMALL;
  1799. break;
  1800. }
  1801. node = m_tinyNodePool->getNodeAtIdx(node->pidx);
  1802. }
  1803. while (node);
  1804. }
  1805. dtVcopy(resultPos, bestPos);
  1806. *visitedCount = n;
  1807. return status;
  1808. }
  1809. dtStatus dtNavMeshQuery::getPortalPoints(dtPolyRef from, dtPolyRef to, float* left, float* right,
  1810. unsigned char& fromType, unsigned char& toType) const
  1811. {
  1812. dtAssert(m_nav);
  1813. const dtMeshTile* fromTile = 0;
  1814. const dtPoly* fromPoly = 0;
  1815. if (dtStatusFailed(m_nav->getTileAndPolyByRef(from, &fromTile, &fromPoly)))
  1816. return DT_FAILURE | DT_INVALID_PARAM;
  1817. fromType = fromPoly->getType();
  1818. const dtMeshTile* toTile = 0;
  1819. const dtPoly* toPoly = 0;
  1820. if (dtStatusFailed(m_nav->getTileAndPolyByRef(to, &toTile, &toPoly)))
  1821. return DT_FAILURE | DT_INVALID_PARAM;
  1822. toType = toPoly->getType();
  1823. return getPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, left, right);
  1824. }
  1825. // Returns portal points between two polygons.
  1826. dtStatus dtNavMeshQuery::getPortalPoints(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
  1827. dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
  1828. float* left, float* right) const
  1829. {
  1830. // Find the link that points to the 'to' polygon.
  1831. const dtLink* link = 0;
  1832. for (unsigned int i = fromPoly->firstLink; i != DT_NULL_LINK; i = fromTile->links[i].next)
  1833. {
  1834. if (fromTile->links[i].ref == to)
  1835. {
  1836. link = &fromTile->links[i];
  1837. break;
  1838. }
  1839. }
  1840. if (!link)
  1841. return DT_FAILURE | DT_INVALID_PARAM;
  1842. // Handle off-mesh connections.
  1843. if (fromPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  1844. {
  1845. // Find link that points to first vertex.
  1846. for (unsigned int i = fromPoly->firstLink; i != DT_NULL_LINK; i = fromTile->links[i].next)
  1847. {
  1848. if (fromTile->links[i].ref == to)
  1849. {
  1850. const int v = fromTile->links[i].edge;
  1851. dtVcopy(left, &fromTile->verts[fromPoly->verts[v]*3]);
  1852. dtVcopy(right, &fromTile->verts[fromPoly->verts[v]*3]);
  1853. return DT_SUCCESS;
  1854. }
  1855. }
  1856. return DT_FAILURE | DT_INVALID_PARAM;
  1857. }
  1858. if (toPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  1859. {
  1860. for (unsigned int i = toPoly->firstLink; i != DT_NULL_LINK; i = toTile->links[i].next)
  1861. {
  1862. if (toTile->links[i].ref == from)
  1863. {
  1864. const int v = toTile->links[i].edge;
  1865. dtVcopy(left, &toTile->verts[toPoly->verts[v]*3]);
  1866. dtVcopy(right, &toTile->verts[toPoly->verts[v]*3]);
  1867. return DT_SUCCESS;
  1868. }
  1869. }
  1870. return DT_FAILURE | DT_INVALID_PARAM;
  1871. }
  1872. // Find portal vertices.
  1873. const int v0 = fromPoly->verts[link->edge];
  1874. const int v1 = fromPoly->verts[(link->edge+1) % (int)fromPoly->vertCount];
  1875. dtVcopy(left, &fromTile->verts[v0*3]);
  1876. dtVcopy(right, &fromTile->verts[v1*3]);
  1877. // If the link is at tile boundary, dtClamp the vertices to
  1878. // the link width.
  1879. if (link->side != 0xff)
  1880. {
  1881. // Unpack portal limits.
  1882. if (link->bmin != 0 || link->bmax != 255)
  1883. {
  1884. const float s = 1.0f/255.0f;
  1885. const float tmin = link->bmin*s;
  1886. const float tmax = link->bmax*s;
  1887. dtVlerp(left, &fromTile->verts[v0*3], &fromTile->verts[v1*3], tmin);
  1888. dtVlerp(right, &fromTile->verts[v0*3], &fromTile->verts[v1*3], tmax);
  1889. }
  1890. }
  1891. return DT_SUCCESS;
  1892. }
  1893. // Returns edge mid point between two polygons.
  1894. dtStatus dtNavMeshQuery::getEdgeMidPoint(dtPolyRef from, dtPolyRef to, float* mid) const
  1895. {
  1896. float left[3], right[3];
  1897. unsigned char fromType, toType;
  1898. if (dtStatusFailed(getPortalPoints(from, to, left,right, fromType, toType)))
  1899. return DT_FAILURE | DT_INVALID_PARAM;
  1900. mid[0] = (left[0]+right[0])*0.5f;
  1901. mid[1] = (left[1]+right[1])*0.5f;
  1902. mid[2] = (left[2]+right[2])*0.5f;
  1903. return DT_SUCCESS;
  1904. }
  1905. dtStatus dtNavMeshQuery::getEdgeMidPoint(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
  1906. dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
  1907. float* mid) const
  1908. {
  1909. float left[3], right[3];
  1910. if (dtStatusFailed(getPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, left, right)))
  1911. return DT_FAILURE | DT_INVALID_PARAM;
  1912. mid[0] = (left[0]+right[0])*0.5f;
  1913. mid[1] = (left[1]+right[1])*0.5f;
  1914. mid[2] = (left[2]+right[2])*0.5f;
  1915. return DT_SUCCESS;
  1916. }
  1917. /// @par
  1918. ///
  1919. /// This method is meant to be used for quick, short distance checks.
  1920. ///
  1921. /// If the path array is too small to hold the result, it will be filled as
  1922. /// far as possible from the start postion toward the end position.
  1923. ///
  1924. /// <b>Using the Hit Parameter (t)</b>
  1925. ///
  1926. /// If the hit parameter is a very high value (FLT_MAX), then the ray has hit
  1927. /// the end position. In this case the path represents a valid corridor to the
  1928. /// end position and the value of @p hitNormal is undefined.
  1929. ///
  1930. /// If the hit parameter is zero, then the start position is on the wall that
  1931. /// was hit and the value of @p hitNormal is undefined.
  1932. ///
  1933. /// If 0 < t < 1.0 then the following applies:
  1934. ///
  1935. /// @code
  1936. /// distanceToHitBorder = distanceToEndPosition * t
  1937. /// hitPoint = startPos + (endPos - startPos) * t
  1938. /// @endcode
  1939. ///
  1940. /// <b>Use Case Restriction</b>
  1941. ///
  1942. /// The raycast ignores the y-value of the end position. (2D check.) This
  1943. /// places significant limits on how it can be used. For example:
  1944. ///
  1945. /// Consider a scene where there is a main floor with a second floor balcony
  1946. /// that hangs over the main floor. So the first floor mesh extends below the
  1947. /// balcony mesh. The start position is somewhere on the first floor. The end
  1948. /// position is on the balcony.
  1949. ///
  1950. /// The raycast will search toward the end position along the first floor mesh.
  1951. /// If it reaches the end position's xz-coordinates it will indicate FLT_MAX
  1952. /// (no wall hit), meaning it reached the end position. This is one example of why
  1953. /// this method is meant for short distance checks.
  1954. ///
  1955. dtStatus dtNavMeshQuery::raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
  1956. const dtQueryFilter* filter,
  1957. float* t, float* hitNormal, dtPolyRef* path, int* pathCount, const int maxPath) const
  1958. {
  1959. dtAssert(m_nav);
  1960. *t = 0;
  1961. if (pathCount)
  1962. *pathCount = 0;
  1963. // Validate input
  1964. if (!startRef || !m_nav->isValidPolyRef(startRef))
  1965. return DT_FAILURE | DT_INVALID_PARAM;
  1966. dtPolyRef curRef = startRef;
  1967. float verts[DT_VERTS_PER_POLYGON*3];
  1968. int n = 0;
  1969. hitNormal[0] = 0;
  1970. hitNormal[1] = 0;
  1971. hitNormal[2] = 0;
  1972. dtStatus status = DT_SUCCESS;
  1973. while (curRef)
  1974. {
  1975. // Cast ray against current polygon.
  1976. // The API input has been cheked already, skip checking internal data.
  1977. const dtMeshTile* tile = 0;
  1978. const dtPoly* poly = 0;
  1979. m_nav->getTileAndPolyByRefUnsafe(curRef, &tile, &poly);
  1980. // Collect vertices.
  1981. int nv = 0;
  1982. for (int i = 0; i < (int)poly->vertCount; ++i)
  1983. {
  1984. dtVcopy(&verts[nv*3], &tile->verts[poly->verts[i]*3]);
  1985. nv++;
  1986. }
  1987. float tmin, tmax;
  1988. int segMin, segMax;
  1989. if (!dtIntersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax))
  1990. {
  1991. // Could not hit the polygon, keep the old t and report hit.
  1992. if (pathCount)
  1993. *pathCount = n;
  1994. return status;
  1995. }
  1996. // Keep track of furthest t so far.
  1997. if (tmax > *t)
  1998. *t = tmax;
  1999. // Store visited polygons.
  2000. if (n < maxPath)
  2001. path[n++] = curRef;
  2002. else
  2003. status |= DT_BUFFER_TOO_SMALL;
  2004. // Ray end is completely inside the polygon.
  2005. if (segMax == -1)
  2006. {
  2007. *t = FLT_MAX;
  2008. if (pathCount)
  2009. *pathCount = n;
  2010. return status;
  2011. }
  2012. // Follow neighbours.
  2013. dtPolyRef nextRef = 0;
  2014. for (unsigned int i = poly->firstLink; i != DT_NULL_LINK; i = tile->links[i].next)
  2015. {
  2016. const dtLink* link = &tile->links[i];
  2017. // Find link which contains this edge.
  2018. if ((int)link->edge != segMax)
  2019. continue;
  2020. // Get pointer to the next polygon.
  2021. const dtMeshTile* nextTile = 0;
  2022. const dtPoly* nextPoly = 0;
  2023. m_nav->getTileAndPolyByRefUnsafe(link->ref, &nextTile, &nextPoly);
  2024. // Skip off-mesh connections.
  2025. if (nextPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  2026. continue;
  2027. // Skip links based on filter.
  2028. if (!filter->passFilter(link->ref, nextTile, nextPoly))
  2029. continue;
  2030. // If the link is internal, just return the ref.
  2031. if (link->side == 0xff)
  2032. {
  2033. nextRef = link->ref;
  2034. break;
  2035. }
  2036. // If the link is at tile boundary,
  2037. // Check if the link spans the whole edge, and accept.
  2038. if (link->bmin == 0 && link->bmax == 255)
  2039. {
  2040. nextRef = link->ref;
  2041. break;
  2042. }
  2043. // Check for partial edge links.
  2044. const int v0 = poly->verts[link->edge];
  2045. const int v1 = poly->verts[(link->edge+1) % poly->vertCount];
  2046. const float* left = &tile->verts[v0*3];
  2047. const float* right = &tile->verts[v1*3];
  2048. // Check that the intersection lies inside the link portal.
  2049. if (link->side == 0 || link->side == 4)
  2050. {
  2051. // Calculate link size.
  2052. const float s = 1.0f/255.0f;
  2053. float lmin = left[2] + (right[2] - left[2])*(link->bmin*s);
  2054. float lmax = left[2] + (right[2] - left[2])*(link->bmax*s);
  2055. if (lmin > lmax) dtSwap(lmin, lmax);
  2056. // Find Z intersection.
  2057. float z = startPos[2] + (endPos[2]-startPos[2])*tmax;
  2058. if (z >= lmin && z <= lmax)
  2059. {
  2060. nextRef = link->ref;
  2061. break;
  2062. }
  2063. }
  2064. else if (link->side == 2 || link->side == 6)
  2065. {
  2066. // Calculate link size.
  2067. const float s = 1.0f/255.0f;
  2068. float lmin = left[0] + (right[0] - left[0])*(link->bmin*s);
  2069. float lmax = left[0] + (right[0] - left[0])*(link->bmax*s);
  2070. if (lmin > lmax) dtSwap(lmin, lmax);
  2071. // Find X intersection.
  2072. float x = startPos[0] + (endPos[0]-startPos[0])*tmax;
  2073. if (x >= lmin && x <= lmax)
  2074. {
  2075. nextRef = link->ref;
  2076. break;
  2077. }
  2078. }
  2079. }
  2080. if (!nextRef)
  2081. {
  2082. // No neighbour, we hit a wall.
  2083. // Calculate hit normal.
  2084. const int a = segMax;
  2085. const int b = segMax+1 < nv ? segMax+1 : 0;
  2086. const float* va = &verts[a*3];
  2087. const float* vb = &verts[b*3];
  2088. const float dx = vb[0] - va[0];
  2089. const float dz = vb[2] - va[2];
  2090. hitNormal[0] = dz;
  2091. hitNormal[1] = 0;
  2092. hitNormal[2] = -dx;
  2093. dtVnormalize(hitNormal);
  2094. if (pathCount)
  2095. *pathCount = n;
  2096. return status;
  2097. }
  2098. // No hit, advance to neighbour polygon.
  2099. curRef = nextRef;
  2100. }
  2101. if (pathCount)
  2102. *pathCount = n;
  2103. return status;
  2104. }
  2105. /// @par
  2106. ///
  2107. /// At least one result array must be provided.
  2108. ///
  2109. /// The order of the result set is from least to highest cost to reach the polygon.
  2110. ///
  2111. /// A common use case for this method is to perform Dijkstra searches.
  2112. /// Candidate polygons are found by searching the graph beginning at the start polygon.
  2113. ///
  2114. /// If a polygon is not found via the graph search, even if it intersects the
  2115. /// search circle, it will not be included in the result set. For example:
  2116. ///
  2117. /// polyA is the start polygon.
  2118. /// polyB shares an edge with polyA. (Is adjacent.)
  2119. /// polyC shares an edge with polyB, but not with polyA
  2120. /// Even if the search circle overlaps polyC, it will not be included in the
  2121. /// result set unless polyB is also in the set.
  2122. ///
  2123. /// The value of the center point is used as the start position for cost
  2124. /// calculations. It is not projected onto the surface of the mesh, so its
  2125. /// y-value will effect the costs.
  2126. ///
  2127. /// Intersection tests occur in 2D. All polygons and the search circle are
  2128. /// projected onto the xz-plane. So the y-value of the center point does not
  2129. /// effect intersection tests.
  2130. ///
  2131. /// If the result arrays are to small to hold the entire result set, they will be
  2132. /// filled to capacity.
  2133. ///
  2134. dtStatus dtNavMeshQuery::findPolysAroundCircle(dtPolyRef startRef, const float* centerPos, const float radius,
  2135. const dtQueryFilter* filter,
  2136. dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
  2137. int* resultCount, const int maxResult) const
  2138. {
  2139. dtAssert(m_nav);
  2140. dtAssert(m_nodePool);
  2141. dtAssert(m_openList);
  2142. *resultCount = 0;
  2143. // Validate input
  2144. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2145. return DT_FAILURE | DT_INVALID_PARAM;
  2146. m_nodePool->clear();
  2147. m_openList->clear();
  2148. dtNode* startNode = m_nodePool->getNode(startRef);
  2149. dtVcopy(startNode->pos, centerPos);
  2150. startNode->pidx = 0;
  2151. startNode->cost = 0;
  2152. startNode->total = 0;
  2153. startNode->id = startRef;
  2154. startNode->flags = DT_NODE_OPEN;
  2155. m_openList->push(startNode);
  2156. dtStatus status = DT_SUCCESS;
  2157. int n = 0;
  2158. if (n < maxResult)
  2159. {
  2160. if (resultRef)
  2161. resultRef[n] = startNode->id;
  2162. if (resultParent)
  2163. resultParent[n] = 0;
  2164. if (resultCost)
  2165. resultCost[n] = 0;
  2166. ++n;
  2167. }
  2168. else
  2169. {
  2170. status |= DT_BUFFER_TOO_SMALL;
  2171. }
  2172. const float radiusSqr = dtSqr(radius);
  2173. while (!m_openList->empty())
  2174. {
  2175. dtNode* bestNode = m_openList->pop();
  2176. bestNode->flags &= ~DT_NODE_OPEN;
  2177. bestNode->flags |= DT_NODE_CLOSED;
  2178. // Get poly and tile.
  2179. // The API input has been cheked already, skip checking internal data.
  2180. const dtPolyRef bestRef = bestNode->id;
  2181. const dtMeshTile* bestTile = 0;
  2182. const dtPoly* bestPoly = 0;
  2183. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  2184. // Get parent poly and tile.
  2185. dtPolyRef parentRef = 0;
  2186. const dtMeshTile* parentTile = 0;
  2187. const dtPoly* parentPoly = 0;
  2188. if (bestNode->pidx)
  2189. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  2190. if (parentRef)
  2191. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  2192. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  2193. {
  2194. const dtLink* link = &bestTile->links[i];
  2195. dtPolyRef neighbourRef = link->ref;
  2196. // Skip invalid neighbours and do not follow back to parent.
  2197. if (!neighbourRef || neighbourRef == parentRef)
  2198. continue;
  2199. // Expand to neighbour
  2200. const dtMeshTile* neighbourTile = 0;
  2201. const dtPoly* neighbourPoly = 0;
  2202. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  2203. // Do not advance if the polygon is excluded by the filter.
  2204. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  2205. continue;
  2206. // Find edge and calc distance to the edge.
  2207. float va[3], vb[3];
  2208. if (!getPortalPoints(bestRef, bestPoly, bestTile, neighbourRef, neighbourPoly, neighbourTile, va, vb))
  2209. continue;
  2210. // If the circle is not touching the next polygon, skip it.
  2211. float tseg;
  2212. float distSqr = dtDistancePtSegSqr2D(centerPos, va, vb, tseg);
  2213. if (distSqr > radiusSqr)
  2214. continue;
  2215. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  2216. if (!neighbourNode)
  2217. {
  2218. status |= DT_OUT_OF_NODES;
  2219. continue;
  2220. }
  2221. if (neighbourNode->flags & DT_NODE_CLOSED)
  2222. continue;
  2223. // Cost
  2224. if (neighbourNode->flags == 0)
  2225. dtVlerp(neighbourNode->pos, va, vb, 0.5f);
  2226. const float total = bestNode->total + dtVdist(bestNode->pos, neighbourNode->pos);
  2227. // The node is already in open list and the new result is worse, skip.
  2228. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  2229. continue;
  2230. neighbourNode->id = neighbourRef;
  2231. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  2232. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  2233. neighbourNode->total = total;
  2234. if (neighbourNode->flags & DT_NODE_OPEN)
  2235. {
  2236. m_openList->modify(neighbourNode);
  2237. }
  2238. else
  2239. {
  2240. if (n < maxResult)
  2241. {
  2242. if (resultRef)
  2243. resultRef[n] = neighbourNode->id;
  2244. if (resultParent)
  2245. resultParent[n] = m_nodePool->getNodeAtIdx(neighbourNode->pidx)->id;
  2246. if (resultCost)
  2247. resultCost[n] = neighbourNode->total;
  2248. ++n;
  2249. }
  2250. else
  2251. {
  2252. status |= DT_BUFFER_TOO_SMALL;
  2253. }
  2254. neighbourNode->flags = DT_NODE_OPEN;
  2255. m_openList->push(neighbourNode);
  2256. }
  2257. }
  2258. }
  2259. *resultCount = n;
  2260. return status;
  2261. }
  2262. /// @par
  2263. ///
  2264. /// The order of the result set is from least to highest cost.
  2265. ///
  2266. /// At least one result array must be provided.
  2267. ///
  2268. /// A common use case for this method is to perform Dijkstra searches.
  2269. /// Candidate polygons are found by searching the graph beginning at the start
  2270. /// polygon.
  2271. ///
  2272. /// The same intersection test restrictions that apply to findPolysAroundCircle()
  2273. /// method apply to this method.
  2274. ///
  2275. /// The 3D centroid of the search polygon is used as the start position for cost
  2276. /// calculations.
  2277. ///
  2278. /// Intersection tests occur in 2D. All polygons are projected onto the
  2279. /// xz-plane. So the y-values of the vertices do not effect intersection tests.
  2280. ///
  2281. /// If the result arrays are is too small to hold the entire result set, they will
  2282. /// be filled to capacity.
  2283. ///
  2284. dtStatus dtNavMeshQuery::findPolysAroundShape(dtPolyRef startRef, const float* verts, const int nverts,
  2285. const dtQueryFilter* filter,
  2286. dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
  2287. int* resultCount, const int maxResult) const
  2288. {
  2289. dtAssert(m_nav);
  2290. dtAssert(m_nodePool);
  2291. dtAssert(m_openList);
  2292. *resultCount = 0;
  2293. // Validate input
  2294. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2295. return DT_FAILURE | DT_INVALID_PARAM;
  2296. m_nodePool->clear();
  2297. m_openList->clear();
  2298. float centerPos[3] = {0,0,0};
  2299. for (int i = 0; i < nverts; ++i)
  2300. dtVadd(centerPos,centerPos,&verts[i*3]);
  2301. dtVscale(centerPos,centerPos,1.0f/nverts);
  2302. dtNode* startNode = m_nodePool->getNode(startRef);
  2303. dtVcopy(startNode->pos, centerPos);
  2304. startNode->pidx = 0;
  2305. startNode->cost = 0;
  2306. startNode->total = 0;
  2307. startNode->id = startRef;
  2308. startNode->flags = DT_NODE_OPEN;
  2309. m_openList->push(startNode);
  2310. dtStatus status = DT_SUCCESS;
  2311. int n = 0;
  2312. if (n < maxResult)
  2313. {
  2314. if (resultRef)
  2315. resultRef[n] = startNode->id;
  2316. if (resultParent)
  2317. resultParent[n] = 0;
  2318. if (resultCost)
  2319. resultCost[n] = 0;
  2320. ++n;
  2321. }
  2322. else
  2323. {
  2324. status |= DT_BUFFER_TOO_SMALL;
  2325. }
  2326. while (!m_openList->empty())
  2327. {
  2328. dtNode* bestNode = m_openList->pop();
  2329. bestNode->flags &= ~DT_NODE_OPEN;
  2330. bestNode->flags |= DT_NODE_CLOSED;
  2331. // Get poly and tile.
  2332. // The API input has been cheked already, skip checking internal data.
  2333. const dtPolyRef bestRef = bestNode->id;
  2334. const dtMeshTile* bestTile = 0;
  2335. const dtPoly* bestPoly = 0;
  2336. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  2337. // Get parent poly and tile.
  2338. dtPolyRef parentRef = 0;
  2339. const dtMeshTile* parentTile = 0;
  2340. const dtPoly* parentPoly = 0;
  2341. if (bestNode->pidx)
  2342. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  2343. if (parentRef)
  2344. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  2345. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  2346. {
  2347. const dtLink* link = &bestTile->links[i];
  2348. dtPolyRef neighbourRef = link->ref;
  2349. // Skip invalid neighbours and do not follow back to parent.
  2350. if (!neighbourRef || neighbourRef == parentRef)
  2351. continue;
  2352. // Expand to neighbour
  2353. const dtMeshTile* neighbourTile = 0;
  2354. const dtPoly* neighbourPoly = 0;
  2355. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  2356. // Do not advance if the polygon is excluded by the filter.
  2357. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  2358. continue;
  2359. // Find edge and calc distance to the edge.
  2360. float va[3], vb[3];
  2361. if (!getPortalPoints(bestRef, bestPoly, bestTile, neighbourRef, neighbourPoly, neighbourTile, va, vb))
  2362. continue;
  2363. // If the poly is not touching the edge to the next polygon, skip the connection it.
  2364. float tmin, tmax;
  2365. int segMin, segMax;
  2366. if (!dtIntersectSegmentPoly2D(va, vb, verts, nverts, tmin, tmax, segMin, segMax))
  2367. continue;
  2368. if (tmin > 1.0f || tmax < 0.0f)
  2369. continue;
  2370. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  2371. if (!neighbourNode)
  2372. {
  2373. status |= DT_OUT_OF_NODES;
  2374. continue;
  2375. }
  2376. if (neighbourNode->flags & DT_NODE_CLOSED)
  2377. continue;
  2378. // Cost
  2379. if (neighbourNode->flags == 0)
  2380. dtVlerp(neighbourNode->pos, va, vb, 0.5f);
  2381. const float total = bestNode->total + dtVdist(bestNode->pos, neighbourNode->pos);
  2382. // The node is already in open list and the new result is worse, skip.
  2383. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  2384. continue;
  2385. neighbourNode->id = neighbourRef;
  2386. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  2387. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  2388. neighbourNode->total = total;
  2389. if (neighbourNode->flags & DT_NODE_OPEN)
  2390. {
  2391. m_openList->modify(neighbourNode);
  2392. }
  2393. else
  2394. {
  2395. if (n < maxResult)
  2396. {
  2397. if (resultRef)
  2398. resultRef[n] = neighbourNode->id;
  2399. if (resultParent)
  2400. resultParent[n] = m_nodePool->getNodeAtIdx(neighbourNode->pidx)->id;
  2401. if (resultCost)
  2402. resultCost[n] = neighbourNode->total;
  2403. ++n;
  2404. }
  2405. else
  2406. {
  2407. status |= DT_BUFFER_TOO_SMALL;
  2408. }
  2409. neighbourNode->flags = DT_NODE_OPEN;
  2410. m_openList->push(neighbourNode);
  2411. }
  2412. }
  2413. }
  2414. *resultCount = n;
  2415. return status;
  2416. }
  2417. /// @par
  2418. ///
  2419. /// This method is optimized for a small search radius and small number of result
  2420. /// polygons.
  2421. ///
  2422. /// Candidate polygons are found by searching the navigation graph beginning at
  2423. /// the start polygon.
  2424. ///
  2425. /// The same intersection test restrictions that apply to the findPolysAroundCircle
  2426. /// mehtod applies to this method.
  2427. ///
  2428. /// The value of the center point is used as the start point for cost calculations.
  2429. /// It is not projected onto the surface of the mesh, so its y-value will effect
  2430. /// the costs.
  2431. ///
  2432. /// Intersection tests occur in 2D. All polygons and the search circle are
  2433. /// projected onto the xz-plane. So the y-value of the center point does not
  2434. /// effect intersection tests.
  2435. ///
  2436. /// If the result arrays are is too small to hold the entire result set, they will
  2437. /// be filled to capacity.
  2438. ///
  2439. dtStatus dtNavMeshQuery::findLocalNeighbourhood(dtPolyRef startRef, const float* centerPos, const float radius,
  2440. const dtQueryFilter* filter,
  2441. dtPolyRef* resultRef, dtPolyRef* resultParent,
  2442. int* resultCount, const int maxResult) const
  2443. {
  2444. dtAssert(m_nav);
  2445. dtAssert(m_tinyNodePool);
  2446. *resultCount = 0;
  2447. // Validate input
  2448. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2449. return DT_FAILURE | DT_INVALID_PARAM;
  2450. static const int MAX_STACK = 48;
  2451. dtNode* stack[MAX_STACK];
  2452. int nstack = 0;
  2453. m_tinyNodePool->clear();
  2454. dtNode* startNode = m_tinyNodePool->getNode(startRef);
  2455. startNode->pidx = 0;
  2456. startNode->id = startRef;
  2457. startNode->flags = DT_NODE_CLOSED;
  2458. stack[nstack++] = startNode;
  2459. const float radiusSqr = dtSqr(radius);
  2460. float pa[DT_VERTS_PER_POLYGON*3];
  2461. float pb[DT_VERTS_PER_POLYGON*3];
  2462. dtStatus status = DT_SUCCESS;
  2463. int n = 0;
  2464. if (n < maxResult)
  2465. {
  2466. resultRef[n] = startNode->id;
  2467. if (resultParent)
  2468. resultParent[n] = 0;
  2469. ++n;
  2470. }
  2471. else
  2472. {
  2473. status |= DT_BUFFER_TOO_SMALL;
  2474. }
  2475. while (nstack)
  2476. {
  2477. // Pop front.
  2478. dtNode* curNode = stack[0];
  2479. for (int i = 0; i < nstack-1; ++i)
  2480. stack[i] = stack[i+1];
  2481. nstack--;
  2482. // Get poly and tile.
  2483. // The API input has been cheked already, skip checking internal data.
  2484. const dtPolyRef curRef = curNode->id;
  2485. const dtMeshTile* curTile = 0;
  2486. const dtPoly* curPoly = 0;
  2487. m_nav->getTileAndPolyByRefUnsafe(curRef, &curTile, &curPoly);
  2488. for (unsigned int i = curPoly->firstLink; i != DT_NULL_LINK; i = curTile->links[i].next)
  2489. {
  2490. const dtLink* link = &curTile->links[i];
  2491. dtPolyRef neighbourRef = link->ref;
  2492. // Skip invalid neighbours.
  2493. if (!neighbourRef)
  2494. continue;
  2495. // Skip if cannot alloca more nodes.
  2496. dtNode* neighbourNode = m_tinyNodePool->getNode(neighbourRef);
  2497. if (!neighbourNode)
  2498. continue;
  2499. // Skip visited.
  2500. if (neighbourNode->flags & DT_NODE_CLOSED)
  2501. continue;
  2502. // Expand to neighbour
  2503. const dtMeshTile* neighbourTile = 0;
  2504. const dtPoly* neighbourPoly = 0;
  2505. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  2506. // Skip off-mesh connections.
  2507. if (neighbourPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  2508. continue;
  2509. // Do not advance if the polygon is excluded by the filter.
  2510. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  2511. continue;
  2512. // Find edge and calc distance to the edge.
  2513. float va[3], vb[3];
  2514. if (!getPortalPoints(curRef, curPoly, curTile, neighbourRef, neighbourPoly, neighbourTile, va, vb))
  2515. continue;
  2516. // If the circle is not touching the next polygon, skip it.
  2517. float tseg;
  2518. float distSqr = dtDistancePtSegSqr2D(centerPos, va, vb, tseg);
  2519. if (distSqr > radiusSqr)
  2520. continue;
  2521. // Mark node visited, this is done before the overlap test so that
  2522. // we will not visit the poly again if the test fails.
  2523. neighbourNode->flags |= DT_NODE_CLOSED;
  2524. neighbourNode->pidx = m_tinyNodePool->getNodeIdx(curNode);
  2525. // Check that the polygon does not collide with existing polygons.
  2526. // Collect vertices of the neighbour poly.
  2527. const int npa = neighbourPoly->vertCount;
  2528. for (int k = 0; k < npa; ++k)
  2529. dtVcopy(&pa[k*3], &neighbourTile->verts[neighbourPoly->verts[k]*3]);
  2530. bool overlap = false;
  2531. for (int j = 0; j < n; ++j)
  2532. {
  2533. dtPolyRef pastRef = resultRef[j];
  2534. // Connected polys do not overlap.
  2535. bool connected = false;
  2536. for (unsigned int k = curPoly->firstLink; k != DT_NULL_LINK; k = curTile->links[k].next)
  2537. {
  2538. if (curTile->links[k].ref == pastRef)
  2539. {
  2540. connected = true;
  2541. break;
  2542. }
  2543. }
  2544. if (connected)
  2545. continue;
  2546. // Potentially overlapping.
  2547. const dtMeshTile* pastTile = 0;
  2548. const dtPoly* pastPoly = 0;
  2549. m_nav->getTileAndPolyByRefUnsafe(pastRef, &pastTile, &pastPoly);
  2550. // Get vertices and test overlap
  2551. const int npb = pastPoly->vertCount;
  2552. for (int k = 0; k < npb; ++k)
  2553. dtVcopy(&pb[k*3], &pastTile->verts[pastPoly->verts[k]*3]);
  2554. if (dtOverlapPolyPoly2D(pa,npa, pb,npb))
  2555. {
  2556. overlap = true;
  2557. break;
  2558. }
  2559. }
  2560. if (overlap)
  2561. continue;
  2562. // This poly is fine, store and advance to the poly.
  2563. if (n < maxResult)
  2564. {
  2565. resultRef[n] = neighbourRef;
  2566. if (resultParent)
  2567. resultParent[n] = curRef;
  2568. ++n;
  2569. }
  2570. else
  2571. {
  2572. status |= DT_BUFFER_TOO_SMALL;
  2573. }
  2574. if (nstack < MAX_STACK)
  2575. {
  2576. stack[nstack++] = neighbourNode;
  2577. }
  2578. }
  2579. }
  2580. *resultCount = n;
  2581. return status;
  2582. }
  2583. struct dtSegInterval
  2584. {
  2585. dtPolyRef ref;
  2586. short tmin, tmax;
  2587. };
  2588. static void insertInterval(dtSegInterval* ints, int& nints, const int maxInts,
  2589. const short tmin, const short tmax, const dtPolyRef ref)
  2590. {
  2591. if (nints+1 > maxInts) return;
  2592. // Find insertion point.
  2593. int idx = 0;
  2594. while (idx < nints)
  2595. {
  2596. if (tmax <= ints[idx].tmin)
  2597. break;
  2598. idx++;
  2599. }
  2600. // Move current results.
  2601. if (nints-idx)
  2602. memmove(ints+idx+1, ints+idx, sizeof(dtSegInterval)*(nints-idx));
  2603. // Store
  2604. ints[idx].ref = ref;
  2605. ints[idx].tmin = tmin;
  2606. ints[idx].tmax = tmax;
  2607. nints++;
  2608. }
  2609. /// @par
  2610. ///
  2611. /// If the @p segmentRefs parameter is provided, then all polygon segments will be returned.
  2612. /// Otherwise only the wall segments are returned.
  2613. ///
  2614. /// A segment that is normally a portal will be included in the result set as a
  2615. /// wall if the @p filter results in the neighbor polygon becoomming impassable.
  2616. ///
  2617. /// The @p segmentVerts and @p segmentRefs buffers should normally be sized for the
  2618. /// maximum segments per polygon of the source navigation mesh.
  2619. ///
  2620. dtStatus dtNavMeshQuery::getPolyWallSegments(dtPolyRef ref, const dtQueryFilter* filter,
  2621. float* segmentVerts, dtPolyRef* segmentRefs, int* segmentCount,
  2622. const int maxSegments) const
  2623. {
  2624. dtAssert(m_nav);
  2625. *segmentCount = 0;
  2626. const dtMeshTile* tile = 0;
  2627. const dtPoly* poly = 0;
  2628. if (dtStatusFailed(m_nav->getTileAndPolyByRef(ref, &tile, &poly)))
  2629. return DT_FAILURE | DT_INVALID_PARAM;
  2630. int n = 0;
  2631. static const int MAX_INTERVAL = 16;
  2632. dtSegInterval ints[MAX_INTERVAL];
  2633. int nints;
  2634. const bool storePortals = segmentRefs != 0;
  2635. dtStatus status = DT_SUCCESS;
  2636. for (int i = 0, j = (int)poly->vertCount-1; i < (int)poly->vertCount; j = i++)
  2637. {
  2638. // Skip non-solid edges.
  2639. nints = 0;
  2640. if (poly->neis[j] & DT_EXT_LINK)
  2641. {
  2642. // Tile border.
  2643. for (unsigned int k = poly->firstLink; k != DT_NULL_LINK; k = tile->links[k].next)
  2644. {
  2645. const dtLink* link = &tile->links[k];
  2646. if (link->edge == j)
  2647. {
  2648. if (link->ref != 0)
  2649. {
  2650. const dtMeshTile* neiTile = 0;
  2651. const dtPoly* neiPoly = 0;
  2652. m_nav->getTileAndPolyByRefUnsafe(link->ref, &neiTile, &neiPoly);
  2653. if (filter->passFilter(link->ref, neiTile, neiPoly))
  2654. {
  2655. insertInterval(ints, nints, MAX_INTERVAL, link->bmin, link->bmax, link->ref);
  2656. }
  2657. }
  2658. }
  2659. }
  2660. }
  2661. else
  2662. {
  2663. // Internal edge
  2664. dtPolyRef neiRef = 0;
  2665. if (poly->neis[j])
  2666. {
  2667. const unsigned int idx = (unsigned int)(poly->neis[j]-1);
  2668. neiRef = m_nav->getPolyRefBase(tile) | idx;
  2669. if (!filter->passFilter(neiRef, tile, &tile->polys[idx]))
  2670. neiRef = 0;
  2671. }
  2672. // If the edge leads to another polygon and portals are not stored, skip.
  2673. if (neiRef != 0 && !storePortals)
  2674. continue;
  2675. if (n < maxSegments)
  2676. {
  2677. const float* vj = &tile->verts[poly->verts[j]*3];
  2678. const float* vi = &tile->verts[poly->verts[i]*3];
  2679. float* seg = &segmentVerts[n*6];
  2680. dtVcopy(seg+0, vj);
  2681. dtVcopy(seg+3, vi);
  2682. if (segmentRefs)
  2683. segmentRefs[n] = neiRef;
  2684. n++;
  2685. }
  2686. else
  2687. {
  2688. status |= DT_BUFFER_TOO_SMALL;
  2689. }
  2690. continue;
  2691. }
  2692. // Add sentinels
  2693. insertInterval(ints, nints, MAX_INTERVAL, -1, 0, 0);
  2694. insertInterval(ints, nints, MAX_INTERVAL, 255, 256, 0);
  2695. // Store segments.
  2696. const float* vj = &tile->verts[poly->verts[j]*3];
  2697. const float* vi = &tile->verts[poly->verts[i]*3];
  2698. for (int k = 1; k < nints; ++k)
  2699. {
  2700. // Portal segment.
  2701. if (storePortals && ints[k].ref)
  2702. {
  2703. const float tmin = ints[k].tmin/255.0f;
  2704. const float tmax = ints[k].tmax/255.0f;
  2705. if (n < maxSegments)
  2706. {
  2707. float* seg = &segmentVerts[n*6];
  2708. dtVlerp(seg+0, vj,vi, tmin);
  2709. dtVlerp(seg+3, vj,vi, tmax);
  2710. if (segmentRefs)
  2711. segmentRefs[n] = ints[k].ref;
  2712. n++;
  2713. }
  2714. else
  2715. {
  2716. status |= DT_BUFFER_TOO_SMALL;
  2717. }
  2718. }
  2719. // Wall segment.
  2720. const int imin = ints[k-1].tmax;
  2721. const int imax = ints[k].tmin;
  2722. if (imin != imax)
  2723. {
  2724. const float tmin = imin/255.0f;
  2725. const float tmax = imax/255.0f;
  2726. if (n < maxSegments)
  2727. {
  2728. float* seg = &segmentVerts[n*6];
  2729. dtVlerp(seg+0, vj,vi, tmin);
  2730. dtVlerp(seg+3, vj,vi, tmax);
  2731. if (segmentRefs)
  2732. segmentRefs[n] = 0;
  2733. n++;
  2734. }
  2735. else
  2736. {
  2737. status |= DT_BUFFER_TOO_SMALL;
  2738. }
  2739. }
  2740. }
  2741. }
  2742. *segmentCount = n;
  2743. return status;
  2744. }
  2745. /// @par
  2746. ///
  2747. /// @p hitPos is not adjusted using the height detail data.
  2748. ///
  2749. /// @p hitDist will equal the search radius if there is no wall within the
  2750. /// radius. In this case the values of @p hitPos and @p hitNormal are
  2751. /// undefined.
  2752. ///
  2753. /// The normal will become unpredicable if @p hitDist is a very small number.
  2754. ///
  2755. dtStatus dtNavMeshQuery::findDistanceToWall(dtPolyRef startRef, const float* centerPos, const float maxRadius,
  2756. const dtQueryFilter* filter,
  2757. float* hitDist, float* hitPos, float* hitNormal) const
  2758. {
  2759. dtAssert(m_nav);
  2760. dtAssert(m_nodePool);
  2761. dtAssert(m_openList);
  2762. // Validate input
  2763. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2764. return DT_FAILURE | DT_INVALID_PARAM;
  2765. m_nodePool->clear();
  2766. m_openList->clear();
  2767. dtNode* startNode = m_nodePool->getNode(startRef);
  2768. dtVcopy(startNode->pos, centerPos);
  2769. startNode->pidx = 0;
  2770. startNode->cost = 0;
  2771. startNode->total = 0;
  2772. startNode->id = startRef;
  2773. startNode->flags = DT_NODE_OPEN;
  2774. m_openList->push(startNode);
  2775. float radiusSqr = dtSqr(maxRadius);
  2776. dtStatus status = DT_SUCCESS;
  2777. while (!m_openList->empty())
  2778. {
  2779. dtNode* bestNode = m_openList->pop();
  2780. bestNode->flags &= ~DT_NODE_OPEN;
  2781. bestNode->flags |= DT_NODE_CLOSED;
  2782. // Get poly and tile.
  2783. // The API input has been cheked already, skip checking internal data.
  2784. const dtPolyRef bestRef = bestNode->id;
  2785. const dtMeshTile* bestTile = 0;
  2786. const dtPoly* bestPoly = 0;
  2787. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  2788. // Get parent poly and tile.
  2789. dtPolyRef parentRef = 0;
  2790. const dtMeshTile* parentTile = 0;
  2791. const dtPoly* parentPoly = 0;
  2792. if (bestNode->pidx)
  2793. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  2794. if (parentRef)
  2795. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  2796. // Hit test walls.
  2797. for (int i = 0, j = (int)bestPoly->vertCount-1; i < (int)bestPoly->vertCount; j = i++)
  2798. {
  2799. // Skip non-solid edges.
  2800. if (bestPoly->neis[j] & DT_EXT_LINK)
  2801. {
  2802. // Tile border.
  2803. bool solid = true;
  2804. for (unsigned int k = bestPoly->firstLink; k != DT_NULL_LINK; k = bestTile->links[k].next)
  2805. {
  2806. const dtLink* link = &bestTile->links[k];
  2807. if (link->edge == j)
  2808. {
  2809. if (link->ref != 0)
  2810. {
  2811. const dtMeshTile* neiTile = 0;
  2812. const dtPoly* neiPoly = 0;
  2813. m_nav->getTileAndPolyByRefUnsafe(link->ref, &neiTile, &neiPoly);
  2814. if (filter->passFilter(link->ref, neiTile, neiPoly))
  2815. solid = false;
  2816. }
  2817. break;
  2818. }
  2819. }
  2820. if (!solid) continue;
  2821. }
  2822. else if (bestPoly->neis[j])
  2823. {
  2824. // Internal edge
  2825. const unsigned int idx = (unsigned int)(bestPoly->neis[j]-1);
  2826. const dtPolyRef ref = m_nav->getPolyRefBase(bestTile) | idx;
  2827. if (filter->passFilter(ref, bestTile, &bestTile->polys[idx]))
  2828. continue;
  2829. }
  2830. // Calc distance to the edge.
  2831. const float* vj = &bestTile->verts[bestPoly->verts[j]*3];
  2832. const float* vi = &bestTile->verts[bestPoly->verts[i]*3];
  2833. float tseg;
  2834. float distSqr = dtDistancePtSegSqr2D(centerPos, vj, vi, tseg);
  2835. // Edge is too far, skip.
  2836. if (distSqr > radiusSqr)
  2837. continue;
  2838. // Hit wall, update radius.
  2839. radiusSqr = distSqr;
  2840. // Calculate hit pos.
  2841. hitPos[0] = vj[0] + (vi[0] - vj[0])*tseg;
  2842. hitPos[1] = vj[1] + (vi[1] - vj[1])*tseg;
  2843. hitPos[2] = vj[2] + (vi[2] - vj[2])*tseg;
  2844. }
  2845. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  2846. {
  2847. const dtLink* link = &bestTile->links[i];
  2848. dtPolyRef neighbourRef = link->ref;
  2849. // Skip invalid neighbours and do not follow back to parent.
  2850. if (!neighbourRef || neighbourRef == parentRef)
  2851. continue;
  2852. // Expand to neighbour.
  2853. const dtMeshTile* neighbourTile = 0;
  2854. const dtPoly* neighbourPoly = 0;
  2855. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  2856. // Skip off-mesh connections.
  2857. if (neighbourPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  2858. continue;
  2859. // Calc distance to the edge.
  2860. const float* va = &bestTile->verts[bestPoly->verts[link->edge]*3];
  2861. const float* vb = &bestTile->verts[bestPoly->verts[(link->edge+1) % bestPoly->vertCount]*3];
  2862. float tseg;
  2863. float distSqr = dtDistancePtSegSqr2D(centerPos, va, vb, tseg);
  2864. // If the circle is not touching the next polygon, skip it.
  2865. if (distSqr > radiusSqr)
  2866. continue;
  2867. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  2868. continue;
  2869. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  2870. if (!neighbourNode)
  2871. {
  2872. status |= DT_OUT_OF_NODES;
  2873. continue;
  2874. }
  2875. if (neighbourNode->flags & DT_NODE_CLOSED)
  2876. continue;
  2877. // Cost
  2878. if (neighbourNode->flags == 0)
  2879. {
  2880. getEdgeMidPoint(bestRef, bestPoly, bestTile,
  2881. neighbourRef, neighbourPoly, neighbourTile, neighbourNode->pos);
  2882. }
  2883. const float total = bestNode->total + dtVdist(bestNode->pos, neighbourNode->pos);
  2884. // The node is already in open list and the new result is worse, skip.
  2885. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  2886. continue;
  2887. neighbourNode->id = neighbourRef;
  2888. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  2889. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  2890. neighbourNode->total = total;
  2891. if (neighbourNode->flags & DT_NODE_OPEN)
  2892. {
  2893. m_openList->modify(neighbourNode);
  2894. }
  2895. else
  2896. {
  2897. neighbourNode->flags |= DT_NODE_OPEN;
  2898. m_openList->push(neighbourNode);
  2899. }
  2900. }
  2901. }
  2902. // Calc hit normal.
  2903. dtVsub(hitNormal, centerPos, hitPos);
  2904. dtVnormalize(hitNormal);
  2905. *hitDist = sqrtf(radiusSqr);
  2906. return status;
  2907. }
  2908. bool dtNavMeshQuery::isValidPolyRef(dtPolyRef ref, const dtQueryFilter* filter) const
  2909. {
  2910. const dtMeshTile* tile = 0;
  2911. const dtPoly* poly = 0;
  2912. dtStatus status = m_nav->getTileAndPolyByRef(ref, &tile, &poly);
  2913. // If cannot get polygon, assume it does not exists and boundary is invalid.
  2914. if (dtStatusFailed(status))
  2915. return false;
  2916. // If cannot pass filter, assume flags has changed and boundary is invalid.
  2917. if (!filter->passFilter(ref, tile, poly))
  2918. return false;
  2919. return true;
  2920. }
  2921. /// @par
  2922. ///
  2923. /// The closed list is the list of polygons that were fully evaluated during
  2924. /// the last navigation graph search. (A* or Dijkstra)
  2925. ///
  2926. bool dtNavMeshQuery::isInClosedList(dtPolyRef ref) const
  2927. {
  2928. if (!m_nodePool) return false;
  2929. const dtNode* node = m_nodePool->findNode(ref);
  2930. return node && node->flags & DT_NODE_CLOSED;
  2931. }