@@ -68,6 +68,23 @@ function GraphPathfinder.GraphEdge:getExit(entry)
6868 end
6969end
7070
71+ -- if the vertex is the first or last vertex of the edge, we can use it directly as the entry/exit,
72+ -- otherwise, we split the edge at the vertex so we can use it as an entry/exit point.
73+ --- @return GraphPathfinder.GraphEdge | nil new edge if the edge was split , nil otherwise
74+ function GraphPathfinder .GraphEdge :splitWhenNeeded (closestVertex )
75+ if closestVertex .ix ~= 1 and closestVertex .ix ~= # self then
76+ local newEdge = GraphPathfinder .GraphEdge (self :getDirection ())
77+ for j = closestVertex .ix , # self do
78+ newEdge :append (self [j ])
79+ end
80+ newEdge :calculateProperties ()
81+ self :cutEndAtIx (closestVertex .ix )
82+ -- remember the piece we cut off, we may need it if the path must be extended after the goal
83+ self .cutEdge = newEdge
84+ return newEdge
85+ end
86+ end
87+
7188function GraphPathfinder .GraphEdge :rollUpIterator (entry )
7289 local from , to , step
7390 if entry == self [1 ] then
191208
192209function GraphPathfinder :start (start , goal , turnRadius , ...)
193210 -- at each run, make a copy of the graph as we'll modify it
211+ --- @type GraphPathfinder.GraphEdge[]
194212 self .graph = {}
195213 for _ , e in ipairs (self .originalGraph ) do
196214 if # e > 1 then
238256--- @return State3D the entry vertex of the graph , closest to start
239257--- @return State3D the exit vertex of the graph , closest to goal
240258function GraphPathfinder :createGraphEntryAndExit (start , goal )
241-
242- local function splitEdgeWhenNeeded (edge , closestVertex )
243- -- if the vertex is the first or last vertex of the edge, we can use it directly as the entry/exit,
244- -- otherwise, we split the edge at the vertex so we can use it as an entry/exit point.
245- if closestVertex .ix ~= 1 and closestVertex .ix ~= # edge then
246- self .logger :debug (' Graph entry/exit found and split at vertex %d, x: %.1f y: %.1f' ,
247- closestVertex .ix , closestVertex .x , closestVertex .y )
248- local newEdge = GraphPathfinder .GraphEdge (edge :getDirection ())
249- for j = closestVertex .ix , # edge do
250- newEdge :append (edge [j ])
251- end
252- newEdge :calculateProperties ()
253- table.insert (self .graph , newEdge )
254- edge :cutEndAtIx (closestVertex .ix )
255- return newEdge
256- end
257- end
258-
259259 -- find the edges closest to node. If the closest vertex isn't the first or last vertex of the edge, we split the
260260 -- edge at that vertex so we can use it as an entry/exit point.
261261 local function findClosestEdges (node , isEntry )
@@ -275,8 +275,11 @@ function GraphPathfinder:createGraphEntryAndExit(start, goal)
275275 for i = 1 , # self .graph do
276276 local edge = self .graph [i ]
277277 local vertex , d = edge :findClosestVertexToPoint (node )
278- local newEdge = splitEdgeWhenNeeded ( edge , vertex )
278+ local newEdge = edge : splitWhenNeeded ( vertex )
279279 if newEdge then
280+ self .logger :debug (' Graph entry/exit found and split at vertex %d, x: %.1f y: %.1f' ,
281+ vertex .ix , vertex .x , vertex .y )
282+ table.insert (self .graph , newEdge )
280283 addToClosestEdge (newEdge , newEdge [1 ], d )
281284 end
282285 addToClosestEdge (edge , vertex , d )
@@ -327,7 +330,6 @@ function GraphPathfinder:addTransitions(edges)
327330 end
328331 path :appendMany (edges [# edges ]) -- append the last edge, this will be the exit edge
329332 path :calculateProperties ()
330- path :splitEdges (5 )
331333 path :ensureMinimumRadius (self .turnRadius , false , 0.5 )
332334 return path
333335end
@@ -341,7 +343,7 @@ GraphPathfinder.GraphMotionPrimitives = CpObject(HybridAStar.MotionPrimitives)
341343--- two edges are considered as connected (and thus can traverse from one to the other)
342344--- @param graph Vector[] the graph as described in the file header
343345function GraphPathfinder .GraphMotionPrimitives :init (range , graph )
344- self .logger = Logger (' GraphMotionPrimitives' , Logger .level .trace , CpDebug .DBG_PATHFINDER )
346+ self .logger = Logger (' GraphMotionPrimitives' , Logger .level .debug , CpDebug .DBG_PATHFINDER )
345347 self .range = range
346348 self .graph = graph
347349end
0 commit comments