VinnoCarotidExtendedData.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace fis.Vid
  4. {
  5. /// <summary>
  6. /// Carotid scan direction
  7. /// </summary>
  8. public enum CarotidDirection
  9. {
  10. TopToBottom,
  11. BottomToTop
  12. }
  13. public enum CarotidType
  14. {
  15. Left,
  16. Right
  17. }
  18. public class VinnoCarotidExtendedData
  19. {
  20. private const string CarotidGroup = "Carotid";
  21. private const string ScanDistanceElement = "ScanDistance";
  22. private const string ScanTypeElement = "ScanType";
  23. private const string ScanDirectionElement = "ScanDirection";
  24. private readonly VidExtendedData _extendedData;
  25. /// <summary>
  26. /// Get CarotidType.
  27. /// </summary>
  28. public CarotidType CarotidType { get; }
  29. /// <summary>
  30. /// Get CarotidDirection
  31. /// </summary>
  32. public CarotidDirection CarotidDirection { get; }
  33. /// <summary>
  34. /// Get ScanDistance.
  35. /// </summary>
  36. public float ScanDistance { get; }
  37. public VinnoCarotidExtendedData(float scanDistance, CarotidType carotidType,CarotidDirection carotidDirection)
  38. {
  39. ScanDistance = scanDistance;
  40. CarotidType = carotidType;
  41. CarotidDirection = carotidDirection;
  42. var dic = new Dictionary<VidTag, IVidValue>
  43. {
  44. {new VidTag(CarotidGroup, ScanDistanceElement), new VidFloatValueElement(scanDistance)},
  45. {new VidTag(CarotidGroup, ScanTypeElement), new VidIntegerValueElement((int)carotidType)},
  46. {new VidTag(CarotidGroup, ScanDirectionElement), new VidIntegerValueElement((int)carotidDirection)}
  47. };
  48. _extendedData = new VidExtendedData(dic);
  49. }
  50. /// <summary>
  51. /// Object to bytes.
  52. /// </summary>
  53. /// <returns></returns>
  54. public byte[] ToBytes()
  55. {
  56. return _extendedData.ToBytes();
  57. }
  58. public static bool IsCarotid(byte[] bytes)
  59. {
  60. if (bytes != null)
  61. {
  62. var extendedData = VidExtendedData.FromBytes(bytes);
  63. if (extendedData != null)
  64. {
  65. var vidTags = extendedData.Data.Keys.Where(k => k.Group == CarotidGroup).ToList();
  66. if (vidTags.Count > 2)
  67. {
  68. return true;
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. /// <summary>
  75. /// Convert from bytes.
  76. /// </summary>
  77. /// <param name="bytes">bytes.</param>
  78. /// <returns>Instance of vinno carotid extended data.</returns>
  79. public static VinnoCarotidExtendedData FromBytes(byte[] bytes)
  80. {
  81. var extendedData = VidExtendedData.FromBytes(bytes);
  82. if (extendedData != null)
  83. {
  84. var vidTags = extendedData.Data.Keys.Where(k => k.Group == CarotidGroup).ToList();
  85. if (vidTags.Count > 2)
  86. {
  87. var scanDistanceTag = vidTags.FirstOrDefault(t => t.Element.Equals(ScanDistanceElement));
  88. var carotidTypeTag = vidTags.FirstOrDefault(t => t.Element.Equals(ScanTypeElement));
  89. var carotidDirectionTag = vidTags.FirstOrDefault(t => t.Element.Equals(ScanDirectionElement));
  90. if (scanDistanceTag == null || carotidTypeTag == null || carotidDirectionTag==null)
  91. {
  92. return null;
  93. }
  94. var scanDistance =extendedData.Data[scanDistanceTag];
  95. var carotidType = extendedData.Data[carotidTypeTag];
  96. var carotidDirection = extendedData.Data[carotidDirectionTag];
  97. if (scanDistance is VidFloatValueElement distance &&
  98. carotidType is VidIntegerValueElement scanType &&
  99. carotidDirection is VidIntegerValueElement direction)
  100. {
  101. return new VinnoCarotidExtendedData(distance.Value, (CarotidType)scanType.Value,(CarotidDirection)direction.Value);
  102. }
  103. }
  104. }
  105. return null;
  106. }
  107. }
  108. }