Skip to content

Commit

Permalink
Don't clear the output vector in StrokeModeler::Update
Browse files Browse the repository at this point in the history
This is more consistent with how this class is expected to be used. StrokeModeler::Update extends a smoothed stroke with new input, while StrokeModeler::Predict replaces an earlier prediction with a new one.

Also update the usage documentation, which missed being updated when Update and Predict were modified to take an output vector as a parameter instead of returning a newly-constructed vector.

PiperOrigin-RevId: 520683308
  • Loading branch information
sfreilich authored and Copybara-Service committed Mar 30, 2023
1 parent f86c1f4 commit d00e811
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 21 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ parameters; this also clears the in-progress stroke, if any. If the `Update` or
`absl::FailedPreconditionError`.

To use the model, pass in an `Input` object to `StrokeModeler::Update()` each
time you recieve an input event:
time you receive an input event. This appends to a `std::vector<Result>` of
stroke smoothing results:

```c++
Input input{
Expand All @@ -187,14 +188,13 @@ Input input{
.orientation = M_PI // Angle in plane of screen in radians
.tilt = 0, // Angle elevated from plane of screen in radians
};
absl::StatusOr<std::vector<Result>> result = modeler.Update(input);
if (status.ok()) {
std::vector<Result> smoothed_input = *result;
// Do something with the result.
} else {
absl::Status error_status = result.status();
// Handle error.
if (absl::Status status = modeler.Update(input, smoothed_stroke);
!status.ok()) {
// Handle error...
return status;
}
// Do something with the smoothed stroke so far...
return absl::OkStatus();
```

`Input`s are expected to come in a *stream*, starting with a `kDown` event,
Expand All @@ -219,16 +219,16 @@ last `Input`, wile the `Result` objects in the middle have their `position`
adjusted to smooth out and interpolate between the input values.

To construct a prediction, call `StrokeModeler::Predict()` while an input stream
is in-progress:
is in-progress. This takes a `std::vector<Result>` and replaces the contents
with the new prediction:

```c++
if (absl::StatusOr<std::vector<Result>> = modeler.Predict()) {
std::vector<Result> smoothed_input = *result;
// Do something with the result.
} else {
absl::Status error_status = result.status();
// Handle error.
if (absl::Status status = modeler.Predict(predicted_stroke); !status.ok) {
// Handle error...
return status;
}
// Do something with the new prediction...
return absl::OkStatus();
```

If no input stream is in-progress, it will instead return
Expand Down
2 changes: 0 additions & 2 deletions ink_stroke_modeler/stroke_modeler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ void StrokeModeler::ResetInternal() {

absl::Status StrokeModeler::Update(const Input &input,
std::vector<Result> &results) {
results.clear();

if (!stroke_model_params_.has_value()) {
return absl::FailedPreconditionError(
"Stroke model has not yet been initialized");
Expand Down
10 changes: 6 additions & 4 deletions ink_stroke_modeler/stroke_modeler.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ class StrokeModeler {
// Reset(StrokeModelParams).
absl::Status Reset();

// Updates the model with a raw input, and then clears and fills the results
// parameter with newly generated Results. Any previously generated Result
// values remain valid.
// Updates the model with a raw input, and appends newly generated Results
// to the results vector. Any previously generated Result values remain valid.
// (This does not require that any previous results returned remain in the
// results vector, the vector is appended to without examining the existing
// contents.)
//
// The function fills an out parameter instead of returning by value to allow
// the caller to reuse allocations. Update is expected to be called 10s to
Expand All @@ -68,7 +70,7 @@ class StrokeModeler {
//
// Returns an error if the the model has not yet been initialized (via Reset)
// or if the input stream is malformed (e.g decreasing time, Up event before
// Down event). In that case, results will be empty after the call.
// Down event). In that case, results will be unmodified after the call.
//
// If this does not return an error, results will contain at least one Result,
// and potentially more than one if the inputs are slower than the minimum
Expand Down

0 comments on commit d00e811

Please sign in to comment.