Skip to content

Commit 7cfbdcd

Browse files
committed
address feedback
1 parent 8db280e commit 7cfbdcd

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

fred2/orienteditor.cpp

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
static char THIS_FILE[] = __FILE__;
2222
#endif
2323

24-
const float input_threshold = 0.01f; // smallest increment of input box
24+
constexpr auto INPUT_THRESHOLD = 0.01f; // smallest increment of input box
25+
constexpr auto INPUT_FORMAT = "%.01f";
2526

2627
/////////////////////////////////////////////////////////////////////////////
2728
// orient_editor dialog
@@ -45,15 +46,15 @@ orient_editor::orient_editor(CWnd* pParent /*=NULL*/)
4546

4647
Assert(query_valid_object());
4748
auto pos = &Objects[cur_object_index].pos;
48-
m_position_x.Format("%.01f", pos->xyz.x);
49-
m_position_y.Format("%.01f", pos->xyz.y);
50-
m_position_z.Format("%.01f", pos->xyz.z);
49+
m_position_x.Format(INPUT_FORMAT, pos->xyz.x);
50+
m_position_y.Format(INPUT_FORMAT, pos->xyz.y);
51+
m_position_z.Format(INPUT_FORMAT, pos->xyz.z);
5152

5253
angles ang;
5354
vm_extract_angles_matrix(&ang, &Objects[cur_object_index].orient);
54-
m_orientation_p.Format("%.01f", to_degrees(ang.p));
55-
m_orientation_b.Format("%.01f", to_degrees(ang.b));
56-
m_orientation_h.Format("%.01f", to_degrees(ang.h));
55+
m_orientation_p.Format(INPUT_FORMAT, to_degrees(ang.p));
56+
m_orientation_b.Format(INPUT_FORMAT, to_degrees(ang.b));
57+
m_orientation_h.Format(INPUT_FORMAT, to_degrees(ang.h));
5758
}
5859

5960
void orient_editor::DoDataExchange(CDataExchange* pDX)
@@ -182,47 +183,47 @@ BOOL orient_editor::OnInitDialog()
182183
return TRUE;
183184
}
184185

185-
bool orient_editor::close(float val, const CString &input_str)
186+
/**
187+
* Checks whether the position value is close enough to the input string value that we can assume the input has not changed.
188+
*/
189+
bool orient_editor::is_close(float val, const CString &input_str)
186190
{
187-
CString str;
188-
str.Format("%.01f", val);
189-
val = convert(str);
190-
191+
val = perform_input_rounding(val);
191192
float input_val = convert(input_str);
192193

193194
float diff = val - input_val;
194-
return diff > -input_threshold && diff < input_threshold;
195+
return abs(diff) < INPUT_THRESHOLD;
195196
}
196197

197-
bool orient_editor::angle_close(float rad, const CString &input_str)
198+
/**
199+
* Checks whether the angle value is close enough to the input string value that we can assume the input has not changed.
200+
*/
201+
bool orient_editor::is_angle_close(float rad, const CString &input_str)
198202
{
199-
CString str;
200-
str.Format("%.01f", to_degrees(rad));
201-
float deg = convert(str);
202-
203+
float deg = perform_input_rounding(to_degrees(rad));
203204
float input_deg = normalize_degrees(convert(input_str));
204205

205206
float diff = deg - input_deg;
206-
return diff > -input_threshold && diff < input_threshold;
207+
return abs(diff) < INPUT_THRESHOLD;
207208
}
208209

209210
bool orient_editor::query_modified()
210211
{
211-
if (!close(Objects[cur_object_index].pos.xyz.x, m_position_x))
212+
if (!is_close(Objects[cur_object_index].pos.xyz.x, m_position_x))
212213
return true;
213-
if (!close(Objects[cur_object_index].pos.xyz.y, m_position_y))
214+
if (!is_close(Objects[cur_object_index].pos.xyz.y, m_position_y))
214215
return true;
215-
if (!close(Objects[cur_object_index].pos.xyz.z, m_position_z))
216+
if (!is_close(Objects[cur_object_index].pos.xyz.z, m_position_z))
216217
return true;
217218

218219
angles ang;
219220
vm_extract_angles_matrix(&ang, &Objects[cur_object_index].orient);
220221

221-
if (!angle_close(ang.p, m_orientation_p))
222+
if (!is_angle_close(ang.p, m_orientation_p))
222223
return true;
223-
if (!angle_close(ang.b, m_orientation_b))
224+
if (!is_angle_close(ang.b, m_orientation_b))
224225
return true;
225-
if (!angle_close(ang.h, m_orientation_h))
226+
if (!is_angle_close(ang.h, m_orientation_h))
226227
return true;
227228

228229
if (((CButton *) GetDlgItem(IDC_POINT_TO_CHECKBOX))->GetCheck() == TRUE)
@@ -242,7 +243,7 @@ void orient_editor::OnOK()
242243

243244
// there's enough difference in our position that we're changing it
244245
cur_pos = &Objects[cur_object_index].pos;
245-
if (!close(cur_pos->xyz.x, m_position_x) || !close(cur_pos->xyz.y, m_position_y) || !close(cur_pos->xyz.z, m_position_z))
246+
if (!is_close(cur_pos->xyz.x, m_position_x) || !is_close(cur_pos->xyz.y, m_position_y) || !is_close(cur_pos->xyz.z, m_position_z))
246247
{
247248
vec3d pos;
248249
pos.xyz.x = convert(m_position_x);
@@ -255,7 +256,7 @@ void orient_editor::OnOK()
255256

256257
// there's enough difference in our orientation that we're changing it
257258
vm_extract_angles_matrix(&ang, &Objects[cur_object_index].orient);
258-
if (!angle_close(ang.p, m_orientation_p) || !angle_close(ang.b, m_orientation_b) || !angle_close(ang.h, m_orientation_h))
259+
if (!is_angle_close(ang.p, m_orientation_p) || !is_angle_close(ang.b, m_orientation_b) || !is_angle_close(ang.h, m_orientation_h))
259260
{
260261
ang.p = fl_radians(convert(m_orientation_p));
261262
ang.b = fl_radians(convert(m_orientation_b));
@@ -387,6 +388,9 @@ float orient_editor::normalize_degrees(float deg)
387388
return deg;
388389
}
389390

391+
/**
392+
* Extract a float from the CString, being mindful of any comma separators.
393+
*/
390394
float orient_editor::convert(const CString &str)
391395
{
392396
char buf[256];
@@ -402,6 +406,18 @@ float orient_editor::convert(const CString &str)
402406
return (float) atof(buf);
403407
}
404408

409+
/**
410+
* Perform rounding of the float value by loading it into the input box format string and parsing it again.
411+
* This accounts for not only decimal rounding to the precision of the input box, but also floating point rounding due to inexact fractions such as 1/10.
412+
* See also GitHub PR #4475.
413+
*/
414+
float orient_editor::perform_input_rounding(float val)
415+
{
416+
CString str;
417+
str.Format(INPUT_FORMAT, val);
418+
return convert(str);
419+
}
420+
405421
void orient_editor::OnPointTo()
406422
{
407423
UpdateData(TRUE);

fred2/orienteditor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ class orient_editor : public CDialog
7171

7272
private:
7373
float convert(const CString &str);
74-
bool close(float val, const CString &input_str);
75-
bool angle_close(float rad, const CString &input_str);
74+
float perform_input_rounding(float val);
75+
76+
bool is_close(float val, const CString &input_str);
77+
bool is_angle_close(float rad, const CString &input_str);
7678
int total;
7779
int index[MAX_OBJECTS];
7880
void actually_point_object(object *ptr);

0 commit comments

Comments
 (0)