Skip to content

Commit

Permalink
Show start distance
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoff97 committed Oct 7, 2024
1 parent 51a3f4e commit 8fd72cb
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 39 deletions.
29 changes: 20 additions & 9 deletions backend-rust/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn search_from_point_memoized(
longitude: Distance,
cell_size: Distance,
query: SearchQueryHashable,
) -> (Vec<Node>, HeightGrid, f32) {
) -> (Vec<Node>, HeightGrid, f32, GridIx) {
let search_result = search_from_point(
latitude.0,
longitude.0,
Expand All @@ -132,6 +132,7 @@ fn search_from_point_memoized(
.collect(),
search_result.height_grid,
search_result.ground_height,
search_result.start_ix,
)
}

Expand All @@ -147,7 +148,7 @@ pub fn search_from_request(
trim_speed_opt: Option<f32>,
safety_margin_opt: Option<f32>,
start_distance_opt: Option<f32>,
) -> (Vec<Node>, HeightGrid, Array2<f32>, Array2<f32>, f32) {
) -> (Vec<Node>, HeightGrid, Array2<f32>, Array2<f32>, f32, GridIx) {
let cell_size = cell_size_opt
.unwrap_or(CELL_SIZE_DEFAULT)
.max(CELL_SIZE_MINIMUM)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)]
Expand All @@ -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,
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -366,7 +377,7 @@ fn get_agl_image<'a>(
safety_margin: Option<f32>,
start_distance: Option<f32>,
) -> (ContentType, Vec<u8>) {
let (_, _, heights, _, _) = search_from_request(
let (_, _, heights, _, _, _) = search_from_request(
lat,
lon,
cell_size,
Expand Down Expand Up @@ -461,7 +472,7 @@ fn get_height_image<'a>(
safety_margin: Option<f32>,
start_distance: Option<f32>,
) -> (ContentType, Vec<u8>) {
let (_, _, _, heights, _) = search_from_request(
let (_, _, _, heights, _, _) = search_from_request(
lat,
lon,
cell_size,
Expand Down Expand Up @@ -577,7 +588,7 @@ fn get_kml<'a>(
safety_margin: Option<f32>,
start_distance: Option<f32>,
) -> (ContentType, Vec<u8>) {
let (nodes, height_grid, heights, node_heights, _) = search_from_request(
let (nodes, height_grid, heights, node_heights, _, _) = search_from_request(
lat,
lon,
cell_size,
Expand Down
18 changes: 15 additions & 3 deletions backend-rust/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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,
}
}

Expand Down
7 changes: 3 additions & 4 deletions frontend/src/components/CurrentNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<CircleMarker
center={new LatLng(lat, lon)}
center={latlng}
radius={(map.getZoom() / 12) * 10}
pathOptions={blackOptions}
>
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/components/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 3 additions & 14 deletions frontend/src/components/SearchComponent.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 ? <></> : <CircleMarker
center={new LatLng(+lat, +lon)}
radius={(map.getZoom() / 12) * 10}
pathOptions={blackOptions}
></CircleMarker>}
{grid.response === undefined ? <></> : <StartMarker response={grid.response} settings={settings}></StartMarker>}
{
grid === undefined ? (<></>) : (
<Grid grid={grid} pathAndNode={pathAndNode}></Grid>
Expand Down
49 changes: 49 additions & 0 deletions frontend/src/components/StartMarker.tsx
Original file line number Diff line number Diff line change
@@ -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 ? (
<Circle
center={start_location}
radius={settings.startDistance}
pathOptions={safety_margin_options}
></Circle >
) : <></>
}
<CircleMarker
center={start_location}
radius={(map.getZoom() / 17) * 10}
pathOptions={startOptions}
></CircleMarker>
</>
);
}
1 change: 1 addition & 0 deletions frontend/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8fd72cb

Please sign in to comment.