Skip to content

Commit ea038f4

Browse files
authored
Merge pull request #236 from vykrum/updates
Updates
2 parents 3c6b7af + 800663d commit ea038f4

1 file changed

Lines changed: 52 additions & 48 deletions

File tree

src/Hywe.Core/Goxel.fs

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -79,46 +79,51 @@ module Goxel =
7979

8080
/// <summary> Removes aliasing/sawtooth artifacts from a sequence of hex coordinates. </summary>
8181
let removeSawtooth (sqn : Sqn) (arr : (int*int)[]) : (int*int)[] =
82-
if arr.Length = 0 then [||] else
83-
let (primary, secondary) =
84-
match sqn with
85-
| Vertical -> (fst, snd)
86-
| Horizontal -> (snd, fst)
82+
match arr.Length with
83+
| 0 -> [||]
84+
| n ->
85+
let (primary, secondary) =
86+
match sqn with
87+
| Vertical -> (fst, snd)
88+
| Horizontal -> (snd, fst)
8789

88-
let result = ResizeArray<int*int>()
89-
let mutable i = 0
90-
let n = arr.Length
91-
while i < n do
92-
let mutable j = i
93-
while j + 1 < n && abs(primary arr.[j+1] - primary arr.[j]) = 2 do
94-
j <- j + 1
95-
96-
let groupLen = j - i + 1
97-
if groupLen > 3 then
98-
let mutable isOscillating = true
99-
for k = i to j - 1 do
100-
if abs(secondary arr.[k+1] - secondary arr.[k]) <> 1 then
101-
isOscillating <- false
102-
103-
if isOscillating then
104-
let f = arr.[i]
105-
let l = arr.[j]
106-
let low = min (secondary f) (secondary l)
107-
match sqn with
108-
| Vertical ->
109-
result.Add((low, snd f))
110-
result.Add((low, snd l))
111-
| Horizontal ->
112-
result.Add((fst f, low))
113-
result.Add((fst l, low))
114-
else
115-
for k = i to j do
116-
result.Add(arr.[k])
117-
else
118-
for k = i to j do
119-
result.Add(arr.[k])
120-
i <- j + 1
121-
result.ToArray()
90+
let rec processGroups i acc =
91+
match i < n with
92+
| false -> acc |> List.rev |> Array.concat
93+
| true ->
94+
let rec findGroupEnd j =
95+
match j + 1 < n && abs(primary arr.[j+1] - primary arr.[j]) = 2 with
96+
| true -> findGroupEnd (j + 1)
97+
| false -> j
98+
99+
let j = findGroupEnd i
100+
let groupLen = j - i + 1
101+
102+
match groupLen > 3 with
103+
| false ->
104+
processGroups (j + 1) (arr.[i..j] :: acc)
105+
| true ->
106+
let rec checkOscillating k =
107+
match k < j with
108+
| false -> true
109+
| true ->
110+
match abs(secondary arr.[k+1] - secondary arr.[k]) <> 1 with
111+
| true -> false
112+
| false -> checkOscillating (k + 1)
113+
114+
match checkOscillating i with
115+
| false -> processGroups (j + 1) (arr.[i..j] :: acc)
116+
| true ->
117+
let f = arr.[i]
118+
let l = arr.[j]
119+
let low = min (secondary f) (secondary l)
120+
let newPts =
121+
match sqn with
122+
| Vertical -> [| (low, snd f); (low, snd l) |]
123+
| Horizontal -> [| (fst f, low); (fst l, low) |]
124+
processGroups (j + 1) (newPts :: acc)
125+
126+
processGroups 0 []
122127

123128
/// <summary> Deduplicates sequential points. </summary>
124129
let dedupeSequential (pts: (int * int)[]) =
@@ -163,9 +168,9 @@ module Goxel =
163168

164169
/// <summary> Bounding box of a polygon in grid coordinates. </summary>
165170
let bounds (pts: (int * int)[]) =
166-
if Array.isEmpty pts then
167-
(0, 0, 0, 0)
168-
else
171+
match Array.isEmpty pts with
172+
| true -> (0, 0, 0, 0)
173+
| false ->
169174
let xs = pts |> Array.map fst
170175
let ys = pts |> Array.map snd
171176
(Array.min xs, Array.min ys, Array.max xs, Array.max ys)
@@ -230,18 +235,17 @@ module Goxel =
230235
let vt1 = Array.concat [| [|xx, yy|]; Array.tail vtx |]
231236
let verts = cleanPolygon sqn vt1
232237

233-
verts |> Array.fold (fun (acc: ResizeArray<Hxl>, lastOpt) pt ->
238+
verts |> Array.fold (fun (acc: Hxl list, lastOpt) pt ->
234239
let (ix, iy) = pt
235240
let current = hxlVld sqn (RV(ix, iy, elv))
236241
match lastOpt with
237242
| None ->
238-
acc.Add(current)
239-
(acc, Some current)
243+
(current :: acc, Some current)
240244
| Some last ->
241245
let seg = hxlLin sqn elv last current
242-
acc.AddRange(seg)
243-
(acc, Some (Array.last seg))
244-
) (ResizeArray<Hxl>(), None) |> fst |> (fun ra -> ra.ToArray())
246+
let newAcc = seg |> Array.fold (fun a c -> c :: a) acc
247+
(newAcc, Some (Array.last seg))
248+
) ([], None) |> fst |> List.rev |> List.toArray
245249

246250
// --- Geometry Parsing ---
247251

0 commit comments

Comments
 (0)