rts-sim-module/modelrepo/model/link.go
walker 7751d9d926 组件定义调整
道岔命名调整,道岔实体构建重构
2024-01-02 18:22:28 +08:00

142 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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总长度
GetLength() int64
// 获取端口A关联的道岔
GetPaTurnout() Points
// 获取端口B关联的道岔
GetPbTurnout() Points
// 下一个link及端口
// port - 当前link端口
// tpos - 当前道岔位置
Next(port Link_Port, tpos PointsPosition) *LinkPort
}
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 {
if offset < 0 || offset > link.GetLength() {
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 {
if a < 0 || a > link.GetLength() {
slog.Error("创建link偏移范围失败: 超出link长度范围", slog.Int64("a", a))
return nil
} else if b < 0 || b > link.GetLength() {
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
}