@@ -104,3 +104,240 @@ pub trait CoordinateSet: CoordinateMetadata {
104
104
}
105
105
}
106
106
}
107
+
108
+ // An experiment with an extended version of Kyle Barron's CoordTrait PR
109
+ // over at https://github.com/georust/geo/pull/1157
110
+
111
+ pub trait CoordNum { }
112
+ impl CoordNum for f32 { }
113
+ impl CoordNum for f64 { }
114
+
115
+ /// A trait for accessing data from a generic Coord.
116
+ pub trait CoordTrait {
117
+ type T : CoordNum ;
118
+
119
+ /// Accessors for the coordinate tuple components
120
+ fn first ( & self ) -> Self :: T ;
121
+ fn second ( & self ) -> Self :: T ;
122
+ fn third ( & self ) -> Self :: T ;
123
+ fn fourth ( & self ) -> Self :: T ;
124
+ fn measure ( & self ) -> Self :: T ;
125
+
126
+ /// Accessors for the coordinate tuple components
127
+ fn first_as_f64 ( & self ) -> f64 ;
128
+ fn second_as_f64 ( & self ) -> f64 ;
129
+ fn third_as_f64 ( & self ) -> f64 ;
130
+ fn fourth_as_f64 ( & self ) -> f64 ;
131
+ fn measure_as_f64 ( & self ) -> f64 ;
132
+
133
+ /// x component of this coord
134
+ fn x ( & self ) -> Self :: T {
135
+ self . first ( )
136
+ }
137
+
138
+ /// y component of this coord
139
+ fn y ( & self ) -> Self :: T {
140
+ self . second ( )
141
+ }
142
+
143
+ /// z component of this coord
144
+ fn z ( & self ) -> Self :: T {
145
+ self . third ( )
146
+ }
147
+
148
+ /// t component of this coord
149
+ fn t ( & self ) -> Self :: T {
150
+ self . fourth ( )
151
+ }
152
+
153
+ /// Returns a tuple that contains the two first components of the coord.
154
+ fn xy ( & self ) -> ( Self :: T , Self :: T ) {
155
+ ( self . first ( ) , self . second ( ) )
156
+ }
157
+
158
+ /// Returns a tuple that contains the three first components of the coord.
159
+ fn xyz ( & self ) -> ( Self :: T , Self :: T , Self :: T ) {
160
+ ( self . first ( ) , self . second ( ) , self . third ( ) )
161
+ }
162
+
163
+ /// Returns a tuple that contains the three first components of the coord.
164
+ fn xyzt ( & self ) -> ( Self :: T , Self :: T , Self :: T , Self :: T ) {
165
+ ( self . first ( ) , self . second ( ) , self . third ( ) , self . fourth ( ) )
166
+ }
167
+
168
+ /// Returns a tuple that contains the two first components of the coord converted to f64.
169
+ fn xy_as_f64 ( & self ) -> ( f64 , f64 ) {
170
+ ( self . first_as_f64 ( ) , self . second_as_f64 ( ) )
171
+ }
172
+
173
+ /// Returns a tuple that contains the three first components of the coord converted to f64.
174
+ fn xyz_as_f64 ( & self ) -> ( f64 , f64 , f64 ) {
175
+ (
176
+ self . first_as_f64 ( ) ,
177
+ self . second_as_f64 ( ) ,
178
+ self . third_as_f64 ( ) ,
179
+ )
180
+ }
181
+
182
+ /// Returns a tuple that contains the three first components of the coord converted to f64.
183
+ fn xyzt_as_f64 ( & self ) -> ( f64 , f64 , f64 , f64 ) {
184
+ (
185
+ self . first_as_f64 ( ) ,
186
+ self . second_as_f64 ( ) ,
187
+ self . third_as_f64 ( ) ,
188
+ self . fourth_as_f64 ( ) ,
189
+ )
190
+ }
191
+ }
192
+
193
+ impl CoordTrait for Coor2D {
194
+ type T = f64 ;
195
+
196
+ /// Accessors for the coordinate tuple components
197
+ fn first ( & self ) -> Self :: T {
198
+ self . 0 [ 0 ]
199
+ }
200
+ fn second ( & self ) -> Self :: T {
201
+ self . 0 [ 1 ]
202
+ }
203
+ fn third ( & self ) -> Self :: T {
204
+ f64:: NAN
205
+ }
206
+ fn fourth ( & self ) -> Self :: T {
207
+ f64:: NAN
208
+ }
209
+ fn measure ( & self ) -> Self :: T {
210
+ f64:: NAN
211
+ }
212
+
213
+ /// Accessors for the coordinate tuple components
214
+ fn first_as_f64 ( & self ) -> f64 {
215
+ self . 0 [ 0 ]
216
+ }
217
+ fn second_as_f64 ( & self ) -> f64 {
218
+ self . 0 [ 1 ]
219
+ }
220
+ fn third_as_f64 ( & self ) -> f64 {
221
+ f64:: NAN
222
+ }
223
+ fn fourth_as_f64 ( & self ) -> f64 {
224
+ f64:: NAN
225
+ }
226
+ fn measure_as_f64 ( & self ) -> f64 {
227
+ f64:: NAN
228
+ }
229
+ }
230
+
231
+ impl CoordTrait for Coor32 {
232
+ type T = f32 ;
233
+
234
+ /// Accessors for the coordinate tuple components
235
+ fn first ( & self ) -> Self :: T {
236
+ self . 0 [ 0 ]
237
+ }
238
+ fn second ( & self ) -> Self :: T {
239
+ self . 0 [ 1 ]
240
+ }
241
+ fn third ( & self ) -> Self :: T {
242
+ f32:: NAN
243
+ }
244
+ fn fourth ( & self ) -> Self :: T {
245
+ f32:: NAN
246
+ }
247
+ fn measure ( & self ) -> Self :: T {
248
+ f32:: NAN
249
+ }
250
+
251
+ /// Accessors for the coordinate tuple components
252
+ fn first_as_f64 ( & self ) -> f64 {
253
+ self . 0 [ 0 ] as f64
254
+ }
255
+ fn second_as_f64 ( & self ) -> f64 {
256
+ self . 0 [ 1 ] as f64
257
+ }
258
+ fn third_as_f64 ( & self ) -> f64 {
259
+ f64:: NAN
260
+ }
261
+ fn fourth_as_f64 ( & self ) -> f64 {
262
+ f64:: NAN
263
+ }
264
+ fn measure_as_f64 ( & self ) -> f64 {
265
+ f64:: NAN
266
+ }
267
+ }
268
+
269
+ impl CoordTrait for Coor3D {
270
+ type T = f64 ;
271
+
272
+ /// Accessors for the coordinate tuple components
273
+ fn first ( & self ) -> Self :: T {
274
+ self . 0 [ 0 ]
275
+ }
276
+ fn second ( & self ) -> Self :: T {
277
+ self . 0 [ 1 ]
278
+ }
279
+ fn third ( & self ) -> Self :: T {
280
+ self . 0 [ 2 ]
281
+ }
282
+ fn fourth ( & self ) -> Self :: T {
283
+ f64:: NAN
284
+ }
285
+ fn measure ( & self ) -> Self :: T {
286
+ f64:: NAN
287
+ }
288
+
289
+ /// Accessors for the coordinate tuple components
290
+ fn first_as_f64 ( & self ) -> f64 {
291
+ self . 0 [ 0 ]
292
+ }
293
+ fn second_as_f64 ( & self ) -> f64 {
294
+ self . 0 [ 1 ]
295
+ }
296
+ fn third_as_f64 ( & self ) -> f64 {
297
+ self . 0 [ 2 ]
298
+ }
299
+ fn fourth_as_f64 ( & self ) -> f64 {
300
+ f64:: NAN
301
+ }
302
+ fn measure_as_f64 ( & self ) -> f64 {
303
+ f64:: NAN
304
+ }
305
+ }
306
+
307
+ impl CoordTrait for Coor4D {
308
+ type T = f64 ;
309
+
310
+ /// Accessors for the coordinate tuple components
311
+ fn first ( & self ) -> Self :: T {
312
+ self . 0 [ 0 ]
313
+ }
314
+ fn second ( & self ) -> Self :: T {
315
+ self . 0 [ 1 ]
316
+ }
317
+ fn third ( & self ) -> Self :: T {
318
+ self . 0 [ 2 ]
319
+ }
320
+ fn fourth ( & self ) -> Self :: T {
321
+ self . 0 [ 3 ]
322
+ }
323
+ fn measure ( & self ) -> Self :: T {
324
+ f64:: NAN
325
+ }
326
+
327
+ /// Accessors for the coordinate tuple components
328
+ fn first_as_f64 ( & self ) -> f64 {
329
+ self . 0 [ 0 ]
330
+ }
331
+ fn second_as_f64 ( & self ) -> f64 {
332
+ self . 0 [ 1 ]
333
+ }
334
+ fn third_as_f64 ( & self ) -> f64 {
335
+ self . 0 [ 2 ]
336
+ }
337
+ fn fourth_as_f64 ( & self ) -> f64 {
338
+ self . 0 [ 3 ]
339
+ }
340
+ fn measure_as_f64 ( & self ) -> f64 {
341
+ f64:: NAN
342
+ }
343
+ }
0 commit comments