Skip to content

Conversation

john-michaelburke
Copy link
Collaborator

@john-michaelburke john-michaelburke commented Mar 26, 2022

There were a few things not working correctly.

  • When switching between Meters <-> Degrees, the solution line was not getting converted.
  • Because we were not relying on a "pending solution line" to get approved before sending to the frontend, if you hit pause, the solution line would continue drawing incoming points.
  • Filter out nan values when calculating our lat/lon sig figs and offsets CPP-697

Also cleaned up some of the conversion logic to reduce duplication.

@john-michaelburke john-michaelburke force-pushed the john-michaelburke/cpp-697 branch 2 times, most recently from 1d12a48 to cd80612 Compare March 29, 2022 19:45
@john-michaelburke john-michaelburke changed the title wip fix solution line drawing. Fix issue with drawn solution line. Mar 29, 2022
@john-michaelburke john-michaelburke changed the title Fix issue with drawn solution line. Fix issue with solution line and switching Degrees<->Meters[CPP-697] Mar 29, 2022
@john-michaelburke john-michaelburke changed the title Fix issue with solution line and switching Degrees<->Meters[CPP-697] Fix solution line and switching Degrees<->Meters[CPP-697] Mar 29, 2022
@john-michaelburke john-michaelburke force-pushed the john-michaelburke/cpp-697 branch from cd80612 to ec51df0 Compare March 29, 2022 20:10
@john-michaelburke john-michaelburke marked this pull request as ready for review March 29, 2022 20:42
@john-michaelburke john-michaelburke requested a review from a team March 29, 2022 20:42
@silverjam silverjam requested a review from notoriaga March 29, 2022 20:46
(lat_offset, lat_sf, lon_offset, lon_sf)
};
let (lat_sf_temp, lat_offset_temp, lon_sf_temp, lon_offset_temp) =
if matches!(&self.unit, &LatLonUnits::Degrees) {
Copy link
Contributor

@notoriaga notoriaga Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to do this without having to match on the units twice/have the _temp set of variables? I'm finding it kind of hard to figure out what's going on. It feels like this function may be trying to do too much by having one code path to perform either direction of the conversion (as also converting units to themselves?). I think you'd be able to simplify the logic at the cost of some duplication. Maybe something like

fn convert_points(&mut self, unit: LatLonUnits) {
    if unit == self.unit {
        return;
    }
    match unit {
        LatLonUnits::Degrees => {
            for mode in &self.mode_strings {
                let lat_str = format!("lat_{}", mode);
                let lon_str = format!("lon_{}", mode);
                if let Some(lats) = self.slns.get_mut(lat_str.as_str()) {
                    for lat in lats.iter_mut() {
                        *lat = lat_meters_to_deg(...);
                    }
                }
                if let Some(lons) = self.slns.get_mut(lon_str.as_str()) {
                    for lon in lons.iter_mut() {
                        *lon = lon_meters_to_deg(...);
                    }
                }
            }
            for (lon, lat) in self.pending_sln_line.iter_mut() {
                *lon = lon_meters_to_deg(...);
                *lat = lat_meters_to_deg(...);
            }
            self.lat_sf = ...;
            self.lon_sf = ...;
            self.lat_offset = ...;
            self.lon_offset = ...;
        }
        LatLonUnits::Meters => {
           for mode in &self.mode_strings {
                let lat_str = format!("lat_{}", mode);
                let lon_str = format!("lon_{}", mode);
                if let Some(lats) = self.slns.get_mut(lat_str.as_str()) {
                    for lat in lats.iter_mut() {
                        *lat = lat_deg_to_meters(...);
                    }
                }
                if let Some(lons) = self.slns.get_mut(lon_str.as_str()) {
                    for lon in lons.iter_mut() {
                        *lon = lon_deg_to_meters(...);
                    }
                }
            }
            for (lon, lat) in self.pending_sln_line.iter_mut()
                *lon = lon_deg_to_meters(...);
                *lat = lat_deg_to_meters(...);
            }
            self.lat_sf = ...;
            self.lon_sf = ...;
            self.lat_offset = ...;
            self.lon_offset = ...;
        }
    }
    self.unit = unit;
}

this also assumes some functions

fn lat_meters_to_deg () { ... }
fn lon_meters_to_deg () { ... }
fn lat_deg_to_meters () { ... }
fn lon_deg_to_meters () { ... }

again seems like a case where being more explicate about the conversions would help readability

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

impl LatLonUnits {
pub fn lat_deg_to_meters(lat: f64, sf: f64, offset: f64) -> f64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't think I fully understood what was going on before, I didn't realize the for formula was the same for lat and lon. I think you can un-duplicate the code a bit, something like

fn convert_points(&mut self, unit: LatLonUnits) {
    let ((lat_offset, lat_sf, lon_offset, lon_sf), convert) = match &unit {
        LatLonUnits::Degrees => (
            (0.0, 1.0, 0.0, 1.0),
            &ll_meters_to_deg as &dyn Fn(f64, f64, f64) -> f64,
        ),
        LatLonUnits::Meters => (
            self.calc_deg_to_meters_lat_lon_sf_and_offset(),
            &ll_deg_to_meters as &dyn Fn(f64, f64, f64) -> f64,
        ),
    };
    for mode in self.mode_strings.iter() {
        let lat_str = format!("lat_{}", mode);
        let lon_str = format!("lon_{}", mode);
        if let Some(lats) = self.slns.get_mut(lat_str.as_str()) {
            for lat in lats.iter_mut() {
                *lat = convert(*lat, self.lat_sf, self.lat_offset);
            }
        }
        if let Some(lons) = self.slns.get_mut(lon_str.as_str()) {
            for lon in lons.iter_mut() {
                *lon = convert(*lon, self.lon_sf, self.lon_offset);
            }
        }
    }
    for (lon, lat) in self.pending_sln_line.iter_mut() {
        *lon = convert(*lon, self.lon_sf, self.lon_offset);
        *lat = convert(*lat, self.lat_sf, self.lat_offset);
    }
    self.lat_sf = lat_sf;
    self.lon_sf = lon_sf;
    self.lat_offset = lat_offset;
    self.lon_offset = lon_offset;
    self.unit = unit;
}

// should probably be free standing functions
fn ll_deg_to_meters(l: f64, sf: f64, offset: f64) -> f64;
fn ll_meters_to_deg(l: f64, sf: f64, offset: f64) -> f64;

Also I'd drop the temp variables I don't think you need them, and I'd make all the modifications to self together after you're done

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sf and offset inputs to the respective convert functions for meters->deg vs deg->meters are different so we would probably need store different temp vars as the input, similar to my original commit, to provide to the generic convert functions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I think I see what you mean. With either way I think what you have now is 👌

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@notoriaga Alright I added the temp values not the prettiest but is definitely less code than before.

@john-michaelburke john-michaelburke enabled auto-merge (squash) March 31, 2022 01:56
@john-michaelburke john-michaelburke merged commit 77d9f36 into main Mar 31, 2022
@john-michaelburke john-michaelburke deleted the john-michaelburke/cpp-697 branch March 31, 2022 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants