-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridLayoutScorer.cs
More file actions
96 lines (86 loc) · 3.16 KB
/
GridLayoutScorer.cs
File metadata and controls
96 lines (86 loc) · 3.16 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
namespace WindowThumbWall;
internal readonly record struct GridCandidateMetrics(
double ThumbnailWidth,
double ThumbnailHeight,
double Deadspace,
double Distortion);
internal static class GridLayoutScorer
{
internal static (int rows, int cols) ChooseGrid(
int count,
double wallWidth,
double wallHeight,
IReadOnlyList<double> aspectRatios,
double titleBarHeight,
double horizontalChrome,
double verticalChrome,
double deadspaceWeight,
double distortionWeight)
{
if (count <= 0)
return (1, 1);
double bestScore = double.PositiveInfinity;
double bestDeadspace = double.PositiveInfinity;
int bestRows = 1;
int bestCols = count;
for (int rows = 1; rows <= count; rows++)
{
int cols = (int)Math.Ceiling((double)count / rows);
GridCandidateMetrics candidate = EvaluateCandidate(
count,
rows,
cols,
wallWidth,
wallHeight,
aspectRatios,
titleBarHeight,
horizontalChrome,
verticalChrome);
double score = deadspaceWeight * candidate.Deadspace + distortionWeight * candidate.Distortion;
bool isBetter = score < bestScore - 0.000001;
bool tieButLessDeadspace = Math.Abs(score - bestScore) <= 0.000001 && candidate.Deadspace < bestDeadspace;
if (isBetter || tieButLessDeadspace)
{
bestScore = score;
bestDeadspace = candidate.Deadspace;
bestRows = rows;
bestCols = cols;
}
}
return (bestRows, bestCols);
}
internal static GridCandidateMetrics EvaluateCandidate(
int count,
int rows,
int cols,
double wallWidth,
double wallHeight,
IReadOnlyList<double> aspectRatios,
double titleBarHeight,
double horizontalChrome,
double verticalChrome)
{
double cellWidth = wallWidth / Math.Max(cols, 1);
double cellHeight = wallHeight / Math.Max(rows, 1);
double thumbnailWidth = Math.Max(cellWidth - horizontalChrome, 0);
double thumbnailHeight = Math.Max(cellHeight - verticalChrome - titleBarHeight, 0);
double coverage =
wallWidth <= 0 || wallHeight <= 0
? 0
: (thumbnailWidth * thumbnailHeight * count) / (wallWidth * wallHeight);
double deadspace = 1.0 - Math.Clamp(coverage, 0.0, 1.0);
double distortion = 0;
if (aspectRatios.Count > 0)
{
double thumbnailAspect = Math.Max(thumbnailWidth, 0.01) / Math.Max(thumbnailHeight, 0.01);
for (int i = 0; i < aspectRatios.Count; i++)
{
double ratio = thumbnailAspect / Math.Max(aspectRatios[i], 0.01);
double delta = Math.Log(ratio);
distortion += delta * delta;
}
distortion /= aspectRatios.Count;
}
return new GridCandidateMetrics(thumbnailWidth, thumbnailHeight, deadspace, distortion);
}
}