feat: solve direct edit constraints
This commit is contained in:
1 parent
67e67e66bf
commit
0eb404d8f7
3 files changed
+390
-47
No files matched your search
@@ -608,8 +608,10 @@ function detectComplexJunctionCandidates(model, junctionPlans, options, diagnost
|
||||
}
|
||||
|
||||
function compileGeometry(model, overrides = { overrides: [] }, options = {}) {
|
||||
model = modelWithDirectEditProfiles(model, options.directEdit);
|
||||
const diagnostics = [...model.diagnostics];
|
||||
const junctionPlans = compileJunctionPlans(model, options, diagnostics);
|
||||
applyDirectJunctionPlans(junctionPlans, options.directEdit);
|
||||
detectComplexJunctionCandidates(model, junctionPlans, options, diagnostics);
|
||||
const features = [];
|
||||
const activeClusters = options.junctionTemplates?.enabled ? options.junctionTemplates.clusters || [] : [];
|
||||
@@ -806,6 +808,76 @@ function compileGeometry(model, overrides = { overrides: [] }, options = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
function modelWithDirectEditProfiles(model, directEdit) {
|
||||
if (!directEdit?.roadProfiles?.size && !directEdit?.junctionPlans?.size) return model;
|
||||
const approachWidths = new Map();
|
||||
for (const plan of directEdit.junctionPlans?.values?.() || [])
|
||||
for (const [segmentId, entry] of Object.entries(plan.approaches || {}))
|
||||
if (Number.isFinite(entry.widthMeters)) approachWidths.set(segmentId, entry.widthMeters);
|
||||
const roads = model.roads.map((road) => {
|
||||
const profile = directEdit.roadProfiles.get(road.id);
|
||||
const approachWidth = approachWidths.get(road.segmentId);
|
||||
const siblingWidth = model.roads
|
||||
.filter((item) => item.segmentId === road.segmentId)
|
||||
.reduce((sum, item) => sum + item.widthMeters, 0);
|
||||
const approachScale = Number.isFinite(approachWidth) && siblingWidth > 0 ? approachWidth / siblingWidth : 1;
|
||||
if (!profile && approachScale === 1) return road;
|
||||
const centerlineShift = profile ? ((profile.edgeOffsets?.left || 0) - (profile.edgeOffsets?.right || 0)) / 2 : 0;
|
||||
const shiftedCenterline =
|
||||
Math.abs(centerlineShift) > 1e-9 ? offsetLine(road.centerline, centerlineShift) : road.centerline;
|
||||
return {
|
||||
...road,
|
||||
centerline: shiftedCenterline || road.centerline,
|
||||
widthMeters: Number.isFinite(profile?.widthMeters)
|
||||
? profile.widthMeters * approachScale
|
||||
: road.widthMeters * approachScale,
|
||||
sidewalkLeft: profile ? profile.sidewalkWidths?.left > 0 : road.sidewalkLeft,
|
||||
sidewalkRight: profile ? profile.sidewalkWidths?.right > 0 : road.sidewalkRight,
|
||||
directEditProfile: profile || null,
|
||||
};
|
||||
});
|
||||
return { ...model, roads };
|
||||
}
|
||||
|
||||
function applyDirectJunctionPlans(plans, directEdit) {
|
||||
if (!directEdit?.junctionPlans?.size) return;
|
||||
for (const [nodeId, directPlan] of directEdit.junctionPlans) {
|
||||
const plan = plans.get(String(nodeId));
|
||||
if (!plan) continue;
|
||||
let changed = false;
|
||||
for (const approach of plan.approaches) {
|
||||
const override = directPlan.approaches?.[approach.segmentId];
|
||||
if (!override) continue;
|
||||
if (Number.isFinite(override.widthMeters)) {
|
||||
approach.widthMeters = override.widthMeters;
|
||||
changed = true;
|
||||
}
|
||||
if (Number.isFinite(override.cutbackMeters)) {
|
||||
approach.cutbackMeters = override.cutbackMeters;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
const cutbacks = Object.values(directPlan.approaches || {})
|
||||
.map((entry) => entry.cutbackMeters)
|
||||
.filter(Number.isFinite);
|
||||
if (cutbacks.length) {
|
||||
plan.cutbackMeters = Math.max(...cutbacks);
|
||||
changed = true;
|
||||
}
|
||||
const requestedRadii = Object.values(directPlan.corners || {}).filter(Number.isFinite);
|
||||
if (requestedRadii.length) changed = true;
|
||||
if (!changed) continue;
|
||||
const referenceWidth = Math.max(...plan.approaches.map((approach) => approach.widthMeters), 1);
|
||||
const cornerRadiusMultiplier = requestedRadii.length
|
||||
? Math.max(0.1, Math.min(3, requestedRadii[0] / referenceWidth))
|
||||
: 1;
|
||||
const boundary = junctionBoundary(plan.approaches, plan.node, plan.cutbackMeters, cornerRadiusMultiplier, 1);
|
||||
plan.boundary = boundary.points;
|
||||
plan.boundaryMode = boundary.mode;
|
||||
plan.boundaryFallbacks = boundary.fallbacks;
|
||||
}
|
||||
}
|
||||
|
||||
function compileComplexPreviewArrows(features, stopLines) {
|
||||
const result = [];
|
||||
for (const feature of features) {
|
||||
@@ -1590,16 +1662,13 @@ function compileSidewalkSurfaces(model, diagnostics, junctionPlans, options = {}
|
||||
const cluster = clusterForRoad(forward, options) || (backward ? clusterForRoad(backward, options) : null);
|
||||
const center = cluster ? clusterCenter(cluster, junctionPlans) : null;
|
||||
for (const [side, enabled] of sides) {
|
||||
if (!enabled) continue;
|
||||
const profile = forward.directEditProfile;
|
||||
const sidewalkWidth = profile?.sidewalkWidths?.[side] ?? DEFAULT_SIDEWALK_WIDTH_METERS;
|
||||
if (!enabled || sidewalkWidth <= 0) continue;
|
||||
const centerline = cluster
|
||||
? trimLineAtComplexCluster(forward.centerline, forward.sourceNodeIds, junctionPlans, cluster, center)
|
||||
: trimLineAtJunctions(forward.centerline, forward.sourceNodeIds, junctionPlans);
|
||||
const ring = sidewalkRing(
|
||||
centerline,
|
||||
totalWidth / 2,
|
||||
totalWidth / 2 + DEFAULT_SIDEWALK_WIDTH_METERS,
|
||||
side === 'left' ? 1 : -1,
|
||||
);
|
||||
const ring = sidewalkRing(centerline, totalWidth / 2, totalWidth / 2 + sidewalkWidth, side === 'left' ? 1 : -1);
|
||||
if (!ring) {
|
||||
diagnostics.push(
|
||||
diagnostic(
|
||||
@@ -1622,7 +1691,7 @@ function compileSidewalkSurfaces(model, diagnostics, junctionPlans, options = {}
|
||||
osm_way_ids: forward.osmWayIds.join(','),
|
||||
source_road_id: forward.sourceRoadId,
|
||||
side,
|
||||
width_m: DEFAULT_SIDEWALK_WIDTH_METERS,
|
||||
width_m: sidewalkWidth,
|
||||
directional_road_ids: directions.map((road) => road.id).join(','),
|
||||
provenance: 'native-road-sidewalk/v1',
|
||||
override_ids: directions.flatMap((road) => road.appliedOverrideIds).join(','),
|
||||
@@ -1874,7 +1943,10 @@ function compileLaneCenterlines(model, diagnostics, junctionPlans, options = {},
|
||||
for (let index = 0; index < road.laneCount; index += 1) {
|
||||
// OSM `turn:lanes` is ordered from left to right. Keep lane 1 on the
|
||||
// driver's left so tag positions and generated lane IDs have one meaning.
|
||||
const offset = carriagewayOffset + (road.widthMeters / 2 - laneWidth * (index + 0.5));
|
||||
const dividerAdjustment = Object.entries(road.directEditProfile?.laneDividerOffsets || {})
|
||||
.filter(([boundary]) => index + 1 > Number(boundary))
|
||||
.reduce((sum, [, value]) => sum + Number(value) / Math.max(road.laneCount, 1), 0);
|
||||
const offset = carriagewayOffset + (road.widthMeters / 2 - laneWidth * (index + 0.5)) + dividerAdjustment;
|
||||
const coordinates = offsetLine(trimLineAtJunctions(road.centerline, road.sourceNodeIds, junctionPlans), offset);
|
||||
const publishedCoordinates = offsetLine(clippedRoadLine, offset);
|
||||
if (!coordinates || !publishedCoordinates) {
|
||||
|
||||
Reference in new issue
Block a user