Mercurial > gemma
comparison pkg/mesh/polygon.go @ 5601:1222b777f51f
Made golint finally happy.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sat, 06 Aug 2022 02:09:57 +0200 |
parents | 5f47eeea988d |
children | 6270951dda28 |
comparison
equal
deleted
inserted
replaced
5600:9967a78e43f4 | 5601:1222b777f51f |
---|---|
26 ) | 26 ) |
27 | 27 |
28 type ( | 28 type ( |
29 ring []float64 | 29 ring []float64 |
30 | 30 |
31 // Polygon has a border and holes. | |
32 // The line segments are spatially indexed. | |
31 Polygon struct { | 33 Polygon struct { |
32 rings []ring | 34 rings []ring |
33 indices []*rtree.RTree | 35 indices []*rtree.RTree |
34 } | 36 } |
35 | 37 |
38 // IntersectionType represents an enum | |
39 // of the type of intersection. | |
36 IntersectionType byte | 40 IntersectionType byte |
37 | 41 |
38 lineSegment []float64 | 42 lineSegment []float64 |
39 ) | 43 ) |
40 | 44 |
41 const ( | 45 const ( |
46 // IntersectionInside is inside the polygon. | |
42 IntersectionInside IntersectionType = iota | 47 IntersectionInside IntersectionType = iota |
48 // IntersectionOutSide is outside the polygon. | |
43 IntersectionOutSide | 49 IntersectionOutSide |
50 // IntersectionOverlaps overlaps the polygon. | |
44 IntersectionOverlaps | 51 IntersectionOverlaps |
45 ) | 52 ) |
46 | 53 |
47 func (ls lineSegment) Rect() ([2]float64, [2]float64) { | 54 func (ls lineSegment) Rect() ([2]float64, [2]float64) { |
48 | 55 |
65 } | 72 } |
66 | 73 |
67 return min, max | 74 return min, max |
68 } | 75 } |
69 | 76 |
77 // Indexify creates a spatial index for thw polygon. | |
70 func (p *Polygon) Indexify() { | 78 func (p *Polygon) Indexify() { |
71 indices := make([]*rtree.RTree, len(p.rings)) | 79 indices := make([]*rtree.RTree, len(p.rings)) |
72 | 80 |
73 for i := range indices { | 81 for i := range indices { |
74 index := new(rtree.RTree) | 82 index := new(rtree.RTree) |
210 // t := tNum / den | 218 // t := tNum / den |
211 // intersection at( p0[0] + (t * s10x), p0[1] + (t * s10y) ) | 219 // intersection at( p0[0] + (t * s10x), p0[1] + (t * s10y) ) |
212 return true | 220 return true |
213 } | 221 } |
214 | 222 |
223 // IntersectionBox2D checks the type of intersection of the | |
224 // given box. | |
215 func (p *Polygon) IntersectionBox2D(box Box2D) IntersectionType { | 225 func (p *Polygon) IntersectionBox2D(box Box2D) IntersectionType { |
216 | 226 |
217 if len(p.rings) == 0 { | 227 if len(p.rings) == 0 { |
218 return IntersectionOutSide | 228 return IntersectionOutSide |
219 } | 229 } |
251 return IntersectionInside | 261 return IntersectionInside |
252 } | 262 } |
253 return IntersectionOutSide | 263 return IntersectionOutSide |
254 } | 264 } |
255 | 265 |
266 // IntersectionWithTriangle checks the intersection type | |
267 // for the given triangle. | |
256 func (p *Polygon) IntersectionWithTriangle(t *Triangle) IntersectionType { | 268 func (p *Polygon) IntersectionWithTriangle(t *Triangle) IntersectionType { |
257 box := t.BBox() | 269 box := t.BBox() |
258 min, max := box.Rect() | 270 min, max := box.Rect() |
259 for _, index := range p.indices { | 271 for _, index := range p.indices { |
260 var intersects bool | 272 var intersects bool |
385 diagSlope := (eY - sY) / (eX - sX) | 397 diagSlope := (eY - sY) / (eX - sX) |
386 | 398 |
387 return raySlope >= diagSlope | 399 return raySlope >= diagSlope |
388 } | 400 } |
389 | 401 |
402 // NumVertices returns the number of vertices of a given ring. | |
390 func (p *Polygon) NumVertices(ring int) int { | 403 func (p *Polygon) NumVertices(ring int) int { |
391 if ring < 0 || ring >= len(p.rings) { | 404 if ring < 0 || ring >= len(p.rings) { |
392 return 0 | 405 return 0 |
393 } | 406 } |
394 return len(p.rings[ring]) / 2 | 407 return len(p.rings[ring]) / 2 |
395 } | 408 } |
396 | 409 |
410 // Vertices passes the vertices of a given ring | |
411 // to the given fn function. | |
397 func (p *Polygon) Vertices(ring int, fn func(float64, float64)) { | 412 func (p *Polygon) Vertices(ring int, fn func(float64, float64)) { |
398 if ring < 0 || ring >= len(p.rings) { | 413 if ring < 0 || ring >= len(p.rings) { |
399 return | 414 return |
400 } | 415 } |
401 rng := p.rings[ring] | 416 rng := p.rings[ring] |
402 for i := 0; i < len(rng); i += 2 { | 417 for i := 0; i < len(rng); i += 2 { |
403 fn(rng[i+0], rng[i+1]) | 418 fn(rng[i+0], rng[i+1]) |
404 } | 419 } |
405 } | 420 } |
406 | 421 |
422 // AsWKB serializes the polygon as WKB. | |
407 func (p *Polygon) AsWKB() []byte { | 423 func (p *Polygon) AsWKB() []byte { |
408 | 424 |
409 size := 1 + 4 + 4 | 425 size := 1 + 4 + 4 |
410 for _, r := range p.rings { | 426 for _, r := range p.rings { |
411 size += 4 + len(r)*8 | 427 size += 4 + len(r)*8 |
426 } | 442 } |
427 | 443 |
428 return buf.Bytes() | 444 return buf.Bytes() |
429 } | 445 } |
430 | 446 |
447 // FromWKB deserializes a polygon from WKB. | |
431 func (p *Polygon) FromWKB(data []byte) error { | 448 func (p *Polygon) FromWKB(data []byte) error { |
432 | 449 |
433 r := bytes.NewReader(data) | 450 r := bytes.NewReader(data) |
434 | 451 |
435 endian, err := r.ReadByte() | 452 endian, err := r.ReadByte() |
495 p.rings = rngs | 512 p.rings = rngs |
496 | 513 |
497 return nil | 514 return nil |
498 } | 515 } |
499 | 516 |
517 // FromLineStringZ creates a polygon from a given linestring z. | |
500 func (p *Polygon) FromLineStringZ(ls LineStringZ) { | 518 func (p *Polygon) FromLineStringZ(ls LineStringZ) { |
501 r := make([]float64, 2*len(ls)) | 519 r := make([]float64, 2*len(ls)) |
502 var pos int | 520 var pos int |
503 for i := range ls { | 521 for i := range ls { |
504 r[pos+0] = ls[i].X | 522 r[pos+0] = ls[i].X |