Skip to content

Commit 9d408c2

Browse files
sync
1 parent 22cf94c commit 9d408c2

File tree

4 files changed

+247
-29
lines changed

4 files changed

+247
-29
lines changed

src/Aardvark.Geometry.Quadtree/Builder.fs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ module Builder =
111111
result |> InMemoryInner
112112

113113
let rec private build'' (config : BuildConfig) (rootCell : Cell2d) (patches : LayerSet[]) =
114+
115+
let minSampleExponent = patches |> Seq.map (fun p -> p.SampleExponent) |> Seq.min
114116

115117
if config.Verbose then
116-
printfn "[build''] rootCell = %A, %d patches" rootCell patches.Length
117-
118-
let minSampleExponent = patches |> Seq.map (fun p -> p.SampleExponent) |> Seq.min
118+
printfn "[build''] ******** BEGIN ******** %d patches; rootCell = %A; minSampleExponent = %d " patches.Length rootCell minSampleExponent
119+
let mutable i = 0
120+
for patch in patches do
121+
printfn "[build''] | patches[%d] : e=%d origin=%A size=%A" i patch.SampleExponent patch.SampleWindow.Min patch.SampleWindow.Size
122+
i <- i + 1
119123

120124
match patches.Length with
121125

@@ -156,7 +160,7 @@ module Builder =
156160
(fun () -> sprintf "Expected config.SplitLimitPowerOfTwo to be non-negative, but found %d." config.SplitLimitPowerOfTwo)
157161
"95c43529-9649-42d6-9b47-c6f8fb6da301"
158162

159-
let tileSize = 2 <<< config.SplitLimitPowerOfTwo
163+
let tileSize = 1 <<< config.SplitLimitPowerOfTwo
160164
let rootBounds = getBoundsForExponent minSampleExponent rootCell
161165

162166
if rootBounds.Size.X <= tileSize && rootBounds.Size.Y <= tileSize then
@@ -167,41 +171,49 @@ module Builder =
167171
// -> resulting layer has finest resolution of all patches
168172
// -> coarser layers will be subsampled
169173
// -> a mask indicates non-defined samples
170-
174+
171175
if config.Verbose then
172176
printfn "[build''] MERGE %d patches; because reached split limit" n
173-
for patch in patches do
174-
printfn "[build''] | e=%d origin=%A size=%A" patch.SampleExponent patch.SampleWindow.Min patch.SampleWindow.Size
175-
176-
failwith "TODO"
177177

178-
let bbWindow = patches |> Seq.map (fun patch -> patch.SampleWindow) |> Box2l
179-
180178
//if debugOutput then
179+
// let bbWindow = patches |> Seq.map (fun patch -> patch.SampleWindow) |> Box2l
181180
// for patch in patches do
182181
// printfn "[DEBUG] %A (exact %A)" (patch.SampleWindow - rootBounds.Min) (bbWindow - rootBounds.Min)
183182

184183
// adjust root cell for split limit
185-
let requiredRootCellSplitLimit = minSampleExponent + config.SplitLimitPowerOfTwo
186-
184+
let requiredRootCellExponent = minSampleExponent + config.SplitLimitPowerOfTwo
185+
186+
if config.Verbose then
187+
printfn "[build''] | requiredRootCellExponent %d = minSampleExponent %d + config.SplitLimitPowerOfTwo %d" requiredRootCellExponent minSampleExponent config.SplitLimitPowerOfTwo
188+
187189
let mutable rootCell = rootCell
190+
191+
if requiredRootCellExponent <> rootCell.Exponent then
188192

189-
if requiredRootCellSplitLimit <> rootCell.Exponent then
190-
191-
invariantm (rootCell.Exponent < requiredRootCellSplitLimit)
192-
(fun () -> sprintf "Expected root cell exponent %d to be smaller than requiredRootCellSplitLimit %d." rootCell.Exponent requiredRootCellSplitLimit)
193+
invariantm (rootCell.Exponent < requiredRootCellExponent)
194+
(fun () -> sprintf "Expected root cell exponent %d to be smaller than requiredRootCellExponent %d." rootCell.Exponent requiredRootCellExponent)
193195
"4911adf3-7b87-4234-9bcc-bc3076df846e"
194196

195197
if config.Verbose then
196-
printfn "[build''] must adjust root cell %A exponent to %d" rootCell requiredRootCellSplitLimit
198+
printfn "[build''] | must adjust root cell %A exponent to %d ..." rootCell requiredRootCellExponent
197199

198-
while rootCell.Exponent < requiredRootCellSplitLimit do rootCell <- rootCell.Parent
200+
while rootCell.Exponent < requiredRootCellExponent do rootCell <- rootCell.Parent
199201

200202
if config.Verbose then
201-
printfn "[build''] adjusted root cell is %A" rootCell
203+
printfn "[build''] | adjusted root cell is %A" rootCell
204+
205+
else
206+
207+
if config.Verbose then
208+
printfn "[build''] | root cell %A already has requiredRootCellExponent %d" rootCell requiredRootCellExponent
202209

203210
let merged = LayerSet.Merge patches
204-
let qnode = QNode(Guid.NewGuid(), ebb, rootCell, config.SplitLimitPowerOfTwo, merged)
211+
212+
let flattended = LayerSet.Flatten patches
213+
214+
failwith "TODO"
215+
216+
let qnode = QNode(Guid.NewGuid(), ebb, rootCell, config.SplitLimitPowerOfTwo, flattended)
205217

206218
if config.Verbose then
207219
printfn "[build''] CREATED QNode with split limit = %d" qnode.SplitLimitExponent

src/Aardvark.Geometry.Quadtree/Defs.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Defs =
2222
let NodeLeaf = def "c74fad23-1211-4073-94e5-54b778e0d295" "Quadtree.NodeLeaf" "A quadtree leaf node. DurableMapAligned16." Durable.Primitives.DurableMapAligned16
2323
let NodeInner = def "1f7baa27-5bcc-420f-89a3-714b65d93a2d" "Quadtree.NodeInner" "A quadtree inner node. DurableMapAligned16." Durable.Primitives.DurableMapAligned16
2424
let NodeMerge = def "2d80c73e-ed3e-442a-a631-4f570ff838fd" "Quadtree.NodeMerge" "A quadtree merge node. DurableMapAligned16." Durable.Primitives.DurableMapAligned16
25+
let NodeMultiMerge = def "26416ce5-891d-4e7e-b29b-1e23a6a99382" "Quadtree.NodeMultiMerge" "A quadtree multi-merge node. DurableMapAligned16." Durable.Primitives.DurableMapAligned16
2526
let NodeLinked = def "8628aab6-a416-42ab-9192-bae0d5590f4f" "Quadtree.LinkedNode" "A reference (link) to another quadtree node. DurableMapAligned16" Durable.Primitives.DurableMapAligned16
2627

2728
let NodeId = def "e46c4163-dd28-43a4-8254-bc21dc3f766b" "Quadtree.NodeId" "Quadtree. Unique id of a node. Guid." Durable.Primitives.GuidDef

src/Aardvark.Geometry.Quadtree/Layer.fs

Lines changed: 160 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,13 @@ module Layer =
276276
let private mergeTyped<'a when 'a : equality> (undefinedValue : 'a) (layers : Layer<'a>[]) : Layer<'a> =
277277
let def = ensureSameDef layers
278278
if verbose then
279-
printfn "[Layer.Merge] .... def = %s" def.Name
279+
printfn "[Layer.mergeTyped] .... def = %s" def.Name
280280

281281
let e = layers.[0].SampleExponent
282282
if not (layers |> Array.forall (fun l -> l.SampleExponent = e)) then
283283
failwith "Cannot merge layers with different resolutions."
284284
if verbose then
285-
printfn "[Layer.Merge] .... e = %d" e
285+
printfn "[Layer.mergeTyped] .... e = %d" e
286286

287287
let finalWindow = layers |> Seq.map (fun l -> l.SampleWindow) |> Box2l
288288
let finalOrigin = Cell2d(finalWindow.Min, e)
@@ -291,11 +291,11 @@ module Layer =
291291
let finalMask = Array.create<byte> (int finalWindow.Size.X * int finalWindow.Size.Y) 255uy
292292

293293
if verbose then
294-
for l in layers do printfn "[Layer.Merge] .... [%A-%A]" l.SampleMin l.SampleMaxIncl
295-
printfn "[Layer.Merge] .... final mapping"
296-
printfn "[Layer.Merge] .... buffer origin: %A" finalOrigin
297-
printfn "[Layer.Merge] .... buffer size : %A" finalWindow.Size
298-
printfn "[Layer.Merge] .... window: %A" finalWindow
294+
for l in layers do printfn "[Layer.mergeTyped] .... [%A-%A]" l.SampleMin l.SampleMaxIncl
295+
printfn "[Layer.mergeTyped] .... final mapping"
296+
printfn "[Layer.mergeTyped] .... buffer origin: %A" finalOrigin
297+
printfn "[Layer.mergeTyped] .... buffer size : %A" finalWindow.Size
298+
printfn "[Layer.mergeTyped] .... window: %A" finalWindow
299299

300300
let mutable debugCountValues = 0
301301
let mutable debugCountCollisions = 0
@@ -362,11 +362,107 @@ module Layer =
362362

363363
Layer(def, finalData, finalMapping, if coundOccupiedSamples > 0 then Some finalMask else None)
364364

365+
let private flattenTyped<'a when 'a : equality> (undefinedValue : 'a) (layers : Layer<'a>[]) : Layer<'a> =
366+
let def = ensureSameDef layers
367+
if verbose then
368+
printfn "[Layer.flattenTyped] .... def = %s" def.Name
369+
370+
failwith "TODO"
371+
372+
let e = layers.[0].SampleExponent
373+
if not (layers |> Array.forall (fun l -> l.SampleExponent = e)) then
374+
failwith "Cannot flatten layers with different resolutions."
375+
376+
if verbose then
377+
printfn "[Layer.flattenTyped] .... e = %d" e
378+
379+
let finalWindow = layers |> Seq.map (fun l -> l.SampleWindow) |> Box2l
380+
let finalOrigin = Cell2d(finalWindow.Min, e)
381+
let finalMapping = DataMapping(finalOrigin, V2i finalWindow.Size, finalWindow)
382+
let finalData = Array.zeroCreate<'a> (int finalWindow.Size.X * int finalWindow.Size.Y)
383+
let finalMask = Array.create<byte> (int finalWindow.Size.X * int finalWindow.Size.Y) 255uy
384+
385+
if verbose then
386+
for l in layers do printfn "[Layer.flattenTyped] .... [%A-%A]" l.SampleMin l.SampleMaxIncl
387+
printfn "[Layer.flattenTyped] .... final mapping"
388+
printfn "[Layer.flattenTyped] .... buffer origin: %A" finalOrigin
389+
printfn "[Layer.flattenTyped] .... buffer size : %A" finalWindow.Size
390+
printfn "[Layer.flattenTyped] .... window: %A" finalWindow
391+
392+
let mutable debugCountValues = 0
393+
let mutable debugCountCollisions = 0
394+
let debugCollisionSamples = HashSet<int>()
395+
396+
let mutable layerIndex = 0uy
397+
for layer in layers do
398+
let w = layer.Mapping.Window
399+
let xMaxIncl = int w.SizeX - 1
400+
let yMaxIncl = int w.SizeY - 1
401+
for y = 0 to yMaxIncl do
402+
for x = 0 to xMaxIncl do
403+
let c = Cell2d(w.Min.X + int64 x, w.Min.Y + int64 y, e)
404+
let i = finalMapping.GetBufferIndex c
405+
let v = layer.GetSample(Fail, c)
406+
407+
match finalMask[i] with
408+
| 255uy ->
409+
finalData[i] <- v
410+
finalMask[i] <- layerIndex
411+
debugCountValues <- debugCountValues + 1
412+
| _ ->
413+
if v <> finalData[i] then
414+
if finalData[i] = undefinedValue then
415+
if v <> undefinedValue then
416+
finalData[i] <- v
417+
finalMask[i] <- layerIndex
418+
else
419+
()
420+
else
421+
if v <> undefinedValue then
422+
debugCollisionSamples.Add(i) |> ignore
423+
debugCountCollisions <- debugCountCollisions + 1
424+
425+
if verbose then
426+
printfn "[DEBUG][Layer.flattenTyped] COLLISION overwriting value %A from layer %d with value %A from layer %d" finalData[i] finalMask[i] v layerIndex
427+
428+
else
429+
()
430+
431+
432+
433+
434+
layerIndex <- layerIndex + 1uy
435+
436+
437+
// count occupied samples
438+
let coundOccupiedSamples = finalMask |> Array.sumBy (fun x -> if x = 255uy then 1 else 0)
439+
440+
if verbose && debugCountCollisions > 0 then
441+
printfn "[DEBUG][Layer.flattenTyped] debugCountValues = %d" debugCountValues
442+
printfn "[DEBUG][Layer.flattenTyped] debugCountCollisions = %d" debugCountCollisions
443+
printfn "[DEBUG][Layer.flattenTyped] debugCollisionSamples.Count = %d" debugCollisionSamples.Count
444+
printfn "[DEBUG][Layer.flattenTyped] debugCountOccupied.Count = %d / %d ... %5.2f" coundOccupiedSamples finalMask.Length (float coundOccupiedSamples / float finalMask.Length)
445+
446+
// rewrite mask (1 ... occupied, 0 ... undefined)
447+
448+
let countOccupied = finalMask |> Array.filter (fun x -> x <> 255uy) |> Array.length
449+
let countUndefined = finalMask |> Array.filter (fun x -> x = 255uy) |> Array.length
450+
printfn "[OCCUPANCY][e = %d][%A][%A] countOccupied = %d, countUndefined = %d" e finalWindow finalWindow.Size countOccupied countUndefined
451+
452+
for i = 0 to finalMask.Length-1 do
453+
finalMask[i] <- if finalMask[i] = 255uy then 0uy else 1uy
454+
455+
Layer(def, finalData, finalMapping, if coundOccupiedSamples > 0 then Some finalMask else None)
456+
457+
365458
let private toTyped<'a when 'a : equality> (layers : ILayer[]) : Layer<'a>[] =
366459
layers |> Array.map (fun x -> x :?> Layer<'a>)
367460

368461
let private mergeUntyped_<'a when 'a : equality> (undefinedValue : 'a) xs : ILayer =
369462
xs |> toTyped<'a> |> (mergeTyped undefinedValue) :> ILayer
463+
464+
let private flattenUntyped_<'a when 'a : equality> (undefinedValue : 'a) xs : ILayer =
465+
xs |> toTyped<'a> |> (flattenTyped undefinedValue) :> ILayer
370466

371467
/// Merge layers of same type (def).
372468
let Merge (layers : ILayer seq) =
@@ -402,6 +498,40 @@ module Layer =
402498

403499
Some mergedLayers
404500

501+
/// Flatten layers of same type (def).
502+
let Flatten (layers : ILayer seq) =
503+
let ls = layers |> Array.ofSeq
504+
match ls.Length with
505+
| 0 ->
506+
if verbose then printfn "[Layer.Flatten] 0 layers -> None"
507+
None
508+
| 1 ->
509+
if verbose then printfn "[Layer.Flatten] 1 layer -> Some ls.[0]"
510+
Some (ls |> Array.exactlyOne)
511+
| n ->
512+
let distinctDefCount = ls |> Seq.distinctBy (fun l -> l.Def) |> Seq.length
513+
if distinctDefCount <> 1 then failwith "Can only flatten layers of same type (def). Error 2611c741-c739-4480-8162-2a60d5d34a4f."
514+
if verbose then printfn "[Layer.Flatten] %d layers" n
515+
let mergedLayers =
516+
match ls.[0] with
517+
| :? Layer<int> -> ls |> flattenUntyped_<int> Int32.MinValue
518+
| :? Layer<int64> -> ls |> flattenUntyped_<int64> Int64.MinValue
519+
| :? Layer<float> -> ls |> flattenUntyped_<float> nan
520+
| :? Layer<float32> -> ls |> flattenUntyped_<float32> nanf
521+
| :? Layer<V2f> -> ls |> flattenUntyped_<V2f> V2f.NaN
522+
| :? Layer<V2d> -> ls |> flattenUntyped_<V2d> V2d.NaN
523+
| :? Layer<V3f> -> ls |> flattenUntyped_<V3f> V3f.NaN
524+
| :? Layer<V3d> -> ls |> flattenUntyped_<V3d> V3d.NaN
525+
| :? Layer<V4f> -> ls |> flattenUntyped_<V4f> V4f.NaN
526+
| :? Layer<V4d> -> ls |> flattenUntyped_<V4d> V4d.NaN
527+
| :? Layer<C3b> -> ls |> flattenUntyped_<C3b> (C3b(0, 0, 0))
528+
| :? Layer<C4b> -> ls |> flattenUntyped_<C4b> (C4b(0, 0, 0, 0))
529+
| :? Layer<C3f> -> ls |> flattenUntyped_<C3f> (C3f(0, 0, 0))
530+
| :? Layer<C4f> -> ls |> flattenUntyped_<C4f> (C4f(0, 0, 0, 0))
531+
| _ -> failwith <| sprintf "Unsupported layer type %A. Invariant 0d379726-27dd-4063-9175-8f13ac44ad83." ls.[0]
532+
533+
Some mergedLayers
534+
405535
type LayerSet(layers : ILayer[]) =
406536

407537
do
@@ -477,8 +607,9 @@ type LayerSet(layers : ILayer[]) =
477607

478608
module LayerSet =
479609

480-
/// Merges multiple layer sets.
610+
/// Merges multiple layer sets with identical resolution.
481611
/// All layer sets must have identical layers (same number, same semantics, same order).
612+
/// TODO: possibly obsolete and replaced by Flatten ?!
482613
let Merge (layerSets : LayerSet seq) : LayerSet =
483614

484615
let layerSets = layerSets |> Array.ofSeq
@@ -494,5 +625,26 @@ module LayerSet =
494625
)
495626
|> Array.ofList
496627

628+
let result = LayerSet layers
629+
result
630+
631+
/// Flattens multiple layer sets (different resolutions are allowed) down to a single layer set.
632+
/// Holes (which were not covered by any source layerset) are marked by an optional mask.
633+
/// All layer sets must have identical layers (same number, same semantics, same order).
634+
let Flatten (layerSets : LayerSet seq) : LayerSet =
635+
636+
let layerSets = layerSets |> Array.ofSeq
637+
let layersPerLayerSet = layerSets[0].Layers.Length
638+
639+
let indices = [0..layersPerLayerSet-1]
640+
641+
let layers =
642+
indices
643+
|> List.choose (fun i ->
644+
// merge the i-th layers from all layer sets
645+
layerSets |> Array.map (fun x -> x.Layers[i]) |> Layer.Flatten
646+
)
647+
|> Array.ofList
648+
497649
let result = LayerSet layers
498650
result

0 commit comments

Comments
 (0)