location.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import 'dart:ui';
  2. import 'dart:math' as math;
  3. import 'package:fis_measure/interfaces/date_types/point.dart';
  4. import 'package:fis_measure/interfaces/date_types/rect_region.dart';
  5. import 'package:fis_measure/interfaces/date_types/vector.dart';
  6. import 'package:fis_measure/interfaces/enums/items.dart';
  7. import 'package:fis_measure/interfaces/process/items/item.dart';
  8. import 'package:fis_measure/interfaces/process/items/item_metas.dart';
  9. import 'package:fis_measure/interfaces/process/items/types.dart';
  10. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  11. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  12. import 'package:fis_measure/process/calcuators/velocity.dart';
  13. import 'package:fis_measure/process/visual/tissue_area.dart';
  14. import 'package:fis_measure/utils/canvas.dart';
  15. import 'package:get/get.dart';
  16. import 'package:vid/us/vid_us_mode.dart';
  17. import 'package:vid/us/vid_us_probe.dart';
  18. import '../calcuators/depth.dart';
  19. import '../items/item.dart';
  20. import '../items/item_feature.dart';
  21. import '../physical_coordinates/convex_tissue.dart';
  22. class Location extends MeasureItem<LocationFeature> {
  23. Location(ItemMeta meta, IMeasureItem? parent) : super(meta, parent);
  24. @override
  25. bool get finishAfterUnactive => false;
  26. @override
  27. bool onExecuteMouse(PointInfo args) {
  28. if (state == ItemStates.finished || state == ItemStates.waiting) {
  29. if (args.pointType == PointInfoType.mouseMove) {
  30. handleMouseMoveWhileFinish(args);
  31. }
  32. }
  33. if (state == ItemStates.running) {
  34. if (args.pointType == PointInfoType.mouseUp) return false;
  35. feature?.point = args;
  36. doCalculate();
  37. if (args.pointType == PointInfoType.mouseDown) {
  38. doFeatureFinish();
  39. }
  40. }
  41. return true;
  42. }
  43. String mapPointInfoType(PointInfoType type) {
  44. switch (type) {
  45. case PointInfoType.mouseUp:
  46. return '⬆️';
  47. case PointInfoType.mouseDown:
  48. return '⬇️';
  49. case PointInfoType.mouseMove:
  50. return '⭐';
  51. case PointInfoType.touchUp:
  52. return '⬆️';
  53. case PointInfoType.touchDown:
  54. return '⬇️';
  55. case PointInfoType.touchMove:
  56. return '⭐';
  57. }
  58. }
  59. @override
  60. bool onExecuteTouch(PointInfo args) {
  61. if (state == ItemStates.finished || state == ItemStates.waiting) {
  62. if (args.pointType == PointInfoType.touchDown) {
  63. handleMouseMoveWhileFinish(args);
  64. }
  65. }
  66. if (state == ItemStates.running) {
  67. if (args.pointType == PointInfoType.touchDown) {
  68. feature?.point = args;
  69. doCalculate();
  70. }
  71. if (args.pointType == PointInfoType.touchMove) {
  72. args.addOffset(0, -0.2);
  73. if (isMoveTargetOutOfRange(args)) return true;
  74. feature?.point = args;
  75. doCalculate();
  76. }
  77. if (args.pointType == PointInfoType.touchUp) {
  78. doFeatureFinish();
  79. }
  80. }
  81. return true;
  82. }
  83. void handleMouseMoveWhileFinish(PointInfo args) {
  84. // TODO: 判断是否当前 area
  85. // 转换为 Area 逻辑位置
  86. final point = args.toAreaLogicPoint();
  87. handleTissue(args, point);
  88. handleTissueTM(args.hostVisualArea!.mode.modeType, point);
  89. if (args.hostVisualArea != null) {
  90. feature!.hostVisualArea = args.hostVisualArea;
  91. }
  92. state = ItemStates.running;
  93. }
  94. void handleTissueTM(VidUsModeType mode, DPoint point) {
  95. if (mode == VidUsModeType.TissueTM || mode == VidUsModeType.Doppler) {
  96. feature = LocationFeature(this, point);
  97. }
  98. }
  99. void handleTissue(PointInfo args, DPoint point) {
  100. final mode = args.hostVisualArea!.mode.modeType;
  101. if (mode == VidUsModeType.Tissue || mode == VidUsModeType.Flow) {
  102. final isProbeConvex = (args.hostVisualArea! as TissueArea).isConvex;
  103. if (isProbeConvex) {
  104. feature = TissueConvexLocationFeature(this, point);
  105. } else {
  106. feature = LocationFeature(this, point);
  107. }
  108. }
  109. }
  110. static Location createTissueConvexDepth(
  111. ItemMeta meta, [
  112. IMeasureItem? parent,
  113. ]) {
  114. Location location = Location(meta, parent);
  115. location.calculator = TissueConvexDepthCal(location);
  116. return location;
  117. }
  118. static Location createTissueDepth(
  119. ItemMeta meta, [
  120. IMeasureItem? parent,
  121. ]) {
  122. final area = Get.find<IApplication>().currentVisualArea;
  123. final isProbeConvex = (area as TissueArea).isConvex;
  124. final fn =
  125. isProbeConvex ? createTissueConvexDepth : createTissueNormalDepth;
  126. return fn(meta, parent);
  127. }
  128. static Location createTissueNormalDepth(
  129. ItemMeta meta, [
  130. IMeasureItem? parent,
  131. ]) {
  132. Location location = Location(meta, parent);
  133. location.calculator = TissueDepthCal(location);
  134. return location;
  135. }
  136. static Location createTissueTMDepth(
  137. ItemMeta meta, [
  138. IMeasureItem? parent,
  139. ]) {
  140. Location location = Location(meta, parent);
  141. location.calculator = MDepthCal(location);
  142. return location;
  143. }
  144. static Location createVelocity(
  145. ItemMeta meta, [
  146. IMeasureItem? parent,
  147. ]) {
  148. Location location = Location(meta, parent);
  149. location.calculator = VelocityCal(location);
  150. return location;
  151. }
  152. }
  153. class TissueConvexLocationFeature extends LocationFeature {
  154. TissueConvexLocationFeature(IMeasureItem refItem, DPoint point)
  155. : super(refItem, point);
  156. @override
  157. void paint(Canvas canvas, Size size) {
  158. super.paint(canvas, size);
  159. if (!measureData.measureSystemSetting.showDepthGuideline) return;
  160. final point = this.point.clone();
  161. final layout = hostVisualArea!.displayRegion;
  162. point.addOffset(-layout.left, -layout.top);
  163. // TODO : fix bug
  164. final viewport = hostVisualArea!.viewport!;
  165. final physical = (viewport.physical! as ConvexTissuePhysicalCoordinate);
  166. final region = viewport.region;
  167. final physicalZeroPoint =
  168. DPoint(region.width / 2, physical.zeroY); // 真实尺寸圆心
  169. final physicalPoint = viewport.convert(point);
  170. final physicalStartPoint = _findCrossPointOnInnerDiameter(
  171. physicalZeroPoint,
  172. physicalPoint,
  173. physical.zeroRadius,
  174. viewport.region,
  175. );
  176. // 在内圈内,不处理
  177. if (physicalStartPoint == null) return;
  178. final logicStartPoint = viewport.convertBack(physicalStartPoint);
  179. logicStartPoint.addOffset(layout.left, layout.top);
  180. point.addOffset(layout.left, layout.top);
  181. Offset viewStartPoint = convert2ViewPoint(size, logicStartPoint).toOffset();
  182. Offset viewPoint = convert2ViewPoint(size, point).toOffset();
  183. if (viewStartPoint.dy < 0) {
  184. // TODO: 处理起点在画布区间外的情况,区间外不绘制
  185. return;
  186. }
  187. canvas.drawDashLine(viewStartPoint, viewPoint, 1, 10, paintLinePan);
  188. }
  189. /// 找到直线(圆心到当前点)和扇形内圈的交叉点
  190. DPoint? _findCrossPointOnInnerDiameter(
  191. DPoint zero,
  192. DPoint point,
  193. double zeroRadius,
  194. RectRegion region,
  195. ) {
  196. final centerOfX = region.width / 2;
  197. final pointWidthOnX = centerOfX - point.x;
  198. final pointHeightOnY = point.y - zero.y;
  199. final pointRadius =
  200. math.sqrt(math.pow(pointWidthOnX, 2) + math.pow(pointHeightOnY, 2));
  201. final ratio = zeroRadius / pointRadius;
  202. if (ratio > 1) return null;
  203. double width = pointWidthOnX * ratio;
  204. double height = pointHeightOnY * ratio;
  205. double x = centerOfX - width;
  206. double y = height + zero.y;
  207. return DPoint(x, y);
  208. }
  209. }
  210. class LocationFeature extends MeasureItemFeature {
  211. LocationFeature(IMeasureItem refItem, DPoint point) : super(refItem) {
  212. innerPoints.add(point);
  213. }
  214. DPoint get point => innerPoints[0];
  215. set point(DPoint value) => innerPoints[0] = value;
  216. @override
  217. void paint(Canvas canvas, Size size) {
  218. drawId(canvas, size);
  219. final viewPoint = convert2ViewPoint(size, point).toOffset();
  220. drawVertex(canvas, viewPoint, isActive);
  221. }
  222. }