11use crate :: area:: { area, contains} ;
22use crate :: error:: { new_error, ErrorKind , Result } ;
33use crate :: isoringbuilder:: IsoRingBuilder ;
4- use crate :: { Band , Contour , Line , Ring } ;
4+ use crate :: { Band , Contour , Float , Line , Ring } ;
55use geo_types:: { LineString , MultiLineString , MultiPolygon , Polygon } ;
66use rustc_hash:: FxHashMap ;
77
@@ -18,13 +18,13 @@ pub struct ContourBuilder {
1818 /// Whether to smooth the contours
1919 smooth : bool ,
2020 /// The horizontal coordinate for the origin of the grid.
21- x_origin : f64 ,
21+ x_origin : Float ,
2222 /// The vertical coordinate for the origin of the grid.
23- y_origin : f64 ,
23+ y_origin : Float ,
2424 /// The horizontal step for the grid
25- x_step : f64 ,
25+ x_step : Float ,
2626 /// The vertical step for the grid
27- y_step : f64 ,
27+ y_step : Float ,
2828}
2929
3030impl ContourBuilder {
@@ -43,38 +43,38 @@ impl ContourBuilder {
4343 dx,
4444 dy,
4545 smooth,
46- x_origin : 0f64 ,
47- y_origin : 0f64 ,
48- x_step : 1f64 ,
49- y_step : 1f64 ,
46+ x_origin : 0. ,
47+ y_origin : 0. ,
48+ x_step : 1. ,
49+ y_step : 1. ,
5050 }
5151 }
5252
5353 /// Sets the x origin of the grid.
54- pub fn x_origin ( mut self , x_origin : impl Into < f64 > ) -> Self {
54+ pub fn x_origin ( mut self , x_origin : impl Into < Float > ) -> Self {
5555 self . x_origin = x_origin. into ( ) ;
5656 self
5757 }
5858
5959 /// Sets the y origin of the grid.
60- pub fn y_origin ( mut self , y_origin : impl Into < f64 > ) -> Self {
60+ pub fn y_origin ( mut self , y_origin : impl Into < Float > ) -> Self {
6161 self . y_origin = y_origin. into ( ) ;
6262 self
6363 }
6464
6565 /// Sets the x step of the grid.
66- pub fn x_step ( mut self , x_step : impl Into < f64 > ) -> Self {
66+ pub fn x_step ( mut self , x_step : impl Into < Float > ) -> Self {
6767 self . x_step = x_step. into ( ) ;
6868 self
6969 }
7070
7171 /// Sets the y step of the grid.
72- pub fn y_step ( mut self , y_step : impl Into < f64 > ) -> Self {
72+ pub fn y_step ( mut self , y_step : impl Into < Float > ) -> Self {
7373 self . y_step = y_step. into ( ) ;
7474 self
7575 }
7676
77- fn smoooth_linear ( & self , ring : & mut Ring , values : & [ f64 ] , value : f64 ) {
77+ fn smoooth_linear ( & self , ring : & mut Ring , values : & [ Float ] , value : Float ) {
7878 let dx = self . dx ;
7979 let dy = self . dy ;
8080 let len_values = values. len ( ) ;
@@ -89,11 +89,11 @@ impl ContourBuilder {
8989 let ix = ( yt * dx + xt) as usize ;
9090 if ix < len_values {
9191 let v1 = values[ ix] ;
92- if x > 0.0 && x < ( dx as f64 ) && ( xt as f64 - x) . abs ( ) < std :: f64 :: EPSILON {
92+ if x > 0.0 && x < ( dx as Float ) && ( xt as Float - x) . abs ( ) < Float :: EPSILON {
9393 v0 = values[ ( yt * dx + xt - 1 ) as usize ] ;
9494 point. x = x + ( value - v0) / ( v1 - v0) - 0.5 ;
9595 }
96- if y > 0.0 && y < ( dy as f64 ) && ( yt as f64 - y) . abs ( ) < std :: f64 :: EPSILON {
96+ if y > 0.0 && y < ( dy as Float ) && ( yt as Float - y) . abs ( ) < Float :: EPSILON {
9797 v0 = values[ ( ( yt - 1 ) * dx + xt) as usize ] ;
9898 point. y = y + ( value - v0) / ( v1 - v0) - 0.5 ;
9999 }
@@ -111,7 +111,7 @@ impl ContourBuilder {
111111 ///
112112 /// * `values` - The slice of values to be used.
113113 /// * `thresholds` - The slice of thresholds values to be used.
114- pub fn lines ( & self , values : & [ f64 ] , thresholds : & [ f64 ] ) -> Result < Vec < Line > > {
114+ pub fn lines ( & self , values : & [ Float ] , thresholds : & [ Float ] ) -> Result < Vec < Line > > {
115115 if values. len ( ) as u32 != self . dx * self . dy {
116116 return Err ( new_error ( ErrorKind :: BadDimension ) ) ;
117117 }
@@ -122,7 +122,12 @@ impl ContourBuilder {
122122 . collect ( )
123123 }
124124
125- fn line ( & self , values : & [ f64 ] , threshold : f64 , isoring : & mut IsoRingBuilder ) -> Result < Line > {
125+ fn line (
126+ & self ,
127+ values : & [ Float ] ,
128+ threshold : Float ,
129+ isoring : & mut IsoRingBuilder ,
130+ ) -> Result < Line > {
126131 let mut result = isoring. compute ( values, threshold) ?;
127132 let mut linestrings = Vec :: new ( ) ;
128133
@@ -132,8 +137,8 @@ impl ContourBuilder {
132137 self . smoooth_linear ( & mut ring, values, threshold) ;
133138 }
134139 // Compute the polygon coordinates according to the grid properties if needed
135- if ( self . x_origin , self . y_origin ) != ( 0f64 , 0f64 )
136- || ( self . x_step , self . y_step ) != ( 1f64 , 1f64 )
140+ if ( self . x_origin , self . y_origin ) != ( 0.0 , 0.0 )
141+ || ( self . x_step , self . y_step ) != ( 1.0 , 1.0 )
137142 {
138143 ring. iter_mut ( ) . for_each ( |point| {
139144 point. x = point. x * self . x_step + self . x_origin ;
@@ -143,7 +148,7 @@ impl ContourBuilder {
143148 linestrings. push ( LineString ( ring) ) ;
144149 } ) ;
145150 Ok ( Line {
146- geometry : MultiLineString ( linestrings) ,
151+ geometry : MultiLineString :: < Float > ( linestrings) ,
147152 threshold,
148153 } )
149154 }
@@ -157,7 +162,7 @@ impl ContourBuilder {
157162 ///
158163 /// * `values` - The slice of values to be used.
159164 /// * `thresholds` - The slice of thresholds values to be used.
160- pub fn contours ( & self , values : & [ f64 ] , thresholds : & [ f64 ] ) -> Result < Vec < Contour > > {
165+ pub fn contours ( & self , values : & [ Float ] , thresholds : & [ Float ] ) -> Result < Vec < Contour > > {
161166 if values. len ( ) as u32 != self . dx * self . dy {
162167 return Err ( new_error ( ErrorKind :: BadDimension ) ) ;
163168 }
@@ -170,8 +175,8 @@ impl ContourBuilder {
170175
171176 fn contour (
172177 & self ,
173- values : & [ f64 ] ,
174- threshold : f64 ,
178+ values : & [ Float ] ,
179+ threshold : Float ,
175180 isoring : & mut IsoRingBuilder ,
176181 ) -> Result < Contour > {
177182 let ( mut polygons, mut holes) = ( Vec :: new ( ) , Vec :: new ( ) ) ;
@@ -183,16 +188,16 @@ impl ContourBuilder {
183188 self . smoooth_linear ( & mut ring, values, threshold) ;
184189 }
185190 // Compute the polygon coordinates according to the grid properties if needed
186- if ( self . x_origin , self . y_origin ) != ( 0f64 , 0f64 )
187- || ( self . x_step , self . y_step ) != ( 1f64 , 1f64 )
191+ if ( self . x_origin , self . y_origin ) != ( 0.0 , 0.0 )
192+ || ( self . x_step , self . y_step ) != ( 1.0 , 1.0 )
188193 {
189194 ring. iter_mut ( ) . for_each ( |point| {
190195 point. x = point. x * self . x_step + self . x_origin ;
191196 point. y = point. y * self . y_step + self . y_origin ;
192197 } ) ;
193198 }
194199 if area ( & ring) > 0.0 {
195- polygons. push ( Polygon :: new ( LineString :: new ( ring) , vec ! [ ] ) )
200+ polygons. push ( Polygon :: < Float > :: new ( LineString :: new ( ring) , vec ! [ ] ) )
196201 } else {
197202 holes. push ( LineString :: new ( ring) ) ;
198203 }
@@ -208,7 +213,7 @@ impl ContourBuilder {
208213 } ) ;
209214
210215 Ok ( Contour {
211- geometry : MultiPolygon ( polygons) ,
216+ geometry : MultiPolygon :: < Float > ( polygons) ,
212217 threshold,
213218 } )
214219 }
@@ -223,7 +228,7 @@ impl ContourBuilder {
223228 /// * `values` - The slice of values to be used.
224229 /// * `thresholds` - The slice of thresholds values to be used
225230 /// (have to be equal to or greater than 2).
226- pub fn isobands ( & self , values : & [ f64 ] , thresholds : & [ f64 ] ) -> Result < Vec < Band > > {
231+ pub fn isobands ( & self , values : & [ Float ] , thresholds : & [ Float ] ) -> Result < Vec < Band > > {
227232 // We will compute rings as previously, but we will
228233 // iterate over the contours in pairs and use the paths from the lower threshold
229234 // and the path from the upper threshold to create the isoband.
@@ -249,8 +254,8 @@ impl ContourBuilder {
249254 }
250255 ring. dedup ( ) ;
251256 // Compute the polygon coordinates according to the grid properties if needed
252- if ( self . x_origin , self . y_origin ) != ( 0f64 , 0f64 )
253- || ( self . x_step , self . y_step ) != ( 1f64 , 1f64 )
257+ if ( self . x_origin , self . y_origin ) != ( 0.0 , 0.0 )
258+ || ( self . x_step , self . y_step ) != ( 1.0 , 1.0 )
254259 {
255260 ring. iter_mut ( ) . for_each ( |point| {
256261 point. x = point. x * self . x_step + self . x_origin ;
@@ -263,7 +268,7 @@ impl ContourBuilder {
263268 . collect :: < Vec < Ring > > ( ) ;
264269 Ok ( ( rings, * threshold) )
265270 } )
266- . collect :: < Result < Vec < ( Vec < Ring > , f64 ) > > > ( ) ?;
271+ . collect :: < Result < Vec < ( Vec < Ring > , Float ) > > > ( ) ?;
267272
268273 // We now have the rings for each isolines for all the given thresholds,
269274 // we can iterate over them in pairs to compute the isobands.
@@ -304,12 +309,12 @@ impl ContourBuilder {
304309 enclosed_by_n. insert ( i, enclosed_by_j) ;
305310 }
306311
307- let mut polygons: Vec < Polygon < f64 > > = Vec :: new ( ) ;
308- let mut interior_rings: Vec < LineString < f64 > > = Vec :: new ( ) ;
312+ let mut polygons: Vec < Polygon < Float > > = Vec :: new ( ) ;
313+ let mut interior_rings: Vec < LineString < Float > > = Vec :: new ( ) ;
309314
310315 for ( i, ( ring, _) ) in rings_and_area. into_iter ( ) . enumerate ( ) {
311316 if * enclosed_by_n. get ( & i) . unwrap ( ) % 2 == 0 {
312- polygons. push ( Polygon :: new ( ring. into ( ) , vec ! [ ] ) ) ;
317+ polygons. push ( Polygon :: < Float > :: new ( ring. into ( ) , vec ! [ ] ) ) ;
313318 } else {
314319 interior_rings. push ( ring. into ( ) ) ;
315320 }
@@ -326,7 +331,7 @@ impl ContourBuilder {
326331 polygons. reverse ( ) ;
327332
328333 bands. push ( Band {
329- geometry : MultiPolygon ( polygons) ,
334+ geometry : MultiPolygon :: < Float > ( polygons) ,
330335 min_v : * min_v,
331336 max_v : * max_v,
332337 } ) ;
0 commit comments