11use crate :: area:: { area, contains} ;
22use crate :: error:: { new_error, ErrorKind , Result } ;
33use crate :: isoringbuilder:: IsoRingBuilder ;
4- use crate :: { Band , Contour , Float , Line , Ring } ;
5- use geo_types:: { LineString , MultiLineString , MultiPolygon , Polygon } ;
4+ use crate :: { Band , Contour , ContourValue , Float , Line , Ring } ;
5+ use geo_types:: { CoordFloat , LineString , MultiLineString , MultiPolygon , Polygon } ;
66use rustc_hash:: FxHashMap ;
77
88/// Contours generator, using builder pattern, to
@@ -43,10 +43,10 @@ impl ContourBuilder {
4343 dx,
4444 dy,
4545 smooth,
46- x_origin : 0. ,
47- y_origin : 0. ,
48- x_step : 1. ,
49- y_step : 1. ,
46+ x_origin : 0.0 ,
47+ y_origin : 0.0 ,
48+ x_step : 1.0 ,
49+ y_step : 1.0 ,
5050 }
5151 }
5252
@@ -74,7 +74,7 @@ impl ContourBuilder {
7474 self
7575 }
7676
77- fn smooth_linear ( & self , ring : & mut Ring , values : & [ Float ] , value : Float ) {
77+ fn smooth_linear < V : ContourValue > ( & self , ring : & mut Ring , values : & [ V ] , value : V ) {
7878 let dx = self . dx ;
7979 let dy = self . dy ;
8080 let len_values = values. len ( ) ;
@@ -91,11 +91,11 @@ impl ContourBuilder {
9191 let v1 = values[ ix] ;
9292 if x > 0.0 && x < ( dx as Float ) && ( xt as Float - x) . abs ( ) < Float :: EPSILON {
9393 v0 = values[ yt * dx + xt - 1 ] ;
94- point. x = x + ( value - v0) / ( v1 - v0) - 0.5 ;
94+ point. x = x + num_traits :: cast :: < V , Float > ( ( value - v0) / ( v1 - v0) - num_traits :: cast ( 0.5 ) . unwrap ( ) ) . unwrap ( ) ;
9595 }
9696 if y > 0.0 && y < ( dy as Float ) && ( yt as Float - y) . abs ( ) < Float :: EPSILON {
9797 v0 = values[ ( yt - 1 ) * dx + xt] ;
98- point. y = y + ( value - v0) / ( v1 - v0) - 0.5 ;
98+ point. y = y + num_traits :: cast :: < V , Float > ( ( value - v0) / ( v1 - v0) - num_traits :: cast ( 0.5 ) . unwrap ( ) ) . unwrap ( ) ;
9999 }
100100 }
101101 } )
@@ -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 : & [ Float ] , thresholds : & [ Float ] ) -> Result < Vec < Line > > {
114+ pub fn lines < V : ContourValue > ( & self , values : & [ V ] , thresholds : & [ V ] ) -> Result < Vec < Line < V > > > {
115115 if values. len ( ) != self . dx * self . dy {
116116 return Err ( new_error ( ErrorKind :: BadDimension ) ) ;
117117 }
@@ -122,12 +122,12 @@ impl ContourBuilder {
122122 . collect ( )
123123 }
124124
125- fn line (
125+ fn line < V : ContourValue > (
126126 & self ,
127- values : & [ Float ] ,
128- threshold : Float ,
127+ values : & [ V ] ,
128+ threshold : V ,
129129 isoring : & mut IsoRingBuilder ,
130- ) -> Result < Line > {
130+ ) -> Result < Line < V > > {
131131 let mut result = isoring. compute ( values, threshold) ?;
132132 let mut linestrings = Vec :: new ( ) ;
133133
@@ -148,7 +148,7 @@ impl ContourBuilder {
148148 linestrings. push ( LineString ( ring) ) ;
149149 } ) ;
150150 Ok ( Line {
151- geometry : MultiLineString :: < Float > ( linestrings) ,
151+ geometry : MultiLineString ( linestrings) ,
152152 threshold,
153153 } )
154154 }
@@ -162,7 +162,7 @@ impl ContourBuilder {
162162 ///
163163 /// * `values` - The slice of values to be used.
164164 /// * `thresholds` - The slice of thresholds values to be used.
165- pub fn contours ( & self , values : & [ Float ] , thresholds : & [ Float ] ) -> Result < Vec < Contour > > {
165+ pub fn contours < V : ContourValue > ( & self , values : & [ V ] , thresholds : & [ V ] ) -> Result < Vec < Contour < V > > > {
166166 if values. len ( ) != self . dx * self . dy {
167167 return Err ( new_error ( ErrorKind :: BadDimension ) ) ;
168168 }
@@ -173,12 +173,12 @@ impl ContourBuilder {
173173 . collect ( )
174174 }
175175
176- fn contour (
176+ fn contour < V : ContourValue > (
177177 & self ,
178- values : & [ Float ] ,
179- threshold : Float ,
178+ values : & [ V ] ,
179+ threshold : V ,
180180 isoring : & mut IsoRingBuilder ,
181- ) -> Result < Contour > {
181+ ) -> Result < Contour < V > > {
182182 let ( mut polygons, mut holes) = ( Vec :: new ( ) , Vec :: new ( ) ) ;
183183 let mut result = isoring. compute ( values, threshold) ?;
184184
@@ -197,7 +197,7 @@ impl ContourBuilder {
197197 } ) ;
198198 }
199199 if area ( & ring) > 0.0 {
200- polygons. push ( Polygon :: < Float > :: new ( LineString :: new ( ring) , vec ! [ ] ) )
200+ polygons. push ( Polygon :: new ( LineString :: new ( ring) , vec ! [ ] ) )
201201 } else {
202202 holes. push ( LineString :: new ( ring) ) ;
203203 }
@@ -213,7 +213,7 @@ impl ContourBuilder {
213213 } ) ;
214214
215215 Ok ( Contour {
216- geometry : MultiPolygon :: < Float > ( polygons) ,
216+ geometry : MultiPolygon ( polygons) ,
217217 threshold,
218218 } )
219219 }
@@ -228,7 +228,7 @@ impl ContourBuilder {
228228 /// * `values` - The slice of values to be used.
229229 /// * `thresholds` - The slice of thresholds values to be used
230230 /// (have to be equal to or greater than 2).
231- pub fn isobands ( & self , values : & [ Float ] , thresholds : & [ Float ] ) -> Result < Vec < Band > > {
231+ pub fn isobands < V : ContourValue > ( & self , values : & [ V ] , thresholds : & [ V ] ) -> Result < Vec < Band < V > > > {
232232 // We will compute rings as previously, but we will
233233 // iterate over the contours in pairs and use the paths from the lower threshold
234234 // and the path from the upper threshold to create the isoband.
@@ -268,7 +268,7 @@ impl ContourBuilder {
268268 . collect :: < Vec < Ring > > ( ) ;
269269 Ok ( ( rings, * threshold) )
270270 } )
271- . collect :: < Result < Vec < ( Vec < Ring > , Float ) > > > ( ) ?;
271+ . collect :: < Result < Vec < ( Vec < Ring > , V ) > > > ( ) ?;
272272
273273 // We now have the rings for each isolines for all the given thresholds,
274274 // we can iterate over them in pairs to compute the isobands.
@@ -281,7 +281,7 @@ impl ContourBuilder {
281281 } )
282282 . collect :: < Vec < _ > > ( ) ;
283283
284- let mut bands: Vec < Band > = Vec :: new ( ) ;
284+ let mut bands: Vec < Band < V > > = Vec :: new ( ) ;
285285 // Reconstruction of the polygons
286286 b. into_iter ( ) . for_each ( |( rings, min_v, max_v) | {
287287 let mut rings_and_area = rings
@@ -314,7 +314,7 @@ impl ContourBuilder {
314314
315315 for ( i, ( ring, _) ) in rings_and_area. into_iter ( ) . enumerate ( ) {
316316 if * enclosed_by_n. get ( & i) . unwrap ( ) % 2 == 0 {
317- polygons. push ( Polygon :: < Float > :: new ( ring. into ( ) , vec ! [ ] ) ) ;
317+ polygons. push ( Polygon :: new ( ring. into ( ) , vec ! [ ] ) ) ;
318318 } else {
319319 interior_rings. push ( ring. into ( ) ) ;
320320 }
@@ -331,7 +331,7 @@ impl ContourBuilder {
331331 polygons. reverse ( ) ;
332332
333333 bands. push ( Band {
334- geometry : MultiPolygon :: < Float > ( polygons) ,
334+ geometry : MultiPolygon ( polygons) ,
335335 min_v : * min_v,
336336 max_v : * max_v,
337337 } ) ;
0 commit comments