Mercurial > gemma
changeset 726:5af9ab39e715
Renamed a few types to uppercase names to prepare the move to the octree package.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Sat, 22 Sep 2018 21:34:12 +0200 |
parents | e0437ec46798 |
children | 41c8dc61f38f |
files | cmd/octree2contour/loader.go cmd/octree2contour/main.go cmd/octree2contour/octree.go cmd/octree2contour/store.go cmd/octree2contour/vertex.go |
diffstat | 5 files changed, 93 insertions(+), 89 deletions(-) [+] |
line wrap: on
line diff
--- a/cmd/octree2contour/loader.go Sat Sep 22 09:55:42 2018 +0200 +++ b/cmd/octree2contour/loader.go Sat Sep 22 21:34:12 2018 +0200 @@ -3,52 +3,32 @@ import ( "bufio" "encoding/binary" - "io" "log" - "math" "os" "github.com/golang/snappy" ) -func (v *vertex) read(r io.Reader) error { - var buf [8]byte - b := buf[:] - if _, err := io.ReadFull(r, b); err != nil { - return nil - } - v.x = math.Float64frombits(binary.LittleEndian.Uint64(b)) - if _, err := io.ReadFull(r, b); err != nil { - return nil - } - v.y = math.Float64frombits(binary.LittleEndian.Uint64(b)) - if _, err := io.ReadFull(r, b); err != nil { - return nil - } - v.z = math.Float64frombits(binary.LittleEndian.Uint64(b)) - return nil -} +func loadOctreeReader(r *bufio.Reader) (*Octree, error) { + tree := new(Octree) -func loadOctreeReader(r *bufio.Reader) (*octree, error) { - tree := new(octree) - - if err := binary.Read(r, binary.LittleEndian, &tree.epsg); err != nil { + if err := binary.Read(r, binary.LittleEndian, &tree.EPSG); err != nil { return nil, err } - log.Printf("EPSG: %d\n", tree.epsg) + log.Printf("EPSG: %d\n", tree.EPSG) - if err := tree.min.read(r); err != nil { + if err := tree.Min.read(r); err != nil { return nil, err } - if err := tree.max.read(r); err != nil { + if err := tree.Max.read(r); err != nil { return nil, err } log.Printf("BBOX: [[%f, %f, %f], [%f, %f, %f]]\n", - tree.min.x, tree.min.y, tree.min.z, - tree.max.x, tree.max.y, tree.max.z) + tree.Min.x, tree.Min.y, tree.Min.z, + tree.Max.x, tree.Max.y, tree.Max.z) var numVertices uint32 if err := binary.Read(r, binary.LittleEndian, &numVertices); err != nil { @@ -57,7 +37,7 @@ log.Printf("vertices: %d\n", numVertices) - vertices := make([]vertex, numVertices) + vertices := make([]Vertex, numVertices) tree.vertices = vertices for i := range vertices { @@ -118,7 +98,7 @@ return tree, nil } -func loadOctree(fname string) (*octree, error) { +func LoadOctree(fname string) (*Octree, error) { f, err := os.Open(fname) if err != nil {
--- a/cmd/octree2contour/main.go Sat Sep 22 09:55:42 2018 +0200 +++ b/cmd/octree2contour/main.go Sat Sep 22 21:34:12 2018 +0200 @@ -16,16 +16,16 @@ ) func processLevels( - tree *octree, + tree *Octree, jobs <-chan float64, results chan<- result, wg *sync.WaitGroup, ) { defer wg.Done() for h := range jobs { - var lines multiLineStringZ - tree.horizontal(h, func(t *triangle) { - line := t.intersectH(h) + var lines MultiLineStringZ + tree.Horizontal(h, func(t *Triangle) { + line := t.IntersectHorizontal(h) if len(line) > 1 { lines = append(lines, line) } @@ -34,12 +34,12 @@ } } -func process(tree *octree) []result { +func process(tree *Octree) []result { if *one { - var lines multiLineStringZ - tree.horizontal(*step, func(t *triangle) { - line := t.intersectH(*step) + var lines MultiLineStringZ + tree.Horizontal(*step, func(t *Triangle) { + line := t.IntersectHorizontal(*step) if len(line) > 0 { lines = append(lines, line) } @@ -68,7 +68,7 @@ wg.Add(1) go processLevels(tree, jobs, results, &wg) } - for h := tree.min.z; h <= tree.max.z; h += *step { + for h := tree.Min.z; h <= tree.Max.z; h += *step { jobs <- h } close(jobs) @@ -88,7 +88,7 @@ for _, fname := range flag.Args() { log.Printf("processing %s\n", fname) start := time.Now() - tree, err := loadOctree(fname) + tree, err := LoadOctree(fname) if err != nil { log.Printf("error: %v\n", err) continue @@ -98,7 +98,7 @@ all := process(tree) log.Printf("processing took: %v\n", time.Since(start)) start = time.Now() - if err = store(all, tree.epsg); err != nil { + if err = store(all, tree.EPSG); err != nil { log.Printf("error: %v\n", err) } log.Printf("storing took: %v\n", time.Since(start))
--- a/cmd/octree2contour/octree.go Sat Sep 22 09:55:42 2018 +0200 +++ b/cmd/octree2contour/octree.go Sat Sep 22 21:34:12 2018 +0200 @@ -4,18 +4,18 @@ "math" ) -type octree struct { - epsg uint32 +type Octree struct { + EPSG uint32 - vertices []vertex + vertices []Vertex triangles [][]int32 index []int32 - min vertex - max vertex + Min Vertex + Max Vertex } -func (ot *octree) horizontal(h float64, fn func(*triangle)) { +func (ot *Octree) Horizontal(h float64, fn func(*Triangle)) { type frame struct { pos int32 @@ -23,11 +23,11 @@ max float64 } - if h < ot.min.z || ot.max.z < h { + if h < ot.Min.z || ot.Max.z < h { return } - stack := []frame{{1, ot.min.z, ot.max.z}} + stack := []frame{{1, ot.Min.z, ot.Max.z}} dupes := map[int32]struct{}{} @@ -64,7 +64,7 @@ continue } tri := ot.triangles[idx] - t := triangle{ + t := Triangle{ ot.vertices[tri[0]], ot.vertices[tri[1]], ot.vertices[tri[2]],
--- a/cmd/octree2contour/store.go Sat Sep 22 09:55:42 2018 +0200 +++ b/cmd/octree2contour/store.go Sat Sep 22 21:34:12 2018 +0200 @@ -7,11 +7,6 @@ "math" ) -type ( - lineStringZ []vertex - multiLineStringZ []lineStringZ -) - const ( wkbNDR byte = 1 wkbPointZ uint32 = 1000 + 1 @@ -21,7 +16,7 @@ type result struct { h float64 - lines multiLineStringZ + lines MultiLineStringZ } const insertSQL = ` @@ -55,7 +50,7 @@ }) } -func (mls multiLineStringZ) asWKB() []byte { +func (mls MultiLineStringZ) asWKB() []byte { var buf bytes.Buffer
--- a/cmd/octree2contour/vertex.go Sat Sep 22 09:55:42 2018 +0200 +++ b/cmd/octree2contour/vertex.go Sat Sep 22 21:34:12 2018 +0200 @@ -1,12 +1,27 @@ package main -type vertex struct { - x float64 - y float64 - z float64 -} +import ( + "encoding/binary" + "io" + "math" +) -func (v *vertex) minimize(w vertex) { +type ( + Vertex struct { + x float64 + y float64 + z float64 + } + + Triangle [3]Vertex + + Line [2]Vertex + + LineStringZ []Vertex + MultiLineStringZ []LineStringZ +) + +func (v *Vertex) Minimize(w Vertex) { if w.x < v.x { v.x = w.x } @@ -18,7 +33,7 @@ } } -func (v *vertex) maximize(w vertex) { +func (v *Vertex) Maximize(w Vertex) { if w.x > v.x { v.x = w.x } @@ -30,34 +45,34 @@ } } -func (v vertex) sub(w vertex) vertex { - return vertex{ +func (v Vertex) Sub(w Vertex) Vertex { + return Vertex{ v.x - w.x, v.y - w.y, v.z - w.z, } } -func (v vertex) add(w vertex) vertex { - return vertex{ +func (v Vertex) Add(w Vertex) Vertex { + return Vertex{ v.x + w.x, v.y + w.y, v.z + w.z, } } -func (v vertex) scale(s float64) vertex { - return vertex{ +func (v Vertex) scale(s float64) Vertex { + return Vertex{ s * v.x, s * v.y, s * v.z, } } -func interpolate(v1, v2 vertex) func(vertex) vertex { - v2 = v2.sub(v1) - return func(s vertex) vertex { - return vertex{ +func Interpolate(v1, v2 Vertex) func(Vertex) Vertex { + v2 = v2.Sub(v1) + return func(s Vertex) Vertex { + return Vertex{ v2.x*s.x + v1.x, v2.y*s.y + v1.y, v2.z*s.z + v1.z, @@ -65,30 +80,26 @@ } } -func (a vertex) less(b vertex) bool { +func (a Vertex) Less(b Vertex) bool { return a.x < b.x || a.y < b.y || a.z < b.z } -type line [2]vertex - -func newLine(p1, p2 vertex) line { - return line{ - p2.sub(p1), +func NewLine(p1, p2 Vertex) Line { + return Line{ + p2.Sub(p1), p1, } } -func (l line) eval(t float64) vertex { - return l[0].scale(t).add(l[1]) +func (l Line) Eval(t float64) Vertex { + return l[0].scale(t).Add(l[1]) } -func (l line) intersectH(h float64) vertex { +func (l Line) IntersectHorizontal(h float64) Vertex { t := (h - l[1].z) / l[0].z - return l.eval(t) + return l.Eval(t) } -type triangle [3]vertex - func side(z, h float64) int { switch { case z < h: @@ -99,14 +110,14 @@ return 0 } -func (t *triangle) intersectH(h float64) lineStringZ { +func (t *Triangle) IntersectHorizontal(h float64) LineStringZ { sides := [3]int{ side(t[0].z, h), side(t[1].z, h), side(t[2].z, h), } - var points lineStringZ + var points LineStringZ for i := 0; i < 3; i++ { j := (i + 1) % 3 @@ -127,10 +138,28 @@ // both on same side default: // real intersection - v := newLine(t[i], t[j]).intersectH(h) + v := NewLine(t[i], t[j]).IntersectHorizontal(h) points = append(points, v) } } return points } + +func (v *Vertex) read(r io.Reader) error { + var buf [8]byte + b := buf[:] + if _, err := io.ReadFull(r, b); err != nil { + return nil + } + v.x = math.Float64frombits(binary.LittleEndian.Uint64(b)) + if _, err := io.ReadFull(r, b); err != nil { + return nil + } + v.y = math.Float64frombits(binary.LittleEndian.Uint64(b)) + if _, err := io.ReadFull(r, b); err != nil { + return nil + } + v.z = math.Float64frombits(binary.LittleEndian.Uint64(b)) + return nil +}