annotate pkg/octree/tin.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 f9fb6c399f3f
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 "bytes"
a4fe07a21ba7 Moved 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 "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
6 "errors"
a4fe07a21ba7 Moved 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 "fmt"
a4fe07a21ba7 Moved 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 "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
9 "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
10 "math"
a4fe07a21ba7 Moved 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 )
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 var (
a4fe07a21ba7 Moved 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 errNoByteSlice = errors.New("Not a byte slice")
a4fe07a21ba7 Moved 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 errTooLessPoints = errors.New("Too less points")
a4fe07a21ba7 Moved 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 const wgs84 = 4326
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 type Tin 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
21 EPSG uint32
a4fe07a21ba7 Moved 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 Vertices []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
23 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
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 Min 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
26 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
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 (t *Tin) FromWKB(data []byte) 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
30 log.Printf("data length %d\n", len(data))
a4fe07a21ba7 Moved 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 r := bytes.NewReader(data)
a4fe07a21ba7 Moved 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 endian, err := r.ReadByte()
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 var order binary.ByteOrder
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 switch {
a4fe07a21ba7 Moved 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 case 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
40 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
41 case endian == wkbNDR:
a4fe07a21ba7 Moved 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 order = binary.LittleEndian
a4fe07a21ba7 Moved 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 case endian == wkbXDR:
a4fe07a21ba7 Moved 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 order = binary.BigEndian
a4fe07a21ba7 Moved 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 default:
a4fe07a21ba7 Moved 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 return fmt.Errorf("unknown byte order %x", endian)
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 var geomType uint32
a4fe07a21ba7 Moved 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 err = binary.Read(r, order, &geomType)
a4fe07a21ba7 Moved 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 switch {
a4fe07a21ba7 Moved 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 case 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
54 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
55 case geomType != wkbTinZ:
a4fe07a21ba7 Moved 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 return fmt.Errorf("unknown geometry type %x", geomType)
a4fe07a21ba7 Moved 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 var num uint32
a4fe07a21ba7 Moved 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 if err = binary.Read(r, order, &num); 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
61 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
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 vertices := make([]Vertex, 0, 100000)
a4fe07a21ba7 Moved 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 var v 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
67
a4fe07a21ba7 Moved 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 v2i := make(map[Vertex]int32, 100000)
a4fe07a21ba7 Moved 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 var indexPool []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
71
a4fe07a21ba7 Moved 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 allocIndices := func() []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
73 if len(indexPool) == 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 indexPool = make([]int32, 3*8*1024)
a4fe07a21ba7 Moved 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 }
a4fe07a21ba7 Moved 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 ids := indexPool[: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
77 indexPool = indexPool[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
78 return ids
a4fe07a21ba7 Moved 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 var 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
82
a4fe07a21ba7 Moved 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 := Vertex{math.MaxFloat64, math.MaxFloat64, math.MaxFloat64}
a4fe07a21ba7 Moved 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 max := Vertex{-math.MaxFloat64, -math.MaxFloat64, -math.MaxFloat64}
a4fe07a21ba7 Moved 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 for i := uint32(0); i < num; 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
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 endian, err = r.ReadByte()
a4fe07a21ba7 Moved 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 switch {
a4fe07a21ba7 Moved 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 case 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
91 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
92 case endian == wkbNDR:
a4fe07a21ba7 Moved 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 order = binary.LittleEndian
a4fe07a21ba7 Moved 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 case endian == wkbXDR:
a4fe07a21ba7 Moved 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 order = binary.BigEndian
a4fe07a21ba7 Moved 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 default:
a4fe07a21ba7 Moved 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 return fmt.Errorf("unknown byte order %x", endian)
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 err = binary.Read(r, order, &geomType)
a4fe07a21ba7 Moved 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 switch {
a4fe07a21ba7 Moved 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 case 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
103 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
104 case geomType != wkbTriangleZ:
a4fe07a21ba7 Moved 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 return fmt.Errorf("unknown geometry type %d", geomType)
a4fe07a21ba7 Moved 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 }
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 var rings uint32
a4fe07a21ba7 Moved 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 if err = binary.Read(r, order, &rings); 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
110 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
111 }
a4fe07a21ba7 Moved 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 triangle := allocIndices()
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 for ring := uint32(0); ring < rings; ring++ {
a4fe07a21ba7 Moved 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 var npoints uint32
a4fe07a21ba7 Moved 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 if err = binary.Read(r, order, &npoints); 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
117 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
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 if npoints < 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
121 return errTooLessPoints
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 for p := uint32(0); p < npoints; p++ {
a4fe07a21ba7 Moved 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 var x, y, z uint64
a4fe07a21ba7 Moved 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 for _, addr := range []*uint64{&x, &y, &z} {
a4fe07a21ba7 Moved 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 if err = binary.Read(r, order, addr); 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
128 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
129 }
a4fe07a21ba7 Moved 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 if p >= 3 || ring >= 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
132 // Don't store the forth point.
a4fe07a21ba7 Moved 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 continue
a4fe07a21ba7 Moved 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 }
a4fe07a21ba7 Moved 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 // Do this conversion later to spare reflect calls
a4fe07a21ba7 Moved 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 // and allocs in binary.Read.
a4fe07a21ba7 Moved 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 v.X = math.Float64frombits(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
138 v.Y = math.Float64frombits(y)
a4fe07a21ba7 Moved 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 v.Z = math.Float64frombits(z)
a4fe07a21ba7 Moved 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 idx, found := v2i[v]
a4fe07a21ba7 Moved 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 if !found {
a4fe07a21ba7 Moved 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 idx = int32(len(vertices))
a4fe07a21ba7 Moved 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 v2i[v] = idx
a4fe07a21ba7 Moved 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 vertices = append(vertices, v)
a4fe07a21ba7 Moved 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 min.Minimize(v)
a4fe07a21ba7 Moved 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 max.Maximize(v)
a4fe07a21ba7 Moved 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 }
a4fe07a21ba7 Moved 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 triangle[p] = idx
a4fe07a21ba7 Moved 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 }
a4fe07a21ba7 Moved 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 triangles = append(triangles, triangle)
a4fe07a21ba7 Moved 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 log.Printf("bbox: [[%f, %f], [%f, %f]]\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
155 min.X, min.Y, max.X, max.Y)
a4fe07a21ba7 Moved 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 *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
158 EPSG: wgs84,
a4fe07a21ba7 Moved 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 Vertices: vertices,
a4fe07a21ba7 Moved 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 Triangles: 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
161 Min: min,
a4fe07a21ba7 Moved 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 Max: 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
163 }
a4fe07a21ba7 Moved 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 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
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 func (t *Tin) Scan(raw interface{}) 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
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 data, ok := raw.([]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
171 if !ok {
a4fe07a21ba7 Moved 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 return errNoByteSlice
a4fe07a21ba7 Moved 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 }
a4fe07a21ba7 Moved 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 return t.FromWKB(data)
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 func (t *Tin) 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
178
a4fe07a21ba7 Moved 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 if err := binary.Write(w, binary.LittleEndian, t.EPSG); 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
180 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
181 }
a4fe07a21ba7 Moved 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
a4fe07a21ba7 Moved 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 if err := t.Min.Write(w); 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
184 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
185 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 if err := t.Max.Write(w); 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
187 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
188 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
190 if err := binary.Write(
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191 w, binary.LittleEndian, uint32(len(t.Vertices))); 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
192 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
193 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 for _, v := range t.Vertices {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 if err := v.Write(w); 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
197 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
198 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
199 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200 log.Printf("vertices %d (%d)\n", len(t.Vertices), len(t.Vertices)*3*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
201
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
202 if err := binary.Write(
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
203 w, binary.LittleEndian, uint32(len(t.Triangles))); 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
204 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
205 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207 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
208 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
209 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
210 for _, triangle := range 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
211 for _, idx := range triangle {
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212 value := idx - 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
213 n := binary.PutVarint(buf[:], int64(value))
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
214 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
215 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
216 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
217 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
218 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
219 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
220 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
221 last = idx
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
222 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
223 }
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
224 log.Printf("compressed tin indices 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
225 written, 3*4*len(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
226
a4fe07a21ba7 Moved octree builder into octree package to be reusable by the sounding result import job.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
227 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
228 }