Mercurial > gemma
comparison cmd/tin2octree/tin.go @ 729:b0bd242ff821
Removed vertex duplicate.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Sat, 22 Sep 2018 22:14:31 +0200 |
parents | a3d722e1f593 |
children |
comparison
equal
deleted
inserted
replaced
728:e9d37300ce99 | 729:b0bd242ff821 |
---|---|
6 "errors" | 6 "errors" |
7 "fmt" | 7 "fmt" |
8 "io" | 8 "io" |
9 "log" | 9 "log" |
10 "math" | 10 "math" |
11 | |
12 "gemma.intevation.de/gemma/pkg/octree" | |
11 ) | 13 ) |
12 | 14 |
13 var ( | 15 var ( |
14 errNoByteSlice = errors.New("Not a byte slice") | 16 errNoByteSlice = errors.New("Not a byte slice") |
15 errTooLessPoints = errors.New("Too less points") | 17 errTooLessPoints = errors.New("Too less points") |
24 | 26 |
25 const wgs84 = 4326 | 27 const wgs84 = 4326 |
26 | 28 |
27 type tin struct { | 29 type tin struct { |
28 epsg uint32 | 30 epsg uint32 |
29 vertices []vertex | 31 vertices []octree.Vertex |
30 triangles [][]int32 | 32 triangles [][]int32 |
31 | 33 |
32 min vertex | 34 min octree.Vertex |
33 max vertex | 35 max octree.Vertex |
34 } | 36 } |
35 | 37 |
36 func (t *tin) FromWKB(data []byte) error { | 38 func (t *tin) FromWKB(data []byte) error { |
37 log.Printf("data length %d\n", len(data)) | 39 log.Printf("data length %d\n", len(data)) |
38 | 40 |
66 var num uint32 | 68 var num uint32 |
67 if err = binary.Read(r, order, &num); err != nil { | 69 if err = binary.Read(r, order, &num); err != nil { |
68 return err | 70 return err |
69 } | 71 } |
70 | 72 |
71 vertices := make([]vertex, 0, 100000) | 73 vertices := make([]octree.Vertex, 0, 100000) |
72 | 74 |
73 var v vertex | 75 var v octree.Vertex |
74 | 76 |
75 v2i := make(map[vertex]int32, 100000) | 77 v2i := make(map[octree.Vertex]int32, 100000) |
76 | 78 |
77 var indexPool []int32 | 79 var indexPool []int32 |
78 | 80 |
79 allocIndices := func() []int32 { | 81 allocIndices := func() []int32 { |
80 if len(indexPool) == 0 { | 82 if len(indexPool) == 0 { |
85 return ids | 87 return ids |
86 } | 88 } |
87 | 89 |
88 var triangles [][]int32 | 90 var triangles [][]int32 |
89 | 91 |
90 min := vertex{math.MaxFloat64, math.MaxFloat64, math.MaxFloat64} | 92 min := octree.Vertex{math.MaxFloat64, math.MaxFloat64, math.MaxFloat64} |
91 max := vertex{-math.MaxFloat64, -math.MaxFloat64, -math.MaxFloat64} | 93 max := octree.Vertex{-math.MaxFloat64, -math.MaxFloat64, -math.MaxFloat64} |
92 | 94 |
93 for i := uint32(0); i < num; i++ { | 95 for i := uint32(0); i < num; i++ { |
94 | 96 |
95 endian, err = r.ReadByte() | 97 endian, err = r.ReadByte() |
96 switch { | 98 switch { |
139 // Don't store the forth point. | 141 // Don't store the forth point. |
140 continue | 142 continue |
141 } | 143 } |
142 // Do this conversion later to spare reflect calls | 144 // Do this conversion later to spare reflect calls |
143 // and allocs in binary.Read. | 145 // and allocs in binary.Read. |
144 v.x = math.Float64frombits(x) | 146 v.X = math.Float64frombits(x) |
145 v.y = math.Float64frombits(y) | 147 v.Y = math.Float64frombits(y) |
146 v.z = math.Float64frombits(z) | 148 v.Z = math.Float64frombits(z) |
147 idx, found := v2i[v] | 149 idx, found := v2i[v] |
148 if !found { | 150 if !found { |
149 idx = int32(len(vertices)) | 151 idx = int32(len(vertices)) |
150 v2i[v] = idx | 152 v2i[v] = idx |
151 vertices = append(vertices, v) | 153 vertices = append(vertices, v) |
152 min.minimize(v) | 154 min.Minimize(v) |
153 max.maximize(v) | 155 max.Maximize(v) |
154 } | 156 } |
155 triangle[p] = idx | 157 triangle[p] = idx |
156 } | 158 } |
157 } | 159 } |
158 triangles = append(triangles, triangle) | 160 triangles = append(triangles, triangle) |
159 } | 161 } |
160 | 162 |
161 log.Printf("bbox: [[%f, %f], [%f, %f]]\n", | 163 log.Printf("bbox: [[%f, %f], [%f, %f]]\n", |
162 min.x, min.y, max.x, max.y) | 164 min.X, min.Y, max.X, max.Y) |
163 | 165 |
164 *t = tin{ | 166 *t = tin{ |
165 epsg: wgs84, | 167 epsg: wgs84, |
166 vertices: vertices, | 168 vertices: vertices, |
167 triangles: triangles, | 169 triangles: triangles, |
179 return errNoByteSlice | 181 return errNoByteSlice |
180 } | 182 } |
181 return t.FromWKB(data) | 183 return t.FromWKB(data) |
182 } | 184 } |
183 | 185 |
184 func (v *vertex) write(w io.Writer) error { | |
185 for _, addr := range []*float64{&v.x, &v.y, &v.z} { | |
186 if err := binary.Write( | |
187 w, binary.LittleEndian, math.Float64bits(*addr)); err != nil { | |
188 return err | |
189 } | |
190 } | |
191 return nil | |
192 } | |
193 | |
194 func (t *tin) Serialize(w io.Writer) error { | 186 func (t *tin) Serialize(w io.Writer) error { |
195 | 187 |
196 if err := binary.Write(w, binary.LittleEndian, t.epsg); err != nil { | 188 if err := binary.Write(w, binary.LittleEndian, t.epsg); err != nil { |
197 return err | 189 return err |
198 } | 190 } |
199 | 191 |
200 if err := t.min.write(w); err != nil { | 192 if err := t.min.Write(w); err != nil { |
201 return err | 193 return err |
202 } | 194 } |
203 if err := t.max.write(w); err != nil { | 195 if err := t.max.Write(w); err != nil { |
204 return err | 196 return err |
205 } | 197 } |
206 | 198 |
207 if err := binary.Write( | 199 if err := binary.Write( |
208 w, binary.LittleEndian, uint32(len(t.vertices))); err != nil { | 200 w, binary.LittleEndian, uint32(len(t.vertices))); err != nil { |
209 return err | 201 return err |
210 } | 202 } |
211 | 203 |
212 for _, v := range t.vertices { | 204 for _, v := range t.vertices { |
213 if err := v.write(w); err != nil { | 205 if err := v.Write(w); err != nil { |
214 return err | 206 return err |
215 } | 207 } |
216 } | 208 } |
217 log.Printf("vertices %d (%d)\n", len(t.vertices), len(t.vertices)*3*8) | 209 log.Printf("vertices %d (%d)\n", len(t.vertices), len(t.vertices)*3*8) |
218 | 210 |