Skip to content

Commit abcb782

Browse files
Ink Open Sourcecopybara-github
authored andcommitted
A minor improvement to numerical stability in StrokeInputBatch encoding.
PiperOrigin-RevId: 831403881
1 parent 62c0686 commit abcb782

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

ink/storage/stroke_input_batch.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ void EncodeStrokeInputBatch(const StrokeInputBatch& input_batch,
9898
bounds_semi_width > 0.f
9999
? (0.5f * kInverseEnvelopeXScale) / bounds_semi_width
100100
: 1.f;
101-
x_stroke_space->set_scale(1.f / inverse_x_scale);
101+
// Recompute 1.0/inverse_x_scale to avoid floating point precision loss.
102+
x_stroke_space->set_scale(bounds_semi_width > 0.f ? 2.0 * bounds_semi_width /
103+
kInverseEnvelopeXScale
104+
: 1.f);
102105
x_stroke_space->set_offset(stroke_space_bounds.XMin());
103106
x_stroke_space->mutable_deltas()->Clear();
104107
x_stroke_space->mutable_deltas()->Reserve(input_batch.Size());
@@ -111,7 +114,11 @@ void EncodeStrokeInputBatch(const StrokeInputBatch& input_batch,
111114
bounds_semi_height > 0.f
112115
? (0.5f * kInverseEnvelopeYScale) / bounds_semi_height
113116
: 1.f;
114-
y_stroke_space->set_scale(1.f / inverse_y_scale);
117+
// Recompute 1.0/inverse_y_scale to avoid floating point precision loss.
118+
y_stroke_space->set_scale(bounds_semi_height > 0.f
119+
? 2.0 * bounds_semi_height /
120+
kInverseEnvelopeYScale
121+
: 1.f);
115122
y_stroke_space->set_offset(stroke_space_bounds.YMin());
116123
y_stroke_space->mutable_deltas()->Clear();
117124
y_stroke_space->mutable_deltas()->Reserve(input_batch.Size());

0 commit comments

Comments
 (0)