1.3.5:更名 EDC|易得设计,空白启动首页 + 可扩展图元库识别,架构图大版本

- 品牌:更名 EDC|易得设计(exe/安卓/仓库 EasyDesignCollection;旧地址 301 兼容老客户端更新)
- 首页:空白启动 + 六模块「新建文档」选择 + 最近打开;品牌菜单重做
- 架构图:手绘识别统一图形;Visio 式可扩展图元库(导入 / 存为图元 / 分类折叠 /
  手绘匹配 / 多笔回溯组合);识别后图形尺寸编辑(宽高长径 + 画布标注 + 双击改值);
  IT 系统图元;mm 标尺(右轴数值 / 实线游标 / A0 默认画布);图层、F8 正交 / F10 极轴、
  对齐均布;连线端点换绑重吸附;DXF / SVG / PNG 导出;缩小时画布溢出到侧栏的裁剪修复
- 时序图:分文件保存(*.seq.json / *.arch.json;旧 .edc.json 兼容)
- 路径:运行时目录迁移 %APPDATA%/EDC;素材库 library/;安卓 Documents/EDC
- 清理:参数化草图入口冻结;Windows Ink 原生桥移除;dist/ 退役
- 构建:exe + APK 双端同步(1.3.5+26);发布脚本切换 release/ 新目录结构
This commit is contained in:
2026-10-11 10:19:01 +08:00
parent fe5e0be131
commit 68c2f5d8f1
76 changed files with 18344 additions and 297 deletions
+201
View File
@@ -0,0 +1,201 @@
/// 手绘识别 · 图元库匹配。
///
/// 把笔迹(可多笔,顺序无关)与图元库每个符号的轮廓做「归一化双向
/// chamfer 距离」匹配,返回最相似的图元。
///
/// 与内置识别的关系:内置识别(矩形 / 圆 / 箭头 / 折线…)优先且保持不变;
/// **内置识别不了的笔迹再走本匹配器**——因此「图元库可扩展」等价于
/// 「能手绘识别的图形可扩展」:用户导入 / 自建的图元也能被画出来识别。
library;
import 'dart:math' as math;
import 'dart:ui' show Offset, Path, PathMetric, Rect, Tangent;
import '../library/symbol_library.dart';
/// 图元匹配结果。
class ArchSymbolMatch {
/// 构造。
const ArchSymbolMatch({required this.symbol, required this.score});
/// 命中的图元。
final LibSymbol symbol;
/// 双向 chamfer 平均距离(归一化坐标 0..1,越小越像)。
final double score;
}
/// 识别阈值(归一化单位):匹配分数大于该值视为「都不像」。
///
/// 标定依据(合成笔迹回归):真命中 0.006–0.03(接地 0.006 / 灯 0.013 /
/// 自定义图元≈0.01),乱涂 / 斜线等假命中 ≥0.07;取 0.06 保守稳妥。
const double kArchSymbolMaxScore = 0.06;
/// 最佳与次佳的分数差下限:相差太小说明特征不明显,宁可不错认。
/// (灯 vs 电机 这类相似图形差约 0.010;取 0.008 兼顾召回与精度。)
const double kArchSymbolMargin = 0.008;
/// 识别笔迹(多笔按点集合并)。
ArchSymbolMatch? recognizeArchSymbol(
List<List<Offset>> strokes,
List<LibSymbol> catalog, {
double maxScore = kArchSymbolMaxScore,
double margin = kArchSymbolMargin,
int inputSamples = 96,
int refSamples = 128,
}) {
// 每笔独立重采样(避免笔与笔之间产生"跳线"污染),按各笔长度分配采样数。
final List<List<Offset>> parts = <List<Offset>>[];
final List<double> lens = <double>[];
double total = 0;
for (final List<Offset> s in strokes) {
if (s.length < 2) continue;
double len = 0;
for (int i = 1; i < s.length; i++) {
len += (s[i] - s[i - 1]).distance;
}
if (len < 1e-6) continue;
parts.add(s);
lens.add(len);
total += len;
}
if (parts.isEmpty || total < 1e-6 || catalog.isEmpty) return null;
final List<Offset> pts = <Offset>[];
for (int i = 0; i < parts.length; i++) {
final int count = math.max(4, (inputSamples * lens[i] / total).round());
pts.addAll(_resample(parts[i], count));
}
if (pts.length < 4) return null;
final _Np input = _Np.of(pts);
if (input.extent < 1e-6) return null;
// 多尺度:手绘大小 / 完整度与图元不完全一致(少画一笔、比例不同都要容忍),
// 对输入做几档缩放,取最像的一档。
const List<double> scales = <double>[0.7, 0.85, 1.0, 1.2, 1.45];
final List<List<Offset>> scaled = <List<Offset>>[
for (final double s in scales)
<Offset>[for (final Offset p in input.pts) p * s],
];
ArchSymbolMatch? best;
double second = double.infinity;
for (final LibSymbol sym in catalog) {
final List<Offset> refPts = _sampleSymbol(sym, refSamples);
if (refPts.length < 4) continue;
final _Np ref = _Np.of(refPts);
if (ref.extent < 1e-6) continue;
double score = double.infinity;
for (final List<Offset> sp in scaled) {
final double sc = _chamfer(sp, ref.pts);
if (sc < score) score = sc;
}
if (best == null || score < best.score) {
second = best?.score ?? second;
best = ArchSymbolMatch(symbol: sym, score: score);
} else if (score < second) {
second = score;
}
}
if (best == null) return null;
if (best.score > maxScore) return null;
if (second.isFinite && second - best.score < margin) return null;
return best;
}
// ===================== 内部实现 =====================
/// 归一化点集:中心化 + 按最大边缩放(对平移 / 缩放不敏感)。
class _Np {
const _Np(this.pts, this.extent);
final List<Offset> pts;
final double extent;
static _Np of(List<Offset> pts) {
if (pts.isEmpty) return const _Np(<Offset>[], 0);
double l = pts.first.dx, r = l, t = pts.first.dy, b = t;
for (final Offset p in pts) {
if (p.dx < l) l = p.dx;
if (p.dx > r) r = p.dx;
if (p.dy < t) t = p.dy;
if (p.dy > b) b = p.dy;
}
final double ext = math.max(r - l, b - t);
if (ext < 1e-6) return _Np(List<Offset>.filled(pts.length, Offset.zero), 0);
final Offset c = Offset((l + r) / 2, (t + b) / 2);
return _Np(<Offset>[
for (final Offset p in pts)
Offset((p.dx - c.dx) / ext, (p.dy - c.dy) / ext),
], ext);
}
}
/// 按弧长均匀重采样折线到 [n] 个点。
List<Offset> _resample(List<Offset> pts, int n) {
if (pts.length <= 1) return List<Offset>.of(pts);
double total = 0;
for (int i = 1; i < pts.length; i++) {
total += (pts[i] - pts[i - 1]).distance;
}
if (total < 1e-6) return <Offset>[pts.first];
final List<Offset> out = <Offset>[pts.first];
int seg = 0;
double acc = 0;
double segLen = (pts[1] - pts[0]).distance;
for (int i = 1; i < n; i++) {
final double target = total * i / (n - 1);
while (acc + segLen < target && seg < pts.length - 2) {
acc += segLen;
seg++;
segLen = (pts[seg + 1] - pts[seg]).distance;
}
final double rest = target - acc;
final double t = segLen <= 1e-9 ? 0 : (rest / segLen).clamp(0.0, 1.0);
out.add(Offset.lerp(pts[seg], pts[seg + 1], t)!);
}
return out;
}
/// 采样图元轮廓(在 100×100 归一化盒内按各子路径长度分配采样点)。
List<Offset> _sampleSymbol(LibSymbol sym, int n) {
final Path path = buildSymbolPath(sym, const Rect.fromLTWH(0, 0, 100, 100));
final List<PathMetric> metrics = path.computeMetrics().toList();
double total = 0;
for (final PathMetric m in metrics) {
total += m.length;
}
if (total < 1e-6) return const <Offset>[];
final List<Offset> out = <Offset>[];
for (final PathMetric m in metrics) {
final int count = math.max(2, (n * m.length / total).round());
for (int i = 0; i <= count; i++) {
final Tangent? t = m.getTangentForOffset(m.length * i / count);
if (t != null) out.add(t.position);
}
}
return out;
}
/// 双向 chamfer 平均距离。
double _chamfer(List<Offset> a, List<Offset> b) {
double d1 = 0;
for (final Offset p in a) {
d1 += _nearest(p, b);
}
d1 /= a.length;
double d2 = 0;
for (final Offset p in b) {
d2 += _nearest(p, a);
}
d2 /= b.length;
return (d1 + d2) / 2;
}
double _nearest(Offset p, List<Offset> pts) {
double best = double.infinity;
for (final Offset q in pts) {
final double d = (p - q).distanceSquared;
if (d < best) best = d;
}
return math.sqrt(best);
}