Make it possible to limit speed on sharp corners

This commit is contained in:
Martin Asprusten 2025-07-02 22:54:16 +02:00
parent d07c040d95
commit e5a4e9d716
6 changed files with 56 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.vscode/
dist/ dist/
native/* native/*
node_modules/ node_modules/

View File

@ -63,6 +63,9 @@
<div class="settings-line"> <div class="settings-line">
<label for="allow-wrong-way-input">Allow going the wrong way down one-way roads:</label><input id="allow-wrong-way-input" type="checkbox" /> <label for="allow-wrong-way-input">Allow going the wrong way down one-way roads:</label><input id="allow-wrong-way-input" type="checkbox" />
</div> </div>
<div class="settings-line">
<label for="limit-corner-speed-input">Limit speed on sharp corners</label><input id="limit-corner-speed-input" type="checkbox" />
</div>
<div class="settings-line"> <div class="settings-line">
<label for="drag-coefficient-input">Drag coeffient (0.005 is a good default):</label><input id="drag-coefficient-input" type="number" min="0" step="0.001" /> <label for="drag-coefficient-input">Drag coeffient (0.005 is a good default):</label><input id="drag-coefficient-input" type="number" min="0" step="0.001" />
</div> </div>

View File

@ -15,6 +15,7 @@ const float GRAVITY_ACCELERATION = 9.81;
struct Connection { struct Connection {
int connected_point_number; int connected_point_number;
float distance; float distance;
float course;
uint8_t speed_limit; uint8_t speed_limit;
bool motorway; bool motorway;
bool tunnel; bool tunnel;
@ -64,6 +65,7 @@ struct JSSearchResult {
struct ListNode { struct ListNode {
int id; int id;
float current_speed; float current_speed;
float current_course;
ListNode* next = NULL; ListNode* next = NULL;
}; };
@ -93,6 +95,7 @@ struct CurrentAreaSearch {
bool allowMotorways; bool allowMotorways;
bool allowTunnels; bool allowTunnels;
bool allowAgainstOneway; bool allowAgainstOneway;
bool limitCornerSpeed;
std::vector<uint32_t> startNodes; std::vector<uint32_t> startNodes;
size_t startNodeCounter = 0; size_t startNodeCounter = 0;
@ -121,10 +124,12 @@ void addLinkToRoadNode(RoadNode* roadNodes, int node_from, int node_to, uint8_t
float to_y = roadNodes[node_to].position_y; float to_y = roadNodes[node_to].position_y;
float distance = sqrt(pow(to_x - from_x, 2) + pow(to_y - from_y, 2)); float distance = sqrt(pow(to_x - from_x, 2) + pow(to_y - from_y, 2));
float course = atan2(to_x - from_x, to_y - from_y) * 180 / 3.14152965;
Connection connection; Connection connection;
connection.connected_point_number = node_to; connection.connected_point_number = node_to;
connection.distance = distance; connection.distance = distance;
connection.course = course;
connection.speed_limit = speed_limit; connection.speed_limit = speed_limit;
connection.motorway = motorway; connection.motorway = motorway;
connection.tunnel = tunnel; connection.tunnel = tunnel;
@ -302,7 +307,7 @@ void getNeighbourConnections(RoadNode node, Connection* targetArray, int &number
} }
} }
SearchResult findAllPathsFromPoint(int startingNode, float minimum_speed, float maximum_speed, int maximumSpeedLimit, float drag_coefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway) { SearchResult findAllPathsFromPoint(int startingNode, float minimum_speed, float maximum_speed, int maximumSpeedLimit, float drag_coefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway, bool limitCornerSpeed) {
SearchResult result; SearchResult result;
result.startingNode = startingNode; result.startingNode = startingNode;
@ -313,10 +318,10 @@ SearchResult findAllPathsFromPoint(int startingNode, float minimum_speed, float
result.reachableNodes[startingNode] = firstNodeInfo; result.reachableNodes[startingNode] = firstNodeInfo;
ListNode *nextNode = new ListNode; ListNode *nextNode = new ListNode;
ListNode *lastNode = nextNode;
nextNode->id = startingNode; nextNode->id = startingNode;
nextNode->current_speed = minimum_speed; nextNode->current_speed = minimum_speed;
nextNode->current_course = 0;
while (nextNode != NULL) { while (nextNode != NULL) {
ListNode *currentNode = nextNode; ListNode *currentNode = nextNode;
@ -324,12 +329,9 @@ SearchResult findAllPathsFromPoint(int startingNode, float minimum_speed, float
int currentId = currentNode->id; int currentId = currentNode->id;
float currentSpeed = currentNode->current_speed; float currentSpeed = currentNode->current_speed;
float currentCourse = currentNode->current_course;
RoadNode bestNode = set.roadNodes[currentId]; RoadNode bestNode = set.roadNodes[currentId];
if (lastNode == currentNode) {
lastNode = NULL;
}
delete currentNode; delete currentNode;
Connection neighbours[10]; Connection neighbours[10];
@ -366,6 +368,21 @@ SearchResult findAllPathsFromPoint(int startingNode, float minimum_speed, float
if (resultingSpeed < 0) { if (resultingSpeed < 0) {
continue; continue;
} }
// If we limit the speed on corners, do that here
if (limitCornerSpeed) {
float courseDifference = fabs(currentCourse - neighbour.course);
if (courseDifference > 180.0) {
courseDifference = 360 - courseDifference;
}
if (courseDifference > 95) {
resultingSpeed = minimum_speed;
} else if (courseDifference > 45.0) {
float maximumCornerSpeed = (95 - courseDifference) / 50.0 * (maximum_speed - minimum_speed) + minimum_speed;
resultingSpeed = fmin(resultingSpeed, maximumCornerSpeed);
}
}
// Check if this node is already in the reachable nodes map // Check if this node is already in the reachable nodes map
auto resultIterator = result.reachableNodes.find(neighbour.connected_point_number); auto resultIterator = result.reachableNodes.find(neighbour.connected_point_number);
@ -383,6 +400,7 @@ SearchResult findAllPathsFromPoint(int startingNode, float minimum_speed, float
ListNode *neighbourListNode = new ListNode; ListNode *neighbourListNode = new ListNode;
neighbourListNode->id = neighbour.connected_point_number; neighbourListNode->id = neighbour.connected_point_number;
neighbourListNode->current_speed = reachableNodeInfo.currentSpeed; neighbourListNode->current_speed = reachableNodeInfo.currentSpeed;
neighbourListNode->current_course = neighbour.course;
if (nextNode == NULL || resultingSpeed < nextNode->current_speed) { if (nextNode == NULL || resultingSpeed < nextNode->current_speed) {
neighbourListNode->next = nextNode; neighbourListNode->next = nextNode;
@ -405,8 +423,8 @@ SearchResult findAllPathsFromPoint(int startingNode, float minimum_speed, float
return result; return result;
} }
JSSearchResult findAllPathsFromPointJS(int startingNode, float minimumSpeed, float maximumSpeed, int maximumSpeedLimit, float dragCoefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway) { JSSearchResult findAllPathsFromPointJS(int startingNode, float minimumSpeed, float maximumSpeed, int maximumSpeedLimit, float dragCoefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway, bool limitCornerSpeed) {
lastSearchResult = findAllPathsFromPoint(startingNode, minimumSpeed, maximumSpeed, maximumSpeedLimit, dragCoefficient, allowMotorways, allowTunnels, allowAgainstOneway); lastSearchResult = findAllPathsFromPoint(startingNode, minimumSpeed, maximumSpeed, maximumSpeedLimit, dragCoefficient, allowMotorways, allowTunnels, allowAgainstOneway, limitCornerSpeed);
float start_x = set.roadNodes[startingNode].position_x; float start_x = set.roadNodes[startingNode].position_x;
float start_y = set.roadNodes[startingNode].position_y; float start_y = set.roadNodes[startingNode].position_y;
@ -721,7 +739,7 @@ std::vector<uint32_t> findPossibleStartNodes(float minimumSpeed, float maximumSp
return possibleStartNodes; return possibleStartNodes;
} }
AreaSearchResult startAreaSearch(float minimumSpeed, float maximumSpeed, float maximumSpeedLimit, float dragCoefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway, std::vector<std::vector<std::vector<PolygonCoordinate>>> searchArea) { AreaSearchResult startAreaSearch(float minimumSpeed, float maximumSpeed, float maximumSpeedLimit, float dragCoefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway, bool limitCornerSpeed, std::vector<std::vector<std::vector<PolygonCoordinate>>> searchArea) {
currentAreaSearch = CurrentAreaSearch(); currentAreaSearch = CurrentAreaSearch();
currentAreaSearch.startNodes = findPossibleStartNodes(minimumSpeed, maximumSpeedLimit, dragCoefficient, allowMotorways, allowTunnels, allowAgainstOneway, searchArea); currentAreaSearch.startNodes = findPossibleStartNodes(minimumSpeed, maximumSpeedLimit, dragCoefficient, allowMotorways, allowTunnels, allowAgainstOneway, searchArea);
currentAreaSearch.minimumSpeed = minimumSpeed; currentAreaSearch.minimumSpeed = minimumSpeed;
@ -731,6 +749,7 @@ AreaSearchResult startAreaSearch(float minimumSpeed, float maximumSpeed, float m
currentAreaSearch.allowMotorways = allowMotorways; currentAreaSearch.allowMotorways = allowMotorways;
currentAreaSearch.allowTunnels = allowTunnels; currentAreaSearch.allowTunnels = allowTunnels;
currentAreaSearch.allowAgainstOneway = allowAgainstOneway; currentAreaSearch.allowAgainstOneway = allowAgainstOneway;
currentAreaSearch.limitCornerSpeed = limitCornerSpeed;
AreaSearchResult result; AreaSearchResult result;
result.remainingNodes = currentAreaSearch.startNodes.size(); result.remainingNodes = currentAreaSearch.startNodes.size();
@ -747,7 +766,8 @@ AreaSearchResult continueAreaSearch() {
currentAreaSearch.dragCoefficient, currentAreaSearch.dragCoefficient,
currentAreaSearch.allowMotorways, currentAreaSearch.allowMotorways,
currentAreaSearch.allowTunnels, currentAreaSearch.allowTunnels,
currentAreaSearch.allowAgainstOneway currentAreaSearch.allowAgainstOneway,
currentAreaSearch.limitCornerSpeed
); );
// Remove all nodes we have reached from here as possible future start nodes // Remove all nodes we have reached from here as possible future start nodes

View File

@ -44,7 +44,8 @@ interface FindPathsFromNode {
dragCoefficient: number, dragCoefficient: number,
allowMotorways: boolean, allowMotorways: boolean,
allowTunnels: boolean, allowTunnels: boolean,
allowAgainstOneway: boolean allowAgainstOneway: boolean,
limitCornerSpeed: boolean
} }
export interface Endpoint { export interface Endpoint {
@ -102,7 +103,8 @@ interface SearchArea {
dragCoefficient: number, dragCoefficient: number,
allowMotorways: boolean, allowMotorways: boolean,
allowTunnels: boolean, allowTunnels: boolean,
allowAgainstOneway: boolean allowAgainstOneway: boolean,
limitCornerSpeed: boolean
} }
interface ContinueSearch { interface ContinueSearch {

View File

@ -16,6 +16,7 @@ interface Settings {
allowMotorways: boolean, allowMotorways: boolean,
allowTunnels: boolean, allowTunnels: boolean,
allowAgainstOneway: boolean, allowAgainstOneway: boolean,
limitCornerSpeed: boolean,
cutoffDistance: number cutoffDistance: number
} }
@ -27,6 +28,7 @@ const DEFAULT_DRAG_COEFFICIENT = 0.005;
const DEFAULT_ALLOW_MOTORWAYS = false; const DEFAULT_ALLOW_MOTORWAYS = false;
const DEFAULT_ALLOW_TUNNELS = false; const DEFAULT_ALLOW_TUNNELS = false;
const DEFAULT_ALLOW_AGAINST_ONE_WAY = false; const DEFAULT_ALLOW_AGAINST_ONE_WAY = false;
const DEFAULT_LIMIT_CORNER_SPEED = true;
const DEFAULT_CUTOFF_DISTANCE = 1000; const DEFAULT_CUTOFF_DISTANCE = 1000;
// Set up variables // Set up variables
@ -58,6 +60,7 @@ let dragCoefficientInput = document.getElementById('drag-coefficient-input');
let allowMotorwaysInput = document.getElementById('allow-motorways-input'); let allowMotorwaysInput = document.getElementById('allow-motorways-input');
let allowTunnelsInput = document.getElementById('allow-tunnels-input'); let allowTunnelsInput = document.getElementById('allow-tunnels-input');
let allowAgainstOnewayInput = document.getElementById('allow-wrong-way-input'); let allowAgainstOnewayInput = document.getElementById('allow-wrong-way-input');
let limitCornerSpeedInput = document.getElementById('limit-corner-speed-input');
let cutoffDistanceInput = document.getElementById('cutoff-distance-input'); let cutoffDistanceInput = document.getElementById('cutoff-distance-input');
@ -79,7 +82,8 @@ routeWorker.onmessage = e => {
dragCoefficient: settings.dragCoefficient, dragCoefficient: settings.dragCoefficient,
allowMotorways: settings.allowMotorways, allowMotorways: settings.allowMotorways,
allowTunnels: settings.allowTunnels, allowTunnels: settings.allowTunnels,
allowAgainstOneway: settings.allowAgainstOneway allowAgainstOneway: settings.allowAgainstOneway,
limitCornerSpeed: settings.limitCornerSpeed
}}; }};
routeWorker.postMessage(findPathsMessage); routeWorker.postMessage(findPathsMessage);
} else if (message.foundPathsFromNode != null) { } else if (message.foundPathsFromNode != null) {
@ -137,7 +141,8 @@ routeWorker.onmessage = e => {
dragCoefficient: settings.dragCoefficient, dragCoefficient: settings.dragCoefficient,
allowMotorways: settings.allowMotorways, allowMotorways: settings.allowMotorways,
allowTunnels: settings.allowTunnels, allowTunnels: settings.allowTunnels,
allowAgainstOneway: settings.allowAgainstOneway allowAgainstOneway: settings.allowAgainstOneway,
limitCornerSpeed: settings.limitCornerSpeed
} }
}; };
routeWorker.postMessage(requestMessage); routeWorker.postMessage(requestMessage);
@ -189,7 +194,8 @@ function setUpMapHandler() {
dragCoefficient: settings.dragCoefficient, dragCoefficient: settings.dragCoefficient,
allowMotorways: settings.allowMotorways, allowMotorways: settings.allowMotorways,
allowTunnels: settings.allowTunnels, allowTunnels: settings.allowTunnels,
allowAgainstOneway: settings.allowAgainstOneway allowAgainstOneway: settings.allowAgainstOneway,
limitCornerSpeed: settings.limitCornerSpeed
}}; }};
routeWorker.postMessage(newRoutesMessage); routeWorker.postMessage(newRoutesMessage);
} }
@ -306,6 +312,7 @@ setUpNumberInput(dragCoefficientInput, 'drag-coefficient', DEFAULT_DRAG_COEFFICI
setUpBooleanInput(allowMotorwaysInput, 'allow-motorways', DEFAULT_ALLOW_MOTORWAYS); setUpBooleanInput(allowMotorwaysInput, 'allow-motorways', DEFAULT_ALLOW_MOTORWAYS);
setUpBooleanInput(allowTunnelsInput, 'allow-tunnels', DEFAULT_ALLOW_TUNNELS); setUpBooleanInput(allowTunnelsInput, 'allow-tunnels', DEFAULT_ALLOW_TUNNELS);
setUpBooleanInput(allowAgainstOnewayInput, 'allow-against-one-way', DEFAULT_ALLOW_AGAINST_ONE_WAY); setUpBooleanInput(allowAgainstOnewayInput, 'allow-against-one-way', DEFAULT_ALLOW_AGAINST_ONE_WAY);
setUpBooleanInput(limitCornerSpeedInput, 'limit-corner-speed', DEFAULT_LIMIT_CORNER_SPEED);
setUpNumberInput(cutoffDistanceInput, 'cutoff-distance', DEFAULT_CUTOFF_DISTANCE); setUpNumberInput(cutoffDistanceInput, 'cutoff-distance', DEFAULT_CUTOFF_DISTANCE);
function getSettings(): Settings { function getSettings(): Settings {
@ -317,6 +324,7 @@ function getSettings(): Settings {
allowMotorways: getBooleanValue(allowMotorwaysInput, DEFAULT_ALLOW_MOTORWAYS), allowMotorways: getBooleanValue(allowMotorwaysInput, DEFAULT_ALLOW_MOTORWAYS),
allowTunnels: getBooleanValue(allowTunnelsInput, DEFAULT_ALLOW_TUNNELS), allowTunnels: getBooleanValue(allowTunnelsInput, DEFAULT_ALLOW_TUNNELS),
allowAgainstOneway: getBooleanValue(allowAgainstOnewayInput, DEFAULT_ALLOW_AGAINST_ONE_WAY), allowAgainstOneway: getBooleanValue(allowAgainstOnewayInput, DEFAULT_ALLOW_AGAINST_ONE_WAY),
limitCornerSpeed: getBooleanValue(limitCornerSpeedInput, DEFAULT_LIMIT_CORNER_SPEED),
cutoffDistance: getNumberValue(cutoffDistanceInput, DEFAULT_CUTOFF_DISTANCE) cutoffDistance: getNumberValue(cutoffDistanceInput, DEFAULT_CUTOFF_DISTANCE)
}; };
} }
@ -329,6 +337,7 @@ function enableSettings(): void {
enableInput(allowMotorwaysInput); enableInput(allowMotorwaysInput);
enableInput(allowTunnelsInput); enableInput(allowTunnelsInput);
enableInput(allowAgainstOnewayInput); enableInput(allowAgainstOnewayInput);
enableInput(limitCornerSpeedInput);
} }
function disableSettings(): void { function disableSettings(): void {
@ -339,6 +348,7 @@ function disableSettings(): void {
disableInput(allowMotorwaysInput); disableInput(allowMotorwaysInput);
disableInput(allowTunnelsInput); disableInput(allowTunnelsInput);
disableInput(allowAgainstOnewayInput); disableInput(allowAgainstOnewayInput);
disableInput(limitCornerSpeedInput);
} }
@ -356,7 +366,8 @@ searchButton?.addEventListener('click', _ => {
dragCoefficient: settings.dragCoefficient, dragCoefficient: settings.dragCoefficient,
allowMotorways: settings.allowMotorways, allowMotorways: settings.allowMotorways,
allowTunnels: settings.allowTunnels, allowTunnels: settings.allowTunnels,
allowAgainstOneway: settings.allowAgainstOneway allowAgainstOneway: settings.allowAgainstOneway,
limitCornerSpeed: settings.limitCornerSpeed
}}; }};
routeWorker.postMessage(message); routeWorker.postMessage(message);
} }

View File

@ -102,7 +102,8 @@ onmessage = async (e) => {
message.findPathsFromNode.dragCoefficient, message.findPathsFromNode.dragCoefficient,
message.findPathsFromNode.allowMotorways, message.findPathsFromNode.allowMotorways,
message.findPathsFromNode.allowTunnels, message.findPathsFromNode.allowTunnels,
message.findPathsFromNode.allowAgainstOneway message.findPathsFromNode.allowAgainstOneway,
message.findPathsFromNode.limitCornerSpeed
); );
let endpoints: Endpoint[] = []; let endpoints: Endpoint[] = [];
@ -186,6 +187,7 @@ onmessage = async (e) => {
settings.allowMotorways, settings.allowMotorways,
settings.allowTunnels, settings.allowTunnels,
settings.allowAgainstOneway, settings.allowAgainstOneway,
settings.limitCornerSpeed,
createCMultiPolygon(module, settings.polygons) createCMultiPolygon(module, settings.polygons)
); );