2023-12-27 15:51:28 +08:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import "log/slog"
|
|
|
|
|
|
|
|
|
|
// link端口
|
|
|
|
|
type Link_Port int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
LinkPort_A Link_Port = -1
|
|
|
|
|
LinkPort_B Link_Port = 1
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Link
|
|
|
|
|
type Link interface {
|
|
|
|
|
Model
|
|
|
|
|
// Link总长度
|
2023-12-28 11:06:46 +08:00
|
|
|
|
GetLength() int64
|
2023-12-27 15:51:28 +08:00
|
|
|
|
// 获取端口A关联的道岔
|
2024-01-02 18:22:28 +08:00
|
|
|
|
GetPaTurnout() Points
|
2023-12-27 15:51:28 +08:00
|
|
|
|
// 获取端口B关联的道岔
|
2024-01-02 18:22:28 +08:00
|
|
|
|
GetPbTurnout() Points
|
2023-12-27 15:51:28 +08:00
|
|
|
|
// 下一个link及端口
|
|
|
|
|
// port - 当前link端口
|
|
|
|
|
// tpos - 当前道岔位置
|
2024-01-02 18:22:28 +08:00
|
|
|
|
Next(port Link_Port, tpos PointsPosition) *LinkPort
|
2023-12-27 15:51:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type LinkPort struct {
|
|
|
|
|
link Link
|
|
|
|
|
port Link_Port
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewLinkPort(link Link, port Link_Port) *LinkPort {
|
|
|
|
|
return &LinkPort{
|
|
|
|
|
link: link,
|
|
|
|
|
port: port,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// link偏移位置
|
|
|
|
|
// 默认linkA端口偏移为0,从A到B为增大,从B到A为减小
|
|
|
|
|
type LinkOffset struct {
|
|
|
|
|
link Link
|
|
|
|
|
offset int64 // 偏移坐标,link相当于一个一维坐标系,以端口A为原点
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建link偏移位置,若offset超出link长度则返回nil
|
|
|
|
|
func NewLinkOffset(link Link, offset int64) *LinkOffset {
|
2023-12-28 11:06:46 +08:00
|
|
|
|
if offset < 0 || offset > link.GetLength() {
|
2023-12-27 15:51:28 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &LinkOffset{
|
|
|
|
|
link: link,
|
|
|
|
|
offset: offset,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *LinkOffset) Link() Link {
|
|
|
|
|
return l.link
|
|
|
|
|
}
|
|
|
|
|
func (l *LinkOffset) Offset() int64 {
|
|
|
|
|
return l.offset
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// link偏移范围
|
|
|
|
|
type LinkRange struct {
|
|
|
|
|
link Link
|
|
|
|
|
start int64
|
|
|
|
|
end int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewLinkRange(link Link, a int64, b int64) *LinkRange {
|
2023-12-28 11:06:46 +08:00
|
|
|
|
if a < 0 || a > link.GetLength() {
|
2023-12-27 15:51:28 +08:00
|
|
|
|
slog.Error("创建link偏移范围失败: 超出link长度范围", slog.Int64("a", a))
|
|
|
|
|
return nil
|
2023-12-28 11:06:46 +08:00
|
|
|
|
} else if b < 0 || b > link.GetLength() {
|
2023-12-27 15:51:28 +08:00
|
|
|
|
slog.Error("创建link偏移范围失败: 超出link长度范围", slog.Int64("b", b))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var start int64
|
|
|
|
|
var end int64
|
|
|
|
|
if a > b {
|
|
|
|
|
start = b
|
|
|
|
|
end = a
|
|
|
|
|
} else {
|
|
|
|
|
start = a
|
|
|
|
|
end = b
|
|
|
|
|
}
|
|
|
|
|
return &LinkRange{
|
|
|
|
|
link: link,
|
|
|
|
|
start: start,
|
|
|
|
|
end: end,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否相交
|
|
|
|
|
func (l *LinkRange) IsIntersect(other *LinkRange) bool {
|
|
|
|
|
if l.link != other.link {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if l.start > other.end || other.start > l.end {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否相交
|
|
|
|
|
func (l *LinkRange) IsIntersect2(a, b int64) bool {
|
|
|
|
|
var start int64
|
|
|
|
|
var end int64
|
|
|
|
|
if a > b {
|
|
|
|
|
start = b
|
|
|
|
|
end = a
|
|
|
|
|
} else {
|
|
|
|
|
start = a
|
|
|
|
|
end = b
|
|
|
|
|
}
|
|
|
|
|
if start > l.end || end < l.start {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否在范围内
|
|
|
|
|
func (l *LinkRange) IsInRange(lo *LinkOffset) bool {
|
|
|
|
|
if lo.link != l.link {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if lo.offset < l.start || lo.offset > l.end {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 偏移坐标是否在范围内
|
|
|
|
|
func (l *LinkRange) IsInRange2(offset int64) bool {
|
|
|
|
|
if offset < l.start || offset > l.end {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|