/// 架构图示例场景:自动化产线系统架构(手绘稿 / 完成图两种状态), /// 以及「手绘识别对照表」的素材(用于演示与截图)。 /// /// 「手绘稿」= 把每个标准图形的轮廓采样成带抖动的单笔墨迹 + 箭头墨迹; /// 「完成图」= 识别后的标准图形 + 吸附连线 + 标签文字。 /// 两个场景共用同一套几何,保证演示「画 → 识别 → 成图」的完整链路。 library; import 'dart:math' as math; import 'dart:ui'; import 'arch_models.dart'; import 'arch_painter.dart'; /// 示例场景种类。 enum ArchDemoKind { /// 手绘稿(全部为墨迹,等待识别)。 ink, /// 完成图(标准图形 + 连线 + 文字)。 done, } /// 示例场景中的一个图形定义。 class ArchDemoEntry { /// 构造图形定义。 const ArchDemoEntry( this.id, this.type, this.rect, this.color, this.text, { this.radius = 0, this.dir = 0, }); /// 图形 id。 final String id; /// 图形类型。 final ArchShapeType type; /// 外接矩形(世界坐标)。 final Rect rect; /// 主题色。 final int color; /// 图内文字。 final String text; /// 圆角半径(圆角矩形)。 final double radius; /// 朝向(三角形)。 final int dir; } /// 示例场景中的一条连线定义。 class ArchDemoConn { /// 构造连线定义。 const ArchDemoConn( this.from, this.to, this.label, { this.dash = false, this.route = ArchRoute.straight, }); /// 起点图形 id。 final String from; /// 终点图形 id。 final String to; /// 连线标签。 final String label; /// 是否虚线。 final bool dash; /// 走线方式(直线 / 正交折线)。 final ArchRoute route; } /// 示例图形清单(两个场景共用)。 List archDemoEntries() => [ const ArchDemoEntry('S1', ArchShapeType.cloud, Rect.fromLTWH(620, 20, 250, 120), 0xFF94A3B8, '云平台 · 远程运维'), const ArchDemoEntry('S2', ArchShapeType.rect, Rect.fromLTWH(600, 186, 290, 78), 0xFF38BDF8, '组态上位机 (Windows)'), const ArchDemoEntry('S3', ArchShapeType.roundRect, Rect.fromLTWH(628, 310, 234, 66), 0xFFA78BFA, '工业交换机', radius: 16), const ArchDemoEntry('S4', ArchShapeType.rect, Rect.fromLTWH(595, 424, 300, 84), 0xFF4CC9F0, 'PLC 主站\n(EtherCAT)'), const ArchDemoEntry('S5', ArchShapeType.rect, Rect.fromLTWH(120, 438, 190, 76), 0xFF34D399, '视觉相机\nGigE'), const ArchDemoEntry('S6', ArchShapeType.roundRect, Rect.fromLTWH(1030, 438, 190, 76), 0xFFF472B6, 'IO 模块', radius: 14), const ArchDemoEntry('S7', ArchShapeType.rect, Rect.fromLTWH(540, 560, 240, 76), 0xFFFFD166, '伺服驱动器'), const ArchDemoEntry('S8', ArchShapeType.ellipse, Rect.fromLTWH(505, 706, 150, 96), 0xFF38BDF8, '伺服电机'), const ArchDemoEntry('S9', ArchShapeType.ellipse, Rect.fromLTWH(820, 706, 180, 96), 0xFF34D399, '气缸 / 传感器'), const ArchDemoEntry('S10', ArchShapeType.cylinder, Rect.fromLTWH(140, 690, 150, 130), 0xFFFF8A5C, '历史数据库'), ]; /// 示例连线清单。 List archDemoConns() => [ const ArchDemoConn('S1', 'S2', '光纤 / 5G', dash: true), const ArchDemoConn('S2', 'S3', 'Profinet'), const ArchDemoConn('S3', 'S4', 'Profinet'), const ArchDemoConn('S4', 'S5', 'GigE', route: ArchRoute.ortho), const ArchDemoConn('S4', 'S7', 'EtherCAT'), const ArchDemoConn('S4', 'S6', 'EtherCAT'), const ArchDemoConn('S7', 'S8', '动力线'), const ArchDemoConn('S6', 'S9', '24V IO / 气路'), const ArchDemoConn('S4', 'S10', 'OPC UA', route: ArchRoute.ortho), ]; /// 由定义构造标准图形。 ArchShape _shapeOf(ArchDemoEntry e) => ArchShape( id: e.id, type: e.type, rect: e.rect, radius: e.radius, dir: e.dir, fill: archFillOf(e.color), stroke: e.color, sw: 1.8, text: e.text, ); /// 手绘稿场景:所有图形与连线的「原始墨迹」。 ArchDoc archDemoInkScene() { final List strokes = []; final Map byId = {}; int seed = 7; const int inkColor = 0xFF64748B; for (final ArchDemoEntry e in archDemoEntries()) { byId[e.id] = _shapeOf(e); final Path path = archShapePath(byId[e.id]!); final List pts = archInkOutline(path, seed: seed++, jitter: 1.7, gapFrac: 0.07); if (pts.length > 4) { strokes.add(ArchStroke( id: 'K${strokes.length + 1}', pts: pts, color: inkColor, )); } } for (final ArchDemoConn c in archDemoConns()) { final ArchShape f = byId[c.from]!; final ArchShape t = byId[c.to]!; final Offset a = archBorderPoint(f, t.rect.center); final Offset b = archBorderPoint(t, f.rect.center); final List pts = _inkArrow(a, b, seed: seed++); if (pts.length > 6) { strokes.add(ArchStroke( id: 'K${strokes.length + 1}', pts: pts, color: inkColor, )); } } return ArchDoc(strokes: strokes); } /// 完成图场景:标准图形 + 吸附连线 + 标题。 ArchDoc archDemoDoneScene() { final List shapes = [ const ArchShape( id: 'T0', type: ArchShapeType.text, rect: Rect.fromLTWH(120, 40, 430, 40), fill: 0, stroke: 0xFF64748B, sw: 0, text: '自动化产线 · 系统架构(示例)', ), for (final ArchDemoEntry e in archDemoEntries()) _shapeOf(e), ]; final Map byId = { for (final ArchShape s in shapes) s.id: s, }; final List conns = []; for (final ArchDemoConn c in archDemoConns()) { final ArchShape f = byId[c.from]!; final ArchShape t = byId[c.to]!; conns.add(ArchConnector( id: 'C${conns.length + 1}', fromId: c.from, from: f.rect.center, toId: c.to, to: t.rect.center, label: c.label, dash: c.dash, route: c.route, )); } return ArchDoc(shapes: shapes, connectors: conns); } /// 「手绘识别对照表」素材:一组(标签, 手绘墨迹),供演示 / 截图逐个识别。 List<(String, List)> archRecognitionSheet() { final List<(String, List)> out = <(String, List)>[]; int seed = 101; void addStroke(String label, List pts) { out.add((label, pts)); } void addOutline(String label, ArchShape proto) { addStroke( label, archInkOutline(archShapePath(proto), seed: seed++, jitter: 1.8, gapFrac: 0.07), ); } ArchShape proto(ArchShapeType type, Rect r, {double radius = 0}) => ArchShape( id: 'x', type: type, rect: r, radius: radius, fill: 0, stroke: 0xFF64748B, sw: 1.8, ); addStroke('直线', _inkLine(const Offset(24, 128), const Offset(196, 66), seed: seed++)); addStroke('箭头', _inkArrow(const Offset(24, 132), const Offset(196, 60), seed: seed++)); addOutline('矩形', proto(ArchShapeType.rect, const Rect.fromLTWH(34, 30, 152, 104))); addOutline('圆角矩形', proto(ArchShapeType.roundRect, const Rect.fromLTWH(34, 30, 152, 104), radius: 22)); addOutline('圆形', proto(ArchShapeType.circle, const Rect.fromLTWH(46, 26, 128, 128))); addOutline('椭圆', proto(ArchShapeType.ellipse, const Rect.fromLTWH(30, 42, 160, 92))); addOutline('三角形', proto(ArchShapeType.triangle, const Rect.fromLTWH(44, 26, 140, 118))); addOutline('菱形', proto(ArchShapeType.diamond, const Rect.fromLTWH(30, 32, 160, 108))); addStroke( '折线', archInkOutline( Path() ..moveTo(26, 30) ..lineTo(26, 96) ..lineTo(118, 96) ..lineTo(118, 142) ..lineTo(196, 142), seed: seed++, jitter: 1.5, gapFrac: 0, ), ); addStroke( '曲线', archInkOutline( Path() ..moveTo(24, 122) ..cubicTo(62, 36, 138, 150, 198, 62), seed: seed++, jitter: 1.7, gapFrac: 0, ), ); return out; } // ===================== 墨迹采样 ===================== /// 沿路径采样成一段带抖动的「手绘墨迹」([gapFrac] 为收笔缺口占比)。 List archInkOutline( Path path, { required int seed, double jitter = 1.7, double gapFrac = 0.07, }) { final List metrics = path.computeMetrics().toList(); double total = 0; for (final PathMetric m in metrics) { total += m.length; } if (total < 10) return []; final math.Random rnd = math.Random(seed); final List out = []; final double stop = total * (1 - gapFrac); final double step = math.max(3.0, total / 260); double walked = 0; for (final PathMetric m in metrics) { double d = 0; while (d < m.length && walked < stop) { final Tangent? tan = m.getTangentForOffset(d); if (tan != null) { final Offset dir = tan.vector; final Offset nrm = Offset(-dir.dy, dir.dx); final double j = (rnd.nextDouble() * 2 - 1) * jitter + math.sin(d * 0.045 + seed) * jitter * 0.6; out.add(tan.position + nrm * j); } d += step; walked += step; } if (walked >= stop) break; } return out; } /// 采样一段直线墨迹。 List _inkLine(Offset a, Offset b, {int seed = 1, double jitter = 1.2}) { final math.Random rnd = math.Random(seed); final List out = []; final Offset d = b - a; final double len = d.distance; if (len < 4) return out; final Offset u = d / len; final Offset n = Offset(-u.dy, u.dx); for (double t = 0; t <= len - 3; t += 6) { out.add(a + u * t + n * ((rnd.nextDouble() * 2 - 1) * jitter)); } out.add(b); return out; } /// 采样一段单笔箭头墨迹:箭杆 + 尖端折返的「>」形头部。 List _inkArrow(Offset a, Offset b, {int seed = 1, double jitter = 1.2}) { final math.Random rnd = math.Random(seed); final List out = []; final Offset d = b - a; final double len = d.distance; if (len < 36) return out; final Offset u = d / len; final Offset n = Offset(-u.dy, u.dx); // 箭杆 for (double t = 0; t <= len - 4; t += 7) { out.add(a + u * t + n * ((rnd.nextDouble() * 2 - 1) * jitter)); } // 头部:尖端 → 左后 → 右后(同一笔完成) final double hs = math.min(40, len * 0.18); final Offset p1 = b + _rot(u, 155) * hs; final Offset p2 = b + _rot(u, -155) * hs; final Offset mid = (p1 + p2) / 2; const int seg = 4; for (int i = 1; i <= seg; i++) { out.add(Offset.lerp(b, p1, i / seg)! + n * ((rnd.nextDouble() * 2 - 1) * jitter * 0.5)); } for (int i = 1; i <= seg; i++) { out.add(i <= 2 ? Offset.lerp(p1, mid, i / 2)! : Offset.lerp(mid, p2, (i - 2) / (seg - 2))!); } return out; } /// 向量旋转(度)。 Offset _rot(Offset v, double deg) { final double r = deg * math.pi / 180; return Offset(v.dx * math.cos(r) - v.dy * math.sin(r), v.dx * math.sin(r) + v.dy * math.cos(r)); }