Skip to content

Commit 2cb5679

Browse files
[builder] add build logic for yet more cases
1 parent 2b6421e commit 2cb5679

File tree

3 files changed

+67
-60
lines changed

3 files changed

+67
-60
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 0.5.8
2+
- [builder] add build logic for yet more cases
3+
14
### 0.5.7
25
- fix Sample.PositionsWithBounds (dict trygetvalue fails when key does not exist, probably due to uninitialized tuple)
36

src/Aardvark.Geometry.Quadtree/Builder.fs

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,13 @@ module Builder =
126126

127127
| 0 ->
128128

129-
//printfn "[00000] %d" rootCell.Exponent
130-
131129
if config.Verbose then
132130
printfn "[build''] ZERO patches"
133131

134132
NoNode
135133

136134
| 1 ->
137135

138-
//printfn "[11111] %d" rootCell.Exponent
139-
140136
if config.Verbose then
141137
printfn "[build''] SINGLE patch (%A)" patches[0].SampleWindow
142138

@@ -146,8 +142,6 @@ module Builder =
146142

147143
| 2 ->
148144

149-
//printfn "[22222] %d" rootCell.Exponent
150-
151145
if config.Verbose then
152146
printfn "[build''] TWO patches (%A, %A)" patches[0].SampleWindow patches[1].SampleWindow
153147

@@ -158,52 +152,37 @@ module Builder =
158152

159153
| n -> // n patches
160154

161-
//printfn "[nnnnn] %d" rootCell.Exponent
162-
163155
if config.Verbose then
164156
printfn "[build''] MULTIPLE patches (n=%d)" patches.Length
165157

166-
let ebb = patches |> Seq.map (fun patch -> patch.BoundingBox) |> Box2d
167-
168158
invariantm (config.SplitLimitPowerOfTwo >= 0)
169159
(fun () -> sprintf "Expected config.SplitLimitPowerOfTwo to be non-negative, but found %d." config.SplitLimitPowerOfTwo)
170160
"95c43529-9649-42d6-9b47-c6f8fb6da301"
171-
172-
let mutable forceFlatten = false
173-
//let tileSize = 1 <<< config.SplitLimitPowerOfTwo
174-
175-
// bounds of root cell at level of highest-resolution patch
161+
162+
// exact bounding box in global space
163+
let ebb = patches |> Seq.map (fun patch -> patch.BoundingBox) |> Box2d
164+
165+
// bounds of root cell in sample space (at sample level of highest-resolution patch)
176166
let rootBounds = getBoundsForExponent minSampleExponent rootCell
177167

178168
// if all remaining patches have the same resolution, then we can flatten all layers
179169
// (because there can be no troubles with overlapping samples of different sizes)
180-
if (patches |> Seq.distinctBy (fun p -> p.SampleExponent) |> Seq.tryExactlyOne).IsSome then
181-
forceFlatten <- true
170+
let allRemainingPatchesHaveSameResolution =
171+
(patches |> Seq.distinctBy (fun p -> p.SampleExponent) |> Seq.tryExactlyOne).IsSome
172+
173+
if config.Verbose then
174+
if allRemainingPatchesHaveSameResolution then
175+
printfn "[build''] FLATTEN all %d patches, because they have same resolution" patches.Length
176+
else
177+
printfn "[build''] DO NOT FLATTEN %d patches, because different resolutions" patches.Length
178+
182179

183180
let patchesWithMinExp = patches |> Array.filter (fun p -> p.SampleExponent = minSampleExponent)
184-
if patchesWithMinExp |> Array.exists (fun p -> p.SampleWindow.Contains(rootBounds)) then
185-
forceFlatten <- true
186-
187-
//let patchesPerQuadrant =
188-
// rootCell.Children
189-
// |> Array.map (fun subCell ->
190-
// let subPatches =
191-
// patches
192-
// |> Array.choose (fun patch ->
193-
// let bbQuadrant = subCell.GetBoundsForExponent(patch.SampleExponent)
194-
// let r = patch.WithWindow bbQuadrant
195-
// //if config.Verbose && r.IsSome then
196-
// // printfn "[build''] | subCell=%A patch.Window=%A bbQuadrant=%A" subCell patch.SampleWindow r.Value.SampleWindow
197-
// r
198-
// )
199-
// (subCell, subPatches)
200-
// )
201-
202-
//let patchesPerQuadrantCounts = patchesPerQuadrant |> Array.map (fun (_, ps) -> ps.Length)
203-
//let canMakeProgress = patchesPerQuadrantCounts |> Array.forall (fun count -> count < n)
204-
205-
//if rootBounds.Size.X <= tileSize && rootBounds.Size.Y <= tileSize then
206-
if rootCell.Exponent = minSampleExponent || forceFlatten (*&& rootBounds.Size.X <= tileSize && rootBounds.Size.Y <= tileSize*) then
181+
let forceFlatten = patchesWithMinExp |> Array.exists (fun p -> p.SampleWindow.Contains(rootBounds))
182+
183+
let stopRecursion = rootCell.Exponent = minSampleExponent || forceFlatten || (allRemainingPatchesHaveSameResolution && rootCell.Exponent < minSampleExponent + config.SplitLimitPowerOfTwo)
184+
185+
if stopRecursion then
207186

208187
// current tile size has reached the split limit:
209188
// 1. sort all patches from fine to coarse (from small to large sample exponents)
@@ -213,7 +192,10 @@ module Builder =
213192
// -> a mask indicates non-defined samples
214193

215194
if config.Verbose then
216-
printfn "[build''] MERGE %d patches; because reached split limit" n
195+
if forceFlatten then
196+
printfn "[build''] MERGE %d patches (forceFlatten = true)" n
197+
else
198+
printfn "[build''] MERGE %d patches; because reached split limit" n
217199

218200
//if debugOutput then
219201
// let bbWindow = patches |> Seq.map (fun patch -> patch.SampleWindow) |> Box2l
@@ -228,24 +210,25 @@ module Builder =
228210

229211
let mutable rootCell = rootCell
230212

231-
if requiredRootCellExponent <> rootCell.Exponent then
213+
if not forceFlatten then
214+
if requiredRootCellExponent <> rootCell.Exponent then
232215

233-
invariantm (rootCell.Exponent < requiredRootCellExponent)
234-
(fun () -> sprintf "Expected root cell exponent %d to be smaller than requiredRootCellExponent %d." rootCell.Exponent requiredRootCellExponent)
235-
"4911adf3-7b87-4234-9bcc-bc3076df846e"
216+
invariantm (rootCell.Exponent < requiredRootCellExponent)
217+
(fun () -> sprintf "Expected root cell exponent %d to be smaller than requiredRootCellExponent %d." rootCell.Exponent requiredRootCellExponent)
218+
"4911adf3-7b87-4234-9bcc-bc3076df846e"
236219

237-
if config.Verbose then
238-
printfn "[build''] | must adjust root cell %A exponent to %d ..." rootCell requiredRootCellExponent
220+
if config.Verbose then
221+
printfn "[build''] | must adjust root cell %A exponent to %d ..." rootCell requiredRootCellExponent
239222

240-
while rootCell.Exponent < requiredRootCellExponent do rootCell <- rootCell.Parent
223+
while rootCell.Exponent < requiredRootCellExponent do rootCell <- rootCell.Parent
241224

242-
if config.Verbose then
243-
printfn "[build''] | adjusted root cell is %A" rootCell
225+
if config.Verbose then
226+
printfn "[build''] | adjusted root cell is %A" rootCell
244227

245-
else
228+
else
246229

247-
if config.Verbose then
248-
printfn "[build''] | root cell %A already has requiredRootCellExponent %d" rootCell requiredRootCellExponent
230+
if config.Verbose then
231+
printfn "[build''] | root cell %A already has requiredRootCellExponent %d" rootCell requiredRootCellExponent
249232

250233
let flattended = LayerSet.Flatten config.Verbose patches
251234

src/Scratch/Program.fs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,21 +1480,42 @@ let cp_20240311_quadtree_exception () =
14801480
printfn "reloaded from file, %d patches" (x.GetPatches() |> Seq.length)
14811481

14821482
let sw = Stopwatch.StartNew()
1483-
let buildConfig = { BuildConfig.Default with Verbose = false; SplitLimitPowerOfTwo = 8 }
1483+
let buildConfig = { BuildConfig.Default with Verbose = true; SplitLimitPowerOfTwo = 8 }
14841484
let maybeQuadtree = x.Build2 buildConfig
14851485
sw.Stop()
14861486
printfn "[TIMING] build: %A" sw.Elapsed
14871487

14881488
match maybeQuadtree with
1489-
| None -> failwith ""
1489+
| None -> failwith "build failed"
14901490
| Some qtree ->
14911491

1492-
let pos = V2d(66077.6476628291, 270082.243676802)
1493-
match Sample.Position Query.Config.Default pos qtree with
1494-
| None -> failwith "foo"
1495-
| Some x -> printfn "sample is: %A" x
1492+
let makeReturnValOfQueryResults (resultChunk : seq<Query.Result>) (def : Aardvark.Data.Durable.Def) =
1493+
1494+
let samples =
1495+
resultChunk
1496+
|> Seq.collect (fun chunk -> chunk.GetSamples<V4f> def)
1497+
|> Seq.toList
14961498

1497-
()
1499+
samples
1500+
1501+
let sw = Stopwatch.StartNew()
1502+
let config = Query.Config.Default //{ Query.Config.Default with Verbose = true }
1503+
let resultCells = qtree |> Query.All config |> Seq.toArray
1504+
let samples = makeReturnValOfQueryResults resultCells Defs.HeightsBilinear4f
1505+
let samplesLength = samples.Length
1506+
sw.Stop()
1507+
printfn "[TIMING] query all samples: %A" sw.Elapsed
1508+
1509+
printfn("SAMPLES: count=%d") samplesLength
1510+
let gs = samples
1511+
|> List.groupBy (fun (c, v) -> c.Exponent)
1512+
|> List.map (fun (e, xs) ->
1513+
let countNaN = xs |> Seq.filter(fun (_, x) -> x.IsNaN) |> Seq.length
1514+
(e, xs.Length, countNaN)
1515+
)
1516+
for (e, c, cNaN) in gs do
1517+
printfn(" e=%d; count=%d; count NaN=%d") e c cNaN
1518+
14981519

14991520
()
15001521

0 commit comments

Comments
 (0)