Mercurial > gemma
annotate pkg/octree/strtree.go @ 4646:89a72e0e2f9b stree-experiment
Add a method to serialize an STRTree.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 13 Oct 2019 15:45:39 +0200 |
parents | a1a9b1eab57c |
children | 18331577a251 |
rev | line source |
---|---|
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package octree |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
4646
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
17 "bytes" |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
18 "encoding/binary" |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
19 "io" |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
20 "log" |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "math" |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 "sort" |
4646
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
23 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
24 "github.com/pierrec/lz4" |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 ) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
27 const STRTreeDefaultEntries = 8 |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 type STRTree struct { |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
30 Entries int |
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
31 tin *Tin |
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
32 index []int32 |
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
33 bboxes []Box2D |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 |
4641
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
36 func (s *STRTree) Value(x, y float64) (float64, bool) { |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
37 |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
38 stack := []int32{s.index[0]} |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
39 |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
40 vertices := s.tin.Vertices |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
41 |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
42 for len(stack) > 0 { |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
43 top := stack[len(stack)-1] |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
44 stack = stack[:len(stack)-1] |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
45 |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
46 if top > 0 { // node |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
47 if s.bbox(top).Contains(x, y) { |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
48 n := s.index[top+1] |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
49 stack = append(stack, s.index[top+2:top+2+n]...) |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
50 } |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
51 } else { // leaf |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
52 top = -top - 1 |
4646
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
53 if !s.bbox(top).Contains(x, y) { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
54 continue |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
55 } |
4641
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
56 for i, n := int32(0), s.index[top+1]; i < n; i++ { |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
57 idx := s.index[top+2+i] |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
58 ti := s.tin.Triangles[idx] |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
59 t := Triangle{ |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
60 vertices[ti[0]], |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
61 vertices[ti[1]], |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
62 vertices[ti[2]], |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
63 } |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
64 if t.Contains(x, y) { |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
65 return t.Plane3D().Z(x, y), true |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
66 } |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
67 } |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
68 } |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
69 } |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
70 return 0, false |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
71 } |
5ef04ae34872
Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4592
diff
changeset
|
72 |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
73 func (s *STRTree) Build(t *Tin) { |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
75 if s.Entries == 0 { |
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
76 s.Entries = STRTreeDefaultEntries |
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
77 } |
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
78 |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
79 s.tin = t |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
81 all := make([]int32, len(t.Triangles)) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 for i := range all { |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 all[i] = int32(i) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 s.index = append(s.index, 0) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 root := s.build(all) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 s.index[0] = root |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 |
4592
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
94 func (s *STRTree) BuildWithout(t *Tin, remove map[int32]struct{}) { |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
95 |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
96 if s.Entries == 0 { |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
97 s.Entries = STRTreeDefaultEntries |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
98 } |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
99 |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
100 s.tin = t |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
101 |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
102 all := make([]int32, 0, len(t.Triangles)-len(remove)) |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
103 |
4642
b5d9647c5bc1
Fixed construction of clipped STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4641
diff
changeset
|
104 for i := 0; i < len(t.Triangles); i++ { |
4592
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
105 idx := int32(i) |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
106 if _, found := remove[idx]; !found { |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
107 all = append(all, idx) |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
108 } |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
109 } |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
110 |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
111 s.index = append(s.index, 0) |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
112 |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
113 root := s.build(all) |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
114 |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
115 s.index[0] = root |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
116 } |
680f1d8802f0
STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4591
diff
changeset
|
117 |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
118 func (s *STRTree) Clip(p *Polygon) map[int32]struct{} { |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
119 |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
120 removed := make(map[int32]struct{}) |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
121 |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
122 stack := []int32{s.index[0]} |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
123 |
2521
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
124 vertices := s.tin.Vertices |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
125 |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
126 for len(stack) > 0 { |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
127 top := stack[len(stack)-1] |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
128 stack = stack[:len(stack)-1] |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
129 |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
130 if top > 0 { // node |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
131 switch p.IntersectionBox2D(s.bbox(top)) { |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
132 case IntersectionInside: |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
133 // all triangles are inside polygon |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
134 case IntersectionOutSide: |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
135 // all triangles are outside polygon |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
136 s.allTriangles(top, removed) |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
137 default: |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
138 // mixed bag |
4643
a1a9b1eab57c
Use append/slicing trick to simplify tree traversals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4642
diff
changeset
|
139 n := s.index[top+1] |
a1a9b1eab57c
Use append/slicing trick to simplify tree traversals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4642
diff
changeset
|
140 stack = append(stack, s.index[top+2:top+2+n]...) |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
141 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
142 } else { // leaf |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
143 top = -top - 1 |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
144 for i, n := int32(0), s.index[top+1]; i < n; i++ { |
2521
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
145 idx := s.index[top+2+i] |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
146 ti := s.tin.Triangles[idx] |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
147 t := Triangle{ |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
148 vertices[ti[0]], |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
149 vertices[ti[1]], |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
150 vertices[ti[2]], |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
151 } |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
152 if p.IntersectionWithTriangle(&t) != IntersectionInside { |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
153 removed[idx] = struct{}{} |
e26000628764
Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2516
diff
changeset
|
154 } |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
155 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
156 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
157 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
158 |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
159 return removed |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
160 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
161 |
4646
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
162 func (s *STRTree) serializeIndex(w io.Writer) error { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
163 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
164 if err := binary.Write(w, binary.LittleEndian, int32(len(s.index))); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
165 return err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
166 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
167 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
168 if err := binary.Write(w, binary.LittleEndian, s.index[0]); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
169 return err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
170 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
171 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
172 var buf [binary.MaxVarintLen32]byte |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
173 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
174 var last int32 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
175 var written int |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
176 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
177 for _, x := range s.index[1:] { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
178 delta := x - last |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
179 n := binary.PutVarint(buf[:], int64(delta)) |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
180 for p := buf[:n]; len(p) > 0; p = p[n:] { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
181 var err error |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
182 if n, err = w.Write(p); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
183 return err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
184 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
185 written += n |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
186 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
187 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
188 last = x |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
189 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
190 log.Printf("info: compressed index in bytes: %d %.2f (%d %.2f)\n", |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
191 written, |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
192 float64(written)/(1024*1024), |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
193 4*len(s.index), |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
194 float64(4*len(s.index))/(1024*1024), |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
195 ) |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
196 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
197 return nil |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
198 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
199 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
200 func (s *STRTree) serializeBBoxes(w io.Writer) (rr error) { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
201 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
202 if err := binary.Write(w, binary.LittleEndian, int32(len(s.bboxes))); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
203 return err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
204 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
205 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
206 var err error |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
207 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
208 write := func(v float64) { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
209 if err == nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
210 err = binary.Write(w, binary.LittleEndian, math.Float64bits(v)) |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
211 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
212 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
213 for _, box := range s.bboxes { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
214 write(box.X1) |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
215 write(box.Y1) |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
216 write(box.X2) |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
217 write(box.Y2) |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
218 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
219 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
220 return err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
221 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
222 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
223 func (s *STRTree) Bytes() ([]byte, error) { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
224 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
225 var buf bytes.Buffer |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
226 w := lz4.NewWriter(&buf) |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
227 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
228 if err := s.tin.serialize(w); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
229 return nil, err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
230 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
231 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
232 if err := binary.Write(w, binary.LittleEndian, uint8(s.Entries)); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
233 return nil, err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
234 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
235 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
236 if err := s.serializeIndex(w); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
237 return nil, err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
238 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
239 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
240 if err := s.serializeBBoxes(w); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
241 return nil, err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
242 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
243 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
244 if err := w.Flush(); err != nil { |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
245 return nil, err |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
246 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
247 |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
248 return buf.Bytes(), nil |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
249 } |
89a72e0e2f9b
Add a method to serialize an STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4643
diff
changeset
|
250 |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
251 func (s *STRTree) allTriangles(pos int32, tris map[int32]struct{}) { |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
252 stack := []int32{pos} |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
253 for len(stack) > 0 { |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
254 top := stack[len(stack)-1] |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
255 stack = stack[:len(stack)-1] |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
256 if top > 0 { // node |
4643
a1a9b1eab57c
Use append/slicing trick to simplify tree traversals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4642
diff
changeset
|
257 n := s.index[top+1] |
a1a9b1eab57c
Use append/slicing trick to simplify tree traversals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4642
diff
changeset
|
258 stack = append(stack, s.index[top+2:top+2+n]...) |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
259 } else { // leaf |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
260 top = -top - 1 |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
261 for i, n := int32(0), s.index[top+1]; i < n; i++ { |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
262 tris[s.index[top+2+i]] = struct{}{} |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
263 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
264 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
265 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
266 } |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
267 |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
268 func (s *STRTree) build(items []int32) int32 { |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
269 sort.Slice(items, func(i, j int) bool { |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
270 ti := s.tin.Triangles[items[i]] |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
271 tj := s.tin.Triangles[items[j]] |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
272 return s.tin.Vertices[ti[0]].X < s.tin.Vertices[tj[0]].X |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
273 }) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
274 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
275 P := int(math.Ceil(float64(len(items)) / float64(s.Entries))) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
276 S := int(math.Ceil(math.Sqrt(float64(P)))) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
277 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
278 slices := s.strSplit(items, S) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
279 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
280 leaves := s.strJoin( |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
281 slices, S, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
282 func(i, j int32) bool { |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
283 ti := s.tin.Triangles[i] |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
284 tj := s.tin.Triangles[j] |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
285 return s.tin.Vertices[ti[0]].Y < s.tin.Vertices[tj[0]].Y |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
286 }, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
287 s.allocLeaf, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
288 ) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
289 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
290 return s.buildNodes(leaves) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
291 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
292 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
293 func (s *STRTree) buildNodes(items []int32) int32 { |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
294 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
295 if len(items) <= s.Entries { |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
296 return s.allocNode(items) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
297 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
298 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
299 sort.Slice(items, func(i, j int) bool { |
2515
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
300 return s.bbox(items[i]).X1 < s.bbox(items[j]).X1 |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
301 }) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
302 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
303 P := int(math.Ceil(float64(len(items)) / float64(s.Entries))) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
304 S := int(math.Ceil(math.Sqrt(float64(P)))) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
305 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
306 slices := s.strSplit(items, S) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
307 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
308 nodes := s.strJoin( |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
309 slices, S, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
310 func(i, j int32) bool { return s.bbox(i).Y1 < s.bbox(j).Y1 }, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
311 s.allocNode, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
312 ) |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
313 |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
314 return s.buildNodes(nodes) |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
315 } |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
316 |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
317 func (s *STRTree) bbox(idx int32) Box2D { |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
318 if idx < 0 { // Don't care if leaf or node. |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
319 idx = -idx - 1 |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
320 } |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
321 return s.bboxes[s.index[idx]] |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
322 } |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
323 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
324 func (s *STRTree) strSplit(items []int32, S int) [][]int32 { |
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
325 sm := S * s.Entries |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
326 slices := make([][]int32, S) |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
327 for i := range slices { |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
328 var n int |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
329 if l := len(items); l < sm { |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
330 n = l |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
331 } else { |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
332 n = sm |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
333 } |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
334 slices[i] = items[:n] |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
335 items = items[n:] |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
336 } |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
337 return slices |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
338 } |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
339 |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
340 func (s *STRTree) strJoin( |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
341 slices [][]int32, S int, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
342 less func(int32, int32) bool, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
343 alloc func([]int32) int32, |
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
344 ) []int32 { |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
345 nodes := make([]int32, 0, S*S) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
346 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
347 for _, slice := range slices { |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
348 sort.Slice(slice, func(i, j int) bool { |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
349 return less(slice[i], slice[j]) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
350 }) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
351 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
352 for len(slice) > 0 { |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
353 var n int |
4591
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
354 if l := len(slice); l >= s.Entries { |
f456ce0a6a0e
STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2565
diff
changeset
|
355 n = s.Entries |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
356 } else { |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
357 n = l |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
358 } |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
359 nodes = append(nodes, alloc(slice[:n])) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
360 slice = slice[n:] |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
361 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
362 } |
2565
114979e97a6c
STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2564
diff
changeset
|
363 return nodes |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
364 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
365 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
366 func (s *STRTree) allocNode(items []int32) int32 { |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
367 pos := len(s.index) |
2515
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
368 s.index = append(s.index, 0, int32(len(items))) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
369 s.index = append(s.index, items...) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
370 if len(items) > 0 { |
2515
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
371 box := s.bbox(items[0]) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
372 for i := 1; i < len(items); i++ { |
2515
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
373 box = box.Union(s.bbox(items[i])) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
374 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
375 s.index[pos] = int32(s.allocBBox(box)) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
376 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
377 return int32(pos) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
378 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
379 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
380 func (s *STRTree) allocBBox(box Box2D) int { |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
381 pos := len(s.bboxes) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
382 s.bboxes = append(s.bboxes, box) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
383 return pos |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
384 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
385 |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
386 func (s *STRTree) allocLeaf(items []int32) int32 { |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
387 pos := len(s.index) |
2515
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
388 s.index = append(s.index, 0, int32(len(items))) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
389 s.index = append(s.index, items...) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
390 if len(items) > 0 { |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
391 vertices := s.tin.Vertices |
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
392 ti := s.tin.Triangles[items[0]] |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
393 t := Triangle{ |
2515
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
394 vertices[ti[0]], |
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
395 vertices[ti[1]], |
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
396 vertices[ti[2]], |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
397 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
398 box := t.BBox() |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
399 for i := 1; i < len(items); i++ { |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
400 it := items[i] |
2516
1ec4c5633eb6
Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2515
diff
changeset
|
401 ti := s.tin.Triangles[it] |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
402 t := Triangle{ |
2515
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
403 vertices[ti[0]], |
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
404 vertices[ti[1]], |
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
405 vertices[ti[2]], |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
406 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
407 box = box.Union(t.BBox()) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
408 } |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
409 s.index[pos] = int32(s.allocBBox(box)) |
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
410 } |
2515
6bcaa8bf2603
STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2513
diff
changeset
|
411 return int32(-(pos + 1)) |
2512
2768f74d54ab
Added an STRTree for the triangulation. Should be better suited for filtering out the clipped triangles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
412 } |