diff --git a/repository/physical_section.go b/repository/physical_section.go index f9a19c1..8244e06 100644 --- a/repository/physical_section.go +++ b/repository/physical_section.go @@ -3,8 +3,8 @@ package repository import ( "errors" "fmt" - "joylink.club/rtsssimulation/repository/model/proto" + "joylink.club/rtsssimulation/util/number" ) // 物理区段 @@ -30,10 +30,7 @@ type PhysicalSection struct { //aKm/bKm对应的LinkPosition aLinkPosition *LinkPosition bLinkPosition *LinkPosition - ////在Link上的区间(根据aKm和bKm计算出的,start的offset一定小于end的offset) - //startLinkPosition *LinkPosition - //endLinkPosition *LinkPosition - //在Link上的区间 + //在Link上的区间(start小于end) linkRanges []*LinkRange //物理区段所属集中站 @@ -46,6 +43,14 @@ func NewPhysicalSection(id string) *PhysicalSection { } } +func (p *PhysicalSection) bindLinkRange(link *Link, one int64, two int64) { + p.linkRanges = append(p.linkRanges, &LinkRange{ + link: link, + start: number.Min(one, two), + end: number.Max(one, two), + }) +} + func (s *PhysicalSection) PortNum() int { return 2 } diff --git a/repository/repository_manager.go b/repository/repository_manager.go index 0163bf9..731fc68 100644 --- a/repository/repository_manager.go +++ b/repository/repository_manager.go @@ -686,11 +686,7 @@ func relatePhysicalSectionAndLink(repo *Repository, section *PhysicalSection, li link: link, offset: bOffset, } - section.linkRanges = append(section.linkRanges, &LinkRange{ - link: link, - start: aOffset, - end: bOffset, - }) + section.bindLinkRange(link, aOffset, bOffset) } func getATurnoutPort(turnouts []*Turnout, visitedTurnoutMap map[string]bool) *TurnoutPort { @@ -764,31 +760,19 @@ func buildLinkRangeOfTurnoutPhysicalSection(link *Link) { //解决[两道岔的道岔物理区段,两道岔间的Link会产生两个LinkRange]的问题 if link.aRelation != nil && link.bRelation != nil && link.aRelation.turnout.section == link.bRelation.turnout.section { section := link.aRelation.turnout.section - section.linkRanges = append(section.linkRanges, &LinkRange{ - link: link, - start: 0, - end: link.length, - }) + section.bindLinkRange(link, 0, link.length) return } if link.aRelation != nil { aSection := link.aRelation.turnout.section //Link A端的关联的道岔物理区段 aLinkPosition := link.aRelation.turnout.FindLinkPositionByPort(link.aRelation.port) //Link A端道岔端点在Link上的位置 - aSection.linkRanges = append(aSection.linkRanges, &LinkRange{ - link: link, - start: 0, - end: aLinkPosition.offset, - }) + aSection.bindLinkRange(link, 0, aLinkPosition.offset) } if link.bRelation != nil { bSection := link.bRelation.turnout.section //Link A端的关联的道岔物理区段 bLinkPosition := link.bRelation.turnout.FindLinkPositionByPort(link.bRelation.port) //Link A端道岔端点在Link上的位置 - bSection.linkRanges = append(bSection.linkRanges, &LinkRange{ - link: link, - start: link.length, - end: bLinkPosition.offset, - }) + bSection.bindLinkRange(link, bLinkPosition.offset, link.length) } }