From 8fd72cba97408e2881d4a57b752e72b2513e7442 Mon Sep 17 00:00:00 2001 From: Frithjof Winkelmann Date: Mon, 7 Oct 2024 11:47:12 +0200 Subject: [PATCH] Show start distance --- backend-rust/src/bin/main.rs | 29 ++++++++---- backend-rust/src/search.rs | 18 ++++++-- frontend/src/components/CurrentNode.tsx | 7 ++- frontend/src/components/Grid.tsx | 16 +++---- frontend/src/components/SearchComponent.tsx | 17 ++----- frontend/src/components/StartMarker.tsx | 49 +++++++++++++++++++++ frontend/src/utils/types.ts | 1 + frontend/src/utils/utils.ts | 2 +- 8 files changed, 100 insertions(+), 39 deletions(-) create mode 100644 frontend/src/components/StartMarker.tsx diff --git a/backend-rust/src/bin/main.rs b/backend-rust/src/bin/main.rs index 33a3d4d..2b5a8af 100644 --- a/backend-rust/src/bin/main.rs +++ b/backend-rust/src/bin/main.rs @@ -115,7 +115,7 @@ fn search_from_point_memoized( longitude: Distance, cell_size: Distance, query: SearchQueryHashable, -) -> (Vec, HeightGrid, f32) { +) -> (Vec, HeightGrid, f32, GridIx) { let search_result = search_from_point( latitude.0, longitude.0, @@ -132,6 +132,7 @@ fn search_from_point_memoized( .collect(), search_result.height_grid, search_result.ground_height, + search_result.start_ix, ) } @@ -147,7 +148,7 @@ pub fn search_from_request( trim_speed_opt: Option, safety_margin_opt: Option, start_distance_opt: Option, -) -> (Vec, HeightGrid, Array2, Array2, f32) { +) -> (Vec, HeightGrid, Array2, Array2, f32, GridIx) { let cell_size = cell_size_opt .unwrap_or(CELL_SIZE_DEFAULT) .max(CELL_SIZE_MINIMUM) @@ -179,7 +180,7 @@ pub fn search_from_request( let lat_rounded = (lat * 1000.0).round() / 1000.0; let lon_rounded = (lon * 1000.0).round() / 1000.0; - let (explored, grid, height_at_start) = search_from_point_memoized( + let (explored, grid, height_at_start, start_ix) = search_from_point_memoized( Distance(lat_rounded), Distance(lon_rounded), Distance(cell_size), @@ -208,7 +209,14 @@ pub fn search_from_request( } } - return (explored, grid, heights, node_heights, height_at_start); + return ( + explored, + grid, + heights, + node_heights, + height_at_start, + start_ix, + ); } #[derive(Serialize)] @@ -229,6 +237,7 @@ struct FlightConeResponse { angular_resolution: (f32, f32), lat: (f32, f32), lon: (f32, f32), + start_ix: GridIx, grid_shape: (usize, usize), start_height: f32, } @@ -251,7 +260,7 @@ fn get_flight_cone( return Result::Err(Status::NotFound); } - let (explored, grid, _, _, height_at_start) = search_from_request( + let (explored, grid, _, _, height_at_start, start_ix) = search_from_request( lat, lon, cell_size, @@ -271,6 +280,7 @@ fn get_flight_cone( nodes: None, cell_size: grid.cell_size, angular_resolution: resolution, + start_ix: start_ix, lat: grid.latitudes, lon: grid.longitudes, min_cell_size: grid.min_cell_size, @@ -315,7 +325,7 @@ fn get_flight_cone_bounds( return Result::Err(Status::NotFound); } - let (_, grid, _, _, height_at_start) = search_from_request( + let (_, grid, _, _, height_at_start, start_ix) = search_from_request( lat, lon, cell_size, @@ -335,6 +345,7 @@ fn get_flight_cone_bounds( nodes: None, cell_size: grid.cell_size, angular_resolution: resolution, + start_ix: start_ix, lat: grid.latitudes, lon: grid.longitudes, min_cell_size: grid.min_cell_size, @@ -366,7 +377,7 @@ fn get_agl_image<'a>( safety_margin: Option, start_distance: Option, ) -> (ContentType, Vec) { - let (_, _, heights, _, _) = search_from_request( + let (_, _, heights, _, _, _) = search_from_request( lat, lon, cell_size, @@ -461,7 +472,7 @@ fn get_height_image<'a>( safety_margin: Option, start_distance: Option, ) -> (ContentType, Vec) { - let (_, _, _, heights, _) = search_from_request( + let (_, _, _, heights, _, _) = search_from_request( lat, lon, cell_size, @@ -577,7 +588,7 @@ fn get_kml<'a>( safety_margin: Option, start_distance: Option, ) -> (ContentType, Vec) { - let (nodes, height_grid, heights, node_heights, _) = search_from_request( + let (nodes, height_grid, heights, node_heights, _, _) = search_from_request( lat, lon, cell_size, diff --git a/backend-rust/src/search.rs b/backend-rust/src/search.rs index 76a2d89..9795259 100644 --- a/backend-rust/src/search.rs +++ b/backend-rust/src/search.rs @@ -785,7 +785,11 @@ fn reindex_node( } } -pub fn reindex(explored: Explored, grid: &HeightGrid) -> (Explored, HeightGrid) { +pub fn reindex( + explored: Explored, + grid: &HeightGrid, + start_ix: GridIx, +) -> (Explored, HeightGrid, GridIx) { let mut lat_min = GridIxType::MAX; let mut lat_max = GridIxType::MIN; let mut lon_min = GridIxType::MAX; @@ -807,6 +811,8 @@ pub fn reindex(explored: Explored, grid: &HeightGrid) -> (Explored, HeightGrid) let old_shape = grid.heights.shape(); + let new_start_ix = (start_ix.0 - lat_min, start_ix.1 - lon_min); + let new_grid = HeightGrid { heights: grid .heights @@ -833,7 +839,7 @@ pub fn reindex(explored: Explored, grid: &HeightGrid) -> (Explored, HeightGrid) ), }; - return (new_explored, new_grid); + return (new_explored, new_grid, new_start_ix); } pub struct SearchSetup { @@ -891,6 +897,7 @@ pub struct SearchResult { pub explored: Explored, pub height_grid: HeightGrid, pub ground_height: f32, + pub start_ix: GridIx, } pub fn search_from_point( @@ -907,12 +914,17 @@ pub fn search_from_point( &search_setup.config, ); - let (explored, new_grid) = reindex(state.explored, &search_setup.config.grid); + let (explored, new_grid, new_start_ix) = reindex( + state.explored, + &search_setup.config.grid, + search_setup.start_ix, + ); SearchResult { explored: explored, height_grid: new_grid, ground_height: search_setup.ground_height, + start_ix: new_start_ix, } } diff --git a/frontend/src/components/CurrentNode.tsx b/frontend/src/components/CurrentNode.tsx index fd83ba7..653381b 100644 --- a/frontend/src/components/CurrentNode.tsx +++ b/frontend/src/components/CurrentNode.tsx @@ -20,15 +20,14 @@ export function CurrentNode({ fillOpacity: 0.5, }; - let lat = 0; - let lon = 0; + let latlng = new LatLng(0, 0); if (grid.response !== undefined) { - [lat, lon] = ixToLatLon(node.index, grid.response); + latlng = ixToLatLon(node.index, grid.response); } return ( diff --git a/frontend/src/components/Grid.tsx b/frontend/src/components/Grid.tsx index baed9a0..298a470 100644 --- a/frontend/src/components/Grid.tsx +++ b/frontend/src/components/Grid.tsx @@ -40,21 +40,21 @@ export function Grid({ grid, pathAndNode }: GridProps) { let current = node; let path = []; while (current.reference !== null) { - let lat = 0; - let lon = 0; + let latlng = new LatLng(0, 0); if (grid.response !== undefined) { - [lat, lon] = ixToLatLon(current.index, grid.response); + latlng = ixToLatLon(current.index, grid.response); } - path.push(new LatLng(lat, lon, current.height)); + latlng.alt = current.height; + path.push(latlng); current = grid.grid[current.reference[0]][current.reference[1]]; } - let lat = 0; - let lon = 0; + let latlng = new LatLng(0, 0); + latlng.alt = current.height; if (grid.response !== undefined) { - [lat, lon] = ixToLatLon(current.index, grid.response); + latlng = ixToLatLon(current.index, grid.response); } - path.push(new LatLng(lat, lon, current.height)); + path.push(latlng); path.reverse(); pathAndNode.setPath(path); pathAndNode.setNode(node); diff --git a/frontend/src/components/SearchComponent.tsx b/frontend/src/components/SearchComponent.tsx index ac253d6..b472da9 100644 --- a/frontend/src/components/SearchComponent.tsx +++ b/frontend/src/components/SearchComponent.tsx @@ -1,8 +1,9 @@ import { LatLng } from "leaflet"; import { doSearchFromLocation } from "../utils/utils"; -import { CircleMarker, useMap, useMapEvents } from "react-leaflet"; +import { useMap, useMapEvents } from "react-leaflet"; import { GridState, ImageState, PathAndNode, Settings } from "../utils/types"; import { Grid } from "./Grid"; +import { StartMarker } from "./StartMarker"; interface SearchComponentProps { setImageState: (state: ImageState | undefined) => void; @@ -42,20 +43,8 @@ export function SearchComponent({ setImageState, settings, setSettings, grid, se doSearchFromLocation(setImageState, setGrid, setSettings, latlon, settings, pathAndNode, map); } - const blackOptions = { - color: "black", - weight: 1.0, - opacity: 1.0, - fillColor: "white", - fillOpacity: 0.5, - }; - return (<> - {lat === null || lon === null ? <> : } + {grid.response === undefined ? <> : } { grid === undefined ? (<>) : ( diff --git a/frontend/src/components/StartMarker.tsx b/frontend/src/components/StartMarker.tsx new file mode 100644 index 0000000..da30f15 --- /dev/null +++ b/frontend/src/components/StartMarker.tsx @@ -0,0 +1,49 @@ +import { Circle, CircleMarker, useMap } from "react-leaflet"; +import { ConeSearchResponse, Settings } from "../utils/types"; +import { LatLng } from "leaflet"; +import { ixToLatLon } from "../utils/utils"; + +interface StartMarkerProps { + response: ConeSearchResponse; + settings: Settings; +} + +export function StartMarker({ response, settings }: StartMarkerProps) { + const map = useMap(); + + const startOptions = { + color: "black", + weight: 1.0, + opacity: 1.0, + fillColor: "white", + fillOpacity: 1.0, + }; + + const safety_margin_options = { + color: "black", + weight: 1.0, + opacity: 1.0, + fillColor: "white", + fillOpacity: 0.0, + }; + + const start_location = ixToLatLon(response.start_ix, response); + + return ( + <> + {settings.safetyMargin > 0 && settings.startDistance > 0 ? ( + + ) : <> + } + + + ); +} \ No newline at end of file diff --git a/frontend/src/utils/types.ts b/frontend/src/utils/types.ts index e50eaee..d5ffc3d 100644 --- a/frontend/src/utils/types.ts +++ b/frontend/src/utils/types.ts @@ -27,6 +27,7 @@ export interface ConeSearchResponse { min_cell_size: number; lat: number[]; lon: number[]; + start_ix: number[]; grid_shape: number[]; angular_resolution: number[]; start_height: number; diff --git a/frontend/src/utils/utils.ts b/frontend/src/utils/utils.ts index e2bf8cf..9b993b9 100644 --- a/frontend/src/utils/utils.ts +++ b/frontend/src/utils/utils.ts @@ -160,7 +160,7 @@ export function ixToLatLon(ix: number[], response: ConeSearchResponse) { response.lon[0] + ((ix[1] + 0.5) / response.grid_shape[1]) * (response.lon[1] - response.lon[0]); - return [lat, lon]; + return new LatLng(lat, lon); } export function searchFromCurrentLocation(