/
EdgeInfo.cs
82 lines (69 loc) · 2.51 KB
/
EdgeInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Drawing;
using System.Text.Json.Serialization;
namespace Common.PieceInfo;
public class EdgeInfo
{
//public required Point[] OriginPoints { get; set; }
//public required PointF OriginCorner1 { get; set; }
//public required PointF OriginCorner2 { get; set; }
public required PointF[] NormalizedPoints { get; set; }
//public required PointF NormalizedCorner1 { get; set; }
//public required PointF NormalizedCorner2 { get; set; }
public required float Length { get; set; }
public required EdgeType Type { get; set; }
[JsonIgnore] public bool IsLine => Type == EdgeType.Line;
[JsonIgnore] public bool IsHead => Type == EdgeType.Head;
[JsonIgnore] public bool IsHole => Type == EdgeType.Hole;
private Dictionary<EdgeType, EdgeType> AllowTypeMap = new()
{
{ EdgeType.Head, EdgeType.Hole },
{ EdgeType.Hole, EdgeType.Head },
{ EdgeType.Line, EdgeType.None },
};
public (bool Result, float Value) Test(EdgeInfo other)
{
var allowType = AllowTypeMap[Type];
if (other.Type != allowType)
{
return (false, 0);
}
if (!CheckLength(Length, other.Length))
{
// 길이가 3% 이내로 차이나면 true
return (false, 0);
}
var shortPoints = NormalizedPoints.Length < other.NormalizedPoints.Length ? NormalizedPoints : other.NormalizedPoints;
var longPoints = NormalizedPoints.Length < other.NormalizedPoints.Length ? other.NormalizedPoints : NormalizedPoints;
float distanceSum = shortPoints
.Sum(left => longPoints.Min(right => Distance(left, right)));
var result = distanceSum / shortPoints.Length;
if (result < 5)
{
// 5 이내로 차이나면 true
return (true, result);
}
else
{
return (false, 0);
}
bool CheckLength(float a, float b)
{
// 3% 이내로 차이나면 true
var min = Math.Min(a, b);
var max = Math.Max(a, b);
return (max - min) / min < 0.03;
}
float Distance(PointF a, PointF b)
{
var x = a.X - b.X;
var y = a.Y - b.Y;
return (float)Math.Sqrt(x * x + y * y);
}
}
public float DiffLength(EdgeInfo other)
{
var shortLength = Math.Min(Length, other.Length);
var longLength = Math.Max(Length, other.Length);
return (longLength - shortLength) / shortLength;
}
}