11//! High level extensions meant for an easy usage
22//! Those functions are exposed in wasm-bindings
33
4- use liblrs:: lrs_ext:: * ;
4+ use liblrs:: {
5+ lrs:: { LrmHandle , LrsBase } ,
6+ lrs_ext:: * ,
7+ } ;
58use wasm_bindgen:: prelude:: * ;
69
710#[ wasm_bindgen]
@@ -20,6 +23,17 @@ pub struct Point {
2023 pub y : f64 ,
2124}
2225
26+ #[ wasm_bindgen]
27+ impl Point {
28+ /// Build a new geographical point.
29+ ///
30+ /// When using spherical coordinates, longitude is x and latitude y
31+ #[ wasm_bindgen( constructor) ]
32+ pub fn new ( x : f64 , y : f64 ) -> Self {
33+ Self { x, y }
34+ }
35+ }
36+
2337impl From < geo_types:: Point > for Point {
2438 fn from ( value : geo_types:: Point ) -> Self {
2539 Self {
@@ -29,6 +43,12 @@ impl From<geo_types::Point> for Point {
2943 }
3044}
3145
46+ impl From < Point > for geo_types:: Point < f64 > {
47+ fn from ( value : Point ) -> Self {
48+ Self :: new ( value. x , value. y )
49+ }
50+ }
51+
3252impl From < geo_types:: Coord > for Point {
3353 fn from ( value : geo_types:: Coord ) -> Self {
3454 Self {
@@ -39,12 +59,13 @@ impl From<geo_types::Coord> for Point {
3959}
4060
4161#[ wasm_bindgen( getter_with_clone) ]
62+ #[ derive( Clone ) ]
4263/// Represent a position on an [`LrmScale`] relative as an `offset` to an [`Anchor`].
4364pub struct LrmScaleMeasure {
4465 /// `name` of the reference [`Anchor`].
45- anchor_name : String ,
66+ pub anchor_name : String ,
4667 /// `offset` to the reference [`Anchor`].
47- scale_offset : f64 ,
68+ pub scale_offset : f64 ,
4869}
4970
5071#[ wasm_bindgen]
@@ -102,6 +123,15 @@ impl From<&liblrs::lrm_scale::Anchor> for Anchor {
102123 }
103124}
104125
126+ #[ wasm_bindgen( getter_with_clone) ]
127+ /// The result of a projection onto an [`LrmScale`].
128+ pub struct LrmProjection {
129+ /// Contains `measure` ([`LrmScaleMeasure`]) and `lrm` ([`LrmHandle`]).
130+ pub measure : LrmScaleMeasure ,
131+ /// How far from the [`Lrm`] is the [`Point`] that has been projected.
132+ pub orthogonal_offset : f64 ,
133+ }
134+
105135#[ wasm_bindgen]
106136impl Lrs {
107137 /// Load the data.
@@ -155,6 +185,24 @@ impl Lrs {
155185 . resolve_range ( lrm_index, & from. into ( ) , & to. into ( ) )
156186 . map ( |coords| coords. into_iter ( ) . map ( |coord| coord. into ( ) ) . collect ( ) )
157187 }
188+
189+ /// Projects a [`Point`] on all applicable [`Traversal`]s to a given [`Lrm`].
190+ /// The [`Point`] must be in the bounding box of the [`Curve`] of the [`Traversal`].
191+ /// The result is sorted by `orthogonal_offset`: the nearest [`Lrm`] to the [`Point`] is the first item.
192+ pub fn lookup ( & self , point : Point , lrm_handle : usize ) -> Vec < LrmProjection > {
193+ self . lrs
194+ . lrs
195+ . lookup ( point. into ( ) , LrmHandle ( lrm_handle) )
196+ . iter ( )
197+ . map ( |p| LrmProjection {
198+ measure : LrmScaleMeasure {
199+ anchor_name : p. measure . measure . anchor_name . to_owned ( ) ,
200+ scale_offset : p. measure . measure . scale_offset ,
201+ } ,
202+ orthogonal_offset : p. orthogonal_offset ,
203+ } )
204+ . collect ( )
205+ }
158206}
159207
160208#[ wasm_bindgen]
0 commit comments