Commit d1eb223d authored by 潘栩锋's avatar 潘栩锋 🚴

添加 RangeStruct.cs,这个 Range 的 struct 版本。 BorderSearch 的 Valid, Border 都是 RangeStruct

parent 0458a2a4
...@@ -87,6 +87,7 @@ ...@@ -87,6 +87,7 @@
<Compile Include="IsErrorAttribute.cs" /> <Compile Include="IsErrorAttribute.cs" />
<Compile Include="PropertiesManager.cs" /> <Compile Include="PropertiesManager.cs" />
<Compile Include="PropertyBinding.cs" /> <Compile Include="PropertyBinding.cs" />
<Compile Include="RangeStruct.cs" />
<Compile Include="Range.cs" /> <Compile Include="Range.cs" />
<Compile Include="RangeF.cs" /> <Compile Include="RangeF.cs" />
<Compile Include="RList.cs" /> <Compile Include="RList.cs" />
......
...@@ -17,12 +17,12 @@ namespace Misc ...@@ -17,12 +17,12 @@ namespace Misc
/// <summary> /// <summary>
/// 开始 /// 开始
/// </summary> /// </summary>
public int Begin { get; set; } public int Begin { get; set; } = Misc.MyBase.NULL_VALUE;
/// <summary> /// <summary>
/// 结束 /// 结束
/// </summary> /// </summary>
public int End { get; set; } public int End { get; set; } = Misc.MyBase.NULL_VALUE;
#endregion #endregion
#region just get property #region just get property
...@@ -76,9 +76,21 @@ namespace Misc ...@@ -76,9 +76,21 @@ namespace Misc
{ {
Misc.SaveToXmlHepler.Regist(typeof(Range)); Misc.SaveToXmlHepler.Regist(typeof(Range));
} }
/// <summary>
///
/// </summary>
/// <returns></returns>
public RangeStruct ToStruct()
{
return new RangeStruct(Begin, End);
}
public Range(RangeStruct rangeStruct) {
Begin = rangeStruct.Begin;
End = rangeStruct.End;
}
/// <summary> /// <summary>
/// 使用无效值初始化 /// 使用无效值初始化
/// </summary> /// </summary>
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Collections;
namespace Misc
{
/// <summary>
/// int范围类,
/// </summary>
public struct RangeStruct
{
#region property
/// <summary>
/// 开始
/// </summary>
public int Begin { get; set; }
/// <summary>
/// 结束
/// </summary>
public int End { get; set; }
#endregion
#region just get property
/// <summary>
/// 本范围 中心位置
/// </summary>
[JsonIgnore]
public int Mid
{
get
{
if (Misc.MyBase.ISVALIDATA(Begin))
return (End + Begin) / 2;
else
return Misc.MyBase.NULL_VALUE;
}
}
/// <summary>
/// 本范围宽度
/// </summary>
[JsonIgnore]
public int Width
{
get
{
if (IsValid)
return End - Begin + 1;
else
return Misc.MyBase.NULL_VALUE;
}
}
/// <summary>
/// 是否有效
/// </summary>
[JsonIgnore]
public bool IsValid
{
get
{
if (Misc.MyBase.ISVALIDATA(Begin) && Misc.MyBase.ISVALIDATA(End))
return true;
else
return false;
}
}
#endregion
public RangeStruct(int begin, int end) {
Begin = begin;
End = end;
}
#region methods
/// <summary>
/// 范围 r 在 本范围内
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public bool Contain(RangeStruct r)
{
if ((Begin <= r.Begin) && (r.End <= End))
return true;
else
return false;
}
/// <summary>
/// 点 p 在 本范围内
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public bool Contain(int p)
{
if ((Begin <= p) && (p <= End))
return true;
else
return false;
}
/// <summary>
/// 无效值
/// </summary>
public static RangeStruct InvalidValue
{
get {
return new RangeStruct(Misc.MyBase.NULL_VALUE, Misc.MyBase.NULL_VALUE);
}
}
/// <summary>
/// 范围并集
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static RangeStruct operator |(RangeStruct a, RangeStruct b)
{
return new RangeStruct()
{
Begin = (a.Begin < b.Begin) ? a.Begin : b.Begin,
End = (a.End > b.End) ? a.End : b.End
};
}
/// <summary>
/// 范围交集
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static RangeStruct operator &(RangeStruct a, RangeStruct b)
{
RangeStruct r = new RangeStruct()
{
Begin = (a.Begin > b.Begin) ? a.Begin : b.Begin,
End = (a.End < b.End) ? a.End : b.End
};
if (r.End < r.Begin)
{
return new RangeStruct();
}
else
{
return r;
}
}
/// <summary>
/// 范围 a小于b
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator <(RangeStruct a, RangeStruct b)
{
if (a.End < b.Begin)//[a.b,a.e] [b.b,b.e]
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 范围 a大于b
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator >(RangeStruct a, RangeStruct b)
{
if (b.End < (a.Begin))// [b.b,b.e][a.b,a.e]
{
return true;
}
else
{
return false;
}
}
public static bool operator <(int p, RangeStruct r)
{
if (p < r.Begin)
{
return true;
}
else
{
return false;
}
}
public static bool operator >(int p, RangeStruct r)
{
if (p > r.End)
{
return true;
}
else
{
return false;
}
}
public static bool operator <(RangeStruct r, int p)
{
if (r.End < p)
{
return true;
}
else
{
return false;
}
}
public static bool operator >(RangeStruct r, int p)
{
if (r.Begin > p)
{
return true;
}
else
{
return false;
}
}
public static RangeStruct operator +(RangeStruct a, int b)
{
return new RangeStruct()
{
Begin = a.Begin + b,
End = a.Begin + b
};
}
public static RangeStruct operator -(RangeStruct a, int b)
{
return new RangeStruct()
{
Begin = a.Begin - b,
End = a.Begin - b
};
}
/// <summary>
/// 是否有交集
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public bool HasIntersection(RangeStruct r)
{
if (Contain(r.Begin))
return true;
if (Contain(r.End))
return true;
if (r.Contain(Begin))
return true;
if (r.Contain(End))
return true;
return false;
}
/// <summary>
/// 可以合并
/// </summary>
/// <returns></returns>
public bool CanUnion(RangeStruct r)
{
if (HasIntersection(r))
return true;
if (End == (r.Begin - 1))
return true;
if (Begin == (r.End + 1))
return true;
return false;
}
#endregion
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (IsValid)
return $"[{Begin},{End}]={Width}";
else
return "invalid";
}
}
/// <summary>
/// Range 辅助类
/// </summary>
public static class EListRangeStruct
{
/// <summary>
/// rlist 必须是由小到大排列
/// </summary>
/// <param name="rlist"></param>
/// <param name="r"></param>
public static void Union(this List<RangeStruct> rlist, RangeStruct r)
{
for (int i = 0; i < rlist.Count(); i++)
{
RangeStruct r_dest = rlist[i];
if (r < r_dest)//在 r_dest 前面
{
rlist.Insert(i, r);
return;//完成
}
else if (r.CanUnion(r_dest)) //r 与 r_dest 有交集
{
//r 与 r_dest 合体
r = r | r_dest;
rlist.RemoveAt(i);
i--;
//还需要继续判断
}
}
//r 在 rlist 的后面
rlist.Add(r);
}
/// <summary>
/// 联合
/// </summary>
/// <param name="rlist1"></param>
/// <param name="rlist2"></param>
public static void Union(this List<RangeStruct> rlist1, List<RangeStruct> rlist2)
{
for (int i = 0; i < rlist2.Count(); i++)
{
rlist1.Union(rlist2[i]);
}
}
}
}
...@@ -272,7 +272,10 @@ namespace FLY.Thick.Base.UI ...@@ -272,7 +272,10 @@ namespace FLY.Thick.Base.UI
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.Enable), this, nameof(Enable)); Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.Enable), this, nameof(Enable));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.IsBreakDetect),this, nameof(IsBreakDetect)); Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.IsBreakDetect),this, nameof(IsBreakDetect));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.Valid),this, nameof(Valid)); Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.Valid), ()=> {
Valid = new Range(BorderSearchService.Valid);
});
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempADBySet),this, nameof(TempADBySet)); Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempADBySet),this, nameof(TempADBySet));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempAD),this, nameof(TempAD)); Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempAD),this, nameof(TempAD));
Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempRange),this, nameof(TempRange)); Misc.BindingOperations.SetBinding(BorderSearchService, nameof(BorderSearchService.TempRange),this, nameof(TempRange));
...@@ -345,7 +348,7 @@ namespace FLY.Thick.Base.UI ...@@ -345,7 +348,7 @@ namespace FLY.Thick.Base.UI
BorderSearchService.Enable = Enable; BorderSearchService.Enable = Enable;
BorderSearchService.IsBreakDetect = IsBreakDetect; BorderSearchService.IsBreakDetect = IsBreakDetect;
BorderSearchService.Valid = Valid; BorderSearchService.Valid = Valid.ToStruct();
BorderSearchService.TempADBySet = TempADBySet; BorderSearchService.TempADBySet = TempADBySet;
BorderSearchService.TempAD = TempAD; BorderSearchService.TempAD = TempAD;
BorderSearchService.TempRange = TempRange; BorderSearchService.TempRange = TempRange;
......
...@@ -96,11 +96,10 @@ namespace FLY.Thick.Base.UI ...@@ -96,11 +96,10 @@ namespace FLY.Thick.Base.UI
{ {
update(); update();
} }
}; else if (e.PropertyName == nameof(borderSearch.Valid))
{
borderSearch.Valid.PropertyChanged += (s, e) => update();
{ }
update();
}; };
DataBindAll(); DataBindAll();
......
...@@ -40,14 +40,14 @@ namespace FLY.Thick.Base.Client ...@@ -40,14 +40,14 @@ namespace FLY.Thick.Base.Client
/// </summary> /// </summary>
public bool IsBreakDetect { get; set; } = true; public bool IsBreakDetect { get; set; } = true;
[PropertyChanged.DoNotCheckEquality]
public Range Valid { get; set; } = new Range(); public RangeStruct Valid { get; set; }
[PropertyChanged.DoNotCheckEquality]
public Range Border_Backw { get; set; } = new Range(); public Range Border_Backw { get; set; } = new Range();
[PropertyChanged.DoNotCheckEquality]
public Range Border_Forw { get; set; } = new Range(); public Range Border_Forw { get; set; } = new Range();
[PropertyChanged.DoNotCheckEquality]
public Range Border { get; set; } = new Range(); public RangeStruct Border { get; set; }
/// <summary> /// <summary>
......
...@@ -25,7 +25,7 @@ namespace FLY.Thick.Base.IService ...@@ -25,7 +25,7 @@ namespace FLY.Thick.Base.IService
/// <summary> /// <summary>
/// 有限范围 /// 有限范围
/// </summary> /// </summary>
Range Valid { get; set; } RangeStruct Valid { get; set; }
/// <summary> /// <summary>
/// 手动设置温修AD值 /// 手动设置温修AD值
...@@ -82,7 +82,7 @@ namespace FLY.Thick.Base.IService ...@@ -82,7 +82,7 @@ namespace FLY.Thick.Base.IService
/// <summary> /// <summary>
/// 边界 /// 边界
/// </summary> /// </summary>
Range Border { get; } RangeStruct Border { get; }
/// <summary> /// <summary>
/// 当前膜宽,测量出来的,单位是 脉冲 /// 当前膜宽,测量出来的,单位是 脉冲
/// </summary> /// </summary>
......
...@@ -141,12 +141,11 @@ namespace FLY.Thick.Base.Server ...@@ -141,12 +141,11 @@ namespace FLY.Thick.Base.Server
if (borders.All(b => b.value.IsValid)) if (borders.All(b => b.value.IsValid))
{ {
Border.Begin = (int)borders.Average(b => b.real.Begin); Border = new RangeStruct((int)borders.Average(b => b.real.Begin), (int)borders.Average(b => b.real.End));
Border.End = (int)borders.Average(b => b.real.End);
} }
else if (border.value.IsValid) else if (border.value.IsValid)
{ {
Border.Copy(border.real); Border = border.real.ToStruct();
} }
getViewReponse.border = border.real; getViewReponse.border = border.real;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment