Compare commits

..

No commits in common. "b3b3a5b652210a86cacca05f5d18f2dc573ae003" and "d07c040d958e9e9557483ba40f6179c5794f5090" have entirely different histories.

6 changed files with 26 additions and 123 deletions

1
.gitignore vendored
View File

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

View File

@ -63,9 +63,6 @@
<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,7 +15,6 @@ 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;
@ -65,7 +64,6 @@ 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;
}; };
@ -95,12 +93,11 @@ 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;
std::vector<AreaSearchEntry> currentAreaSearchResults; std::vector<AreaSearchEntry> currentAreaSearchResults;
std::map<uint32_t, std::vector<uint32_t>> utilizedNodes; std::set<uint32_t> utilizedNodes;
}; };
// Data // Data
@ -124,12 +121,10 @@ 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;
@ -307,7 +302,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, bool limitCornerSpeed) { SearchResult findAllPathsFromPoint(int startingNode, float minimum_speed, float maximum_speed, int maximumSpeedLimit, float drag_coefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway) {
SearchResult result; SearchResult result;
result.startingNode = startingNode; result.startingNode = startingNode;
@ -318,10 +313,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;
@ -329,9 +324,12 @@ 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];
@ -368,21 +366,6 @@ 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);
@ -400,7 +383,6 @@ 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;
@ -423,8 +405,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, bool limitCornerSpeed) { JSSearchResult findAllPathsFromPointJS(int startingNode, float minimumSpeed, float maximumSpeed, int maximumSpeedLimit, float dragCoefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway) {
lastSearchResult = findAllPathsFromPoint(startingNode, minimumSpeed, maximumSpeed, maximumSpeedLimit, dragCoefficient, allowMotorways, allowTunnels, allowAgainstOneway, limitCornerSpeed); lastSearchResult = findAllPathsFromPoint(startingNode, minimumSpeed, maximumSpeed, maximumSpeedLimit, dragCoefficient, allowMotorways, allowTunnels, allowAgainstOneway);
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;
@ -739,7 +721,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, bool limitCornerSpeed, std::vector<std::vector<std::vector<PolygonCoordinate>>> searchArea) { AreaSearchResult startAreaSearch(float minimumSpeed, float maximumSpeed, float maximumSpeedLimit, float dragCoefficient, bool allowMotorways, bool allowTunnels, bool allowAgainstOneway, 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;
@ -749,7 +731,6 @@ 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();
@ -766,8 +747,7 @@ 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
@ -801,72 +781,18 @@ AreaSearchResult continueAreaSearch() {
} }
} }
std::map<uint32_t, std::vector<uint32_t>> path; std::set<uint32_t> path;
path[farthestNode] = std::vector<uint32_t>(); path.insert(farthestNode);
uint32_t currentNode = farthestNode; uint32_t currentNode = farthestNode;
while (result.previous.find(currentNode) != result.previous.end()) { while (result.previous.find(currentNode) != result.previous.end()) {
currentNode = result.previous.find(currentNode)->second; currentNode = result.previous.find(currentNode)->second;
path[currentNode] = std::vector<uint32_t>(); path.insert(currentNode);
} }
// Check if our path overlaps too much with already travelled paths // Check if our path overlaps too much with already travelled paths
std::map<uint32_t, std::vector<uint32_t>> overlap; std::vector<uint32_t> overlap;
auto inserterIterator = overlap.begin(); std::set_intersection(path.begin(), path.end(), currentAreaSearch.utilizedNodes.begin(), currentAreaSearch.utilizedNodes.end(), std::back_inserter(overlap));
std::set_intersection( if (overlap.size() < 0.5 * path.size()) {
currentAreaSearch.utilizedNodes.begin(),
currentAreaSearch.utilizedNodes.end(),
path.begin(),
path.end(),
std::inserter(overlap, inserterIterator),
currentAreaSearch.utilizedNodes.value_comp()
);
std::map<uint32_t, uint32_t> overlapCounts;
for (auto it = overlap.begin(); it != overlap.end(); it++) {
for (auto startingNodeIt = it->second.begin(); startingNodeIt != it->second.end(); startingNodeIt++) {
uint32_t startingNode = *startingNodeIt;
if (overlapCounts.find(startingNode) == overlapCounts.end()) {
overlapCounts[startingNode] = 0;
}
overlapCounts[startingNode] = overlapCounts[startingNode] + 1;
}
}
auto overlapIterator = overlapCounts.begin();
while (overlapIterator != overlapCounts.end()) {
uint32_t overlapCount = overlapIterator->second;
if (overlapCount < path.size() * 0.5) {
overlapIterator = overlapCounts.erase(overlapIterator);
continue;
}
// In case we overlap more than 50% with another path, check if the current path is longer
uint32_t otherPath = overlapIterator->first;
bool isLongerThanOtherPath = true;
for (auto otherPathIterator = currentAreaSearch.currentAreaSearchResults.begin(); otherPathIterator != currentAreaSearch.currentAreaSearchResults.end(); otherPathIterator++) {
if (otherPathIterator->nodeId != otherPath) {
continue;
}
if (farthestDistance > otherPathIterator->longestRoute) {
currentAreaSearch.currentAreaSearchResults.erase(otherPathIterator);
break;
} else {
isLongerThanOtherPath = false;
break;
}
}
if (isLongerThanOtherPath) {
overlapIterator = overlapCounts.erase(overlapIterator);
continue;
}
// If we got here, we found another path that shares at least 50% of or route points, that is longer than the current route
break;
}
if (overlapCounts.size() == 0) {
AreaSearchEntry searchEntry; AreaSearchEntry searchEntry;
searchEntry.nodeId = currentStartNode; searchEntry.nodeId = currentStartNode;
searchEntry.positionX = startNode.position_x; searchEntry.positionX = startNode.position_x;
@ -883,12 +809,8 @@ AreaSearchResult continueAreaSearch() {
searchEntry searchEntry
); );
for (std::pair<uint32_t, std::vector<uint32_t>> pathNodeEntry : path) { for (uint32_t pathNode : path) {
uint32_t pathNode = pathNodeEntry.first; currentAreaSearch.utilizedNodes.insert(pathNode);
if (currentAreaSearch.utilizedNodes.find(pathNode) == currentAreaSearch.utilizedNodes.end()) {
currentAreaSearch.utilizedNodes[pathNode] = std::vector<uint32_t>();
}
currentAreaSearch.utilizedNodes[pathNode].push_back(currentStartNode);
} }
} }

View File

@ -44,8 +44,7 @@ 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 {
@ -103,8 +102,7 @@ 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,7 +16,6 @@ interface Settings {
allowMotorways: boolean, allowMotorways: boolean,
allowTunnels: boolean, allowTunnels: boolean,
allowAgainstOneway: boolean, allowAgainstOneway: boolean,
limitCornerSpeed: boolean,
cutoffDistance: number cutoffDistance: number
} }
@ -28,7 +27,6 @@ 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
@ -60,7 +58,6 @@ 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');
@ -82,8 +79,7 @@ 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) {
@ -141,8 +137,7 @@ 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);
@ -194,8 +189,7 @@ 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);
} }
@ -312,7 +306,6 @@ 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 {
@ -324,7 +317,6 @@ 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)
}; };
} }
@ -337,7 +329,6 @@ function enableSettings(): void {
enableInput(allowMotorwaysInput); enableInput(allowMotorwaysInput);
enableInput(allowTunnelsInput); enableInput(allowTunnelsInput);
enableInput(allowAgainstOnewayInput); enableInput(allowAgainstOnewayInput);
enableInput(limitCornerSpeedInput);
} }
function disableSettings(): void { function disableSettings(): void {
@ -348,7 +339,6 @@ function disableSettings(): void {
disableInput(allowMotorwaysInput); disableInput(allowMotorwaysInput);
disableInput(allowTunnelsInput); disableInput(allowTunnelsInput);
disableInput(allowAgainstOnewayInput); disableInput(allowAgainstOnewayInput);
disableInput(limitCornerSpeedInput);
} }
@ -366,8 +356,7 @@ 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,8 +102,7 @@ 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[] = [];
@ -187,7 +186,6 @@ 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)
); );