annotate pkg/mesh/meshserialize_v2.go @ 5686:c33a5354328d sr-v2

Add stubs for de/serialize v2 meshes.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 12 Feb 2024 01:17:22 +0100
parents 0ee8ace01b60
children 8ff842858434
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5680
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2024 by via donau
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package mesh
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
5684
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
16 import (
5686
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
17 "bufio"
5684
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
18 "cmp"
5686
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
19 "errors"
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
20 "io"
5684
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
21 "math"
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
22 "slices"
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
23 "sort"
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
24 )
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
25
5686
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
26 func (s *STRTree) serializeV2(w io.Writer) error {
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
27 // TODO: implement me!
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
28 return errors.New("not implemented, yet")
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
29 }
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
30
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
31 func (s *STRTree) deserializeV2(r *bufio.Reader) error {
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
32 // TODO: implement me!
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
33 return errors.New("not implemented, yet")
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
34 }
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
35
c33a5354328d Add stubs for de/serialize v2 meshes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5685
diff changeset
36 // optimizeForSerializationV2 prepares the mesh to be stored in V2.
5680
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 func (s *STRTree) optimizeForSerializationV2() {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 s.removeUnusedTriangles()
5684
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
39 s.sortVertices()
5682
33499bd1b829 Sort indices in spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5680
diff changeset
40 s.sortIndices()
5680
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 }
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 // removeUnusedTriangles removes all triangles from the
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 // TIN that are not registered in the spatial index.
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 func (s *STRTree) removeUnusedTriangles() {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 used := make([]bool, len(s.tin.Triangles))
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 unused := len(used)
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 s.allTriangleIndices(func(indices []int32) {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 for _, idx := range indices {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 used[idx] = true
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 unused-- // They only occur once in the spatial index.
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 }
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 })
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 if unused <= 0 {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 return
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 }
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 newTris := make([][]int32, 0, len(s.tin.Triangles)-unused)
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 remap := map[int32]int32{}
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 for idx, tri := range s.tin.Triangles {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 if used[idx] {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 remap[int32(idx)] = int32(len(newTris))
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 newTris = append(newTris, tri)
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 }
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 s.tin.Triangles = newTris
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 // Update the spatial index as the gaps are now closed.
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 s.allTriangleIndices(func(indices []int32) {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 for i, idx := range indices {
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 indices[i] = remap[idx]
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 })
a87900c0fd11 Remove triangles that are not registered in the spatial index.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 }
5684
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
73
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
74 // sortVertices so that the deltas between neighbors are minimized.
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
75 func (s *STRTree) sortVertices() {
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
76 vertices := s.tin.Vertices
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
77 indices := make([]int32, len(vertices))
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
78 for i := range len(indices) {
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
79 indices[i] = int32(i)
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
80 }
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
81
5685
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
82 // Take aspect ratio of bbox into account.
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
83 width := s.tin.Max.X - s.tin.Min.X
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
84 height := s.tin.Max.Y - s.tin.Min.Y
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
85 if width == 0 || height == 0 {
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
86 return
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
87 }
5684
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
88
5685
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
89 // v = len(vertices)
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
90 // r = max(w, h)/min(w, h)
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
91 // n * t = v <=> n = v/t
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
92 // n / t = r <=> n = r*t
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
93 // v/t = r*t
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
94 // v = r*t^2
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
95 // v/r = t^2
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
96 // t = sqrt(v/r)
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
97 r := float64(max(width, height)) / float64(min(width, height))
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
98 t := int(math.Ceil(math.Sqrt(float64(len(vertices)) / r)))
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
99
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
100 var d1, d2 func(int32, int32) int
5684
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
101
5685
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
102 if width > height {
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
103 // Sort in X first and slices alternating up and down
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
104 slices.SortFunc(indices, func(a, b int32) int {
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
105 return cmp.Compare(vertices[a].X, vertices[b].X)
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
106 })
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
107 d1 = func(a, b int32) int { return cmp.Compare(vertices[a].Y, vertices[b].Y) }
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
108 d2 = func(a, b int32) int { return cmp.Compare(vertices[b].Y, vertices[a].Y) }
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
109 } else {
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
110 // Sort in Y first and slices alternating left and right
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
111 slices.SortFunc(indices, func(a, b int32) int {
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
112 return cmp.Compare(vertices[a].Y, vertices[b].Y)
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
113 })
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
114 d1 = func(a, b int32) int { return cmp.Compare(vertices[a].X, vertices[b].X) }
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
115 d2 = func(a, b int32) int { return cmp.Compare(vertices[b].X, vertices[a].X) }
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
116 }
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
117
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
118 for p := indices; len(p) > 0; d1, d2 = d2, d1 {
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
119 l := min(len(p), t)
0ee8ace01b60 When sorting vertices by x/y take aspectio ratio of bbox into acount.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5684
diff changeset
120 slices.SortStableFunc(p[:l], d2)
5684
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
121 p = p[l:]
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
122 }
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
123
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
124 // We really need to to sort the vertices actually, too.
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
125 sort.Slice(vertices, func(i, j int) bool { return indices[i] < indices[j] })
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
126
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
127 reindices := make([]int32, len(indices))
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
128 for i, idx := range indices {
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
129 reindices[idx] = int32(i)
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
130 }
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
131
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
132 // Update the indices in the triangles.
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
133 for _, tri := range s.tin.Triangles {
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
134 for i, idx := range tri {
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
135 tri[i] = reindices[idx]
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
136 }
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
137 }
536e842d9bfa Reorder vertices in tins to minimize delta distances.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5682
diff changeset
138 }