-
Notifications
You must be signed in to change notification settings - Fork 12
/
WallsLOD200.cs
175 lines (151 loc) · 6.68 KB
/
WallsLOD200.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using Elements;
using Elements.Geometry;
namespace WallsLOD200
{
public static partial class WallsLOD200
{
public static double tolerance = 0.0001;
/// <summary>
/// The WallsLOD200 function.
/// </summary>
/// <param name="model">The input model.</param>
/// <param name="input">The arguments to the execution.</param>
/// <returns>A WallsLOD200Outputs instance containing computed results and the model with any new elements.</returns>
public static WallsLOD200Outputs Execute(Dictionary<string, Model> inputModels, WallsLOD200Inputs input)
{
Random random = new Random(21);
var output = new WallsLOD200Outputs();
if (inputModels.TryGetValue("Walls", out var wallsModel))
{
var walls = wallsModel.AllElementsOfType<StandardWall>();
var wallGroups = walls.GroupBy(w => w.AdditionalProperties["Level"] ?? w.Transform.Origin.Z);
var levels = new List<Level>();
if (inputModels.TryGetValue("Levels", out var levelsModel))
{
levels = levelsModel.AllElementsOfType<Level>().ToList();
}
foreach (var group in wallGroups)
{
var level = levels.FirstOrDefault(l => l.Id.ToString() == group.Key.ToString()) ?? new Level(0, 3, null);
var lines = UnifyLines(group.ToList().Select(g => g.CenterLine).ToList());
var roundedZLines = lines.Select(l =>
{
var roundedStart = new Vector3(l.Start.X, l.Start.Y, Math.Round(l.Start.Z, 5));
var roundedEnd = new Vector3(l.End.X, l.End.Y, Math.Round(l.End.Z, 5));
return new Line(roundedStart, roundedEnd);
}
);
var newWalls = roundedZLines.Select(mc => new StandardWall(mc, 0.1, level.Height ?? 3, random.NextMaterial(), new Transform().Moved(0, 0, level.Elevation)));
output.Model.AddElements(newWalls);
}
}
return output;
}
public static List<Line> UnifyLines(List<Line> lines)
{
// Remove duplicate lines based on their hash codes
List<Line> dedupedlines = RemoveDuplicateLines(lines);
// Merge collinear lines that are touching or overlapping
List<Line> mergedLines = MergeCollinearLines(dedupedlines);
return mergedLines;
}
private static List<Line> RemoveDuplicateLines(List<Line> lines)
{
HashSet<Line> uniqueLines = new(new LineEqualityComparer());
foreach (Line line in lines)
{
uniqueLines.Add(line);
}
return uniqueLines.ToList();
}
static List<List<Line>> GroupLinesByCollinearity(List<Line> lines)
{
Dictionary<int, Line> collinearGroups = new Dictionary<int, Line>();
List<List<Line>> lineGroups = new List<List<Line>>();
int groupId = 0;
foreach (var line in lines)
{
bool addedToGroup = false;
foreach (var kvp in collinearGroups)
{
if (line.IsCollinear(kvp.Value))
{
// Add line to existing group
lineGroups[kvp.Key].Add(line);
addedToGroup = true;
break;
}
}
if (!addedToGroup)
{
// Create new group
collinearGroups.Add(groupId, line);
lineGroups.Add(new List<Line>() { line });
groupId++;
}
}
return lineGroups;
}
private static List<Line> MergeCollinearLines(List<Line> lines)
{
var groupedLines = GroupLinesByCollinearity(lines);
List<Line> merged = new List<Line>();
foreach (var group in groupedLines)
{
List<Line> mergedLines = new List<Line>(group);
bool linesMerged;
do
{
linesMerged = false;
for (int i = 0; i < mergedLines.Count; i++)
{
Line line = mergedLines[i];
for (int j = i + 1; j < mergedLines.Count; j++)
{
Line otherLine = mergedLines[j];
if (line.TryGetOverlap(otherLine, out var overlap) || line.DistanceTo(otherLine) < tolerance)
{
// project lines with points within tolerance of eachother but further than epsilon
if (LinesWithinTolerance(line, otherLine, tolerance))
{
otherLine = otherLine.Projected(line);
}
// Merge collinear lines
Line mergedLine = line.MergedCollinearLine(otherLine);
// Update the list with the merged line
mergedLines.RemoveAt(j);
mergedLines[i] = mergedLine;
linesMerged = true;
break; // Exit the inner loop as we have merged the lines
}
}
if (linesMerged)
break; // Exit the outer loop to restart the merging process
}
} while (linesMerged);
merged.AddRange(mergedLines);
}
return merged;
}
public static bool LinesWithinTolerance(Line line1, Line line2, double tolerance)
{
// Calculate distances between all point pairs
var distances = new List<double>()
{
line1.Start.DistanceTo(line2.Start),
line1.Start.DistanceTo(line2.End),
line1.End.DistanceTo(line2.Start),
line1.End.DistanceTo(line2.End)
};
// Check if any distance is within the tolerance but larger than double.Epsilon
foreach (var distance in distances)
{
if (distance > double.Epsilon && distance <= tolerance)
{
return true;
}
}
return false;
}
}
}