diff pkg/octree/tree.go @ 1879:9a2fbeaabd52 dev-pdf-generation

merging in from branch default
author Bernhard Reiter <bernhard@intevation.de>
date Tue, 15 Jan 2019 10:07:10 +0100
parents fe1aa62195c2
children a1e751c08c56
line wrap: on
line diff
--- a/pkg/octree/tree.go	Tue Jan 15 09:54:46 2019 +0100
+++ b/pkg/octree/tree.go	Tue Jan 15 10:07:10 2019 +0100
@@ -17,14 +17,18 @@
 	"math"
 )
 
+// Tree is an Octree holding triangles.
 type Tree struct {
+	// EPSG is the projection.
 	EPSG uint32
 
 	vertices  []Vertex
 	triangles [][]int32
 	index     []int32
 
+	// Min is the lower left corner of the bbox.
 	Min Vertex
+	// Max is the upper right corner of the bbox.
 	Max Vertex
 }
 
@@ -35,6 +39,7 @@
 	{0.5, 0.5, 1.0, 1.0},
 }
 
+// Vertical does a vertical cross cut from (x1, y1) to (x2, y2).
 func (ot *Tree) Vertical(x1, y1, x2, y2 float64, fn func(*Triangle)) {
 
 	box := Box2D{
@@ -70,26 +75,28 @@
 		stack = stack[:len(stack)-1]
 
 		if top.pos > 0 { // node
-			for i := int32(0); i < 4; i++ {
-				a := ot.index[top.pos+i]
-				b := ot.index[top.pos+i+4]
-				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,
-				}
-				if nbox.Intersects(box) && nbox.IntersectsPlane(line) {
-					if a != 0 {
-						stack = append(stack, frame{a, nbox})
+			if index := ot.index[top.pos:]; len(index) > 7 {
+				for i := 0; i < 4; i++ {
+					a := index[i]
+					b := index[i+4]
+					if a == 0 && b == 0 {
+						continue
 					}
-					if b != 0 {
-						stack = append(stack, frame{b, nbox})
+					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 a != 0 {
+							stack = append(stack, frame{a, nbox})
+						}
+						if b != 0 {
+							stack = append(stack, frame{b, nbox})
+						}
 					}
 				}
 			}
@@ -123,6 +130,7 @@
 	}
 }
 
+// Horizontal does a horizontal cross cut.
 func (ot *Tree) Horizontal(h float64, fn func(*Triangle)) {
 
 	if h < ot.Min.Z || ot.Max.Z < h {
@@ -156,11 +164,13 @@
 			} else {
 				max = mid
 			}
-			stack = append(stack,
-				frame{ot.index[pos+0], min, max},
-				frame{ot.index[pos+1], min, max},
-				frame{ot.index[pos+2], min, max},
-				frame{ot.index[pos+3], min, max})
+			if index := ot.index[pos:]; len(index) > 3 {
+				stack = append(stack,
+					frame{index[0], min, max},
+					frame{index[1], min, max},
+					frame{index[2], min, max},
+					frame{index[3], min, max})
+			}
 		} else { // leaf
 			pos = -pos - 1
 			n := ot.index[pos]