@@ -258,6 +258,41 @@ private static IEnumerable<GridVoxelSet> TraceLineIterator(
258258 SwiftDictionary < VoxelGrid , SwiftList < Voxel > > gridVoxelMapping = new ( ) ;
259259 SwiftHashSet < Voxel > voxelRedundancyCheck = SwiftHashSetPool < Voxel > . Shared . Rent ( ) ;
260260
261+ try
262+ {
263+ AddTraceLineVoxelsToMapping (
264+ world ,
265+ start ,
266+ end ,
267+ padding ,
268+ gridVoxelMapping ,
269+ voxelRedundancyCheck ) ;
270+
271+ AddTraceLineEndVoxel (
272+ world ,
273+ end ,
274+ includeEnd ,
275+ gridVoxelMapping ,
276+ voxelRedundancyCheck ) ;
277+
278+ foreach ( KeyValuePair < VoxelGrid , SwiftList < Voxel > > kvp in gridVoxelMapping )
279+ yield return new GridVoxelSet ( kvp . Key , kvp . Value ) ;
280+ }
281+ finally
282+ {
283+ ReleaseTraceLineMapping ( gridVoxelMapping ) ;
284+ SwiftHashSetPool < Voxel > . Shared . Release ( voxelRedundancyCheck ) ;
285+ }
286+ }
287+
288+ private static void AddTraceLineVoxelsToMapping (
289+ GridWorld world ,
290+ Vector3d start ,
291+ Vector3d end ,
292+ Fixed64 ? padding ,
293+ SwiftDictionary < VoxelGrid , SwiftList < Voxel > > gridVoxelMapping ,
294+ SwiftHashSet < Voxel > voxelRedundancyCheck )
295+ {
261296 ( Vector3d snappedMin , Vector3d snappedMax ) =
262297 world . SnapBoundsToVoxelSize ( start , end , padding ) ;
263298
@@ -273,9 +308,7 @@ private static IEnumerable<GridVoxelSet> TraceLineIterator(
273308
274309 Vector3d diff = traceEnd - traceStart ;
275310 Vector3d delta = Vector3d . Abs ( diff ) ;
276-
277311 Fixed64 steps = FixedMath . Ceiling ( FixedMath . Max ( FixedMath . Max ( delta . x , delta . y ) , delta . z ) ) ;
278-
279312 Fixed64 stepX = diff . x / ( steps + Fixed64 . One ) ;
280313 Fixed64 stepY = diff . y / ( steps + Fixed64 . One ) ;
281314 Fixed64 stepZ = diff . z / ( steps + Fixed64 . One ) ;
@@ -285,49 +318,103 @@ private static IEnumerable<GridVoxelSet> TraceLineIterator(
285318 if ( ! world . SpatialGridHash . TryGetValue ( cellIndex , out SwiftHashSet < ushort > gridList ) )
286319 continue ;
287320
288- foreach ( ushort gridIndex in gridList )
289- {
290- if ( ! world . ActiveGrids . IsAllocated ( gridIndex ) )
291- continue ;
292-
293- VoxelGrid currentGrid = world . ActiveGrids [ gridIndex ] ;
294- if ( gridVoxelMapping . ContainsKey ( currentGrid ) )
295- continue ;
321+ AddTraceLineVoxelsForCell (
322+ world ,
323+ gridList ,
324+ traceStart ,
325+ steps ,
326+ stepX ,
327+ stepY ,
328+ stepZ ,
329+ gridVoxelMapping ,
330+ voxelRedundancyCheck ) ;
331+ }
332+ }
296333
297- SwiftList < Voxel > voxelList = SwiftListPool < Voxel > . Shared . Rent ( ) ;
298- gridVoxelMapping . Add ( currentGrid , voxelList ) ;
334+ private static void AddTraceLineVoxelsForCell (
335+ GridWorld world ,
336+ SwiftHashSet < ushort > gridList ,
337+ Vector3d traceStart ,
338+ Fixed64 steps ,
339+ Fixed64 stepX ,
340+ Fixed64 stepY ,
341+ Fixed64 stepZ ,
342+ SwiftDictionary < VoxelGrid , SwiftList < Voxel > > gridVoxelMapping ,
343+ SwiftHashSet < Voxel > voxelRedundancyCheck )
344+ {
345+ foreach ( ushort gridIndex in gridList )
346+ {
347+ if ( ! world . ActiveGrids . IsAllocated ( gridIndex ) )
348+ continue ;
299349
300- for ( Fixed64 i = Fixed64 . Zero ; i <= steps ; i += Fixed64 . One )
301- {
302- Vector3d tracePos = world . FloorToVoxelSize (
303- new Vector3d (
304- traceStart . x + stepX * i ,
305- traceStart . y + stepY * i ,
306- traceStart . z + stepZ * i ) ) ;
350+ VoxelGrid currentGrid = world . ActiveGrids [ gridIndex ] ;
351+ if ( gridVoxelMapping . ContainsKey ( currentGrid ) )
352+ continue ;
307353
308- if ( ! currentGrid . TryGetVoxel ( tracePos , out Voxel ? voxel ) || voxelRedundancyCheck . Add ( voxel ! ) != true )
309- continue ;
354+ SwiftList < Voxel > voxelList = SwiftListPool < Voxel > . Shared . Rent ( ) ;
355+ gridVoxelMapping . Add ( currentGrid , voxelList ) ;
310356
311- voxelList . Add ( voxel ! ) ;
312- }
313- }
357+ AddTraceLineGridVoxels (
358+ world ,
359+ currentGrid ,
360+ traceStart ,
361+ steps ,
362+ stepX ,
363+ stepY ,
364+ stepZ ,
365+ voxelList ,
366+ voxelRedundancyCheck ) ;
314367 }
368+ }
315369
316- if ( includeEnd
317- && world . TryGetGridAndVoxel ( end , out VoxelGrid ? endGrid , out Voxel ? endVoxel )
318- && gridVoxelMapping . TryGetValue ( endGrid ! , out SwiftList < Voxel > endVoxelList )
319- && voxelRedundancyCheck . Add ( endVoxel ! ) )
370+ private static void AddTraceLineGridVoxels (
371+ GridWorld world ,
372+ VoxelGrid currentGrid ,
373+ Vector3d traceStart ,
374+ Fixed64 steps ,
375+ Fixed64 stepX ,
376+ Fixed64 stepY ,
377+ Fixed64 stepZ ,
378+ SwiftList < Voxel > voxelList ,
379+ SwiftHashSet < Voxel > voxelRedundancyCheck )
380+ {
381+ for ( Fixed64 i = Fixed64 . Zero ; i <= steps ; i += Fixed64 . One )
320382 {
321- endVoxelList . Add ( endVoxel ! ) ;
383+ Vector3d tracePos = world . FloorToVoxelSize (
384+ new Vector3d (
385+ traceStart . x + stepX * i ,
386+ traceStart . y + stepY * i ,
387+ traceStart . z + stepZ * i ) ) ;
388+
389+ if ( ! currentGrid . TryGetVoxel ( tracePos , out Voxel ? voxel ) || voxelRedundancyCheck . Add ( voxel ! ) != true )
390+ continue ;
391+
392+ voxelList . Add ( voxel ! ) ;
322393 }
394+ }
323395
324- foreach ( KeyValuePair < VoxelGrid , SwiftList < Voxel > > kvp in gridVoxelMapping )
396+ private static void AddTraceLineEndVoxel (
397+ GridWorld world ,
398+ Vector3d end ,
399+ bool includeEnd ,
400+ SwiftDictionary < VoxelGrid , SwiftList < Voxel > > gridVoxelMapping ,
401+ SwiftHashSet < Voxel > voxelRedundancyCheck )
402+ {
403+ if ( ! includeEnd
404+ || ! world . TryGetGridAndVoxel ( end , out VoxelGrid ? endGrid , out Voxel ? endVoxel )
405+ || ! gridVoxelMapping . TryGetValue ( endGrid ! , out SwiftList < Voxel > endVoxelList )
406+ || ! voxelRedundancyCheck . Add ( endVoxel ! ) )
325407 {
326- yield return new GridVoxelSet ( kvp . Key , kvp . Value ) ;
327- SwiftListPool < Voxel > . Shared . Release ( kvp . Value ) ;
408+ return ;
328409 }
329410
330- SwiftHashSetPool < Voxel > . Shared . Release ( voxelRedundancyCheck ) ;
411+ endVoxelList . Add ( endVoxel ! ) ;
412+ }
413+
414+ private static void ReleaseTraceLineMapping ( SwiftDictionary < VoxelGrid , SwiftList < Voxel > > gridVoxelMapping )
415+ {
416+ foreach ( KeyValuePair < VoxelGrid , SwiftList < Voxel > > kvp in gridVoxelMapping )
417+ SwiftListPool < Voxel > . Shared . Release ( kvp . Value ) ;
331418 }
332419
333420 private static IEnumerable < GridVoxelSet > GetCoveredVoxelsIterator (
0 commit comments