using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace fis.Vid
{
///
/// vid extended data
///
public class VidExtendedData
{
///
/// version tag
///
public static VidTag VersionTag => new VidTag("Vid", "Version");
///
/// version num
///
public static VidIntegerValueElement VersionValue => new VidIntegerValueElement(1);
///
/// vid data dict
///
public Dictionary Data { get; set; }
public VidExtendedData(Dictionary data)
{
Data = data;
Add(VersionTag, VersionValue);
}
///
/// Add item
///
/// key
/// value
public void Add(VidTag key, IVidValue value)
{
Data.Add(key, value);
}
///
/// Object to bytes.
///
///
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();
}
}
///
/// Parse from bytes to object.
///
///
///
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());
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;
}
}
///
/// 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.
///
public static class FISTissueCategory
{
///
/// 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
///
#region
///
/// The carotid group name to create the type
///
public const string CarotidVidTagGroupName = "Carotid";
///
/// The carotid element name for position to create the type
///
public const string CarotidVidTagElementPostionName = "Position";
///
/// The left carotid value to create a string element
///
public const string LeftCarotidName = "Left";
///
/// The right carotid value to create a string element
///
public const string RightCarotidName = "Right";
///
/// The 'top to bottom' carotid value to create a string element
///
public const string TopToBottomCarotidName = "TopToBottom";
///
/// The 'bottom to top' carotid value to create a string element
///
public const string BottomToTopCarotidName = "BottomToTop";
///
/// The breast group name to create the type
///
public const string BreastVidTagVIDGroupName = "Breast";
///
/// The breast element name for position to create the type
///
public const string BreastVidTagElementPostionName = "Position";
///
/// The left breast position value to create a string element
///
public const string LeftBreastName = "Left";
///
/// The right breast position value to create a string element
///
public const string RightBreastName = "Right";
///
/// The liver group name to create the type
///
public const string LiverVidTagVIDGroupName = "Liver";
///
/// The liver element name for position to create the type
///
public const string LiverVidTagElementPostionName = "Position";
///
/// The left lobe liver position value to create a string element
///
public const string LeftLobeName = "LeftLobeName";
///
/// The right lobe under liver position value to create a string element
///
public const string RightLobeOfUnderName = "RightLobeOfUnder";
///
/// The right lobe inter liver position value to create a string element
///
public const string RightLobeOfInterName = "RightLobeOfInter";
///
/// The breast element name for quadrant to create the type
///
public const string BreastVidTagElementQuadrantName = "Quadrant";
///
/// The breast quadrant 'outer upper' value to create a string element
///
public const string BreastOuterUpperName = "OuterUpper";
///
/// The breast quadrant 'inner upper' value to create a string element
///
public const string BreastInnerUpperName = "InnerUpper";
///
/// The breast quadrant 'outer lower' value to create a string element
///
public const string BreastOuterLowerName = "OuterLower";
///
/// The breast quadrant 'inner lower' value to create a string element
///
public const string BreastInnerLowerName = "InnerLower";
#endregion
///
/// This region is defined for new coming tissue information, if you are adding the new ones, just reference code hear.
///
#region
///
/// The thyroid group name to create the type
///
public const string ThyroidVIDGroupName = "Thyroid";
///
/// The thyroid element name for position to create the type
///
public const string ThyroidVidTagElementPostionName = "Position";
///
/// The left thyroid value to create a string element
///
public const string LeftThyroidName = "LeftThyroid";
///
/// The middle thyroid value to create a string element
///
public const string MiddleThyroidName = "MiddleThyroid";
///
/// The right thyroid value to create a string element
///
public const string RightThyroidName = "RightThyroid";
#endregion
public const string Position = "Position";
}
///
/// vid tag
///
public class VidTag
{
public VidTag(string group, string element)
{
Group = group;
Element = element;
}
///
/// vid group num
///
public string Group { get; set; }
///
/// vid element num
///
public string Element { get; set; }
}
///
/// interface vid value
///
public interface IVidValue
{
ValueType Type { get; }
object GetValue();
}
///
/// vid value type
///
public enum ValueType
{
String,
Integer,
Double,
Float,
Uint,
Short,
Ushort,
Long,
Ulong,
Byte
}
///
/// abstract vid element
///
///
public abstract class VidValueElement : IVidValue
{
public T Value { get; protected set; }
protected VidValueElement(T value)
{
}
public abstract ValueType Type { get; }
public object GetValue()
{
return Value;
}
}
///
/// Integer element for vid
///
public class VidIntegerValueElement : VidValueElement
{
public VidIntegerValueElement(int value) : base(value)
{
Value = value;
}
public override ValueType Type => ValueType.Integer;
}
///
/// string element for vid
///
public class VidStringValueElement : VidValueElement
{
public override ValueType Type => ValueType.String;
public VidStringValueElement(string value) : base(value)
{
Value = value;
}
}
///
/// double element for vid
///
public class VidDoubleValueElement : VidValueElement
{
public override ValueType Type => ValueType.Double;
public VidDoubleValueElement(double value) : base(value)
{
Value = value;
}
}
///
/// float element for vid
///
public class VidFloatValueElement : VidValueElement
{
public override ValueType Type => ValueType.Float;
public VidFloatValueElement(float value) : base(value)
{
Value = value;
}
}
///
/// Uint element for vid
///
public class VidUintValueElement : VidValueElement
{
public override ValueType Type => ValueType.Uint;
public VidUintValueElement(uint value) : base(value)
{
Value = value;
}
}
///
/// short element for vid
///
public class VidShortValueElement : VidValueElement
{
public override ValueType Type => ValueType.Short;
public VidShortValueElement(short value) : base(value)
{
Value = value;
}
}
///
/// ushort element for vid
///
{
public override ValueType Type => ValueType.Ushort;
public VidUshortValueElement(ushort value) : base(value)
{
Value = value;
}
}
///
/// long element for vid
///
{
public override ValueType Type => ValueType.Long;
public VidLongValueElement(long value) : base(value)
{
Value = value;
}
}
///
/// ulong element for vid
///
{
public override ValueType Type => ValueType.Ulong;
public VidUlongValueElement(ulong value) : base(value)
{
Value = value;
}
}
///
/// byte element for vid
///
{
public override ValueType Type => ValueType.Byte;
public VidByteValueElement(byte value) : base(value)
{
Value = value;
}
}
}