NextSegmentLockScopeBuilder.cs
27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
using System;
using System.Collections.Generic;
using System.Linq;
using Rcs.Application.Services.PathFind.Models;
using Rcs.Application.Services.PathFind.Realtime;
using Rcs.Domain.Entities;
using Rcs.Domain.Enums;
namespace Rcs.Infrastructure.PathFinding.Services;
/// <summary>
/// 构建“下一段路径”的真实锁定范围,确保发送前预扫描与协议层实际加锁一致。
/// </summary>
public static class NextSegmentLockScopeBuilder
{
/// <summary>
/// 基于当前缓存位置构建下一段锁定范围。
/// </summary>
public static NextSegmentLockScope? Build(
VdaSegmentedPathCache cache,
int junctionIndex,
int resourceIndex,
PathGraph graph,
Robot robot)
{
if (junctionIndex < 0 || junctionIndex >= cache.JunctionSegments.Count)
{
return null;
}
var junction = cache.JunctionSegments[junctionIndex];
if (resourceIndex < 0 || resourceIndex >= junction.ResourceSegments.Count)
{
return null;
}
var dispatchSegments = junction.ResourceSegments[resourceIndex].Segments
.Select(CloneSegment)
.ToList();
if (dispatchSegments.Count == 0)
{
return null;
}
var scopeBaseSegments = resourceIndex == 0
? junction.ResourceSegments.SelectMany(r => r.Segments).ToList()
: dispatchSegments;
var scopeResources = resourceIndex == 0
? junction.ResourceSegments
.Select((segmentCache, index) => (ResourceIndex: index, SegmentCache: segmentCache))
.ToList()
: new List<(int ResourceIndex, VdaSegmentCacheItem SegmentCache)>
{
(resourceIndex, junction.ResourceSegments[resourceIndex])
};
return new NextSegmentLockScope
{
JunctionIndex = junctionIndex,
ResourceIndex = resourceIndex,
DispatchSegments = dispatchSegments,
LockCandidates = BuildSweptLockCandidates(scopeBaseSegments, graph, robot),
BindingPlans = BuildBindingPlans(scopeResources, junctionIndex, graph, robot),
HoldNodeCode = dispatchSegments.FirstOrDefault()?.FromNodeCode,
LocksWholeJunctionScope = resourceIndex == 0
};
}
/// <summary>
/// 基于资源段构建严格 binding 锁计划。
/// </summary>
public static List<VdaLockBindingPlan> BuildBindingPlans(
IEnumerable<(int ResourceIndex, VdaSegmentCacheItem SegmentCache)> scopeResources,
int junctionIndex,
PathGraph graph,
Robot robot)
{
var resources = scopeResources.ToList();
if (resources.Count == 0)
{
return new List<VdaLockBindingPlan>();
}
var nodeLookup = SweptAreaCoverageResolver.BuildNodeLookup(graph);
var edgePolylineLookup = SweptAreaCoverageResolver.BuildEdgePolylineLookup(graph);
var dimensions = SweptAreaCoverageResolver.ResolveDimensions(
robot.Width,
robot.Length,
robot.SafetyDistance,
robot.CoordinateScale);
var edgeByCode = graph.Edges.Values
.Where(edge => !string.IsNullOrWhiteSpace(edge.EdgeCode))
.GroupBy(edge => edge.EdgeCode, StringComparer.OrdinalIgnoreCase)
.ToDictionary(group => group.Key, group => group.First(), StringComparer.OrdinalIgnoreCase);
var result = new List<VdaLockBindingPlan>();
var bindingIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var (currentResourceIndex, segmentCache) in resources)
{
var segments = segmentCache.Segments;
if (segments.Count == 0)
{
continue;
}
var orderIndex = 0;
var firstSegment = segments[0];
var firstFromPoint = TryResolveNodePoint(nodeLookup, firstSegment.FromNodeCode);
if (firstFromPoint != null)
{
var startNodeRadius = SweptAreaCoverageResolver.CalculateNodeSweepRadius(
dimensions.Length,
dimensions.Width,
dimensions.SafetyDistance,
null,
firstSegment.StartTheta,
null,
firstSegment.Angle);
RegisterBindingPlan(
result,
bindingIds,
BuildNodeBindingPlan(
junctionIndex,
currentResourceIndex,
orderIndex++,
firstSegment.FromNodeCode,
ResolveNodeReleaseTrigger(segments, -1, firstSegment.FromNodeCode),
firstFromPoint.Value,
startNodeRadius,
graph,
edgeByCode));
}
for (var segmentIndex = 0; segmentIndex < segments.Count; segmentIndex++)
{
var segment = segments[segmentIndex];
var fromPoint = TryResolveNodePoint(nodeLookup, segment.FromNodeCode);
var toPoint = TryResolveNodePoint(nodeLookup, segment.ToNodeCode);
if (fromPoint == null || toPoint == null)
{
continue;
}
if (HasRealEdgeBinding(segment))
{
var edgePolyline = BuildEdgePolyline(segment, fromPoint.Value, toPoint.Value, edgePolylineLookup);
var edgeRadius = SweptAreaCoverageResolver.CalculateEdgeSweepRadius(
dimensions.Length,
dimensions.Width,
dimensions.SafetyDistance,
segment.Angle);
RegisterBindingPlan(
result,
bindingIds,
BuildEdgeBindingPlan(
junctionIndex,
currentResourceIndex,
orderIndex++,
segment.EdgeCode,
segment.ToNodeCode,
edgePolyline,
edgeRadius,
graph,
edgeByCode));
}
if (IsInPlaceNodeOnlySegment(segment))
{
continue;
}
var nextTheta = segmentIndex + 1 < segments.Count ? segments[segmentIndex + 1].StartTheta : (double?)null;
var nextEdgeAngle = segmentIndex + 1 < segments.Count ? segments[segmentIndex + 1].Angle : (double?)null;
var endNodeRadius = SweptAreaCoverageResolver.CalculateNodeSweepRadius(
dimensions.Length,
dimensions.Width,
dimensions.SafetyDistance,
segment.EndTheta,
nextTheta,
segment.Angle,
nextEdgeAngle);
RegisterBindingPlan(
result,
bindingIds,
BuildNodeBindingPlan(
junctionIndex,
currentResourceIndex,
orderIndex++,
segment.ToNodeCode,
ResolveNodeReleaseTrigger(segments, segmentIndex, segment.ToNodeCode),
toPoint.Value,
endNodeRadius,
graph,
edgeByCode));
}
}
return result;
}
/// <summary>
/// 基于扫掠面扩展锁候选集合。
/// </summary>
public static List<PathSegmentWithCode> BuildSweptLockCandidates(
List<PathSegmentWithCode> baseSegments,
PathGraph graph,
Robot robot)
{
if (baseSegments.Count == 0)
{
return baseSegments;
}
var nodeLookup = SweptAreaCoverageResolver.BuildNodeLookup(graph);
var edgePolylineLookup = SweptAreaCoverageResolver.BuildEdgePolylineLookup(graph);
var dimensions = SweptAreaCoverageResolver.ResolveDimensions(
robot.Width,
robot.Length,
robot.SafetyDistance,
robot.CoordinateScale);
SweptAreaCoverageResult coverage;
if (!dimensions.UseDefaultWidth || !dimensions.UseDefaultLength)
{
var zones = SweptAreaCoverageResolver.BuildSweptZones(
baseSegments,
dimensions,
nodeLookup,
edgePolylineLookup);
if (zones.Count > 0)
{
coverage = SweptAreaCoverageResolver.ExpandFromGraphWithZones(graph, zones);
}
else
{
var centerLine = SweptAreaCoverageResolver.BuildPolylineFromSegments(
baseSegments,
nodeLookup,
edgePolylineLookup);
var radius = SweptAreaCoverageResolver.CalculateSweepRadius(
dimensions.Length,
dimensions.Width,
dimensions.SafetyDistance);
coverage = SweptAreaCoverageResolver.ExpandFromGraph(graph, centerLine, radius);
}
}
else
{
var centerLine = SweptAreaCoverageResolver.BuildPolylineFromSegments(
baseSegments,
nodeLookup,
edgePolylineLookup);
if (centerLine.Count == 0)
{
return baseSegments;
}
var radius = SweptAreaCoverageResolver.CalculateSweepRadius(
dimensions.Length,
dimensions.Width,
dimensions.SafetyDistance);
coverage = SweptAreaCoverageResolver.ExpandFromGraph(graph, centerLine, radius);
}
return MergeCoverageIntoLockSegments(baseSegments, graph, coverage);
}
/// <summary>
/// 构建从当前发送指针到缓存末尾的整条剩余任务扫掠锁候选。
/// </summary>
public static List<PathSegmentWithCode> BuildRemainingTaskLockCandidates(
VdaSegmentedPathCache cache,
PathGraph graph,
Robot robot)
{
var remainingSegments = CollectRemainingSegments(cache);
return remainingSegments.Count == 0
? new List<PathSegmentWithCode>()
: BuildSweptLockCandidates(remainingSegments, graph, robot);
}
/// <summary>
/// 构建从当前发送指针到缓存末尾的整条剩余任务连续扫掠区域。
/// </summary>
public static List<SweptZone> BuildRemainingTaskSweptZones(
VdaSegmentedPathCache cache,
PathGraph graph,
Robot robot)
{
var remainingSegments = CollectRemainingSegments(cache);
return remainingSegments.Count == 0
? new List<SweptZone>()
: BuildSweptZones(remainingSegments, graph, robot);
}
/// <summary>
/// 构建目标节点驻车时的保守扫掠锁候选。
/// </summary>
public static List<PathSegmentWithCode> BuildNodeParkingLockCandidates(
string targetNodeCode,
PathGraph graph,
Robot robot)
{
if (string.IsNullOrWhiteSpace(targetNodeCode))
{
return new List<PathSegmentWithCode>();
}
var targetNode = graph.Nodes.Values.FirstOrDefault(node =>
string.Equals(node.NodeCode, targetNodeCode, StringComparison.OrdinalIgnoreCase));
if (targetNode == null)
{
return new List<PathSegmentWithCode>();
}
var dimensions = SweptAreaCoverageResolver.ResolveDimensions(
robot.Width,
robot.Length,
robot.SafetyDistance,
robot.CoordinateScale);
var radius = SweptAreaCoverageResolver.CalculateSweepRadius(
dimensions.Length,
dimensions.Width,
dimensions.SafetyDistance);
var coverage = SweptAreaCoverageResolver.ExpandFromGraph(
graph,
new[] { new SweepPoint(targetNode.X, targetNode.Y) },
radius);
var anchorSegment = new List<PathSegmentWithCode>
{
new()
{
EdgeId = Guid.Empty,
EdgeCode = string.Empty,
FromNodeId = targetNode.NodeId,
ToNodeId = targetNode.NodeId,
FromNodeCode = targetNode.NodeCode,
ToNodeCode = targetNode.NodeCode
}
};
return MergeCoverageIntoLockSegments(anchorSegment, graph, coverage);
}
/// <summary>
/// 构建目标节点驻车时的连续扫掠区域。
/// </summary>
public static List<SweptZone> BuildNodeParkingSweptZones(
string targetNodeCode,
PathGraph graph,
Robot robot)
{
if (string.IsNullOrWhiteSpace(targetNodeCode))
{
return new List<SweptZone>();
}
var targetNode = graph.Nodes.Values.FirstOrDefault(node =>
string.Equals(node.NodeCode, targetNodeCode, StringComparison.OrdinalIgnoreCase));
if (targetNode == null)
{
return new List<SweptZone>();
}
var dimensions = SweptAreaCoverageResolver.ResolveDimensions(
robot.Width,
robot.Length,
robot.SafetyDistance,
robot.CoordinateScale);
var radius = SweptAreaCoverageResolver.CalculateSweepRadius(
dimensions.Length,
dimensions.Width,
dimensions.SafetyDistance);
return new List<SweptZone>
{
new(new[] { new SweepPoint(targetNode.X, targetNode.Y) }, radius)
};
}
/// <summary>
/// 将锁候选规范化为节点/边资源键集合,便于做交集统计。
/// </summary>
public static HashSet<string> BuildResourceKeys(IEnumerable<PathSegmentWithCode> lockCandidates)
{
var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var segment in lockCandidates)
{
if (!string.IsNullOrWhiteSpace(segment.FromNodeCode))
{
result.Add($"N:{segment.FromNodeCode}");
}
if (!string.IsNullOrWhiteSpace(segment.ToNodeCode))
{
result.Add($"N:{segment.ToNodeCode}");
}
if (!string.IsNullOrWhiteSpace(segment.EdgeCode))
{
result.Add($"E:{segment.EdgeCode}");
}
}
return result;
}
/// <summary>
/// 基于路径段构建连续扫掠区域列表。
/// </summary>
public static List<SweptZone> BuildSweptZones(
List<PathSegmentWithCode> baseSegments,
PathGraph graph,
Robot robot)
{
if (baseSegments.Count == 0)
{
return new List<SweptZone>();
}
var nodeLookup = SweptAreaCoverageResolver.BuildNodeLookup(graph);
var edgePolylineLookup = SweptAreaCoverageResolver.BuildEdgePolylineLookup(graph);
var dimensions = SweptAreaCoverageResolver.ResolveDimensions(
robot.Width,
robot.Length,
robot.SafetyDistance,
robot.CoordinateScale);
if (!dimensions.UseDefaultWidth || !dimensions.UseDefaultLength)
{
var zones = SweptAreaCoverageResolver.BuildSweptZones(
baseSegments,
dimensions,
nodeLookup,
edgePolylineLookup);
if (zones.Count > 0)
{
return zones;
}
}
var centerLine = SweptAreaCoverageResolver.BuildPolylineFromSegments(
baseSegments,
nodeLookup,
edgePolylineLookup);
if (centerLine.Count == 0)
{
return new List<SweptZone>();
}
var radius = SweptAreaCoverageResolver.CalculateSweepRadius(
dimensions.Length,
dimensions.Width,
dimensions.SafetyDistance);
return new List<SweptZone> { new(centerLine, radius) };
}
private static List<PathSegmentWithCode> CollectRemainingSegments(VdaSegmentedPathCache cache)
{
var result = new List<PathSegmentWithCode>();
if (cache.CurrentJunctionIndex < 0 || cache.CurrentJunctionIndex >= cache.JunctionSegments.Count)
{
return result;
}
for (var junctionIndex = cache.CurrentJunctionIndex; junctionIndex < cache.JunctionSegments.Count; junctionIndex++)
{
var junction = cache.JunctionSegments[junctionIndex];
var resourceStart = junctionIndex == cache.CurrentJunctionIndex
? Math.Max(cache.CurrentResourceIndex, 0)
: 0;
for (var resourceIndex = resourceStart; resourceIndex < junction.ResourceSegments.Count; resourceIndex++)
{
var resource = junction.ResourceSegments[resourceIndex];
if (resource.Segments.Count == 0)
{
continue;
}
result.AddRange(resource.Segments.Select(CloneSegment));
}
}
return result;
}
private static List<PathSegmentWithCode> MergeCoverageIntoLockSegments(
List<PathSegmentWithCode> baseSegments,
PathGraph graph,
SweptAreaCoverageResult coverage)
{
var result = new List<PathSegmentWithCode>(baseSegments.Select(CloneSegment));
var nodeCodes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var edgeCodes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var segment in baseSegments)
{
if (!string.IsNullOrWhiteSpace(segment.FromNodeCode))
{
nodeCodes.Add(segment.FromNodeCode);
}
if (!string.IsNullOrWhiteSpace(segment.ToNodeCode))
{
nodeCodes.Add(segment.ToNodeCode);
}
if (!string.IsNullOrWhiteSpace(segment.EdgeCode))
{
edgeCodes.Add(segment.EdgeCode);
}
}
var nodeByCode = graph.Nodes.Values
.Where(n => !string.IsNullOrWhiteSpace(n.NodeCode))
.GroupBy(n => n.NodeCode, StringComparer.OrdinalIgnoreCase)
.ToDictionary(g => g.Key, g => g.First(), StringComparer.OrdinalIgnoreCase);
var edgeByCode = graph.Edges.Values
.Where(e => !string.IsNullOrWhiteSpace(e.EdgeCode))
.GroupBy(e => e.EdgeCode, StringComparer.OrdinalIgnoreCase)
.ToDictionary(g => g.Key, g => g.First(), StringComparer.OrdinalIgnoreCase);
foreach (var edgeCode in coverage.EdgeCodes)
{
if (edgeCodes.Contains(edgeCode) || !edgeByCode.TryGetValue(edgeCode, out var edge))
{
continue;
}
result.Add(new PathSegmentWithCode
{
EdgeId = edge.EdgeId,
EdgeCode = edge.EdgeCode,
FromNodeId = edge.FromNodeId,
ToNodeId = edge.ToNodeId,
FromNodeCode = edge.FromNodeCode,
ToNodeCode = edge.ToNodeCode
});
edgeCodes.Add(edgeCode);
}
foreach (var nodeCode in coverage.NodeCodes)
{
if (nodeCodes.Contains(nodeCode) || !nodeByCode.TryGetValue(nodeCode, out var node))
{
continue;
}
result.Add(new PathSegmentWithCode
{
EdgeId = Guid.Empty,
EdgeCode = string.Empty,
FromNodeId = node.NodeId,
ToNodeId = node.NodeId,
FromNodeCode = node.NodeCode,
ToNodeCode = node.NodeCode
});
nodeCodes.Add(nodeCode);
}
return result;
}
private static PathSegmentWithCode CloneSegment(PathSegmentWithCode segment) =>
new()
{
EdgeId = segment.EdgeId,
FromNodeId = segment.FromNodeId,
ToNodeId = segment.ToNodeId,
Angle = segment.Angle,
Length = segment.Length,
MaxSpeed = segment.MaxSpeed,
RequiredAngle = segment.RequiredAngle,
StartTheta = segment.StartTheta,
EndTheta = segment.EndTheta,
EdgeCode = segment.EdgeCode,
FromNodeCode = segment.FromNodeCode,
ToNodeCode = segment.ToNodeCode,
ForkAngleOffset = segment.ForkAngleOffset,
StartNodeActions = new List<StepAction>(segment.StartNodeActions),
StartNodeNetActionTypes = new Dictionary<Guid, ActionType>(segment.StartNodeNetActionTypes),
EndNodeActions = new List<StepAction>(segment.EndNodeActions),
EndNodeNetActionTypes = new Dictionary<Guid, ActionType>(segment.EndNodeNetActionTypes)
};
private static SweepPoint? TryResolveNodePoint(
IReadOnlyDictionary<string, SweepPoint> nodeLookup,
string? nodeCode)
{
if (string.IsNullOrWhiteSpace(nodeCode) || !nodeLookup.TryGetValue(nodeCode, out var point))
{
return null;
}
return point;
}
private static List<SweepPoint> BuildEdgePolyline(
PathSegmentWithCode segment,
SweepPoint fromPoint,
SweepPoint toPoint,
IReadOnlyDictionary<string, List<SweepPoint>> edgePolylineLookup)
{
if (!string.IsNullOrWhiteSpace(segment.EdgeCode) &&
edgePolylineLookup.TryGetValue(segment.EdgeCode, out var edgePolyline) &&
edgePolyline.Count > 0)
{
return EnsurePolylineEndpoints(edgePolyline, fromPoint, toPoint);
}
return new List<SweepPoint> { fromPoint, toPoint };
}
private static List<SweepPoint> EnsurePolylineEndpoints(
IReadOnlyList<SweepPoint> polyline,
SweepPoint fromPoint,
SweepPoint toPoint)
{
var result = new List<SweepPoint>();
if (polyline.Count == 0)
{
result.Add(fromPoint);
result.Add(toPoint);
return result;
}
if (!IsSamePoint(polyline[0], fromPoint))
{
result.Add(fromPoint);
}
result.AddRange(polyline);
if (!IsSamePoint(result[^1], toPoint))
{
result.Add(toPoint);
}
return result;
}
private static bool IsSamePoint(SweepPoint left, SweepPoint right)
{
return Math.Abs(left.X - right.X) < 1e-6d && Math.Abs(left.Y - right.Y) < 1e-6d;
}
private static string? ResolveNodeReleaseTrigger(
IReadOnlyList<PathSegmentWithCode> segments,
int currentSegmentIndex,
string anchorNodeCode)
{
var nextSegmentIndex = currentSegmentIndex + 1;
if (nextSegmentIndex < 0 || nextSegmentIndex >= segments.Count)
{
return null;
}
for (var index = nextSegmentIndex; index < segments.Count; index++)
{
var candidateNodeCode = segments[index].ToNodeCode;
if (!string.Equals(candidateNodeCode, anchorNodeCode, StringComparison.OrdinalIgnoreCase))
{
return candidateNodeCode;
}
}
return null;
}
private static bool HasRealEdgeBinding(PathSegmentWithCode segment)
{
return segment.EdgeId != Guid.Empty && !string.IsNullOrWhiteSpace(segment.EdgeCode);
}
private static bool IsInPlaceNodeOnlySegment(PathSegmentWithCode segment)
{
return segment.EdgeId == Guid.Empty &&
string.IsNullOrWhiteSpace(segment.EdgeCode) &&
string.Equals(segment.FromNodeCode, segment.ToNodeCode, StringComparison.OrdinalIgnoreCase) &&
Math.Abs(segment.Length) < 0.000001d;
}
private static VdaLockBindingPlan BuildNodeBindingPlan(
int junctionIndex,
int resourceIndex,
int orderIndex,
string anchorNodeCode,
string? releaseTriggerNodeCode,
SweepPoint anchorPoint,
double radius,
PathGraph graph,
IReadOnlyDictionary<string, PathEdge> edgeByCode)
{
var coverage = SweptAreaCoverageResolver.ExpandFromGraphWithZones(
graph,
new List<SweptZone> { new(new List<SweepPoint> { anchorPoint }, radius) });
return new VdaLockBindingPlan
{
BindingId = $"J{junctionIndex}:R{resourceIndex}:NODE:{anchorNodeCode}",
BindingType = VdaLockBindingType.Node,
AnchorCode = anchorNodeCode,
JunctionIndex = junctionIndex,
ResourceIndex = resourceIndex,
OrderIndex = orderIndex,
ReleaseTriggerNodeCode = releaseTriggerNodeCode,
CoveredNodeCodes = coverage.NodeCodes
.OrderBy(code => code, StringComparer.OrdinalIgnoreCase)
.ToList(),
CoveredEdgeCodes = coverage.EdgeCodes
.OrderBy(code => code, StringComparer.OrdinalIgnoreCase)
.ToList(),
CoveredEdges = BuildCoveredEdges(coverage.EdgeCodes, edgeByCode)
};
}
private static VdaLockBindingPlan BuildEdgeBindingPlan(
int junctionIndex,
int resourceIndex,
int orderIndex,
string anchorEdgeCode,
string? releaseTriggerNodeCode,
IReadOnlyList<SweepPoint> polyline,
double radius,
PathGraph graph,
IReadOnlyDictionary<string, PathEdge> edgeByCode)
{
var coverage = SweptAreaCoverageResolver.ExpandFromGraphWithZones(
graph,
new List<SweptZone> { new(polyline, radius) });
return new VdaLockBindingPlan
{
BindingId = $"J{junctionIndex}:R{resourceIndex}:EDGE:{anchorEdgeCode}",
BindingType = VdaLockBindingType.Edge,
AnchorCode = anchorEdgeCode,
JunctionIndex = junctionIndex,
ResourceIndex = resourceIndex,
OrderIndex = orderIndex,
ReleaseTriggerNodeCode = releaseTriggerNodeCode,
CoveredNodeCodes = coverage.NodeCodes
.OrderBy(code => code, StringComparer.OrdinalIgnoreCase)
.ToList(),
CoveredEdgeCodes = coverage.EdgeCodes
.OrderBy(code => code, StringComparer.OrdinalIgnoreCase)
.ToList(),
CoveredEdges = BuildCoveredEdges(coverage.EdgeCodes, edgeByCode)
};
}
private static List<VdaLockCoveredEdge> BuildCoveredEdges(
IEnumerable<string> edgeCodes,
IReadOnlyDictionary<string, PathEdge> edgeByCode)
{
return edgeCodes
.Where(code => !string.IsNullOrWhiteSpace(code) && edgeByCode.ContainsKey(code))
.Select(code =>
{
var edge = edgeByCode[code];
return new VdaLockCoveredEdge
{
EdgeCode = edge.EdgeCode,
FromNodeCode = edge.FromNodeCode,
ToNodeCode = edge.ToNodeCode
};
})
.OrderBy(edge => edge.EdgeCode, StringComparer.OrdinalIgnoreCase)
.ToList();
}
private static void RegisterBindingPlan(
ICollection<VdaLockBindingPlan> result,
ISet<string> bindingIds,
VdaLockBindingPlan bindingPlan)
{
if (string.IsNullOrWhiteSpace(bindingPlan.BindingId))
{
throw new InvalidOperationException("严格 binding 生成失败:BindingId 不能为空。");
}
if (!bindingIds.Add(bindingPlan.BindingId))
{
throw new InvalidOperationException($"严格 binding 生成失败:发现重复 BindingId={bindingPlan.BindingId}。");
}
result.Add(bindingPlan);
}
}