diff --git a/src/Aardvark.Geometry.Quadtree/Builder.fs b/src/Aardvark.Geometry.Quadtree/Builder.fs index 1add0db..dc32c8c 100644 --- a/src/Aardvark.Geometry.Quadtree/Builder.fs +++ b/src/Aardvark.Geometry.Quadtree/Builder.fs @@ -4,6 +4,7 @@ open Aardvark.Base open System open System.Collections.Generic open Serialization +open System.Diagnostics #nowarn "1337" @@ -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 @@ -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) ) @@ -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) @@ -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 () = @@ -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 = diff --git a/src/Aardvark.Geometry.Quadtree/DataMapping.fs b/src/Aardvark.Geometry.Quadtree/DataMapping.fs index 1326f41..fcd2abe 100644 --- a/src/Aardvark.Geometry.Quadtree/DataMapping.fs +++ b/src/Aardvark.Geometry.Quadtree/DataMapping.fs @@ -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 diff --git a/src/Aardvark.Geometry.Quadtree/Layer.fs b/src/Aardvark.Geometry.Quadtree/Layer.fs index 5942062..652d360 100644 --- a/src/Aardvark.Geometry.Quadtree/Layer.fs +++ b/src/Aardvark.Geometry.Quadtree/Layer.fs @@ -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 @@ -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) diff --git a/src/Scratch/Program.fs b/src/Scratch/Program.fs index 12b6a54..1ba4e6b 100644 --- a/src/Scratch/Program.fs +++ b/src/Scratch/Program.fs @@ -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) (def : Aardvark.Data.Durable.Def) =