standard_line.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // ignore_for_file: non_constant_identifier_names, constant_identifier_names
  2. import 'dart:typed_data';
  3. import 'package:fis_common/event/event_type.dart';
  4. import 'package:fis_vid_ext/vid_extended_data.dart';
  5. import 'package:fis_vid_ext/vid_tag.dart';
  6. import 'package:fis_vid_ext/vid_value.dart';
  7. import 'pixel_space.dart';
  8. /// 参考线
  9. class StandardLine {
  10. /// Default Standard Line Physical Length (unit cm)
  11. static const double DefaultPhysicsLength = 4.0;
  12. /// 0028,0030 PixelSpacing from dicom
  13. static final VidTag DicomTagPixelSpacing = VidTag("0028", "0030");
  14. /// 0018, 602E Physical Delta Y
  15. static final VidTag DicomTagPhysicalDeltaY = VidTag("0018", "602E");
  16. /// 0018,602C Physical Delta X
  17. static final VidTag DicomTagPhysicalDeltaX = VidTag("0018", "602C");
  18. /// 0018, 6024 Physical Units X
  19. static final VidTag DicomTagPhysicalUnitsX = VidTag("0018", "6024");
  20. /// 0018, 6026 Physical Units Y
  21. static final VidTag DicomTagPhysicalUnitsY = VidTag("0018", "6026");
  22. PixelSpacing _currentPixelSpacing = PixelSpacing();
  23. double _physicsLength = DefaultPhysicsLength;
  24. double _pixelLength = 0.0;
  25. double _perPixelPhysicalLength = 0.0;
  26. /// 参考线物理长度(cm)
  27. double get physicsLength => _physicsLength;
  28. set physicsLength(double val) {
  29. if (val != _physicsLength) {
  30. _physicsLength = val;
  31. _onLineLengthChanged();
  32. }
  33. }
  34. /// 参考线像素长度
  35. double get pixelLength => _pixelLength;
  36. set pixelLength(double val) {
  37. if (val != _pixelLength) {
  38. _pixelLength = val;
  39. _onLineLengthChanged();
  40. }
  41. }
  42. /// 单位像素物理长度(cm)
  43. double get perPixelPhysicalLength => _perPixelPhysicalLength;
  44. /// 当前像素距离尺
  45. PixelSpacing get currentPixelSpacing => _currentPixelSpacing;
  46. set currentPixelSpacing(PixelSpacing val) {
  47. if (val != _currentPixelSpacing) {
  48. _currentPixelSpacing = val;
  49. _onPixelSpacingChanged();
  50. }
  51. }
  52. /// 像素距离尺变化事件
  53. final pixelSpacingChanged = FEventHandler<PixelSpacing?>();
  54. /// 通过vid扩展信息加载像素距离信息
  55. void loadFromVidExtData(Uint8List bytes) {
  56. final extInfo = VidExtendedData.fromBytes(bytes);
  57. if (extInfo == null) {
  58. currentPixelSpacing = PixelSpacing();
  59. return;
  60. }
  61. PixelSpacing pixelSpacing = PixelSpacing();
  62. var pixelSpacingValue = _getVidValueByTag(DicomTagPixelSpacing, extInfo);
  63. if (pixelSpacingValue != null) {
  64. var pixelSpacingArray =
  65. pixelSpacingValue.getValue().toString().split('\\');
  66. if (pixelSpacingArray.length > 1) {
  67. double? pixelSpacingX = double.tryParse(pixelSpacingArray[0]);
  68. if (pixelSpacingX != null) {
  69. pixelSpacing.physicalDeltaX = pixelSpacingX;
  70. }
  71. double? pixelSpacingY = double.tryParse(pixelSpacingArray[1]);
  72. if (pixelSpacingY != null) {
  73. pixelSpacing.physicalDeltaY = pixelSpacingY;
  74. }
  75. }
  76. } else {
  77. pixelSpacing.physicalDeltaX = _getPhysicalDeltaX(extInfo);
  78. pixelSpacing.physicalDeltaY = _getPhysicalDeltaY(extInfo);
  79. }
  80. if (pixelSpacing.physicalDeltaX > 0 &&
  81. pixelSpacing.physicalDeltaX < 1.0 &&
  82. pixelSpacing.physicalDeltaY > 0 &&
  83. pixelSpacing.physicalDeltaY < 1.0) {
  84. currentPixelSpacing = pixelSpacing;
  85. } else {
  86. currentPixelSpacing = PixelSpacing();
  87. }
  88. }
  89. void _onLineLengthChanged() {
  90. final physicalDeltaX = physicsLength * 10 / pixelLength;
  91. final physicalDeltaY = physicalDeltaX;
  92. currentPixelSpacing = PixelSpacing.fill(physicalDeltaX, physicalDeltaY);
  93. }
  94. void _onPixelSpacingChanged() {
  95. _perPixelPhysicalLength =
  96. _getPixelLengthWithUnit(currentPixelSpacing.physicalDeltaX);
  97. }
  98. double _getPixelLengthWithUnit(double length) {
  99. // if (_mearsureUnit == MearsureUnit.cm)
  100. // {
  101. length = length / 10;
  102. // }
  103. return length;
  104. }
  105. IVidValue? _getVidValueByTag(VidTag vidTag, VidExtendedData extendedData) {
  106. var matchKeys = extendedData.data.keys
  107. .where((v) => v.group == vidTag.group && v.element == vidTag.element);
  108. if (matchKeys.isNotEmpty) {
  109. final vidTagKey = matchKeys.first;
  110. var vidValue = extendedData.data[vidTagKey];
  111. return vidValue;
  112. }
  113. return null;
  114. }
  115. double _getPhysicalDeltaX(VidExtendedData extendedData) {
  116. final physicalDeltaXvalue =
  117. _getVidValueByTag(DicomTagPhysicalDeltaX, extendedData);
  118. if (physicalDeltaXvalue != null) {
  119. final physicalUnitsXvalue =
  120. _getVidValueByTag(DicomTagPhysicalUnitsX, extendedData);
  121. if (physicalUnitsXvalue != null) {
  122. var unitsvalue = int.parse(physicalUnitsXvalue.getValue().toString());
  123. if (unitsvalue == 3) {
  124. var physicalDeltaX =
  125. double.parse(physicalDeltaXvalue.getValue().toString()) * 10;
  126. return physicalDeltaX;
  127. }
  128. }
  129. }
  130. return 1.0;
  131. }
  132. double _getPhysicalDeltaY(VidExtendedData extendedData) {
  133. final physicalDeltaYvalue =
  134. _getVidValueByTag(DicomTagPhysicalDeltaY, extendedData);
  135. if (physicalDeltaYvalue != null) {
  136. final physicalUnitsYvalue =
  137. _getVidValueByTag(DicomTagPhysicalUnitsY, extendedData);
  138. if (physicalUnitsYvalue != null) {
  139. var unitsvalue = int.parse(physicalUnitsYvalue.getValue().toString());
  140. if (unitsvalue == 3) {
  141. var physicalDeltaX =
  142. double.parse(physicalDeltaYvalue.getValue().toString()) * 10;
  143. return physicalDeltaX;
  144. }
  145. }
  146. }
  147. return 1.0;
  148. }
  149. }