Skip to content

Commit

Permalink
core: standalone_sim: factor out pathSignals
Browse files Browse the repository at this point in the history
  • Loading branch information
multun committed Jul 20, 2023
1 parent 4cfabfa commit eb4e9ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fun run(

// Compute signal updates
val startOffset = trainPathBlockOffset(trainPath)
val pathSignals = pathSignals(startOffset, blockPath, blockInfra, envelopeWithStops, rawInfra)
val pathSignals = pathSignalsInEnvelope(startOffset, blockPath, blockInfra, envelopeWithStops, rawInfra)
val zoneOccupationChangeEvents =
zoneOccupationChangeEvents(startOffset, blockPath, blockInfra, envelopeWithStops, rawInfra, trainLength)

Expand Down Expand Up @@ -299,42 +299,51 @@ private fun zoneOccupationChangeEvents(

data class PathSignal(val signal: LogicalSignalId, val offset: Distance, val blockIndexInPath: Int)

// This doesn't generate path signals outside the envelope
// The reason being that even if a train see a red signal, it won't
// matter since the train was going to stop before it anyway
private fun pathSignals(

// Returns all the signals on the path
fun pathSignals(
startOffset: Distance,
blockPath: StaticIdxList<Block>,
blockInfra: BlockInfra,
envelope: EnvelopeTimeInterpolate,
rawInfra: SimInfraAdapter
): MutableList<PathSignal> {
): List<PathSignal> {
val pathSignals = mutableListOf<PathSignal>()
var currentOffset = startOffset
for ((blockIdx, block) in blockPath.withIndex()) {
var blockSize = Distance.ZERO
for (zonePath in blockInfra.getBlockPath(block)) {
blockSize += rawInfra.getZonePathLength(zonePath)
}

val blockSignals = blockInfra.getBlockSignals(block)
val blockSignalPositions = blockInfra.getSignalsPositions(block)
val numExclusiveSignalInBlock =
if (blockIdx == blockPath.size - 1) blockSignals.size else blockSignals.size - 1
for ((signal, position) in blockSignals.zip(blockSignalPositions).take(numExclusiveSignalInBlock)) {
val signalOffset = currentOffset + position
if (0.meters < signalOffset && signalOffset < envelope.endPos.meters)
pathSignals.add(PathSignal(signal, signalOffset, blockIdx))
}

for (zonePath in blockInfra.getBlockPath(block)) {
if (currentOffset > envelope.endPos.meters) break

currentOffset += rawInfra.getZonePathLength(zonePath)
if (currentOffset > envelope.endPos.meters) {
break
}
pathSignals.add(PathSignal(signal, currentOffset + position, blockIdx))
}
currentOffset += blockSize
}

return pathSignals
}


// This doesn't generate path signals outside the envelope
// The reason being that even if a train see a red signal, it won't
// matter since the train was going to stop before it anyway
private fun pathSignalsInEnvelope(
startOffset: Distance,
blockPath: StaticIdxList<Block>,
blockInfra: BlockInfra,
envelope: EnvelopeTimeInterpolate,
rawInfra: SimInfraAdapter
): List<PathSignal> {
return pathSignals(startOffset, blockPath, blockInfra, rawInfra).filter { signal ->
signal.offset >= 0.meters && signal.offset <= envelope.endPos.meters
}
}

/**
* Computes the offset between the beginning of the first block and the beginning of the train path - and
* thus of the envelope
Expand Down
28 changes: 0 additions & 28 deletions core/src/main/java/fr/sncf/osrd/standalone_sim/SignalProjection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,6 @@ fun project(
return SignalProjectionResult(signalUpdates)
}

// Returns all the signals on the path
private fun pathSignals(
startOffset: Distance,
blockPath: StaticIdxList<Block>,
blockInfra: BlockInfra,
rawInfra: SimInfraAdapter
): MutableList<PathSignal> {
val pathSignals = mutableListOf<PathSignal>()
var currentOffset = startOffset
for ((blockIdx, block) in blockPath.withIndex()) {
var blockSize = Distance.ZERO
for (zonePath in blockInfra.getBlockPath(block)) {
blockSize += rawInfra.getZonePathLength(zonePath)
}

val blockSignals = blockInfra.getBlockSignals(block)
val blockSignalPositions = blockInfra.getSignalsPositions(block)
val numExclusiveSignalInBlock =
if (blockIdx == blockPath.size - 1) blockSignals.size else blockSignals.size - 1
for ((signal, position) in blockSignals.zip(blockSignalPositions).take(numExclusiveSignalInBlock)) {
pathSignals.add(PathSignal(signal, currentOffset + position, blockIdx))
}
currentOffset += blockSize
}

return pathSignals
}


private fun computeSignalAspectChangeEvents(
blockPath: StaticIdxList<Block>,
Expand Down

0 comments on commit eb4e9ee

Please sign in to comment.