view pkg/octree/loader.go @ 1234:1a5564655f2a

refac: Sidebar reorganized In order to make context switches between administrative tasks which are map related and those which are system related, we now have a category "administration" and "systemadministration". The Riverbedmorphology does nothing than display the map, so it is renamed to that (map). In case the context of "systemadministration" is chosen, the "map" brings you just back to the map.
author Thomas Junk <thomas.junk@intevation.de>
date Tue, 20 Nov 2018 09:54:53 +0100
parents a244b18cb916
children de09bd3b5c05
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2018 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>

package octree

import (
	"bufio"
	"bytes"
	"encoding/binary"
	"log"
	"os"

	"github.com/golang/snappy"
)

func loadReader(r *bufio.Reader) (*Tree, error) {
	tree := new(Tree)

	if err := binary.Read(r, binary.LittleEndian, &tree.EPSG); err != nil {
		return nil, err
	}

	log.Printf("EPSG: %d\n", tree.EPSG)

	if err := tree.Min.Read(r); err != nil {
		return nil, err
	}

	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)

	var numVertices uint32
	if err := binary.Read(r, binary.LittleEndian, &numVertices); err != nil {
		return nil, err
	}

	log.Printf("vertices: %d\n", numVertices)

	vertices := make([]Vertex, numVertices)
	tree.vertices = vertices

	for i := range vertices {
		if err := vertices[i].Read(r); err != nil {
			return nil, err
		}
	}

	var numTriangles uint32
	if err := binary.Read(r, binary.LittleEndian, &numTriangles); err != nil {
		return nil, err
	}

	log.Printf("triangles: %d\n", numTriangles)

	indices := make([]int32, 3*numTriangles)
	triangles := make([][]int32, numTriangles)
	tree.triangles = triangles

	var last int32

	for i := range triangles {
		tri := indices[:3]
		indices = indices[3:]
		triangles[i] = tri
		for j := range tri {
			v, err := binary.ReadVarint(r)
			if err != nil {
				return nil, err
			}
			value := int32(v) + last
			tri[j] = value
			last = value
		}
	}

	var numNodes uint32
	if err := binary.Read(r, binary.LittleEndian, &numNodes); err != nil {
		return nil, err
	}

	log.Printf("num nodes: %d\n", numNodes)

	tree.index = make([]int32, numNodes)
	entries := tree.index[1:]

	last = 0
	for i := range entries {
		v, err := binary.ReadVarint(r)
		if err != nil {
			return nil, err
		}
		value := int32(v) + last
		entries[i] = value
		last = value
	}

	return tree, nil
}

func LoadTree(fname string) (*Tree, error) {

	f, err := os.Open(fname)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	return loadReader(
		bufio.NewReader(
			snappy.NewReader(f)))
}

func Deserialize(data []byte) (*Tree, error) {
	return loadReader(
		bufio.NewReader(
			snappy.NewReader(
				bytes.NewReader(data))))
}