From 27eccb0aa59e95073648982fa703b71652e8b63d Mon Sep 17 00:00:00 2001 From: RCRalph Date: Tue, 14 Apr 2026 19:46:45 +0200 Subject: [PATCH] feat: Used paths from payload --- go.mod | 1 - go.sum | 2 - openapi.json | 45 +++--- pkg/api/client.gen.go | 148 ++++-------------- pkg/city/city.go | 4 + pkg/city/graph/graph_node.go | 6 +- ...graph_track_node.go => graph_road_node.go} | 12 +- pkg/controlcenter/control_center.go | 35 ++--- pkg/controlcenter/path.go | 94 ++--------- pkg/simulation/tram/tram.go | 3 +- 10 files changed, 85 insertions(+), 265 deletions(-) rename pkg/city/graph/{graph_track_node.go => graph_road_node.go} (53%) diff --git a/go.mod b/go.mod index b03c94a..debe9a8 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.24.2 require ( github.com/oapi-codegen/runtime v1.1.2 - github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 github.com/wailsapp/wails/v2 v2.10.1 ) diff --git a/go.sum b/go.sum index d4d503d..08cf7d6 100644 --- a/go.sum +++ b/go.sum @@ -59,8 +59,6 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tkrajina/go-reflector v0.5.8 h1:yPADHrwmUbMq4RGEyaOUpz2H90sRsETNVpjzo3DLVQQ= github.com/tkrajina/go-reflector v0.5.8/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4= -github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 h1:UFHFmFfixpmfRBcxuu+LA9l8MdURWVdVNUHxO5n1d2w= -github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26/go.mod h1:IGhd0qMDsUa9acVjsbsT7bu3ktadtGOHI79+idTew/M= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= diff --git a/openapi.json b/openapi.json index 800d0f8..3ada071 100644 --- a/openapi.json +++ b/openapi.json @@ -337,7 +337,7 @@ }, "ResponseCityData": { "properties": { - "tram_track_graph": { + "graph": { "items": { "oneOf": [ { @@ -356,7 +356,7 @@ } }, "type": "array", - "title": "Tram Track Graph" + "title": "Graph" }, "tram_routes": { "items": { @@ -365,33 +365,13 @@ "type": "array", "title": "Tram Routes" }, - "bus_road_graph": { - "items": { - "oneOf": [ - { - "$ref": "#/components/schemas/ResponseGraphNode" - }, - { - "$ref": "#/components/schemas/ResponseGraphStop" - } - ], - "discriminator": { - "propertyName": "node_type", - "mapping": { - "node": "#/components/schemas/ResponseGraphNode", - "stop": "#/components/schemas/ResponseGraphStop" - } - } - }, - "type": "array", - "title": "Bus Road Graph" - }, "bus_routes": { "items": { "$ref": "#/components/schemas/ResponseRoute" }, "type": "array", - "title": "Bus Routes" + "title": "Bus Routes", + "x-go-type-skip-optional-pointer": true }, "paths": { "additionalProperties": { @@ -404,11 +384,13 @@ "type": "object" }, "type": "object", - "title": "Paths" + "title": "Paths", + "x-go-type": "map[uint64]map[uint64][]uint64", + "x-go-type-skip-optional-pointer": true } }, "type": "object", - "required": ["tram_track_graph", "tram_routes"], + "required": ["graph", "tram_routes"], "title": "ResponseCityData" }, "ResponseGraphEdge": { @@ -430,10 +412,19 @@ "max_speed": { "type": "number", "title": "Max Speed" + }, + "transit_type": { + "$ref": "#/components/schemas/TransitType" } }, "type": "object", - "required": ["id", "distance", "azimuth", "max_speed"], + "required": [ + "id", + "distance", + "azimuth", + "max_speed", + "transit_type" + ], "title": "ResponseGraphEdge" }, "ResponseGraphNode": { diff --git a/pkg/api/client.gen.go b/pkg/api/client.gen.go index 3881709..a016c37 100644 --- a/pkg/api/client.gen.go +++ b/pkg/api/client.gen.go @@ -85,29 +85,24 @@ type HTTPValidationError struct { // ResponseCityData defines model for ResponseCityData. type ResponseCityData struct { - BusRoadGraph *[]ResponseCityData_BusRoadGraph_Item `json:"bus_road_graph,omitempty"` - BusRoutes *[]ResponseRoute `json:"bus_routes,omitempty"` - Paths *map[string]map[string][]int `json:"paths,omitempty"` - TramRoutes []ResponseRoute `json:"tram_routes"` - TramTrackGraph []ResponseCityData_TramTrackGraph_Item `json:"tram_track_graph"` + BusRoutes []ResponseRoute `json:"bus_routes,omitempty"` + Graph []ResponseCityData_Graph_Item `json:"graph"` + Paths map[uint64]map[uint64][]uint64 `json:"paths,omitempty"` + TramRoutes []ResponseRoute `json:"tram_routes"` } -// ResponseCityData_BusRoadGraph_Item defines model for ResponseCityData.bus_road_graph.Item. -type ResponseCityData_BusRoadGraph_Item struct { - union json.RawMessage -} - -// ResponseCityData_TramTrackGraph_Item defines model for ResponseCityData.tram_track_graph.Item. -type ResponseCityData_TramTrackGraph_Item struct { +// ResponseCityData_Graph_Item defines model for ResponseCityData.graph.Item. +type ResponseCityData_Graph_Item struct { union json.RawMessage } // ResponseGraphEdge defines model for ResponseGraphEdge. type ResponseGraphEdge struct { - Azimuth float32 `json:"azimuth"` - Distance float32 `json:"distance"` - ID uint64 `json:"id"` - MaxSpeed float32 `json:"max_speed"` + Azimuth float32 `json:"azimuth"` + Distance float32 `json:"distance"` + ID uint64 `json:"id"` + MaxSpeed float32 `json:"max_speed"` + TransitType TransitType `json:"transit_type"` } // ResponseGraphNode defines model for ResponseGraphNode. @@ -209,112 +204,23 @@ type GetCityDataWithCustomScheduleCitiesCityIdPostParams struct { // GetCityDataWithCustomScheduleCitiesCityIdPostMultipartRequestBody defines body for GetCityDataWithCustomScheduleCitiesCityIdPost for multipart/form-data ContentType. type GetCityDataWithCustomScheduleCitiesCityIdPostMultipartRequestBody = BodyGetCityDataWithCustomScheduleCitiesCityIdPost -// AsResponseGraphNode returns the union data inside the ResponseCityData_BusRoadGraph_Item as a ResponseGraphNode -func (t ResponseCityData_BusRoadGraph_Item) AsResponseGraphNode() (ResponseGraphNode, error) { - var body ResponseGraphNode - err := json.Unmarshal(t.union, &body) - return body, err -} - -// FromResponseGraphNode overwrites any union data inside the ResponseCityData_BusRoadGraph_Item as the provided ResponseGraphNode -func (t *ResponseCityData_BusRoadGraph_Item) FromResponseGraphNode(v ResponseGraphNode) error { - v.NodeType = "node" - b, err := json.Marshal(v) - t.union = b - return err -} - -// MergeResponseGraphNode performs a merge with any union data inside the ResponseCityData_BusRoadGraph_Item, using the provided ResponseGraphNode -func (t *ResponseCityData_BusRoadGraph_Item) MergeResponseGraphNode(v ResponseGraphNode) error { - v.NodeType = "node" - b, err := json.Marshal(v) - if err != nil { - return err - } - - merged, err := runtime.JSONMerge(t.union, b) - t.union = merged - return err -} - -// AsResponseGraphStop returns the union data inside the ResponseCityData_BusRoadGraph_Item as a ResponseGraphStop -func (t ResponseCityData_BusRoadGraph_Item) AsResponseGraphStop() (ResponseGraphStop, error) { - var body ResponseGraphStop - err := json.Unmarshal(t.union, &body) - return body, err -} - -// FromResponseGraphStop overwrites any union data inside the ResponseCityData_BusRoadGraph_Item as the provided ResponseGraphStop -func (t *ResponseCityData_BusRoadGraph_Item) FromResponseGraphStop(v ResponseGraphStop) error { - v.NodeType = "stop" - b, err := json.Marshal(v) - t.union = b - return err -} - -// MergeResponseGraphStop performs a merge with any union data inside the ResponseCityData_BusRoadGraph_Item, using the provided ResponseGraphStop -func (t *ResponseCityData_BusRoadGraph_Item) MergeResponseGraphStop(v ResponseGraphStop) error { - v.NodeType = "stop" - b, err := json.Marshal(v) - if err != nil { - return err - } - - merged, err := runtime.JSONMerge(t.union, b) - t.union = merged - return err -} - -func (t ResponseCityData_BusRoadGraph_Item) Discriminator() (string, error) { - var discriminator struct { - Discriminator string `json:"node_type"` - } - err := json.Unmarshal(t.union, &discriminator) - return discriminator.Discriminator, err -} - -func (t ResponseCityData_BusRoadGraph_Item) ValueByDiscriminator() (interface{}, error) { - discriminator, err := t.Discriminator() - if err != nil { - return nil, err - } - switch discriminator { - case "node": - return t.AsResponseGraphNode() - case "stop": - return t.AsResponseGraphStop() - default: - return nil, errors.New("unknown discriminator value: " + discriminator) - } -} - -func (t ResponseCityData_BusRoadGraph_Item) MarshalJSON() ([]byte, error) { - b, err := t.union.MarshalJSON() - return b, err -} - -func (t *ResponseCityData_BusRoadGraph_Item) UnmarshalJSON(b []byte) error { - err := t.union.UnmarshalJSON(b) - return err -} - -// AsResponseGraphNode returns the union data inside the ResponseCityData_TramTrackGraph_Item as a ResponseGraphNode -func (t ResponseCityData_TramTrackGraph_Item) AsResponseGraphNode() (ResponseGraphNode, error) { +// AsResponseGraphNode returns the union data inside the ResponseCityData_Graph_Item as a ResponseGraphNode +func (t ResponseCityData_Graph_Item) AsResponseGraphNode() (ResponseGraphNode, error) { var body ResponseGraphNode err := json.Unmarshal(t.union, &body) return body, err } -// FromResponseGraphNode overwrites any union data inside the ResponseCityData_TramTrackGraph_Item as the provided ResponseGraphNode -func (t *ResponseCityData_TramTrackGraph_Item) FromResponseGraphNode(v ResponseGraphNode) error { +// FromResponseGraphNode overwrites any union data inside the ResponseCityData_Graph_Item as the provided ResponseGraphNode +func (t *ResponseCityData_Graph_Item) FromResponseGraphNode(v ResponseGraphNode) error { v.NodeType = "node" b, err := json.Marshal(v) t.union = b return err } -// MergeResponseGraphNode performs a merge with any union data inside the ResponseCityData_TramTrackGraph_Item, using the provided ResponseGraphNode -func (t *ResponseCityData_TramTrackGraph_Item) MergeResponseGraphNode(v ResponseGraphNode) error { +// MergeResponseGraphNode performs a merge with any union data inside the ResponseCityData_Graph_Item, using the provided ResponseGraphNode +func (t *ResponseCityData_Graph_Item) MergeResponseGraphNode(v ResponseGraphNode) error { v.NodeType = "node" b, err := json.Marshal(v) if err != nil { @@ -326,23 +232,23 @@ func (t *ResponseCityData_TramTrackGraph_Item) MergeResponseGraphNode(v Response return err } -// AsResponseGraphStop returns the union data inside the ResponseCityData_TramTrackGraph_Item as a ResponseGraphStop -func (t ResponseCityData_TramTrackGraph_Item) AsResponseGraphStop() (ResponseGraphStop, error) { +// AsResponseGraphStop returns the union data inside the ResponseCityData_Graph_Item as a ResponseGraphStop +func (t ResponseCityData_Graph_Item) AsResponseGraphStop() (ResponseGraphStop, error) { var body ResponseGraphStop err := json.Unmarshal(t.union, &body) return body, err } -// FromResponseGraphStop overwrites any union data inside the ResponseCityData_TramTrackGraph_Item as the provided ResponseGraphStop -func (t *ResponseCityData_TramTrackGraph_Item) FromResponseGraphStop(v ResponseGraphStop) error { +// FromResponseGraphStop overwrites any union data inside the ResponseCityData_Graph_Item as the provided ResponseGraphStop +func (t *ResponseCityData_Graph_Item) FromResponseGraphStop(v ResponseGraphStop) error { v.NodeType = "stop" b, err := json.Marshal(v) t.union = b return err } -// MergeResponseGraphStop performs a merge with any union data inside the ResponseCityData_TramTrackGraph_Item, using the provided ResponseGraphStop -func (t *ResponseCityData_TramTrackGraph_Item) MergeResponseGraphStop(v ResponseGraphStop) error { +// MergeResponseGraphStop performs a merge with any union data inside the ResponseCityData_Graph_Item, using the provided ResponseGraphStop +func (t *ResponseCityData_Graph_Item) MergeResponseGraphStop(v ResponseGraphStop) error { v.NodeType = "stop" b, err := json.Marshal(v) if err != nil { @@ -354,7 +260,7 @@ func (t *ResponseCityData_TramTrackGraph_Item) MergeResponseGraphStop(v Response return err } -func (t ResponseCityData_TramTrackGraph_Item) Discriminator() (string, error) { +func (t ResponseCityData_Graph_Item) Discriminator() (string, error) { var discriminator struct { Discriminator string `json:"node_type"` } @@ -362,7 +268,7 @@ func (t ResponseCityData_TramTrackGraph_Item) Discriminator() (string, error) { return discriminator.Discriminator, err } -func (t ResponseCityData_TramTrackGraph_Item) ValueByDiscriminator() (interface{}, error) { +func (t ResponseCityData_Graph_Item) ValueByDiscriminator() (interface{}, error) { discriminator, err := t.Discriminator() if err != nil { return nil, err @@ -377,12 +283,12 @@ func (t ResponseCityData_TramTrackGraph_Item) ValueByDiscriminator() (interface{ } } -func (t ResponseCityData_TramTrackGraph_Item) MarshalJSON() ([]byte, error) { +func (t ResponseCityData_Graph_Item) MarshalJSON() ([]byte, error) { b, err := t.union.MarshalJSON() return b, err } -func (t *ResponseCityData_TramTrackGraph_Item) UnmarshalJSON(b []byte) error { +func (t *ResponseCityData_Graph_Item) UnmarshalJSON(b []byte) error { err := t.union.UnmarshalJSON(b) return err } diff --git a/pkg/city/city.go b/pkg/city/city.go index 616177b..b6de7a2 100644 --- a/pkg/city/city.go +++ b/pkg/city/city.go @@ -175,6 +175,10 @@ func (c *City) GetTripByID(tripID uint) *trip.Trip { return c.tripsByID[tripID] } +func (c *City) GetPaths() map[uint64]map[uint64][]uint64 { + return c.responseCityData.Paths +} + type RouteInfo struct { Name string `json:"name"` TextColor string `json:"text_color"` diff --git a/pkg/city/graph/graph_node.go b/pkg/city/graph/graph_node.go index 70ac4e4..f2cf170 100644 --- a/pkg/city/graph/graph_node.go +++ b/pkg/city/graph/graph_node.go @@ -16,9 +16,9 @@ type GraphNode interface { } func GraphNodesFromCityData(responseCityData *api.ResponseCityData) (map[uint64]GraphNode, error) { - nodesByID := make(map[uint64]GraphNode, len(responseCityData.TramTrackGraph)) + nodesByID := make(map[uint64]GraphNode, len(responseCityData.Graph)) - for _, nodeItem := range responseCityData.TramTrackGraph { + for _, nodeItem := range responseCityData.Graph { value, err := nodeItem.ValueByDiscriminator() if err != nil { return nil, err @@ -28,7 +28,7 @@ func GraphNodesFromCityData(responseCityData *api.ResponseCityData) (map[uint64] case api.ResponseGraphStop: nodesByID[node.ID] = &GraphStop{Details: node} case api.ResponseGraphNode: - nodesByID[node.ID] = &GraphTrackNode{Details: node} + nodesByID[node.ID] = &GraphRoadNode{Details: node} default: return nil, fmt.Errorf("Unrecognized node type: %s", node) } diff --git a/pkg/city/graph/graph_track_node.go b/pkg/city/graph/graph_road_node.go similarity index 53% rename from pkg/city/graph/graph_track_node.go rename to pkg/city/graph/graph_road_node.go index a5cf936..5308cc8 100644 --- a/pkg/city/graph/graph_track_node.go +++ b/pkg/city/graph/graph_road_node.go @@ -2,28 +2,28 @@ package graph import "github.com/TNSEngineerEdition/WailsClient/pkg/api" -type GraphTrackNode struct { +type GraphRoadNode struct { NodeBlock Details api.ResponseGraphNode `json:"details"` } -func (g *GraphTrackNode) IsStop() bool { +func (g *GraphRoadNode) IsStop() bool { return false } -func (g *GraphTrackNode) GetID() uint64 { +func (g *GraphRoadNode) GetID() uint64 { return g.Details.ID } -func (g *GraphTrackNode) GetCoordinates() (float32, float32) { +func (g *GraphRoadNode) GetCoordinates() (float32, float32) { return g.Details.Lat, g.Details.Lon } -func (g *GraphTrackNode) GetNeighbors() map[uint64]api.ResponseGraphEdge { +func (g *GraphRoadNode) GetNeighbors() map[uint64]api.ResponseGraphEdge { return g.Details.Neighbors } -func (g *GraphTrackNode) UpdateMaxSpeed(neighborID uint64, maxSpeed float32) { +func (g *GraphRoadNode) UpdateMaxSpeed(neighborID uint64, maxSpeed float32) { neighbor := g.Details.Neighbors[neighborID] neighbor.MaxSpeed = maxSpeed g.Details.Neighbors[neighborID] = neighbor diff --git a/pkg/controlcenter/control_center.go b/pkg/controlcenter/control_center.go index b8cb59c..290e00b 100644 --- a/pkg/controlcenter/control_center.go +++ b/pkg/controlcenter/control_center.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/TNSEngineerEdition/WailsClient/pkg/city" - "github.com/TNSEngineerEdition/WailsClient/pkg/city/graph" "github.com/TNSEngineerEdition/WailsClient/pkg/city/trip" "github.com/TNSEngineerEdition/WailsClient/pkg/structs" ) @@ -33,16 +32,9 @@ func NewControlCenter(city *city.City) ControlCenter { paths: make(map[stopPair]Path), segmentsByRouteName: make(map[string][]RouteSegment), } + controlCenter.setPaths(city) tramRoutes := city.GetTramRoutes() - nodesByID := city.GetNodesByID() - - for _, route := range tramRoutes { - for _, trip := range route.Trips { - controlCenter.addPathsFromTrip(&trip, &nodesByID) - } - } - for _, route := range tramRoutes { if route.Variants == nil { continue @@ -54,18 +46,21 @@ func NewControlCenter(city *city.City) ControlCenter { return controlCenter } -func (c *ControlCenter) addPathsFromTrip( - trip *trip.Trip, - nodesByID *map[uint64]graph.GraphNode, -) { - for i := 0; i < len(trip.Stops)-1; i++ { - stopPair := stopPair{ - source: trip.Stops[i].ID, - destination: trip.Stops[i+1].ID, - } +func (c *ControlCenter) setPaths(city *city.City) { + nodesByID := city.GetNodesByID() + paths := city.GetPaths() + + for startNodeID := range paths { + for endNodeID := range paths[startNodeID] { + stopPair := stopPair{ + source: startNodeID, + destination: endNodeID, + } - if _, ok := c.paths[stopPair]; !ok { - c.paths[stopPair] = getShortestPath(nodesByID, stopPair) + c.paths[stopPair] = pathFromNodeIDs( + paths[startNodeID][endNodeID], + &nodesByID, + ) } } } diff --git a/pkg/controlcenter/path.go b/pkg/controlcenter/path.go index 215fbb2..a29c121 100644 --- a/pkg/controlcenter/path.go +++ b/pkg/controlcenter/path.go @@ -1,12 +1,7 @@ package controlcenter import ( - "fmt" - "slices" - "github.com/TNSEngineerEdition/WailsClient/pkg/city/graph" - "github.com/TNSEngineerEdition/WailsClient/pkg/structs" - "github.com/umahmood/haversine" ) type Path struct { @@ -15,71 +10,16 @@ type Path struct { TimePrefixSum []float32 } -func (p *Path) GetProgressForIndex(index int) float32 { - return p.TimePrefixSum[index] / p.TimePrefixSum[len(p.TimePrefixSum)-1] -} - -func getShortestPath(nodesByID *map[uint64]graph.GraphNode, stops stopPair) (result Path) { - nodesToProcess := structs.NewPriorityQueueOrdered[uint64, float32]() - nodesToProcess.Push(stops.source, 0) - - predecessors := make(map[uint64]uint64) - tentativeDistFromSource := make(map[uint64]float32) - visitedNodes := structs.NewSet[uint64]() - - for nodesToProcess.Len() > 0 { - currentID := nodesToProcess.Pop() - - if currentID == stops.destination { - result.Nodes = reconstructPath(predecessors, nodesByID, currentID) - result.MaxSpeeds = getMaxSpeeds(result.Nodes) - result.TimePrefixSum = getPathTimePrefixSum(result.Nodes) - return - } - - if visitedNodes.Includes(currentID) { - continue - } - - visitedNodes.Add(currentID) - - for _, neighbor := range (*nodesByID)[currentID].GetNeighbors() { - tentativeDistance := tentativeDistFromSource[currentID] + neighbor.Distance - cost, wasVisited := tentativeDistFromSource[neighbor.ID] - - if wasVisited && tentativeDistance >= cost { - continue - } - - predecessors[neighbor.ID] = currentID - tentativeDistFromSource[neighbor.ID] = tentativeDistance +func pathFromNodeIDs(nodeIDs []uint64, nodesByID *map[uint64]graph.GraphNode) (result Path) { + result.Nodes = make([]graph.GraphNode, 0, len(nodeIDs)) - heuristicDistance := getDistanceInMeters( - (*nodesByID)[neighbor.ID], (*nodesByID)[stops.destination], - ) - nodesToProcess.Push(neighbor.ID, heuristicDistance+tentativeDistance) - } + for _, nodeID := range nodeIDs { + result.Nodes = append(result.Nodes, (*nodesByID)[nodeID]) } - panic(fmt.Sprintf("No path found between %d and %d nodes", stops.source, stops.destination)) -} - -func reconstructPath( - predecessors map[uint64]uint64, - nodesByID *map[uint64]graph.GraphNode, - currentID uint64, -) (nodes []graph.GraphNode) { - for { - nodes = append(nodes, (*nodesByID)[currentID]) - - if previousNodeID, ok := predecessors[currentID]; ok { - currentID = previousNodeID - } else { - break - } - } + result.MaxSpeeds = getMaxSpeeds(result.Nodes) + result.TimePrefixSum = getPathTimePrefixSum(result.Nodes) - slices.Reverse(nodes) return } @@ -99,24 +39,6 @@ func getMaxSpeeds(nodes []graph.GraphNode) []float32 { return maxSpeeds } -func getDistanceInMeters(source, destination graph.GraphNode) float32 { - sourceLat, sourceLon := source.GetCoordinates() - destLat, destLon := destination.GetCoordinates() - - _, kilometers := haversine.Distance( - haversine.Coord{ - Lat: float64(sourceLat), - Lon: float64(sourceLon), - }, - haversine.Coord{ - Lat: float64(destLat), - Lon: float64(destLon), - }, - ) - - return float32(kilometers * 1000) -} - func getPathTimePrefixSum(nodes []graph.GraphNode) []float32 { prefixSum := make([]float32, len(nodes)) @@ -128,3 +50,7 @@ func getPathTimePrefixSum(nodes []graph.GraphNode) []float32 { return prefixSum } + +func (p *Path) GetProgressForIndex(index int) float32 { + return p.TimePrefixSum[index] / p.TimePrefixSum[len(p.TimePrefixSum)-1] +} diff --git a/pkg/simulation/tram/tram.go b/pkg/simulation/tram/tram.go index 111d640..a62a9d5 100644 --- a/pkg/simulation/tram/tram.go +++ b/pkg/simulation/tram/tram.go @@ -1,6 +1,7 @@ package tram import ( + "fmt" "math" "math/rand/v2" @@ -145,7 +146,7 @@ func (t *Tram) getDistanceToNeighbor(v graph.GraphNode, u graph.GraphNode) float } else if neighbor, ok := u.GetNeighbors()[v.GetID()]; ok { return neighbor.Distance } else { - panic("Distance between nodes not found") + panic(fmt.Sprintf("Distance between nodes %d and %d not found", v.GetID(), u.GetID())) } }