123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace fis.Vid
- {
- /// <summary>
- /// vid extended data
- /// </summary>
- public class VidExtendedData
- {
- /// <summary>
- /// version tag
- /// </summary>
- public static VidTag VersionTag => new VidTag("Vid", "Version");
- /// <summary>
- /// version num
- /// </summary>
- public static VidIntegerValueElement VersionValue => new VidIntegerValueElement(1);
- /// <summary>
- /// vid data dict
- /// </summary>
- public Dictionary<VidTag, IVidValue> Data { get; set; }
- public VidExtendedData(Dictionary<VidTag, IVidValue> data)
- {
- Data = data;
- Add(VersionTag, VersionValue);
- }
- /// <summary>
- /// Add item
- /// </summary>
- /// <param name="key">key</param>
- /// <param name="value">value</param>
- public void Add(VidTag key, IVidValue value)
- {
- Data.Add(key, value);
- }
- /// <summary>
- /// Object to bytes.
- /// </summary>
- /// <returns></returns>
- public byte[] ToBytes()
- {
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- writer.WriteInt(Data.Count);
- for (int cursor = 0; cursor < Data.Count; cursor++)
- {
- VidTag key = Data.ElementAt(cursor).Key;
- var value = Data.ElementAt(cursor).Value;
- var keys = $"{key.Group},{key.Element},{value.Type}";
- writer.WriteString(keys);
- var values = $"{value.GetValue()}";
- writer.WriteString(values);
- }
- return stream.ToArray();
- }
- }
- /// <summary>
- /// Parse from bytes to object.
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static VidExtendedData FromBytes(byte[] data)
- {
- try
- {
- if (data != null && data.Any())
- {
- using (var stream = new MemoryStream(data))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var count = reader.ReadInt();
- if (count > 0 && count <= data.Length)
- {
- var vidExtendedData = new VidExtendedData(new Dictionary<VidTag, IVidValue>());
- for (int cursor = 0; cursor < count; cursor++)
- {
- var key = reader.ReadString();
- var keys = key.Split(',');
- if (keys.Length != 3)
- {
- vidExtendedData = null;
- break;
- }
- var vidTag = new VidTag(keys[0], keys[1]);
- var value = reader.ReadString();
- var valueType = (ValueType) Enum.Parse(typeof(ValueType), keys[2]);
- IVidValue vidValue = null;
- switch (valueType)
- {
- case ValueType.String:
- vidValue = new VidStringValueElement(value);
- break;
- case ValueType.Double:
- vidValue = new VidDoubleValueElement(double.Parse(value));
- break;
- case ValueType.Float:
- vidValue = new VidFloatValueElement(float.Parse(value));
- break;
- case ValueType.Integer:
- vidValue = new VidIntegerValueElement(int.Parse(value));
- break;
- case ValueType.Long:
- vidValue = new VidLongValueElement(long.Parse(value));
- break;
- case ValueType.Uint:
- vidValue = new VidUintValueElement(uint.Parse(value));
- break;
- case ValueType.Ulong:
- vidValue = new VidUlongValueElement(ulong.Parse(value));
- break;
- case ValueType.Ushort:
- vidValue = new VidUshortValueElement(ushort.Parse(value));
- break;
- case ValueType.Byte:
- vidValue = new VidByteValueElement(byte.Parse(value));
- break;
- case ValueType.Short:
- vidValue = new VidShortValueElement(short.Parse(value));
- break;
- }
- vidExtendedData?.Add(vidTag, vidValue);
- }
- return vidExtendedData;
- }
- }
- }
- }
- catch (Exception e)
- {
- }
- return null;
- }
- }
- /// <summary>
- /// This is a const dictionary for tissue description which used in FLYINSONO
- /// Any questions for this dictionary, please contact the FLYINSONO team to confirm
- /// Be cautious about its reference, wrong reference will result in bugs here.
- /// </summary>
- public static class FISTissueCategory
- {
- /// <summary>
- /// This region is defined for old code, which already realized through old ways,not from this category
- /// 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
- /// </summary>
- #region
- /// <summary>
- /// The carotid group name to create the <see cref="VidTag"/> type
- /// </summary>
- public const string CarotidVidTagGroupName = "Carotid";
- /// <summary>
- /// The carotid element name for position to create the <see cref="VidTag"/> type
- /// </summary>
- public const string CarotidVidTagElementPostionName = "Position";
- /// <summary>
- /// The left carotid value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string LeftCarotidName = "Left";
- /// <summary>
- /// The right carotid value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string RightCarotidName = "Right";
- /// <summary>
- /// The 'top to bottom' carotid value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string TopToBottomCarotidName = "TopToBottom";
- /// <summary>
- /// The 'bottom to top' carotid value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string BottomToTopCarotidName = "BottomToTop";
- /// <summary>
- /// The breast group name to create the <see cref="VidTag"/> type
- /// </summary>
- public const string BreastVidTagVIDGroupName = "Breast";
- /// <summary>
- /// The breast element name for position to create the <see cref="VidTag"/> type
- /// </summary>
- public const string BreastVidTagElementPostionName = "Position";
- /// <summary>
- /// The left breast position value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string LeftBreastName = "Left";
- /// <summary>
- /// The right breast position value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string RightBreastName = "Right";
- /// <summary>
- /// The liver group name to create the <see cref="VidTag"/> type
- /// </summary>
- public const string LiverVidTagVIDGroupName = "Liver";
- /// <summary>
- /// The liver element name for position to create the <see cref="VidTag"/> type
- /// </summary>
- public const string LiverVidTagElementPostionName = "Position";
- /// <summary>
- /// The left lobe liver position value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string LeftLobeName = "LeftLobeName";
- /// <summary>
- /// The right lobe under liver position value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string RightLobeOfUnderName = "RightLobeOfUnder";
- /// <summary>
- /// The right lobe inter liver position value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string RightLobeOfInterName = "RightLobeOfInter";
- /// <summary>
- /// The breast element name for quadrant to create the <see cref="VidTag"/> type
- /// </summary>
- public const string BreastVidTagElementQuadrantName = "Quadrant";
- /// <summary>
- /// The breast quadrant 'outer upper' value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string BreastOuterUpperName = "OuterUpper";
- /// <summary>
- /// The breast quadrant 'inner upper' value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string BreastInnerUpperName = "InnerUpper";
- /// <summary>
- /// The breast quadrant 'outer lower' value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string BreastOuterLowerName = "OuterLower";
- /// <summary>
- /// The breast quadrant 'inner lower' value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string BreastInnerLowerName = "InnerLower";
- #endregion
- /// <summary>
- /// This region is defined for new coming tissue information, if you are adding the new ones, just reference code hear.
- /// </summary>
- #region
- /// <summary>
- /// The thyroid group name to create the <see cref="VidTag"/> type
- /// </summary>
- public const string ThyroidVIDGroupName = "Thyroid";
- /// <summary>
- /// The thyroid element name for position to create the <see cref="VidTag"/> type
- /// </summary>
- public const string ThyroidVidTagElementPostionName = "Position";
- /// <summary>
- /// The left thyroid value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string LeftThyroidName = "LeftThyroid";
- /// <summary>
- /// The middle thyroid value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string MiddleThyroidName = "MiddleThyroid";
- /// <summary>
- /// The right thyroid value to create a string element <see cref="VidStringValueElement"/>
- /// </summary>
- public const string RightThyroidName = "RightThyroid";
- #endregion
- public const string Position = "Position";
- }
- /// <summary>
- /// vid tag
- /// </summary>
- public class VidTag
- {
- public VidTag(string group, string element)
- {
- Group = group;
- Element = element;
- }
- /// <summary>
- /// vid group num
- /// </summary>
- public string Group { get; set; }
- /// <summary>
- /// vid element num
- /// </summary>
- public string Element { get; set; }
- }
- /// <summary>
- /// interface vid value
- /// </summary>
- public interface IVidValue
- {
- ValueType Type { get; }
- object GetValue();
- }
- /// <summary>
- /// vid value type
- /// </summary>
- public enum ValueType
- {
- String,
- Integer,
- Double,
- Float,
- Uint,
- Short,
- Ushort,
- Long,
- Ulong,
- Byte
- }
- /// <summary>
- /// abstract vid element
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public abstract class VidValueElement<T> : IVidValue
- {
- public T Value { get; protected set; }
- protected VidValueElement(T value)
- {
- }
- public abstract ValueType Type { get; }
- public object GetValue()
- {
- return Value;
- }
- }
- /// <summary>
- /// Integer element for vid
- /// </summary>
- public class VidIntegerValueElement : VidValueElement<int>
- {
- public VidIntegerValueElement(int value) : base(value)
- {
- Value = value;
- }
- public override ValueType Type => ValueType.Integer;
- }
- /// <summary>
- /// string element for vid
- /// </summary>
- public class VidStringValueElement : VidValueElement<string>
- {
- public override ValueType Type => ValueType.String;
- public VidStringValueElement(string value) : base(value)
- {
- Value = value;
- }
- }
- /// <summary>
- /// double element for vid
- /// </summary>
- public class VidDoubleValueElement : VidValueElement<double>
- {
- public override ValueType Type => ValueType.Double;
- public VidDoubleValueElement(double value) : base(value)
- {
- Value = value;
- }
- }
- /// <summary>
- /// float element for vid
- /// </summary>
- public class VidFloatValueElement : VidValueElement<float>
- {
- public override ValueType Type => ValueType.Float;
- public VidFloatValueElement(float value) : base(value)
- {
- Value = value;
- }
- }
- /// <summary>
- /// Uint element for vid
- /// </summary>
- public class VidUintValueElement : VidValueElement<uint>
- {
- public override ValueType Type => ValueType.Uint;
- public VidUintValueElement(uint value) : base(value)
- {
- Value = value;
- }
- }
- /// <summary>
- /// short element for vid
- /// </summary>
- public class VidShortValueElement : VidValueElement<short>
- {
- public override ValueType Type => ValueType.Short;
- public VidShortValueElement(short value) : base(value)
- {
- Value = value;
- }
- }
- /// <summary>
- /// ushort element for vid
- /// </summary
- public class VidUshortValueElement : VidValueElement<ushort>
- {
- public override ValueType Type => ValueType.Ushort;
- public VidUshortValueElement(ushort value) : base(value)
- {
- Value = value;
- }
- }
- /// <summary>
- /// long element for vid
- /// </summary
- public class VidLongValueElement : VidValueElement<long>
- {
- public override ValueType Type => ValueType.Long;
- public VidLongValueElement(long value) : base(value)
- {
- Value = value;
- }
- }
- /// <summary>
- /// ulong element for vid
- /// </summary
- public class VidUlongValueElement : VidValueElement<ulong>
- {
- public override ValueType Type => ValueType.Ulong;
- public VidUlongValueElement(ulong value) : base(value)
- {
- Value = value;
- }
- }
- /// <summary>
- /// byte element for vid
- /// </summary
- public class VidByteValueElement : VidValueElement<byte>
- {
- public override ValueType Type => ValueType.Byte;
- public VidByteValueElement(byte value) : base(value)
- {
- Value = value;
- }
- }
- }
|