-
Notifications
You must be signed in to change notification settings - Fork 2
Fix solution line and switching Degrees<->Meters[CPP-697] #483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
1d12a48
to
cd80612
Compare
cd80612
to
ec51df0
Compare
console_backend/src/solution_tab.rs
Outdated
(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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
console_backend/src/solution_tab.rs
Outdated
} | ||
|
||
impl LatLonUnits { | ||
pub fn lat_deg_to_meters(lat: f64, sf: f64, offset: f64) -> f64 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👌
There was a problem hiding this comment.
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.
There were a few things not working correctly.
Also cleaned up some of the conversion logic to reduce duplication.