annotate pkg/octree/builder.go @ 968:a4fe07a21ba7

Moved octree builder into octree package to be reusable by the sounding result import job.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 17 Oct 2018 16:00:49 +0200
parents
children b6fec8f85599
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
968
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package octree
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import (
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 "encoding/binary"
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 "io"
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 "log"
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 "github.com/golang/snappy"
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 )
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 type Builder struct {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 t *Tin
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 nodes int
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 leaves int
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 index []int32
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 var cubes = [8][2]Vertex{
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 makeCube(0),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 makeCube(1),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 makeCube(2),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 makeCube(3),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 makeCube(4),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 makeCube(5),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 makeCube(6),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 makeCube(7),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 func makeCube(i int) [2]Vertex {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 var d Vertex
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 if i&1 == 1 {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 d.X = 0.5
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 if i&2 == 2 {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 d.Y = 0.5
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 if i&4 == 4 {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 d.Z = 0.5
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 return [2]Vertex{
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 Vertex{0.0, 0.0, 0.0}.Add(d),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 Vertex{0.5, 0.5, 0.5}.Add(d),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 func NewBuilder(t *Tin) *Builder {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 return &Builder{t: t}
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 func (tb *Builder) Build() {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 triangles := make([]int32, len(tb.t.Triangles))
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 for i := range triangles {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 triangles[i] = int32(i)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 tb.index = append(tb.index, 0)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 tb.buildRecursive(triangles, tb.t.Min, tb.t.Max, 0)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 tb.index[0] = int32(len(tb.index))
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 log.Printf("num nodes: %d\n", tb.index[0])
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 log.Printf("nodes: %d leaves: %d index %d\n",
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 tb.nodes, tb.leaves, tb.index[0])
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 func (tb *Builder) buildRecursive(
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 triangles []int32,
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 min, max Vertex,
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 depth int,
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 ) int32 {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 if len(triangles) <= 16 || depth > 8 {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 pos := len(tb.index)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 tb.index = append(tb.index, int32(len(triangles)))
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 tb.index = append(tb.index, triangles...)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 //log.Printf("leaf entries: %d (%d)\n", len(triangles), depth)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 tb.leaves++
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 return int32(-(pos + 1))
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 pos := len(tb.index)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 tb.index = append(tb.index,
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 0, 0, 0, 0,
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 0, 0, 0, 0)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 bbox := Interpolate(min, max)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 bboxes := make([][2]Vertex, len(cubes))
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 for i := range cubes {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 bboxes[i] = [2]Vertex{
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 bbox(cubes[i][0]),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 bbox(cubes[i][1]),
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 var quandrants [8][]int32
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 for _, tri := range triangles {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 triangle := tb.t.Triangles[tri]
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 v0 := tb.t.Vertices[triangle[0]]
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 v1 := tb.t.Vertices[triangle[1]]
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 v2 := tb.t.Vertices[triangle[2]]
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 l := v0
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 l.Minimize(v1)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 l.Minimize(v2)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 h := v0
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 h.Maximize(v1)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 h.Maximize(v2)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 for i := range bboxes {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 if !(h.Less(bboxes[i][0]) || bboxes[i][1].Less(l)) {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 quandrants[i] = append(quandrants[i], tri)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 for i := range quandrants {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 if len(quandrants[i]) > 0 {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 child := tb.buildRecursive(
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 quandrants[i],
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 bboxes[i][0], bboxes[i][1],
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 depth+1)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 tb.index[pos+i] = child
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 tb.nodes++
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 return int32(pos)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 func (tb *Builder) Serialize(w io.Writer) error {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 var buf [binary.MaxVarintLen32]byte
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 if err := binary.Write(w, binary.LittleEndian, tb.index[0]); err != nil {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 return err
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 var last int32
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 var written int
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 for _, x := range tb.index[1:] {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 delta := x - last
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 n := binary.PutVarint(buf[:], int64(delta))
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 for p := buf[:n]; len(p) > 0; p = p[n:] {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 var err error
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 if n, err = w.Write(p); err != nil {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 return err
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 written += n
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 last = x
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 log.Printf("compressed octree index in bytes: %d (%d)\n",
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 written, 4*len(tb.index))
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 return nil
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 func (tb *Builder) WriteTo(w io.Writer) error {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 out := snappy.NewBufferedWriter(w)
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 if err := tb.t.Serialize(out); err != nil {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 return err
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 if err := tb.Serialize(out); err != nil {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 return err
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 return out.Flush()
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 }