comparison pkg/mesh/loader.go @ 4827:f4abfd0ee8ad remove-octree-debris

Renamed octree package to mesh as there is no octree any more.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 05 Nov 2019 14:30:22 +0100
parents pkg/octree/loader.go@c0eb491aaaa7
children 5f47eeea988d
comparison
equal deleted inserted replaced
4826:ec5afada70ec 4827:f4abfd0ee8ad
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package mesh
15
16 import (
17 "bufio"
18 "bytes"
19 "compress/gzip"
20 "encoding/binary"
21 "log"
22 )
23
24 func (s *STRTree) deserializeIndex(r *bufio.Reader) error {
25 var numIndex int32
26 if err := binary.Read(r, binary.LittleEndian, &numIndex); err != nil {
27 return err
28 }
29 index := make([]int32, numIndex)
30 s.index = index
31
32 var last int32
33 for i := range index {
34 v, err := binary.ReadVarint(r)
35 if err != nil {
36 return err
37 }
38 value := int32(v) + last
39 index[i] = value
40 last = value
41 }
42
43 return nil
44 }
45
46 func (s *STRTree) deserializeBBoxes(r *bufio.Reader) error {
47
48 var numBBoxes int32
49 if err := binary.Read(r, binary.LittleEndian, &numBBoxes); err != nil {
50 return err
51 }
52
53 bboxes := make([]Box2D, numBBoxes)
54 s.bboxes = bboxes
55
56 var err error
57
58 read := func(v *float64) {
59 if err == nil {
60 err = binary.Read(r, binary.LittleEndian, v)
61 }
62 }
63
64 for i := range bboxes {
65 read(&bboxes[i].X1)
66 read(&bboxes[i].Y1)
67 read(&bboxes[i].X2)
68 read(&bboxes[i].Y2)
69 }
70
71 return err
72 }
73
74 func (s *STRTree) deserialize(r *bufio.Reader) error {
75 s.tin = new(Tin)
76
77 if err := s.tin.Deserialize(r); err != nil {
78 return err
79 }
80 var numEntries uint8
81 if err := binary.Read(r, binary.LittleEndian, &numEntries); err != nil {
82 return err
83 }
84 s.Entries = int(numEntries)
85
86 if err := s.deserializeIndex(r); err != nil {
87 return err
88 }
89
90 return s.deserializeBBoxes(r)
91 }
92
93 func (s *STRTree) FromBytes(data []byte) error {
94 r, err := gzip.NewReader(bytes.NewReader(data))
95 if err != nil {
96 return err
97 }
98 return s.deserialize(bufio.NewReader(r))
99 }
100
101 func (t *Tin) Deserialize(r *bufio.Reader) error {
102
103 if err := binary.Read(r, binary.LittleEndian, &t.EPSG); err != nil {
104 return err
105 }
106
107 log.Printf("info: EPSG: %d\n", t.EPSG)
108
109 if err := t.Min.Read(r); err != nil {
110 return err
111 }
112
113 if err := t.Max.Read(r); err != nil {
114 return err
115 }
116
117 log.Printf("info: BBOX: [[%f, %f, %f], [%f, %f, %f]]\n",
118 t.Min.X, t.Min.Y, t.Min.Z,
119 t.Max.X, t.Max.Y, t.Max.Z)
120
121 var numVertices uint32
122 if err := binary.Read(r, binary.LittleEndian, &numVertices); err != nil {
123 return err
124 }
125
126 log.Printf("info: vertices: %d\n", numVertices)
127
128 vertices := make([]Vertex, numVertices)
129 t.Vertices = vertices
130
131 for i := range vertices {
132 if err := vertices[i].Read(r); err != nil {
133 return err
134 }
135 }
136
137 var numTriangles uint32
138 if err := binary.Read(r, binary.LittleEndian, &numTriangles); err != nil {
139 return err
140 }
141
142 log.Printf("info: triangles: %d\n", numTriangles)
143
144 indices := make([]int32, 3*numTriangles)
145 triangles := make([][]int32, numTriangles)
146 t.Triangles = triangles
147
148 var last int32
149
150 for i := range triangles {
151 tri := indices[:3]
152 indices = indices[3:]
153 triangles[i] = tri
154 for j := range tri {
155 v, err := binary.ReadVarint(r)
156 if err != nil {
157 return err
158 }
159 value := int32(v) + last
160 tri[j] = value
161 last = value
162 }
163 }
164
165 return nil
166 }