Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanmaierhofer committed Feb 7, 2024
1 parent bae6779 commit 84b36e9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
36 changes: 26 additions & 10 deletions src/Aardvark.Geometry.Quadtree/Builder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open Aardvark.Base
open System
open System.Collections.Generic
open Serialization
open System.Diagnostics

#nowarn "1337"

Expand Down Expand Up @@ -116,15 +117,12 @@ module Builder =
let result = { Id = Guid.NewGuid(); ExactBoundingBox = ebb; Cell = rootCell; SplitLimitExponent = BuildConfig.Default.SplitLimitPowerOfTwo; HasMask = hasMask; SubNodes = subNodes }
result |> InMemoryInner

let rec private build'' (minSampleExponent : int) (rootCell : Cell2d) (patches : LayerSet[]) =
let rec private build'' (rootCell : Cell2d) (patches : LayerSet[]) =

if debugOutput then
printfn "[DEBUG] build' rootCell = %A, %d patches" rootCell patches.Length

for p in patches do
invariantm (p.SampleExponent >= minSampleExponent)
(fun () -> sprintf "Patch sample exponent %d is smaller than specified minimum sample exponent %d." p.SampleExponent minSampleExponent)
"28d1fdd1-2da6-4329-a065-c134c1351ffc"
let minSampleExponent = patches |> Seq.map (fun p -> p.SampleExponent) |> Seq.min

match patches.Length with

Expand Down Expand Up @@ -192,7 +190,13 @@ module Builder =
rootCell.Children
|> Array.map (fun subCell ->
let bbQuadrant = subCell.GetBoundsForExponent(minSampleExponent)
let subPatches = patches |> Array.choose (fun b -> b.WithWindow bbQuadrant)
let subPatches =
patches // TODO: ensure that bbQuadrant is in same resolution as patch ?!?
|> Array.choose (fun patch ->
if patch.SampleExponent <> minSampleExponent then Debugger.Break()
patch.WithWindow bbQuadrant
)

(subCell, subPatches)
)

Expand All @@ -207,7 +211,7 @@ module Builder =
let subNodes = patchesPerQuadrant |> Array.map (fun (subCell, subPatches) ->
match subPatches.Length with
| 0 -> NoNode
| _ -> build'' minSampleExponent subCell subPatches
| _ -> build'' subCell subPatches
)

let hasMask = subNodes |> Array.exists (fun n -> n.HasMask)
Expand All @@ -219,9 +223,12 @@ module Builder =
/// Creates a quadtree from many small patches.
let Build (patches : LayerSet seq) : QNodeRef =
let patches = patches |> Array.ofSeq
let rootCell = patches |> Array.map (fun patch -> patch.BoundingBox) |> Box2d |> Cell2d
let sampleExponent = (patches |> Array.distinctBy (fun x -> x.SampleExponent) |> Array.exactlyOne).SampleExponent
build' sampleExponent rootCell patches
let rootCell = patches |> Seq.map (fun patch -> patch.BoundingBox) |> Box2d |> Cell2d

//let sampleExponent = (patches |> Array.distinctBy (fun x -> x.SampleExponent) |> Array.exactlyOne).SampleExponent
//build' sampleExponent rootCell patches

build'' rootCell patches

/// Creates a quadtree from many small patches.
type Builder () =
Expand Down Expand Up @@ -306,6 +313,15 @@ type Builder () =
Quadtree.Merge Dominance.SecondDominates state item |> Some
)
None // initial state

/// Build a quadtree from all the patches that have been added to this builder.
member this.Build2 () : QNodeRef option =

let mutable mergesCount = 0

let allPatches = this.GetPatches()
Builder.Build allPatches |> Some


/// Enumerate all patches.
member this.GetPatches () : seq<LayerSet> =
Expand Down
1 change: 1 addition & 0 deletions src/Aardvark.Geometry.Quadtree/DataMapping.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type DataMapping(bufferOrigin : Cell2d, bufferSize : V2i, window : Box2l) =
let max = Cell2d(window.Max, bufferOrigin.Exponent).BoundingBox.Min
Box2d(min, max)

/// Returns this data mapping with new window (Some), or None if new window is not inside the current window.
member this.WithWindow (newWindow : Box2l) =
let o = window.Intersection(newWindow)
if o.IsInvalid || o.Area = 0L then
Expand Down
4 changes: 3 additions & 1 deletion src/Aardvark.Geometry.Quadtree/Layer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ open System.Diagnostics
type ILayer =
abstract member Def : Durable.Def
abstract member Mapping : DataMapping
/// Returns this layer with new window w (Some), or None if new window is not inside the current layer window.
abstract member WithWindow : Box2l -> ILayer option
abstract member MapSingleCenteredSampleTo : Cell2d -> ILayer
abstract member WithSemantic : Durable.Def -> ILayer
Expand Down Expand Up @@ -62,7 +63,8 @@ type Layer<'a when 'a : equality>(def : Durable.Def, data : 'a[], mapping : Data

member this.Mapping with get() = mapping

member this.WithWindow (w : Box2l) =
/// Returns this layer with new window w (Some), or None if new window is not inside the current layer window.
member this.WithWindow (w : Box2l) : ILayer option =
mapping.WithWindow(w)
|> Option.map (fun m -> Layer(def, data, m, mask) :> ILayer)

Expand Down
2 changes: 1 addition & 1 deletion src/Scratch/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ let cp_20240202_quadtreetest () =
|> Seq.sumBy(fun patch -> patch.SampleWindow.Area)
printfn("total samples count with e = -3: %d") samplesCount

match x.Build () with
match x.Build2 () with
| None -> failwith ""
| Some qtree ->
let makeReturnValOfQueryResults (resultChunk : seq<Query.Result>) (def : Aardvark.Data.Durable.Def) =
Expand Down

0 comments on commit 84b36e9

Please sign in to comment.