annotate pkg/octree/builder.go @ 1017:a244b18cb916

Added GNU Affero General Public License. Adjusted the headers of the Go files to be AGPLv3.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 23 Oct 2018 18:15:14 +0200
parents 7a89313f0ead
children fbdd7c3cfeac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1017
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
2 // without warranty, see README.md and license for details.
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
3 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
6 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
7 // Copyright (C) 2018 by via donau
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
9 // Software engineering by Intevation GmbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
10 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
11 // Author(s):
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 974
diff changeset
13
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
14 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
15
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 import (
973
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
17 "bytes"
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
18 "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
19 "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
20 "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
21
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 "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
23 )
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
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 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
26 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
27 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
28 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
29 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
30 }
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
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 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
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 }
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
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 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
44 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
45 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
46 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
47 }
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 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
49 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
50 }
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 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
52 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
53 }
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 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
55 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
56 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
57 }
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
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 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
61 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
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
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 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
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 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
67 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
68 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
69 }
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
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 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
72
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 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
74 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
75 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
76
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 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
78 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
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 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
82 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
83 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
84 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
85 ) 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
86 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
87 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
88 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
89 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
90 //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
91 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
92 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
93 }
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 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
96 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
97 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
98 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
99
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 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
101
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 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
103
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 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
105 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
106 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
107 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
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 }
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
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 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
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 _, 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
114 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
115 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
116 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
117 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
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 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
120 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
121 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
122
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 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
124 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
125 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
126
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 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
128 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
129 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
130 }
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
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 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
135 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
136 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
137 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
138 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
139 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
140 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
141 }
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 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
144 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
145 }
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
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 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
148 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
149
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 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
151 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
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 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
155 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
156
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 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
158 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
159 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
160 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
161 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
162 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
163 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
164 }
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 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
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
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 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
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 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
171 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
172
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
173 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
174 }
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
175
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
176 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
177 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
178 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
179 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
180 }
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
181 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
182 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
183 }
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
184 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
185 }
973
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
186
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
187 func (tb *Builder) Bytes() ([]byte, error) {
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
188 var buf bytes.Buffer
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
189 if err := tb.WriteTo(&buf); err != nil {
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
190 return nil, err
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
191 }
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
192 return buf.Bytes(), nil
b6fec8f85599 Generate TINs and octrees in sounding result importer.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 968
diff changeset
193 }
974
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
194
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
195 func (tb *Builder) Tree() *Tree {
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
196 return &Tree{
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
197 EPSG: tb.t.EPSG,
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
198 vertices: tb.t.Vertices,
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
199 triangles: tb.t.Triangles,
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
200 index: tb.index,
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
201 Min: tb.t.Min,
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
202 Max: tb.t.Max,
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
203 }
7a89313f0ead Fetch the octree directly from the builder. No need to deserialize it from the blob.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 973
diff changeset
204 }