annotate pkg/mesh/strtree.go @ 5688:6281c18b109f sr-v2

Finsh serializing v2 meshes.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 12 Feb 2024 02:27:41 +0100
parents 33499bd1b829
children a68e8eae7273
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
4827
f4abfd0ee8ad Renamed octree package to mesh as there is no octree any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4768
diff changeset
14 package mesh
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
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 (
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
17 "cmp"
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
18 "math"
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
19 "slices"
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
20 )
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
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
22 // STRTreeDefaultEntries is the default number of children per node and leaf.
4591
f456ce0a6a0e STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2565
diff changeset
23 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
24
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
25 // STRTree is a Sort Tile Recurse tree.
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
26 type STRTree struct {
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
27 // Entries is the total number of entries.
4591
f456ce0a6a0e STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2565
diff changeset
28 Entries int
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
29 // tin is a reference to the TIN this tree is build from.
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
30 tin *Tin
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
31 // index is the internal tree representation.
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
32 index []int32
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
33 // bboxes are the bounding boxes used in nodes and leaves.
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
34 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
35 }
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
36
4658
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
37 // Vertical does a vertical cross cut from (x1, y1) to (x2, y2).
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
38 func (s *STRTree) Vertical(x1, y1, x2, y2 float64, fn func(*Triangle)) {
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
39 box := Box2D{
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
40 X1: math.Min(x1, x2),
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
41 Y1: math.Min(y1, y2),
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
42 X2: math.Max(x1, x2),
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
43 Y2: math.Max(y1, y2),
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
44 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
45
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
46 // out of bounding box
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
47 if box.X2 < s.tin.Min.X || s.tin.Max.X < box.X1 ||
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
48 box.Y2 < s.tin.Min.Y || s.tin.Max.Y < box.Y1 {
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
49 return
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
50 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
51
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
52 line := NewPlane2D(x1, y1, x2, y2)
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
53
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
54 vertices := s.tin.Vertices
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
55
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
56 stack := []int32{s.index[0]}
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
57
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
58 for len(stack) > 0 {
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
59 top := stack[len(stack)-1]
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
60 stack = stack[:len(stack)-1]
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
61
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
62 if top > 0 { // node
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
63 if b := s.bbox(top); b.Intersects(box) && b.IntersectsPlane(line) {
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
64 n := s.index[top+1]
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
65 stack = append(stack, s.index[top+2:top+2+n]...)
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
66 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
67 } else { // leaf
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
68 top = -top - 1
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
69 if b := s.bbox(top); !b.Intersects(box) || !b.IntersectsPlane(line) {
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
70 continue
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
71 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
72 n := s.index[top+1]
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
73 for _, idx := range s.index[top+2 : top+2+n] {
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
74 ti := s.tin.Triangles[idx]
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
75 t := Triangle{
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
76 vertices[ti[0]],
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
77 vertices[ti[1]],
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
78 vertices[ti[2]],
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
79 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
80 v0 := line.Eval(t[0].X, t[0].Y)
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
81 v1 := line.Eval(t[1].X, t[1].Y)
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
82 v2 := line.Eval(t[2].X, t[2].Y)
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
83
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
84 if onPlane(v0) || onPlane(v1) || onPlane(v2) ||
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
85 sides(sides(sides(0, v0), v1), v2) == 3 {
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
86 fn(&t)
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
87 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
88 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
89 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
90 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
91 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
92
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
93 // Min return the minimum vertex of the TIN this tree is build from.
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
94 func (s *STRTree) Min() Vertex { return s.tin.Min }
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
95
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
96 // Max return the maximum vertex of the TIN this tree is build from.
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
97 func (s *STRTree) Max() Vertex { return s.tin.Max }
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
98
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
99 // EPSG return the EPSF code of the TIN this tree is build from.
4654
3eda5a7215ab Generate STRTrees instaead of Octree during sounding result imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4652
diff changeset
100 func (s *STRTree) EPSG() uint32 { return s.tin.EPSG }
3eda5a7215ab Generate STRTrees instaead of Octree during sounding result imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4652
diff changeset
101
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
102 // Value returns the Z Value for a given X/Y point.
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
103 // Second return value is false if the point is out of bound.
4641
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
104 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
105
4658
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
106 if len(s.index) == 0 {
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
107 return 0, false
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
108 }
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
109
4641
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
110 stack := []int32{s.index[0]}
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
111
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
112 vertices := s.tin.Vertices
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
113
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
114 for len(stack) > 0 {
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
115 top := stack[len(stack)-1]
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
116 stack = stack[:len(stack)-1]
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
117
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
118 if top > 0 { // node
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
119 if s.bbox(top).Contains(x, y) {
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
120 n := s.index[top+1]
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
121 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
122 }
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
123 } else { // leaf
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
124 top = -top - 1
4645
946689a56fc2 Forgot a bbox check when evaluating STRTree speeding it significantly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4643
diff changeset
125 if !s.bbox(top).Contains(x, y) {
946689a56fc2 Forgot a bbox check when evaluating STRTree speeding it significantly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4643
diff changeset
126 continue
946689a56fc2 Forgot a bbox check when evaluating STRTree speeding it significantly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4643
diff changeset
127 }
4658
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
128 n := s.index[top+1]
4bbfe3dd2ab5 Completed usage of STRTrees.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4654
diff changeset
129 for _, idx := range s.index[top+2 : top+2+n] {
4641
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
130 ti := s.tin.Triangles[idx]
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
131 t := Triangle{
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
132 vertices[ti[0]],
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
133 vertices[ti[1]],
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
134 vertices[ti[2]],
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
135 }
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
136 if t.Contains(x, y) {
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
137 return t.Plane3D().Z(x, y), true
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
138 }
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
139 }
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
140 }
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
141 }
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
142 return 0, false
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
143 }
5ef04ae34872 Used STRTree for caluculating the diff.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4592
diff changeset
144
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
145 // Build builds a tree for a given TIN.
2516
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
146 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
147
4591
f456ce0a6a0e STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2565
diff changeset
148 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
149 s.Entries = STRTreeDefaultEntries
f456ce0a6a0e STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2565
diff changeset
150 }
f456ce0a6a0e STRTree: Made number of entries per node a parameter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2565
diff changeset
151
2516
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
152 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
153
2516
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
154 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
155
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
156 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
157 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
158 }
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
159
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
160 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
161
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
162 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
163
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
164 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
165 }
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
166
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
167 // BuildWithout builds a tree for a given TIN ignoring the triangles
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
168 // with the indices given in the remove map.
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
169 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
170
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
171 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
172 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
173 }
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
174
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
175 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
176
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
177 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
178
4642
b5d9647c5bc1 Fixed construction of clipped STRTree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4641
diff changeset
179 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
180 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
181 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
182 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
183 }
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
184 }
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
185
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
186 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
187
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
188 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
189
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
190 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
191 }
680f1d8802f0 STRTree: Add a builder that ignores triangles which are filtered out before.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4591
diff changeset
192
4855
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
193 // Clip returns the indices of the triangles of the TIN
01b593ea2311 Added missing doc strings for the STR tree in the mesh package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
194 // which are not fully covered by the given polygon.
2516
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
195 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
196
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
197 removed := make(map[int32]struct{})
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
198
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
199 stack := []int32{s.index[0]}
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
200
2521
e26000628764 Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2516
diff changeset
201 vertices := s.tin.Vertices
e26000628764 Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2516
diff changeset
202
2516
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
203 for len(stack) > 0 {
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
204 top := stack[len(stack)-1]
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
205 stack = stack[:len(stack)-1]
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
206
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
207 if top > 0 { // node
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
208 switch p.IntersectionBox2D(s.bbox(top)) {
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
209 case IntersectionInside:
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
210 // all triangles are inside polygon
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
211 case IntersectionOutSide:
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
212 // all triangles are outside polygon
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
213 s.allTriangles(top, removed)
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
214 default:
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
215 // mixed bag
4643
a1a9b1eab57c Use append/slicing trick to simplify tree traversals.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4642
diff changeset
216 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
217 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
218 }
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
219 } else { // leaf
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
220 top = -top - 1
4703
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
221 switch p.IntersectionBox2D(s.bbox(top)) {
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
222 case IntersectionInside:
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
223 // all triangles are inside polygon
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
224 case IntersectionOutSide:
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
225 // all triangles are outside polygon
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
226 n := s.index[top+1]
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
227 for _, idx := range s.index[top+2 : top+2+n] {
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
228 removed[idx] = struct{}{}
2521
e26000628764 Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2516
diff changeset
229 }
4703
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
230 default:
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
231 n := s.index[top+1]
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
232 for _, idx := range s.index[top+2 : top+2+n] {
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
233 ti := s.tin.Triangles[idx]
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
234 t := Triangle{
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
235 vertices[ti[0]],
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
236 vertices[ti[1]],
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
237 vertices[ti[2]],
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
238 }
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
239 if p.IntersectionWithTriangle(&t) != IntersectionInside {
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
240 removed[idx] = struct{}{}
6e179b338f1a STRTree: Improve usage of bounding boxes in leaves of the tree. Speeds stuff 10x up.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4658
diff changeset
241 }
2521
e26000628764 Clip triangle in STR tree leaves correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2516
diff changeset
242 }
2516
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
243 }
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
244 }
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
245 }
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
246
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
247 return removed
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
248 }
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
249
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
250 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
251 stack := []int32{pos}
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
252 for len(stack) > 0 {
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
253 top := stack[len(stack)-1]
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
254 stack = stack[:len(stack)-1]
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
255 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
256 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
257 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
258 } else { // leaf
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
259 top = -top - 1
4704
9eb708176b43 STRTree: more efficent usage of slicing in collecting triangles during clipping.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4703
diff changeset
260 n := s.index[top+1]
9eb708176b43 STRTree: more efficent usage of slicing in collecting triangles during clipping.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4703
diff changeset
261 for _, idx := range s.index[top+2 : top+2+n] {
9eb708176b43 STRTree: more efficent usage of slicing in collecting triangles during clipping.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4703
diff changeset
262 tris[idx] = struct{}{}
2516
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 {
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
269 slices.SortFunc(items, func(i, j int32) int {
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
270 ti := s.tin.Triangles[i]
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
271 tj := s.tin.Triangles[j]
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
272 return cmp.Compare(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,
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
282 func(i, j int32) int {
2565
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]
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
285 return cmp.Compare(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
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
299 slices.SortFunc(items, func(i, j int32) int {
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
300 return cmp.Compare(s.bbox(i).X1, s.bbox(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,
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
310 func(i, j int32) int { return cmp.Compare(s.bbox(i).Y1, s.bbox(j).Y1) },
2565
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(
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
341 slics [][]int32, S int,
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
342 cmp func(int32, int32) int,
2565
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
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
347 for _, slice := range slics {
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
348 slices.SortFunc(slice, cmp)
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
349
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 for len(slice) > 0 {
2565
114979e97a6c STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2564
diff changeset
351 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
352 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
353 n = s.Entries
2565
114979e97a6c STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2564
diff changeset
354 } 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
355 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
356 }
2565
114979e97a6c STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2564
diff changeset
357 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
358 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
359 }
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 }
2565
114979e97a6c STR tree: More code simplification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2564
diff changeset
361 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
362 }
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
363
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 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
365 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
366 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
367 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
368 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
369 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
370 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
371 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
372 }
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
373 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
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 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
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
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 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
379 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
380 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
381 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
382 }
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
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 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
385 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
386 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
387 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
388 if len(items) > 0 {
2516
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
389 vertices := s.tin.Vertices
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
390 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
391 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
392 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
393 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
394 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
395 }
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
396 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
397 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
398 it := items[i]
2516
1ec4c5633eb6 Clip STR tree and not Octree.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2515
diff changeset
399 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
400 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
401 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
402 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
403 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
404 }
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
405 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
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 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
408 }
2515
6bcaa8bf2603 STR tree: Fixed sorting. Stored num items per nodes. Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2513
diff changeset
409 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
410 }
5680
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
411
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
412 // allTriangleIndices passes all triangle indices to callback fn.
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
413 func (s *STRTree) allTriangleIndices(fn func([]int32)) {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
414 stack := []int32{s.index[0]}
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
415 for len(stack) > 0 {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
416 top := stack[len(stack)-1]
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
417 stack = stack[:len(stack)-1]
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
418 if top > 0 { // node
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
419 n := s.index[top+1]
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
420 stack = append(stack, s.index[top+2:top+2+n]...)
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
421 } else { // leaf
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
422 top = -top - 1
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
423 n := s.index[top+1]
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
424 fn(s.index[top+2 : top+2+n])
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
425 }
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
426 }
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5676
diff changeset
427 }
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
428
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
429 // sortIndices sorts the indices in the inner nodes and leaves of the tree.
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
430 // This helps if the index array is delta encoded in serialization.
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
431 // It also may help reduding CPU cache line misses during usage because
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
432 // redirects are ordered closer together.
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
433 func (s *STRTree) sortIndices() {
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
434 stack := []int32{s.index[0]}
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
435 for len(stack) > 0 {
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
436 top := stack[len(stack)-1]
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
437 stack = stack[:len(stack)-1]
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
438 if top > 0 { // node
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
439 n := s.index[top+1]
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
440 children := s.index[top+2 : top+2+n]
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
441 stack = append(stack, children...)
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
442 slices.Sort(children)
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
443 } else { // leaf
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
444 top = -top - 1
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
445 n := s.index[top+1]
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
446 triangles := s.index[top+2 : top+2+n]
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
447 slices.Sort(triangles)
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
448 }
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
449 }
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
450 }