normalizeToLocalCenter method
Normalizes interactive stroke geometry into local coordinates.
Preconditions (validated at runtime):
- transform must be the identity transform.
- All point coordinates must be finite.
Throws StateError when preconditions are violated.
This method is intended for interactive drawing: while the user draws,
the engine may temporarily store points in world/scene coordinates with
transform == identity. Call this when the gesture finishes to convert
geometry to local space and store the world center in transform.
Implementation
void normalizeToLocalCenter() {
final t = transform;
if (!_isExactIdentityTransform(t)) {
throw StateError(
'StrokeNode.normalizeToLocalCenter requires transform == identity. '
'Use StrokeNode.fromWorldPoints for non-interactive creation.',
);
}
for (final p in points) {
if (!p.dx.isFinite || !p.dy.isFinite) {
throw StateError(
'StrokeNode.normalizeToLocalCenter requires finite point coordinates.',
);
}
}
if (points.isEmpty) return;
final bounds = aabbFromPoints(points);
final centerWorld = bounds.center;
for (var i = 0; i < points.length; i++) {
points[i] = points[i] - centerWorld;
}
transform = Transform2D.trs(translation: centerWorld);
}