Skip to content
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

Pressure processing when simulatePressure = false & isComplete = false #4

Open
ercgeek opened this issue Nov 24, 2021 · 1 comment
Open

Comments

@ercgeek
Copy link

ercgeek commented Nov 24, 2021

First big thanks for publishing this.

Using the library to render strokes passing on iPad stylus pressure, the stroke is incorrectly rendered with a constant pressure, whatever is passed on points[0].

I think I found the issue on the use of lrp() to smooth out the points. Seems lrp() returns the pressure of the first point and not the interpolation of the pressures from both points.

Changed lib\src\get_stroke_points.dart as follows and seems to corrects the issue. Using the pressure from the second point, not an interpolated value.

// Original code
// if (isComplete && i == pts.length - 1) {
//   point = pts[i];
// } else {
//   point = lrp(prev.point, pts[i], t);
// }

// New code
if (isComplete && i == pts.length - 1) {
  point = pts[i];
} else {
  final tempPoint = lrp(prev.point, pts[i], t);
  point = Point(tempPoint.x, tempPoint.y, pts[i].p);
}

I'm not a dart expert, please let me know if I'm missing something here.

@ercgeek
Copy link
Author

ercgeek commented Nov 28, 2021

An additional change is needed in lib\src\get_stroke_outline_points.dart

// Original code
// if (thinning != 0) {
//   if (simulatePressure) {
//     sp = min(1, curr.distance / size);
//     rp = min(1, 1 - sp);
//     pressure = min(
//       1,
//       prevPressure + (rp - prevPressure) * (sp * rateOfPressureChange),
//     );
//     radius = getStrokeRadius(
//       size,
//       thinning,
//       pressure,
//     );
//   } else {
//     radius = size / 2;
//   }
// }

// New code
if (thinning != 0) {
  if (simulatePressure) {
    sp = min(1, curr.distance / size);
    rp = min(1, 1 - sp);
    pressure = min(
      1,
      prevPressure + (rp - prevPressure) * (sp * rateOfPressureChange),
    );
  }
  radius = getStrokeRadius(
    size,
    thinning,
    pressure,
  );
}

adil192 added a commit that referenced this issue Feb 6, 2024
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'dart:ui/lerp.dart': Failed assertion: line 19 pos 10: '<optimized out>': Cannot interpolate between finite and non-finite values
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
#2      lerpDouble (dart:ui/lerp.dart:19:10)
#3      PointVector.lerp (package:perfect_freehand/src/types/point_vector.dart:54:7)
#4      getStrokeOutlinePoints (package:perfect_freehand/src/get_stroke_outline_points.dart:238:31)
#5      getStroke (package:perfect_freehand/src/get_stroke.dart:19:10)
#6      Stroke._getPolygon (package:saber/components/canvas/_stroke.dart:200:21)
#7      Stroke._updatePolygon (package:saber/components/canvas/_stroke.dart:48:16)
#8      Stroke.polygon (package:saber/components/canvas/_stroke.dart:38:32)
#9      Stroke.toSvgPath (package:saber/components/canvas/_stroke.dart:247:9)
#10     EditorExporter.generatePdf.<anonymous closure>.<anonymous closure> (package:saber/data/editor/editor_exporter.dart:111:52)
#11     CustomPaint.paint (package:pdf/src/widgets/basic.dart:623:25)
#12     SingleChildWidget.paintChild (package:pdf/src/widgets/widget.dart:320:14)
#13     ConstrainedBox.paint (package:pdf/src/widgets/basic.dart:449:5)
#14     StatelessWidget.paint (package:pdf/src/widgets/widget.dart:260:15)
#15     Page.paint (package:pdf/src/widgets/page.dart:246:13)
#16     Page.postProcess (package:pdf/src/widgets/page.dart:179:5)
#17     Document.save (package:pdf/src/widgets/document.dart:130:14)
#18     EditorState.exportAsPdf (package:saber/pages/editor/editor.dart:1213:72)
<asynchronous suspension>
#19     _ExportBarState._onPressed.<anonymous closure>.<anonymous closure> (package:saber/components/toolbar/export_bar.dart:38:29)
<asynchronous suspension>
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

No branches or pull requests

1 participant