VidExtendedData.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace fis.Vid
  6. {
  7. /// <summary>
  8. /// vid extended data
  9. /// </summary>
  10. public class VidExtendedData
  11. {
  12. /// <summary>
  13. /// version tag
  14. /// </summary>
  15. public static VidTag VersionTag => new VidTag("Vid", "Version");
  16. /// <summary>
  17. /// version num
  18. /// </summary>
  19. public static VidIntegerValueElement VersionValue => new VidIntegerValueElement(1);
  20. /// <summary>
  21. /// vid data dict
  22. /// </summary>
  23. public Dictionary<VidTag, IVidValue> Data { get; set; }
  24. public VidExtendedData(Dictionary<VidTag, IVidValue> data)
  25. {
  26. Data = data;
  27. Add(VersionTag, VersionValue);
  28. }
  29. /// <summary>
  30. /// Add item
  31. /// </summary>
  32. /// <param name="key">key</param>
  33. /// <param name="value">value</param>
  34. public void Add(VidTag key, IVidValue value)
  35. {
  36. Data.Add(key, value);
  37. }
  38. /// <summary>
  39. /// Object to bytes.
  40. /// </summary>
  41. /// <returns></returns>
  42. public byte[] ToBytes()
  43. {
  44. using (var stream = new MemoryStream())
  45. {
  46. var writer = new VinnoStreamWriter(stream);
  47. writer.WriteInt(Data.Count);
  48. for (int cursor = 0; cursor < Data.Count; cursor++)
  49. {
  50. VidTag key = Data.ElementAt(cursor).Key;
  51. var value = Data.ElementAt(cursor).Value;
  52. var keys = $"{key.Group},{key.Element},{value.Type}";
  53. writer.WriteString(keys);
  54. var values = $"{value.GetValue()}";
  55. writer.WriteString(values);
  56. }
  57. return stream.ToArray();
  58. }
  59. }
  60. /// <summary>
  61. /// Parse from bytes to object.
  62. /// </summary>
  63. /// <param name="data"></param>
  64. /// <returns></returns>
  65. public static VidExtendedData FromBytes(byte[] data)
  66. {
  67. try
  68. {
  69. if (data != null && data.Any())
  70. {
  71. using (var stream = new MemoryStream(data))
  72. {
  73. stream.Position = 0;
  74. var reader = new VinnoStreamReader(stream);
  75. var count = reader.ReadInt();
  76. if (count > 0 && count <= data.Length)
  77. {
  78. var vidExtendedData = new VidExtendedData(new Dictionary<VidTag, IVidValue>());
  79. for (int cursor = 0; cursor < count; cursor++)
  80. {
  81. var key = reader.ReadString();
  82. var keys = key.Split(',');
  83. if (keys.Length != 3)
  84. {
  85. vidExtendedData = null;
  86. break;
  87. }
  88. var vidTag = new VidTag(keys[0], keys[1]);
  89. var value = reader.ReadString();
  90. var valueType = (ValueType) Enum.Parse(typeof(ValueType), keys[2]);
  91. IVidValue vidValue = null;
  92. switch (valueType)
  93. {
  94. case ValueType.String:
  95. vidValue = new VidStringValueElement(value);
  96. break;
  97. case ValueType.Double:
  98. vidValue = new VidDoubleValueElement(double.Parse(value));
  99. break;
  100. case ValueType.Float:
  101. vidValue = new VidFloatValueElement(float.Parse(value));
  102. break;
  103. case ValueType.Integer:
  104. vidValue = new VidIntegerValueElement(int.Parse(value));
  105. break;
  106. case ValueType.Long:
  107. vidValue = new VidLongValueElement(long.Parse(value));
  108. break;
  109. case ValueType.Uint:
  110. vidValue = new VidUintValueElement(uint.Parse(value));
  111. break;
  112. case ValueType.Ulong:
  113. vidValue = new VidUlongValueElement(ulong.Parse(value));
  114. break;
  115. case ValueType.Ushort:
  116. vidValue = new VidUshortValueElement(ushort.Parse(value));
  117. break;
  118. case ValueType.Byte:
  119. vidValue = new VidByteValueElement(byte.Parse(value));
  120. break;
  121. case ValueType.Short:
  122. vidValue = new VidShortValueElement(short.Parse(value));
  123. break;
  124. }
  125. vidExtendedData?.Add(vidTag, vidValue);
  126. }
  127. return vidExtendedData;
  128. }
  129. }
  130. }
  131. }
  132. catch (Exception e)
  133. {
  134. }
  135. return null;
  136. }
  137. }
  138. /// <summary>
  139. /// This is a const dictionary for tissue description which used in FLYINSONO
  140. /// Any questions for this dictionary, please contact the FLYINSONO team to confirm
  141. /// Be cautious about its reference, wrong reference will result in bugs here.
  142. /// </summary>
  143. public static class FISTissueCategory
  144. {
  145. /// <summary>
  146. /// This region is defined for old code, which already realized through old ways,not from this category
  147. /// If you want to do some refactor to suit current concept, please confirm that with FLYINSONO team, and the FLYINSONO team will make it work
  148. /// </summary>
  149. #region
  150. /// <summary>
  151. /// The carotid group name to create the <see cref="VidTag"/> type
  152. /// </summary>
  153. public const string CarotidVidTagGroupName = "Carotid";
  154. /// <summary>
  155. /// The carotid element name for position to create the <see cref="VidTag"/> type
  156. /// </summary>
  157. public const string CarotidVidTagElementPostionName = "Position";
  158. /// <summary>
  159. /// The left carotid value to create a string element <see cref="VidStringValueElement"/>
  160. /// </summary>
  161. public const string LeftCarotidName = "Left";
  162. /// <summary>
  163. /// The right carotid value to create a string element <see cref="VidStringValueElement"/>
  164. /// </summary>
  165. public const string RightCarotidName = "Right";
  166. /// <summary>
  167. /// The 'top to bottom' carotid value to create a string element <see cref="VidStringValueElement"/>
  168. /// </summary>
  169. public const string TopToBottomCarotidName = "TopToBottom";
  170. /// <summary>
  171. /// The 'bottom to top' carotid value to create a string element <see cref="VidStringValueElement"/>
  172. /// </summary>
  173. public const string BottomToTopCarotidName = "BottomToTop";
  174. /// <summary>
  175. /// The breast group name to create the <see cref="VidTag"/> type
  176. /// </summary>
  177. public const string BreastVidTagVIDGroupName = "Breast";
  178. /// <summary>
  179. /// The breast element name for position to create the <see cref="VidTag"/> type
  180. /// </summary>
  181. public const string BreastVidTagElementPostionName = "Position";
  182. /// <summary>
  183. /// The left breast position value to create a string element <see cref="VidStringValueElement"/>
  184. /// </summary>
  185. public const string LeftBreastName = "Left";
  186. /// <summary>
  187. /// The right breast position value to create a string element <see cref="VidStringValueElement"/>
  188. /// </summary>
  189. public const string RightBreastName = "Right";
  190. /// <summary>
  191. /// The liver group name to create the <see cref="VidTag"/> type
  192. /// </summary>
  193. public const string LiverVidTagVIDGroupName = "Liver";
  194. /// <summary>
  195. /// The liver element name for position to create the <see cref="VidTag"/> type
  196. /// </summary>
  197. public const string LiverVidTagElementPostionName = "Position";
  198. /// <summary>
  199. /// The left lobe liver position value to create a string element <see cref="VidStringValueElement"/>
  200. /// </summary>
  201. public const string LeftLobeName = "LeftLobeName";
  202. /// <summary>
  203. /// The right lobe under liver position value to create a string element <see cref="VidStringValueElement"/>
  204. /// </summary>
  205. public const string RightLobeOfUnderName = "RightLobeOfUnder";
  206. /// <summary>
  207. /// The right lobe inter liver position value to create a string element <see cref="VidStringValueElement"/>
  208. /// </summary>
  209. public const string RightLobeOfInterName = "RightLobeOfInter";
  210. /// <summary>
  211. /// The breast element name for quadrant to create the <see cref="VidTag"/> type
  212. /// </summary>
  213. public const string BreastVidTagElementQuadrantName = "Quadrant";
  214. /// <summary>
  215. /// The breast quadrant 'outer upper' value to create a string element <see cref="VidStringValueElement"/>
  216. /// </summary>
  217. public const string BreastOuterUpperName = "OuterUpper";
  218. /// <summary>
  219. /// The breast quadrant 'inner upper' value to create a string element <see cref="VidStringValueElement"/>
  220. /// </summary>
  221. public const string BreastInnerUpperName = "InnerUpper";
  222. /// <summary>
  223. /// The breast quadrant 'outer lower' value to create a string element <see cref="VidStringValueElement"/>
  224. /// </summary>
  225. public const string BreastOuterLowerName = "OuterLower";
  226. /// <summary>
  227. /// The breast quadrant 'inner lower' value to create a string element <see cref="VidStringValueElement"/>
  228. /// </summary>
  229. public const string BreastInnerLowerName = "InnerLower";
  230. #endregion
  231. /// <summary>
  232. /// This region is defined for new coming tissue information, if you are adding the new ones, just reference code hear.
  233. /// </summary>
  234. #region
  235. /// <summary>
  236. /// The thyroid group name to create the <see cref="VidTag"/> type
  237. /// </summary>
  238. public const string ThyroidVIDGroupName = "Thyroid";
  239. /// <summary>
  240. /// The thyroid element name for position to create the <see cref="VidTag"/> type
  241. /// </summary>
  242. public const string ThyroidVidTagElementPostionName = "Position";
  243. /// <summary>
  244. /// The left thyroid value to create a string element <see cref="VidStringValueElement"/>
  245. /// </summary>
  246. public const string LeftThyroidName = "LeftThyroid";
  247. /// <summary>
  248. /// The middle thyroid value to create a string element <see cref="VidStringValueElement"/>
  249. /// </summary>
  250. public const string MiddleThyroidName = "MiddleThyroid";
  251. /// <summary>
  252. /// The right thyroid value to create a string element <see cref="VidStringValueElement"/>
  253. /// </summary>
  254. public const string RightThyroidName = "RightThyroid";
  255. #endregion
  256. public const string Position = "Position";
  257. }
  258. /// <summary>
  259. /// vid tag
  260. /// </summary>
  261. public class VidTag
  262. {
  263. public VidTag(string group, string element)
  264. {
  265. Group = group;
  266. Element = element;
  267. }
  268. /// <summary>
  269. /// vid group num
  270. /// </summary>
  271. public string Group { get; set; }
  272. /// <summary>
  273. /// vid element num
  274. /// </summary>
  275. public string Element { get; set; }
  276. }
  277. /// <summary>
  278. /// interface vid value
  279. /// </summary>
  280. public interface IVidValue
  281. {
  282. ValueType Type { get; }
  283. object GetValue();
  284. }
  285. /// <summary>
  286. /// vid value type
  287. /// </summary>
  288. public enum ValueType
  289. {
  290. String,
  291. Integer,
  292. Double,
  293. Float,
  294. Uint,
  295. Short,
  296. Ushort,
  297. Long,
  298. Ulong,
  299. Byte
  300. }
  301. /// <summary>
  302. /// abstract vid element
  303. /// </summary>
  304. /// <typeparam name="T"></typeparam>
  305. public abstract class VidValueElement<T> : IVidValue
  306. {
  307. public T Value { get; protected set; }
  308. protected VidValueElement(T value)
  309. {
  310. }
  311. public abstract ValueType Type { get; }
  312. public object GetValue()
  313. {
  314. return Value;
  315. }
  316. }
  317. /// <summary>
  318. /// Integer element for vid
  319. /// </summary>
  320. public class VidIntegerValueElement : VidValueElement<int>
  321. {
  322. public VidIntegerValueElement(int value) : base(value)
  323. {
  324. Value = value;
  325. }
  326. public override ValueType Type => ValueType.Integer;
  327. }
  328. /// <summary>
  329. /// string element for vid
  330. /// </summary>
  331. public class VidStringValueElement : VidValueElement<string>
  332. {
  333. public override ValueType Type => ValueType.String;
  334. public VidStringValueElement(string value) : base(value)
  335. {
  336. Value = value;
  337. }
  338. }
  339. /// <summary>
  340. /// double element for vid
  341. /// </summary>
  342. public class VidDoubleValueElement : VidValueElement<double>
  343. {
  344. public override ValueType Type => ValueType.Double;
  345. public VidDoubleValueElement(double value) : base(value)
  346. {
  347. Value = value;
  348. }
  349. }
  350. /// <summary>
  351. /// float element for vid
  352. /// </summary>
  353. public class VidFloatValueElement : VidValueElement<float>
  354. {
  355. public override ValueType Type => ValueType.Float;
  356. public VidFloatValueElement(float value) : base(value)
  357. {
  358. Value = value;
  359. }
  360. }
  361. /// <summary>
  362. /// Uint element for vid
  363. /// </summary>
  364. public class VidUintValueElement : VidValueElement<uint>
  365. {
  366. public override ValueType Type => ValueType.Uint;
  367. public VidUintValueElement(uint value) : base(value)
  368. {
  369. Value = value;
  370. }
  371. }
  372. /// <summary>
  373. /// short element for vid
  374. /// </summary>
  375. public class VidShortValueElement : VidValueElement<short>
  376. {
  377. public override ValueType Type => ValueType.Short;
  378. public VidShortValueElement(short value) : base(value)
  379. {
  380. Value = value;
  381. }
  382. }
  383. /// <summary>
  384. /// ushort element for vid
  385. /// </summary
  386. public class VidUshortValueElement : VidValueElement<ushort>
  387. {
  388. public override ValueType Type => ValueType.Ushort;
  389. public VidUshortValueElement(ushort value) : base(value)
  390. {
  391. Value = value;
  392. }
  393. }
  394. /// <summary>
  395. /// long element for vid
  396. /// </summary
  397. public class VidLongValueElement : VidValueElement<long>
  398. {
  399. public override ValueType Type => ValueType.Long;
  400. public VidLongValueElement(long value) : base(value)
  401. {
  402. Value = value;
  403. }
  404. }
  405. /// <summary>
  406. /// ulong element for vid
  407. /// </summary
  408. public class VidUlongValueElement : VidValueElement<ulong>
  409. {
  410. public override ValueType Type => ValueType.Ulong;
  411. public VidUlongValueElement(ulong value) : base(value)
  412. {
  413. Value = value;
  414. }
  415. }
  416. /// <summary>
  417. /// byte element for vid
  418. /// </summary
  419. public class VidByteValueElement : VidValueElement<byte>
  420. {
  421. public override ValueType Type => ValueType.Byte;
  422. public VidByteValueElement(byte value) : base(value)
  423. {
  424. Value = value;
  425. }
  426. }
  427. }