annotate pkg/mesh/polygon.go @ 5678:4abbb62d2bed sr-v2

Write mesh version to database.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 11 Feb 2024 10:25:50 +0100
parents 1222b777f51f
children 6270951dda28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2018 by via donau
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
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: 4769
diff changeset
14 package mesh
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
16 import (
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
17 "bytes"
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
18 "encoding/binary"
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
19 "fmt"
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
20 "math"
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
21
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
22 "github.com/tidwall/rtree"
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
23
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5320
diff changeset
24 "gemma.intevation.de/gemma/pkg/log"
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
25 "gemma.intevation.de/gemma/pkg/wkb"
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
26 )
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
27
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
28 type (
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
29 ring []float64
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
30
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
31 // Polygon has a border and holes.
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
32 // The line segments are spatially indexed.
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
33 Polygon struct {
2574
2833ff156cb2 Morphological differences: Moved loading of clipping polygon into octree package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2498
diff changeset
34 rings []ring
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
35 indices []*rtree.RTree
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
36 }
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
38 // IntersectionType represents an enum
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
39 // of the type of intersection.
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
40 IntersectionType byte
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
41
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
42 lineSegment []float64
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
43 )
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 const (
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
46 // IntersectionInside is inside the polygon.
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 IntersectionInside IntersectionType = iota
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
48 // IntersectionOutSide is outside the polygon.
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 IntersectionOutSide
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
50 // IntersectionOverlaps overlaps the polygon.
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 IntersectionOverlaps
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 )
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53
5320
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
54 func (ls lineSegment) Rect() ([2]float64, [2]float64) {
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
55
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
56 var min, max [2]float64
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
57
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
58 if ls[0] < ls[2] {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
59 min[0] = ls[0]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
60 max[0] = ls[2]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
61 } else {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
62 min[0] = ls[2]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
63 max[0] = ls[0]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
64 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
65
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
66 if ls[1] < ls[3] {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
67 min[1] = ls[1]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
68 max[1] = ls[3]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
69 } else {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
70 min[1] = ls[3]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
71 max[1] = ls[1]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
72 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
73
5320
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
74 return min, max
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
75 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
76
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
77 // Indexify creates a spatial index for thw polygon.
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
78 func (p *Polygon) Indexify() {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
79 indices := make([]*rtree.RTree, len(p.rings))
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
80
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
81 for i := range indices {
5320
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
82 index := new(rtree.RTree)
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
83 indices[i] = index
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
84
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
85 rng := p.rings[i]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
86
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
87 for i := 0; i < len(rng); i += 2 {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
88 var ls lineSegment
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
89 if i+4 <= len(rng) {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
90 ls = lineSegment(rng[i : i+4])
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
91 } else {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
92 ls = []float64{rng[i], rng[i+1], rng[0], rng[1]}
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
93 }
5320
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
94 min, max := ls.Rect()
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
95 index.Insert(min, max, ls)
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
96 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
97 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
98
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
99 p.indices = indices
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
100 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
101
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
102 func (ls lineSegment) intersects(a Box2D) bool {
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
103 p1x := ls[0]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
104 p1y := ls[1]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
105 p2x := ls[2]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
106 p2y := ls[3]
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
107
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
108 left := a.X1
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
109 right := a.X2
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
110 top := a.Y1
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
111 bottom := a.Y2
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
112
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
113 // The direction of the ray
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
114 dx := p2x - p1x
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
115 dy := p2y - p1y
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
116
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
117 min, max := 0.0, 1.0
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
118
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
119 var t0, t1 float64
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
120
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
121 // Left and right sides.
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
122 // - If the line is parallel to the y axis.
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
123 if dx == 0 {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
124 if p1x < left || p1x > right {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
125 return false
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
126 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
127 } else {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
128 // - Make sure t0 holds the smaller value by checking the direction of the line.
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
129 if dx > 0 {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
130 t0 = (left - p1x) / dx
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
131 t1 = (right - p1x) / dx
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
132 } else {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
133 t1 = (left - p1x) / dx
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
134 t0 = (right - p1x) / dx
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
135 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
136
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
137 if t0 > min {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
138 min = t0
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
139 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
140 if t1 < max {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
141 max = t1
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
142 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
143 if min > max || max < 0 {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
144 return false
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
145 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
146 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
147
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
148 // The top and bottom side.
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
149 // - If the line is parallel to the x axis.
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
150 if dy == 0 {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
151 if p1y < top || p1y > bottom {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
152 return false
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
153 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
154 } else {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
155 // - Make sure t0 holds the smaller value by checking the direction of the line.
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
156 if dy > 0 {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
157 t0 = (top - p1y) / dy
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
158 t1 = (bottom - p1y) / dy
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
159 } else {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
160 t1 = (top - p1y) / dy
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
161 t0 = (bottom - p1y) / dy
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
162 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
163
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
164 if t0 > min {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
165 min = t0
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
166 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
167 if t1 < max {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
168 max = t1
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
169 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
170 if min > max || max < 0 {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
171 return false
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
172 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
173 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
174
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
175 // The point of intersection
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
176 // ix = p1x + dx*min
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
177 // iy = p1y + dy*min
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
178 return true
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
179 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
180
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
181 func (ls lineSegment) intersectsLineSegment(o lineSegment) bool {
2495
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
182
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
183 p0 := ls[:2]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
184 p1 := ls[2:4]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
185 p2 := o[:2]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
186 p3 := o[2:4]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
187
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
188 s10x := p1[0] - p0[0]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
189 s10y := p1[1] - p0[1]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
190 s32x := p3[0] - p2[0]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
191 s32y := p3[1] - p2[1]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
192
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
193 den := s10x*s32y - s32x*s10y
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
194
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
195 if den == 0 {
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
196 return false
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
197 }
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
198
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
199 denPos := den > 0
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
200
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
201 s02x := p0[0] - p2[0]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
202 s02y := p0[1] - p2[1]
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
203
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
204 sNum := s10x*s02y - s10y*s02x
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
205 if sNum < 0 == denPos {
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
206 return false
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
207 }
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
208
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
209 tNum := s32x*s02y - s32y*s02x
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
210 if tNum < 0 == denPos {
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
211 return false
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
212 }
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
213
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
214 if sNum > den == denPos || tNum > den == denPos {
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
215 return false
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
216 }
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
217
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
218 // t := tNum / den
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
219 // intersection at( p0[0] + (t * s10x), p0[1] + (t * s10y) )
98bc023750cf Impelemented line segment / line segment intersection test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2494
diff changeset
220 return true
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
221 }
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
222
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
223 // IntersectionBox2D checks the type of intersection of the
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
224 // given box.
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
225 func (p *Polygon) IntersectionBox2D(box Box2D) IntersectionType {
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
226
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
227 if len(p.rings) == 0 {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
228 return IntersectionOutSide
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
229 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
230
5320
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
231 min, max := box.Rect()
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
232
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
233 for _, index := range p.indices {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
234 var intersects bool
5320
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
235 index.Search(min, max, func(_, _ [2]float64, item interface{}) bool {
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
236 if item.(lineSegment).intersects(box) {
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
237 intersects = true
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
238 return false
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
239 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
240 return true
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
241 })
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
242 if intersects {
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
243 return IntersectionOverlaps
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
244 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
245 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
246
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
247 // No intersection -> check inside or outside
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
248 // if an abritrary point is inside or not.
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
249
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
250 // Check holes first: inside a hole means outside.
2498
843f39b9327e Handle hole more precisely.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2495
diff changeset
251 if len(p.rings) > 1 {
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
252 for _, hole := range p.rings[1:] {
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
253 if contains(hole, box.X1, box.Y1) {
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
254 return IntersectionOutSide
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
255 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
256 }
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
257 }
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
258
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
259 // Check shell
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
260 if contains(p.rings[0], box.X1, box.Y1) {
2492
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
261 return IntersectionInside
10681749371d Implemented the BBox/clipping polygon test. TODO: triangle/clipping polygon test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2490
diff changeset
262 }
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
263 return IntersectionOutSide
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
264 }
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
265
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
266 // IntersectionWithTriangle checks the intersection type
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
267 // for the given triangle.
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
268 func (p *Polygon) IntersectionWithTriangle(t *Triangle) IntersectionType {
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
269 box := t.BBox()
5320
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
270 min, max := box.Rect()
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
271 for _, index := range p.indices {
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
272 var intersects bool
5320
866eae1bd888 Upgraded the dependencies of the Gemma server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4827
diff changeset
273 index.Search(min, max, func(_, _ [2]float64, item interface{}) bool {
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
274 ls := item.(lineSegment)
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
275 other := make(lineSegment, 4)
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
276 for i := range t {
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
277 other[0] = t[i].X
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
278 other[1] = t[i].Y
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
279 other[2] = t[(i+1)%len(t)].X
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
280 other[3] = t[(i+1)%len(t)].Y
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
281 if ls.intersectsLineSegment(other) {
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
282 intersects = true
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
283 return false
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
284 }
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
285 }
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
286 return true
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
287 })
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
288 if intersects {
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
289 return IntersectionOverlaps
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
290 }
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
291 }
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
292 // No intersection -> check inside or outside
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
293 // if an abritrary point is inside or not.
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
294 pX, pY := t[0].X, t[0].Y
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
295
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
296 // Check holes first: inside a hole means outside.
2498
843f39b9327e Handle hole more precisely.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2495
diff changeset
297 if len(p.rings) > 1 {
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
298 for _, hole := range p.rings[1:] {
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
299 if contains(hole, pX, pY) {
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
300 return IntersectionOutSide
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
301 }
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
302 }
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
303 }
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
304
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
305 // Check shell
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
306 if contains(p.rings[0], pX, pY) {
2494
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
307 return IntersectionInside
a727e0426240 More on triangle/clipping polygon intersection. TODO: line segment / line segment intersection.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2492
diff changeset
308 }
2488
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
309 return IntersectionOutSide
cb55d7eaaa36 Started with the idea to clip an octree by an bounding polygon.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
310 }
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
311
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
312 func (rng ring) length() int {
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
313 return len(rng) / 2
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
314 }
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
315
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
316 func (rng ring) point(i int) (float64, float64) {
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
317 i *= 2
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
318 return rng[i], rng[i+1]
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
319 }
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
320
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
321 type segments interface {
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
322 length() int
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
323 point(int) (float64, float64)
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
324 }
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
325
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
326 func contains(s segments, pX, pY float64) bool {
4764
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
327
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
328 n := s.length()
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
329 if n < 3 {
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
330 return false
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
331 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
332
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
333 sX, sY := s.point(0)
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
334 eX, eY := s.point(n - 1)
2498
843f39b9327e Handle hole more precisely.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2495
diff changeset
335
4764
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
336 const eps = 0.0000001
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
337
4765
db6c2955ee31 Fixed logical error in detecting if a ring is closed or not.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4764
diff changeset
338 if math.Abs(sX-eX) > eps || math.Abs(sY-eY) > eps {
4764
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
339 // It's not closed!
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
340 return false
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
341 }
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
342
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
343 var inside bool
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
344
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
345 for i := 1; i < n; i++ {
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
346 eX, eY := s.point(i)
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
347 if intersectsWithRaycast(pX, pY, sX, sY, eX, eY) {
4764
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
348 inside = !inside
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
349 }
4764
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
350 sX, sY = eX, eY
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
351 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
352
4764
5c80a33edd44 Fixed handling of none-closed polygons in containment test.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4729
diff changeset
353 return inside
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
354 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
355
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
356 // Using the raycast algorithm, this returns whether or not the passed in point
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
357 // Intersects with the edge drawn by the passed in start and end points.
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
358 // Original implementation: http://rosettacode.org/wiki/Ray-casting_algorithm#Go
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
359 func intersectsWithRaycast(pX, pY, sX, sY, eX, eY float64) bool {
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
360
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
361 // Always ensure that the the first point
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
362 // has a y coordinate that is less than the second point
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
363 if sY > eY {
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
364 // Switch the points if otherwise.
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
365 sX, sY, eX, eY = eX, eY, sX, sY
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
366 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
367
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
368 // Move the point's y coordinate
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
369 // outside of the bounds of the testing region
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
370 // so we can start drawing a ray
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
371 for pY == sY || pY == eY {
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
372 pY = math.Nextafter(pY, math.Inf(1))
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
373 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
374
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
375 // If we are outside of the polygon, indicate so.
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
376 if pY < sY || pY > eY {
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
377 return false
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
378 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
379
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
380 if sX > eX {
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
381 if pX > sX {
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
382 return false
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
383 }
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
384 if pX < eX {
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
385 return true
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
386 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
387 } else {
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
388 if pX > eX {
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
389 return false
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
390 }
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
391 if pX < sX {
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
392 return true
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
393 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
394 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
395
4729
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
396 raySlope := (pY - sY) / (pX - sX)
1137c5a18242 Virtualized point in polygon test with an interface to be usable for contours, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4706
diff changeset
397 diagSlope := (eY - sY) / (eX - sX)
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
398
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
399 return raySlope >= diagSlope
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
400 }
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
401
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
402 // NumVertices returns the number of vertices of a given ring.
3658
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
403 func (p *Polygon) NumVertices(ring int) int {
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
404 if ring < 0 || ring >= len(p.rings) {
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
405 return 0
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
406 }
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
407 return len(p.rings[ring]) / 2
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
408 }
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
409
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
410 // Vertices passes the vertices of a given ring
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
411 // to the given fn function.
3658
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
412 func (p *Polygon) Vertices(ring int, fn func(float64, float64)) {
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
413 if ring < 0 || ring >= len(p.rings) {
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
414 return
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
415 }
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
416 rng := p.rings[ring]
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
417 for i := 0; i < len(rng); i += 2 {
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
418 fn(rng[i+0], rng[i+1])
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
419 }
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
420 }
1c3df921361d Handle th case that a boundary polygon is uploaded along side with the single beam scan.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3650
diff changeset
421
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
422 // AsWKB serializes the polygon as WKB.
3650
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
423 func (p *Polygon) AsWKB() []byte {
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
424
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
425 size := 1 + 4 + 4
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
426 for _, r := range p.rings {
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
427 size += 4 + len(r)*8
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
428 }
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
429
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
430 buf := bytes.NewBuffer(make([]byte, 0, size))
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
431
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
432 binary.Write(buf, binary.LittleEndian, wkb.NDR)
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
433 binary.Write(buf, binary.LittleEndian, wkb.Polygon)
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
434 binary.Write(buf, binary.LittleEndian, uint32(len(p.rings)))
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
435
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
436 for _, r := range p.rings {
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
437 binary.Write(buf, binary.LittleEndian, uint32(len(r)/2))
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
438 for i := 0; i < len(r); i += 2 {
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
439 binary.Write(buf, binary.LittleEndian, math.Float64bits(r[i+0]))
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
440 binary.Write(buf, binary.LittleEndian, math.Float64bits(r[i+1]))
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
441 }
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
442 }
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
443
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
444 return buf.Bytes()
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
445 }
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
446
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
447 // FromWKB deserializes a polygon from WKB.
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
448 func (p *Polygon) FromWKB(data []byte) error {
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
449
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
450 r := bytes.NewReader(data)
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
451
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
452 endian, err := r.ReadByte()
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
453
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
454 var order binary.ByteOrder
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
455
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
456 switch {
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
457 case err != nil:
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
458 return err
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
459 case endian == wkb.NDR:
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
460 order = binary.LittleEndian
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
461 case endian == wkb.XDR:
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
462 order = binary.BigEndian
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
463 default:
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
464 return fmt.Errorf("unknown byte order %x", endian)
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
465 }
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
466
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
467 var geomType uint32
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
468 err = binary.Read(r, order, &geomType)
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
469
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
470 switch {
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
471 case err != nil:
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
472 return err
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
473 case geomType != wkb.Polygon:
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
474 return fmt.Errorf("unknown geometry type %x", geomType)
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
475 }
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
476
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
477 var numRings uint32
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
478 if err = binary.Read(r, order, &numRings); err != nil {
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
479 return err
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
480 }
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
481
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
482 rngs := make([]ring, numRings)
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
483
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5320
diff changeset
484 log.Infof("number of rings: %d\n", len(rngs))
2498
843f39b9327e Handle hole more precisely.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2495
diff changeset
485
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
486 for rng := uint32(0); rng < numRings; rng++ {
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
487 var numVertices uint32
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
488 if err = binary.Read(r, order, &numVertices); err != nil {
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
489 return err
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
490 }
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
491
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5320
diff changeset
492 log.Infof("number of vertices in ring %d: %d\n", rng, numVertices)
2498
843f39b9327e Handle hole more precisely.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2495
diff changeset
493
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
494 numVertices *= 2
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
495 vertices := make([]float64, numVertices)
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
496
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
497 for v := uint32(0); v < numVertices; v += 2 {
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
498 var lat, lon uint64
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
499 if err = binary.Read(r, order, &lat); err != nil {
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
500 return err
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
501 }
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
502 if err = binary.Read(r, order, &lon); err != nil {
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
503 return err
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
504 }
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
505 vertices[v] = math.Float64frombits(lat)
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
506 vertices[v+1] = math.Float64frombits(lon)
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
507 }
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
508
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
509 rngs[rng] = vertices
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
510 }
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
511
2490
c9164ff98871 Started with point in polygon check.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2489
diff changeset
512 p.rings = rngs
2489
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
513
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
514 return nil
4f292ff74d9e Added a WKB reader for clipping polygon.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 2488
diff changeset
515 }
3650
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
516
5601
1222b777f51f Made golint finally happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
517 // FromLineStringZ creates a polygon from a given linestring z.
3650
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
518 func (p *Polygon) FromLineStringZ(ls LineStringZ) {
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
519 r := make([]float64, 2*len(ls))
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
520 var pos int
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
521 for i := range ls {
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
522 r[pos+0] = ls[i].X
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
523 r[pos+1] = ls[i].Y
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
524 pos += 2
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
525 }
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
526 p.rings = []ring{r}
01ce3ba9b0d0 Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2587
diff changeset
527 }