Mercurial > gemma
comparison pkg/mesh/loader.go @ 5676:d56e043bbbca sr-v2
More code moving.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 11 Feb 2024 09:49:25 +0100 |
parents | e54635c20d43 |
children |
comparison
equal
deleted
inserted
replaced
5675:b8da63027b48 | 5676:d56e043bbbca |
---|---|
18 "bytes" | 18 "bytes" |
19 "compress/gzip" | 19 "compress/gzip" |
20 "encoding/binary" | 20 "encoding/binary" |
21 "errors" | 21 "errors" |
22 "fmt" | 22 "fmt" |
23 | |
24 "gemma.intevation.de/gemma/pkg/log" | |
25 ) | 23 ) |
26 | 24 |
27 const magicHeader = "SR3D" | 25 const magicHeader = "SR3D" |
28 | 26 |
29 func (s *STRTree) deserializeIndex(r *bufio.Reader) error { | 27 // Bytes serializes this tree to a byte slice. |
30 var numIndex int32 | 28 func (s *STRTree) Bytes() ([]byte, error) { |
31 if err := binary.Read(r, binary.LittleEndian, &numIndex); err != nil { | 29 |
30 var buf bytes.Buffer | |
31 w, err := gzip.NewWriterLevel(&buf, gzip.BestSpeed) | |
32 if err != nil { | |
33 return nil, err | |
34 } | |
35 | |
36 if err := s.serializeV1(w); err != nil { | |
37 return nil, err | |
38 } | |
39 | |
40 if err := w.Close(); err != nil { | |
41 return nil, err | |
42 } | |
43 | |
44 return buf.Bytes(), nil | |
45 } | |
46 | |
47 // FromBytes restores a STRTree from a binary representation. | |
48 func (s *STRTree) FromBytes(data []byte, version *int64) error { | |
49 r, err := gzip.NewReader(bytes.NewReader(data)) | |
50 if err != nil { | |
32 return err | 51 return err |
33 } | 52 } |
34 index := make([]int32, numIndex) | 53 return s.deserialize(bufio.NewReader(r), version) |
35 s.index = index | |
36 | |
37 var last int32 | |
38 for i := range index { | |
39 v, err := binary.ReadVarint(r) | |
40 if err != nil { | |
41 return err | |
42 } | |
43 value := int32(v) + last | |
44 index[i] = value | |
45 last = value | |
46 } | |
47 | |
48 return nil | |
49 } | |
50 | |
51 func (s *STRTree) deserializeBBoxes(r *bufio.Reader) error { | |
52 | |
53 var numBBoxes int32 | |
54 if err := binary.Read(r, binary.LittleEndian, &numBBoxes); err != nil { | |
55 return err | |
56 } | |
57 | |
58 bboxes := make([]Box2D, numBBoxes) | |
59 s.bboxes = bboxes | |
60 | |
61 var err error | |
62 | |
63 read := func(v *float64) { | |
64 if err == nil { | |
65 err = binary.Read(r, binary.LittleEndian, v) | |
66 } | |
67 } | |
68 | |
69 for i := range bboxes { | |
70 read(&bboxes[i].X1) | |
71 read(&bboxes[i].Y1) | |
72 read(&bboxes[i].X2) | |
73 read(&bboxes[i].Y2) | |
74 } | |
75 | |
76 return err | |
77 } | 54 } |
78 | 55 |
79 func (s *STRTree) deserialize(r *bufio.Reader, version *int64) error { | 56 func (s *STRTree) deserialize(r *bufio.Reader, version *int64) error { |
80 | 57 |
81 header, err := r.Peek(8) | 58 header, err := r.Peek(8) |
100 return s.deserializeV1(r) | 77 return s.deserializeV1(r) |
101 } | 78 } |
102 // TODO: Implement me! | 79 // TODO: Implement me! |
103 return errors.New("not implemented, yet") | 80 return errors.New("not implemented, yet") |
104 } | 81 } |
105 | |
106 func (s *STRTree) deserializeV1(r *bufio.Reader) error { | |
107 s.tin = new(Tin) | |
108 if err := s.tin.Deserialize(r); err != nil { | |
109 return err | |
110 } | |
111 var numEntries uint8 | |
112 if err := binary.Read(r, binary.LittleEndian, &numEntries); err != nil { | |
113 return err | |
114 } | |
115 s.Entries = int(numEntries) | |
116 | |
117 if err := s.deserializeIndex(r); err != nil { | |
118 return err | |
119 } | |
120 | |
121 return s.deserializeBBoxes(r) | |
122 } | |
123 | |
124 // FromBytes restores a STRTree from a binary representation. | |
125 func (s *STRTree) FromBytes(data []byte, version *int64) error { | |
126 r, err := gzip.NewReader(bytes.NewReader(data)) | |
127 if err != nil { | |
128 return err | |
129 } | |
130 return s.deserialize(bufio.NewReader(r), version) | |
131 } | |
132 | |
133 // Deserialize restores a TIN from a binary representation. | |
134 func (t *Tin) Deserialize(r *bufio.Reader) error { | |
135 | |
136 if err := binary.Read(r, binary.LittleEndian, &t.EPSG); err != nil { | |
137 return err | |
138 } | |
139 | |
140 log.Infof("EPSG: %d\n", t.EPSG) | |
141 | |
142 if err := t.Min.Read(r); err != nil { | |
143 return err | |
144 } | |
145 | |
146 if err := t.Max.Read(r); err != nil { | |
147 return err | |
148 } | |
149 | |
150 log.Infof("BBOX: [[%f, %f, %f], [%f, %f, %f]]\n", | |
151 t.Min.X, t.Min.Y, t.Min.Z, | |
152 t.Max.X, t.Max.Y, t.Max.Z) | |
153 | |
154 var numVertices uint32 | |
155 if err := binary.Read(r, binary.LittleEndian, &numVertices); err != nil { | |
156 return err | |
157 } | |
158 | |
159 log.Infof("vertices: %d\n", numVertices) | |
160 | |
161 vertices := make([]Vertex, numVertices) | |
162 t.Vertices = vertices | |
163 | |
164 for i := range vertices { | |
165 if err := vertices[i].Read(r); err != nil { | |
166 return err | |
167 } | |
168 } | |
169 | |
170 var numTriangles uint32 | |
171 if err := binary.Read(r, binary.LittleEndian, &numTriangles); err != nil { | |
172 return err | |
173 } | |
174 | |
175 log.Infof("triangles: %d\n", numTriangles) | |
176 | |
177 indices := make([]int32, 3*numTriangles) | |
178 triangles := make([][]int32, numTriangles) | |
179 t.Triangles = triangles | |
180 | |
181 var last int32 | |
182 | |
183 for i := range triangles { | |
184 tri := indices[:3] | |
185 indices = indices[3:] | |
186 triangles[i] = tri | |
187 for j := range tri { | |
188 v, err := binary.ReadVarint(r) | |
189 if err != nil { | |
190 return err | |
191 } | |
192 value := int32(v) + last | |
193 tri[j] = value | |
194 last = value | |
195 } | |
196 } | |
197 | |
198 return nil | |
199 } |