Mercurial > gemma
changeset 777:f27bfc57143b
merge
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Wed, 26 Sep 2018 12:44:22 +0200 |
parents | bb3558142b18 (current diff) acd8e7af29aa (diff) |
children | 9be20bd0f131 |
files | |
diffstat | 2 files changed, 106 insertions(+), 106 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/octree/tree.go Wed Sep 26 12:43:44 2018 +0200 +++ b/pkg/octree/tree.go Wed Sep 26 12:44:22 2018 +0200 @@ -15,91 +15,6 @@ Max Vertex } -type box2d struct { - x1 float64 - y1 float64 - x2 float64 - y2 float64 -} - -func (a box2d) intersects(b box2d) bool { - return !(a.x2 < a.x1 || a.x2 < b.x1 || - a.y2 < a.y1 || a.y2 < b.y1) -} - -func (a box2d) mid() (float64, float64) { - return (a.x2-a.x1)*0.5 + a.x1, (a.y2-a.y1)*0.5 + a.y1 -} - -func (a box2d) xi(i int) float64 { - if i == 0 { - return a.x1 - } - return a.x2 -} - -func (a box2d) yi(i int) float64 { - if i == 0 { - return a.y1 - } - return a.y2 -} - -type plane2d struct { - a float64 - b float64 - c float64 -} - -func newPlane2d(x1, y1, x2, y2 float64) plane2d { - a := x2 - x1 - b := y2 - y1 - - l := math.Sqrt(a*a + b*b) - a /= l - b /= l - - // a*x1 + b*y1 + c = 0 - c := -(a*x1 + b*y1) - return plane2d{a, b, c} -} - -func (p plane2d) eval(x, y float64) float64 { - return p.a*x + p.b*y + p.c -} - -const epsPlane = 1e-5 - -func sides(s int, x float64) int { - if math.Signbit(x) { - return s | 2 - } - return s | 1 -} - -const eps = 1e-05 - -func (a box2d) intersectsPlane(p plane2d) bool { - var s int - for i := 0; i < 2; i++ { - x := a.xi(i) - for j := 0; j < 2; j++ { - y := a.yi(j) - v := p.eval(x, y) - if math.Abs(v) < epsPlane { - //log.Printf("on line") - return true - } - if s = sides(s, v); s == 3 { - //log.Printf("... on both sides (djt)") - return true - } - } - } - //log.Printf("side: %d\n", s) - return false -} - var scale = [4][4]float64{ {0.0, 0.0, 0.5, 0.5}, {0.5, 0.0, 1.0, 0.5}, @@ -109,29 +24,29 @@ func (ot *Tree) Vertical(x1, y1, x2, y2 float64, fn func(*Triangle)) { - box := box2d{ - x1: math.Min(x1, x2), - y1: math.Min(y1, y2), - x2: math.Max(x1, x2), - y2: math.Max(y1, y2), + box := Box2D{ + X1: math.Min(x1, x2), + Y1: math.Min(y1, y2), + X2: math.Max(x1, x2), + Y2: math.Max(y1, y2), } // out of bounding box - if box.x2 < ot.Min.X || ot.Max.X < box.x1 || - box.y2 < ot.Min.Y || ot.Max.Y < box.y1 { + if box.X2 < ot.Min.X || ot.Max.X < box.X1 || + box.Y2 < ot.Min.Y || ot.Max.Y < box.Y1 { return } - line := newPlane2d(x1, y1, x2, y2) + line := NewPlane2D(x1, y1, x2, y2) type frame struct { pos int32 - box2d + Box2D } dupes := map[int32]struct{}{} - all := box2d{ot.Min.X, ot.Min.Y, ot.Max.X, ot.Max.Y} + all := Box2D{ot.Min.X, ot.Min.Y, ot.Max.X, ot.Max.Y} //log.Printf("area: %f\n", (box.x2-box.x1)*(box.y2-box.y1)) //log.Printf("all: %f\n", (all.x2-all.x1)*(all.y2-all.y1)) @@ -148,15 +63,15 @@ if a == 0 && b == 0 { continue } - dx := top.x2 - top.x1 - dy := top.y2 - top.y1 - nbox := box2d{ - dx*scale[i][0] + top.x1, - dy*scale[i][1] + top.y1, - dx*scale[i][2] + top.x1, - dy*scale[i][3] + top.y1, + dx := top.X2 - top.X1 + dy := top.Y2 - top.Y1 + nbox := Box2D{ + dx*scale[i][0] + top.X1, + dy*scale[i][1] + top.Y1, + dx*scale[i][2] + top.X1, + dy*scale[i][3] + top.Y1, } - if nbox.intersects(box) && nbox.intersectsPlane(line) { + if nbox.Intersects(box) && nbox.IntersectsPlane(line) { if a != 0 { stack = append(stack, frame{a, nbox}) } @@ -181,9 +96,9 @@ ot.vertices[tri[2]], } - v0 := line.eval(t[0].X, t[0].Y) - v1 := line.eval(t[1].X, t[1].Y) - v2 := line.eval(t[2].X, t[2].Y) + v0 := line.Eval(t[0].X, t[0].Y) + v1 := line.Eval(t[1].X, t[1].Y) + v2 := line.Eval(t[2].X, t[2].Y) if math.Abs(v0) < eps || math.Abs(v1) < eps || math.Abs(v2) < eps ||
--- a/pkg/octree/vertex.go Wed Sep 26 12:43:44 2018 +0200 +++ b/pkg/octree/vertex.go Wed Sep 26 12:44:22 2018 +0200 @@ -22,6 +22,19 @@ LineStringZ []Vertex MultiLineStringZ []LineStringZ + + Box2D struct { + X1 float64 + Y1 float64 + X2 float64 + Y2 float64 + } + + Plane2D struct { + A float64 + B float64 + C float64 + } ) const ( @@ -297,3 +310,75 @@ return buf.Bytes() } + +func (a Box2D) Intersects(b Box2D) bool { + return !(a.X2 < a.X1 || a.X2 < b.X1 || + a.Y2 < a.Y1 || a.Y2 < b.Y1) +} + +func (a Box2D) Mid() (float64, float64) { + return (a.X2-a.X1)*0.5 + a.X1, (a.Y2-a.Y1)*0.5 + a.Y1 +} + +func (a Box2D) Xi(i int) float64 { + if i == 0 { + return a.X1 + } + return a.X2 +} + +func (a Box2D) Yi(i int) float64 { + if i == 0 { + return a.Y1 + } + return a.Y2 +} + +func NewPlane2D(x1, y1, x2, y2 float64) Plane2D { + a := x2 - x1 + b := y2 - y1 + + l := math.Sqrt(a*a + b*b) + a /= l + b /= l + + // a*x1 + b*y1 + c = 0 + c := -(a*x1 + b*y1) + return Plane2D{a, b, c} +} + +func (p Plane2D) Eval(x, y float64) float64 { + return p.A*x + p.B*y + p.C +} + +const epsPlane = 1e-5 + +func sides(s int, x float64) int { + if math.Signbit(x) { + return s | 2 + } + return s | 1 +} + +const eps = 1e-05 + +func (a Box2D) IntersectsPlane(p Plane2D) bool { + var s int + for i := 0; i < 2; i++ { + x := a.Xi(i) + for j := 0; j < 2; j++ { + y := a.Yi(j) + v := p.Eval(x, y) + if math.Abs(v) < epsPlane { + //log.Printf("on line") + return true + } + if s = sides(s, v); s == 3 { + //log.Printf("... on both sides (djt)") + return true + } + } + } + //log.Printf("side: %d\n", s) + return false +}