rustgeomapping 0.1.1

A set of base tools that are designed to allow RGBD cameras create 2.5D heightmaps from the captured pointclouds. Primarily aimed at autonomous geotechnical feature reconstruction.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
///Analyses test results in the form of displaying heightmap results
use crate::analysis::data_handler::DataHandler;
use crate::data_types::heightmap::Heightmap;
use crate::data_types::pointcloud::PointCloud;
use anyhow::bail;
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};

///The analyser structure which contains all the relevant test information
pub struct Analyser {
    ///Filepath location of the test data
    filepath: String,
    ///Name of the test
    test_name: String,
    ///Folder location holding the test data
    test_fp: String,
    ///The struct containing all the raw pos/force info etc
    data_handler: Option<DataHandler>,
    ///Number of pointclouds taken in this test
    no_of_pcl: i32,
    ///Coordination pre-transform flag - True if trajectory already transformed
    pre_trans: bool,
}

impl Analyser {
    ///Create an analyser structure based on the provided file
    pub fn init(filepath: String, test_name: String) -> Result<Self, anyhow::Error> {
        //Check that the filepath exists
        if !fs::read_dir(&filepath)?
            .any(|e| e.unwrap().file_name().into_string().unwrap() == test_name)
        {
            bail!("Test {} does not exist!", test_name);
        }

        let test_fp = format!("{}/{}", filepath, test_name);

        //Count the number of pointclouds
        let mut no_of_pcl = 0;
        for path in fs::read_dir(&test_fp)? {
            if path?.file_name().to_str().unwrap().starts_with("pcl_") {
                no_of_pcl += 1;
            }
        }

        //Create the data handler
        let data_fp = format!("{}/data_{}.txt", test_fp, test_name);

        //println!("Loading data - {data_fp}");
        let data_handler: Option<DataHandler>;
        if let Ok(data) = DataHandler::read_data_from_file(data_fp) {
            data_handler = Some(data);
        } else {
            println!("WARNING: No data file");
            data_handler = None;
        }

        //Get the config info
        //println!("Loading config - {test_fp}");

        let mut pre_trans = false;
        if let Ok(info) = get_config(&test_fp, &test_name) {
            pre_trans = info;
        }

        Ok(Self {
            filepath,
            test_name: test_name.to_string(),
            test_fp,
            data_handler,
            no_of_pcl,
            pre_trans,
        })
    }

    ///Get the rectangular trajectory bound of the test
    pub fn get_traj_bounds(&self) -> Result<[f64; 4], anyhow::Error> {
        if self.data_handler.is_some() {
            self.data_handler
                .as_ref()
                .unwrap()
                .get_traj_rect_bnds()
        } else {
            bail!("No data file associated with test");
        }
    }

    ///Counts the number of generated hmaps saved in the test dir
    fn get_hmap_cnt(&mut self) -> Result<i32, anyhow::Error> {
        //Count the number of heightmaps saved in the test
        let mut hmap_cnt = 0;

        for path in fs::read_dir(&self.test_fp)? {
            if path?.file_name().to_str().unwrap().starts_with("hmap_") {
                hmap_cnt += 1;
            }
        }

        Ok(hmap_cnt)
    }

    ///Generate a heightmap from a specific point cloud
    fn gen_hmap_n(
        &mut self,
        width: usize,
        height: usize,
        n: i32,
    ) -> Result<Heightmap, anyhow::Error> {
        println!("GENERATING HEIGHTMAP {n} ---------");

        //Check that the n number is valid
        if n >= self.no_of_pcl {
            bail!("Invalid hmap number")
        }

        //Create the filepath
        let fp: String;
        if n == -1 {
            fp = format!("{}/pcl_{}_START.txt", self.test_fp, self.test_name);
        } else if n == -2 {
            fp = format!("{}/pcl_{}_END.txt", self.test_fp, self.test_name);
        } else {
            fp = format!("{}/pcl_{}_{}.txt", self.test_fp, self.test_name, n);
        }
        //Generate the heightmap from the pcl file
        let hmap = Heightmap::create_from_pcl_file(fp, width, height)?;

        Ok(hmap)
    }

    ///Load all the hmaps saved from a test directory to a vector
    fn load_all_genned_hmaps(&mut self) -> Result<Vec<Heightmap>, anyhow::Error> {
        println!("LOADING HEIGHTMAPS ---------");

        let mut heightmaps: Vec<Heightmap> = vec![];

        //Iterate through each file
        for path in fs::read_dir(&self.test_fp)? {
            let path_str = path?.file_name();
            let path_str = path_str.to_str().unwrap();

            //Identify the hmap files
            if path_str.starts_with("hmap_") {
                let fp = format!("{}/{}", self.test_fp, path_str);

                //Load the heightmap file
                heightmaps.push(Heightmap::create_from_file(fp)?)
            }
        }
        Ok(heightmaps)
    }

    ///Load all the pointclouds saved from a test directory to a vector
    fn load_all_pcl(&mut self) -> Result<Vec<PointCloud>, anyhow::Error> {
        println!("LOADING POINTCLOUDS ----------");

        let mut pcls: Vec<PointCloud> = vec![];

        //Iterate through each file
        for path in fs::read_dir(&self.test_fp)? {
            let path_str = path?.file_name();
            let path_str = path_str.to_str().unwrap();

            //Identify the hmap files
            if path_str.starts_with("pcl_") {
                let fp = format!("{}/{}", self.test_fp, path_str);

                //Load the heightmap file
                pcls.push(PointCloud::create_from_file(fp)?)
            }
        }
        Ok(pcls)
    }

    ///Get pcls that have the identifier in the filepath
    pub fn get_pcl_with_identifier(
        &self,
        identifier: &str,
    ) -> Result<Vec<PointCloud>, anyhow::Error> {
        let mut pcls: Vec<PointCloud> = vec![];

        //Iterate through each file
        for path in fs::read_dir(&self.test_fp)? {
            let path_str = path?.file_name();
            let path_str = path_str.to_str().unwrap();

            //Identify the hmap files
            if path_str.starts_with("pcl_") && path_str.contains(identifier) {
                let fp = format!("{}/{}", self.test_fp, path_str);

                //Load the heightmap file
                pcls.push(PointCloud::create_from_file(fp)?)
            }
        }
        Ok(pcls)
    }

    ///Load all PCLs and cut them to meet the specified passband
    ///WARNING: Will delete your data
    pub fn apply_passband(
        &mut self,
        min_x: f32,
        max_x: f32,
        min_y: f32,
        max_y: f32,
        min_z: f32,
        max_z: f32,
    ) -> Result<(), anyhow::Error> {
        let mut cnt = 0;

        //Iterate through every pcl file in the test
        //Iterate through each file
        for path in fs::read_dir(&self.test_fp)? {
            let path_str = path?.file_name();
            let path_str = path_str.to_str().unwrap();

            //Identify the pcl files
            if path_str.starts_with("pcl_") {
                println!("Trimming PCL: {}", cnt);

                let pcl_fp = format!("{}/{}", self.test_fp, path_str);

                let mut curr_pcl = PointCloud::create_from_file(pcl_fp.clone())?;

                //Rotate the PCL
                curr_pcl.crop(min_x, max_x, min_y, max_y, min_z, max_z);

                //Resave the PCL
                curr_pcl.save_to_file(pcl_fp.strip_suffix(".txt").unwrap())?;

                cnt += 1;
            }
        }

        println!("Trimming complete");

        Ok(())
    }

    ///From a tests pointclouds - create a set of parametric heightmaps
    ///Define the resolutions, averages and identifiers
    pub fn create_parametric_hmaps(
        &mut self,
        resolutions: Vec<usize>,
        averages: Vec<u32>,
        identifiers: Vec<&str>,
    ) -> Result<(), anyhow::Error> {
        //Get the maximum average value
        let max_avg = *averages.iter().max().expect("Failed to get maximum") as i32;

        //For each identifier specified
        for identifier in identifiers.iter() {
            //Loda all the pointclouds with that identifier
            let mut curr_pcls: Vec<PointCloud> = self
                .get_pcl_with_identifier(identifier)
                .expect("Failed to find identifier");

            //check that there are enough for the maxmimum average
            if curr_pcls.len() < max_avg as usize {
                bail!("Not enough pointclouds for the average required!")
            }

            for resolution in resolutions.iter() {
                let mut cnt: u32 = 0;

                //Turn all of the pointclouds into heightmaps
                let mut curr_hmaps: Vec<Heightmap> = vec![];

                for pcl in curr_pcls.iter() {
                    curr_hmaps.push(Heightmap::create_using_pcl_ref(
                        pcl,
                        *resolution,
                        *resolution,
                    )?);

                    cnt += 1;

                    if averages.contains(&cnt) {
                        let filepath = format!(
                            "{}/hmap_{}_{}_res_{}_avg_{}",
                            self.test_fp, self.test_name, *identifier, *resolution, cnt
                        );

                        let _ = average_heightmaps(
                            &curr_hmaps,
                            curr_hmaps[0].lower_coord_bounds(),
                            curr_hmaps[0].upper_coord_bounds(),
                        )?
                        .save_to_file(&filepath);

                        println!(
                            "Average created for {} number of pcls at resolution {} at identifier {}",
                            cnt, *resolution, *identifier
                        );
                    }
                }
                println!("Completed for resolution {}", *resolution);
            }

            //Empty the current list
            curr_pcls.clear();

            println!("Completed for identifier {}", *identifier);
        }

        Ok(())
    }

    ///From a tests pointclouds - create a set of parametric heightmaps
    ///Define the resolutions, averages and identifiers
    /// Compares these heightmaps with a provided pointcloud ground truth
    /// Save the generated error map
    /// Allows a user to specify height deltas to offset height differences
    pub fn save_parametric_error_maps(
        &mut self,
        resolutions: Vec<usize>,
        averages: Vec<u32>,
        identifiers: Vec<&str>,
        gnd_truth_pcl: &PointCloud,
        height_deltas: Vec<f32>,
    ) -> Result<(), anyhow::Error> {
        //Get the maximum average value
        let max_avg = *averages.iter().max().expect("Failed to get maximum") as i32;

        //For each identifier specified
        for (i, identifier) in identifiers.iter().enumerate() {
            //Loda all the pointclouds with that identifier
            let mut curr_pcls: Vec<PointCloud> = self
                .get_pcl_with_identifier(identifier)
                .expect("Failed to find identifier");

            //check that there are enough for the maxmimum average
            if curr_pcls.len() < max_avg as usize {
                bail!("Not enough pointclouds for the average required!")
            }

            if identifiers.len() != height_deltas.len() {
                bail!("Number of identifiers does not equal number of height deltas!")
            }

            for resolution in resolutions.iter() {
                let mut cnt: u32 = 0;

                //Turn all of the pointclouds into heightmaps
                let mut curr_hmaps: Vec<Heightmap> = vec![];

                let curr_gnd_truth =
                    Heightmap::create_using_pcl_ref(gnd_truth_pcl, *resolution, *resolution)?;

                for pcl in curr_pcls.iter() {
                    curr_hmaps.push(Heightmap::create_using_pcl_ref(
                        pcl,
                        *resolution,
                        *resolution,
                    )?);

                    cnt += 1;

                    if averages.contains(&cnt) {
                        let mut curr_hmap = average_heightmaps(
                            &curr_hmaps,
                            curr_hmaps[0].lower_coord_bounds(),
                            curr_hmaps[0].upper_coord_bounds(),
                        )?;

                        //Acount for user specified height offsets
                        if height_deltas[i] != 0.0 {
                            curr_hmap.offset_map(height_deltas[i]);
                        }

                        let err_map =
                            comp_maps(&curr_gnd_truth, &curr_hmap).expect("Failed to calc err map");

                        //Save the stats file
                        let filepath = format!(
                            "{}/hmap_err_{}_{}_{}_{}.txt",
                            self.test_fp, self.test_name, *identifier, resolution, cnt
                        );

                        err_map.save_to_file(&filepath)?;
                    }
                }
                println!("{}-Completed for resolution {}", identifier, *resolution);
            }

            //Empty the current list
            curr_pcls.clear();

            println!("Completed for identifier {}", *identifier);
        }

        Ok(())
    }

    ///From a tests pointclouds - create a set of parametric heightmaps
    ///Define the resolutions, averages and identifiers
    /// Compares these heightmaps with a provided pointcloud ground truth
    /// Calculates the mean error and std_dev err
    /// Allows a user to specify height deltas to offset height differences
    pub fn save_parametric_hmap_stats(
        &mut self,
        resolutions: Vec<usize>,
        averages: Vec<u32>,
        identifiers: Vec<&str>,
        gnd_truth_pcl: &PointCloud,
    ) -> Result<(), anyhow::Error> {
        //Get the maximum average value
        let max_avg = *averages.iter().max().expect("Failed to get maximum") as i32;

        //For each identifier specified
        for identifier in identifiers.iter() {
            let mut dist_stats: Vec<(usize, u32, f32, f32, u32)> = vec![];

            //Loda all the pointclouds with that identifier
            let mut curr_pcls: Vec<PointCloud> = self
                .get_pcl_with_identifier(identifier)
                .expect("Failed to find identifier");

            //check that there are enough for the maxmimum average
            if curr_pcls.len() < max_avg as usize {
                bail!("Not enough pointclouds for the average required!")
            }

            for resolution in resolutions.iter() {
                let mut cnt: u32 = 0;

                //Turn all of the pointclouds into heightmaps
                let mut curr_hmaps: Vec<Heightmap> = vec![];

                let curr_gnd_truth =
                    Heightmap::create_using_pcl_ref(gnd_truth_pcl, *resolution, *resolution)?;

                for pcl in curr_pcls.iter() {
                    curr_hmaps.push(Heightmap::create_using_pcl_ref(
                        pcl,
                        *resolution,
                        *resolution,
                    )?);

                    cnt += 1;

                    if averages.contains(&cnt) {
                        let curr_hmap = average_heightmaps(
                            &curr_hmaps,
                            curr_hmaps[0].lower_coord_bounds(),
                            curr_hmaps[0].upper_coord_bounds(),
                        )?;

                        let err_map =
                            comp_maps(&curr_gnd_truth, &curr_hmap).expect("Failed to calc err map");

                        let stats = get_err_stats(&err_map);

                        dist_stats.push((*resolution, cnt, stats.0, stats.1, err_map.no_of_nans()));
                    }
                }
                println!("{}-Completed for resolution {}", identifier, *resolution);
            }

            //Save the stats file
            let filepath = format!(
                "{}/comp_stats_{}_{}.csv",
                self.test_fp, self.test_name, *identifier
            );

            let mut file = fs::OpenOptions::new()
                .append(true)
                .create(true)
                .open(filepath)
                .unwrap();

            let csv_header = "resolution,average,mean_err,std.dev_err,no_of_nans\n".to_string();
            let _ = file.write_all(csv_header.as_bytes());

            for stat in dist_stats.iter() {
                let stats_str = format!("{},{},{},{},{}\n", stat.0, stat.1, stat.2, stat.3, stat.4);
                let _ = file.write_all(stats_str.as_bytes());
            }

            //Empty the current list
            curr_pcls.clear();

            println!("Completed for identifier {}", *identifier);
        }

        Ok(())
    }

    ///Go through the pcls in sets of identifiers
    ///Calculate the point density (no. of points / area) - assume rectangular plane
    ///
    pub fn save_avg_point_density(&self, identifiers: Vec<&str>) {
        //Density list
        let mut avg_densities: Vec<(&str, f32)> = vec![];

        //For each identifier specified
        for identifier in identifiers.iter() {
            //Loda all the pointclouds with that identifier
            let curr_pcls: Vec<PointCloud> = self
                .get_pcl_with_identifier(identifier)
                .expect("Failed to find identifier");

            let mut avg_density = 0.0;
            //Calculate the density for each pointcloud
            for pcl in curr_pcls.iter() {
                let pcl_cnt = pcl.size() as f32;

                let area = pcl.get_xy_area();

                avg_density += pcl_cnt / area;
            }

            avg_density /= curr_pcls.len() as f32;
            //Log the overall average
            avg_densities.push((identifier, avg_density))
        }

        //Save the avg densities in a csv
        //Save the stats file
        let filepath = format!("{}/pnt_density_{}.csv", self.test_fp, self.test_name);

        let mut file = fs::OpenOptions::new()
            .write(true)
            .truncate(true)
            .create(true)
            .open(filepath)
            .unwrap();

        let csv_header = "distance,density\n".to_string();
        let _ = file.write_all(csv_header.as_bytes());

        for density in avg_densities.iter() {
            let stats_str = format!("{},{}\n", density.0, density.1);
            let _ = file.write_all(stats_str.as_bytes());
        }
    }

    ///Make a simple estimate of a simple feature
    /// Calculates the depth/width and slope of a set of heightmaps created from pcls with a specific identifier
    pub fn group_simple_feature_estimator(
        &self,
        resolutions: Vec<usize>,
        averages: Vec<u32>,
        identifiers: Vec<&str>,
    ) -> Result<(), anyhow::Error> {
        //Get the maximum average value
        let max_avg = *averages.iter().max().expect("Failed to get maximum") as i32;

        //For each identifier specified
        for (i, identifier) in identifiers.iter().enumerate() {
            //List of measured features - (depth, width, slope)
            let mut features: Vec<(usize, u32, f32, f32, f32)> = vec![];

            //Loda all the pointclouds with that identifier
            let curr_pcls: Vec<PointCloud> = self
                .get_pcl_with_identifier(identifier)
                .expect("Failed to find identifier");

            //check that there are enough for the maxmimum average
            if curr_pcls.len() < max_avg as usize {
                bail!("Not enough pointclouds for the average required!")
            }

            for resolution in resolutions.iter() {
                let mut cnt: u32 = 0;

                //Turn all of the pointclouds into heightmaps
                let mut curr_hmaps: Vec<Heightmap> = vec![];

                for pcl in curr_pcls.iter() {
                    curr_hmaps.push(Heightmap::create_using_pcl_ref(
                        pcl,
                        *resolution,
                        *resolution,
                    )?);

                    cnt += 1;

                    if averages.contains(&cnt) {
                        let mut curr_hmap = average_heightmaps(
                            &curr_hmaps,
                            curr_hmaps[0].lower_coord_bounds(),
                            curr_hmaps[0].upper_coord_bounds(),
                        )?;

                        let feature_info: (usize, u32, f32, f32, f32);
                        //Calculate the feature size
                        if let Ok((depth, width, slope_avg)) = curr_hmap.calc_simple_feature() {
                            feature_info = (*resolution, cnt, depth, width, slope_avg);
                        } else {
                            //If feature isn't identified
                            feature_info = (*resolution, cnt, f32::NAN, f32::NAN, f32::NAN);
                        }
                        features.push(feature_info)
                    }
                }
            }

            //Save the stats file
            let filepath = format!("{}/features_{}.csv", self.test_fp, self.test_name);

            let mut file = fs::OpenOptions::new()
                .append(true)
                .create(true)
                .open(filepath)
                .unwrap();

            if i == 0 {
                let csv_header = "distance,resolution,average,depth,width,slope avg\n".to_string();
                let _ = file.write_all(csv_header.as_bytes());
            }

            for feature in features.iter() {
                let stats_str = format!(
                    "{},{},{},{},{},{}\n",
                    identifier, feature.0, feature.1, feature.2, feature.3, feature.4
                );
                let _ = file.write_all(stats_str.as_bytes());
            }
            //Empty the features vector
            features.clear();
        }

        Ok(())
    }
}

///For a given error heightmap, calculate the average (mean) error, and the standard deviation
fn get_err_stats(err_map: &Heightmap) -> (f32, f32) {
    //First flatten the map
    let flattened_cells = err_map.get_flattened_cells().expect("Failed to flatten");

    //Calculate the mean value of the cells
    let mut no_of_cells = flattened_cells.len() as f32;
    let mut sum = 0.0;
    for cell in flattened_cells.iter() {
        if cell.is_nan() {
            //Dont include the NaN cells in the mean count
            no_of_cells -= 1.0;
            continue;
        }
        sum = add_nan_f32(sum, cell.abs());
    }
    let mean = sum / no_of_cells;

    //Calculate the std deviation of the cells
    let mut sum = 0.0;
    for cell in flattened_cells.iter() {
        //Ignore nan cells
        if cell.is_nan() {
            continue;
        }
        sum += (*cell - mean).powf(2.0);
    }
    let std_dev = (sum / no_of_cells).sqrt();

    (mean, std_dev)
}

///Determines the method for force intensity calculation
pub enum ForceSel {
    ForceAvg,
    MomAvg,
    X,
    Y,
    Z,
    Xmom,
    Ymom,
    Zmom,
}

///Loads the stored test configuration
fn get_config(filepath: &String, test_name: &String) -> Result<bool, anyhow::Error> {
    //Get the first line of the cam config file
    let config_fp = format!("{}/conf_{}.txt", filepath, test_name);

    let conf_file = File::open(config_fp)?;
    let mut line_reader = BufReader::new(conf_file);

    //Read the second line to get the robot info
    let mut rob_info_line = String::new();
    line_reader.read_line(&mut rob_info_line)?;

    //Check if trajectory data has already been transformed (to the box frame)
    println!("Loading transform flag");
    let mut trans_flag_str = String::new();
    line_reader.read_line(&mut trans_flag_str)?;
    println!("{trans_flag_str}");
    let trans_flag = trans_flag_str.contains("true");

    //Attempt to create both the configs inplace
    Ok(trans_flag)
}

///Compares a given map with a desired map and outputs a map of height differences
pub fn comp_maps(
    curr_map: &Heightmap,
    desired_map: &Heightmap,
) -> Result<Heightmap, anyhow::Error> {
    //Check the maps are the same size - if not exit
    if curr_map.height() != desired_map.height() || curr_map.width() != desired_map.width() {
        bail!("Warning - Maps are not the same size - cannot be compared");
    }

    //Create a new empty map that holds the difference
    let diff_map: Heightmap = Heightmap::new(curr_map.width(), curr_map.height());

    //Sweep through each cell and replace with the new map height
    for (m, row) in diff_map.cells().iter_mut().enumerate() {
        for (n, col) in row.iter_mut().enumerate() {
            //First index is the row number (i.e. the height)
            let diff = curr_map.get_cell_height(n, m)? - desired_map.get_cell_height(n, m)?;

            //Not entirely sure why y and x are the opposite way rounds but hey ho
            *col = diff;
        }
    }

    Ok(diff_map)
}

//General heightmap statistical functions

///Takes a list of heightmaps that are the same size and averages them
pub fn average_heightmaps(
    hmap_list: &[Heightmap],
    lower_bounds: [f32; 2],
    upper_bounds: [f32; 2],
) -> Result<Heightmap, anyhow::Error> {
    //Create an empty heightmap the same size as the heightmaps in the list
    let mut avg_hmap = Heightmap::new(hmap_list[0].width(), hmap_list[0].height());

    //Go through each point in the empty heightmap and take the mean of the genned heightmaps
    for (y, row) in avg_hmap.cells().iter_mut().enumerate() {
        for (x, val) in row.iter_mut().enumerate() {
            let mut total_val = 0.0;
            let mut valid_pnt_cnt = 0;
            for hmap in hmap_list {
                let cell_val = hmap.get_cell_height(x, y)?;

                //Ignore nan valued cells - dont diminish mean error
                if cell_val.is_nan() {
                    continue;
                }
                total_val += cell_val;
                valid_pnt_cnt += 1;
            }

            if total_val == 0.0 {
                *val = f32::NAN
            } else {
                *val = total_val / valid_pnt_cnt as f32;
            }
        }
    }

    avg_hmap.set_lower_coord_bounds(lower_bounds);
    avg_hmap.set_upper_coord_bounds(upper_bounds);

    Ok(avg_hmap)
}

///Adds a value (that could possibly be NaN) to a variable
fn add_nan_f32(var: f32, val: f32) -> f32 {
    //If the value is NaN just add nothing
    if val.is_nan() { var } else { var + val }
}

///Subtracts a value (that could possibly be NaN) to a variable
fn sub_nan_f32(var: f32, val: f32) -> f32 {
    //If the value is NaN just add nothing
    add_nan_f32(var, -val)
}