diff pkg/mesh/loader.go @ 4827:f4abfd0ee8ad remove-octree-debris

Renamed octree package to mesh as there is no octree any more.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 05 Nov 2019 14:30:22 +0100
parents pkg/octree/loader.go@c0eb491aaaa7
children 5f47eeea988d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/mesh/loader.go	Tue Nov 05 14:30:22 2019 +0100
@@ -0,0 +1,166 @@
+// 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 mesh
+
+import (
+	"bufio"
+	"bytes"
+	"compress/gzip"
+	"encoding/binary"
+	"log"
+)
+
+func (s *STRTree) deserializeIndex(r *bufio.Reader) error {
+	var numIndex int32
+	if err := binary.Read(r, binary.LittleEndian, &numIndex); err != nil {
+		return err
+	}
+	index := make([]int32, numIndex)
+	s.index = index
+
+	var last int32
+	for i := range index {
+		v, err := binary.ReadVarint(r)
+		if err != nil {
+			return err
+		}
+		value := int32(v) + last
+		index[i] = value
+		last = value
+	}
+
+	return nil
+}
+
+func (s *STRTree) deserializeBBoxes(r *bufio.Reader) error {
+
+	var numBBoxes int32
+	if err := binary.Read(r, binary.LittleEndian, &numBBoxes); err != nil {
+		return err
+	}
+
+	bboxes := make([]Box2D, numBBoxes)
+	s.bboxes = bboxes
+
+	var err error
+
+	read := func(v *float64) {
+		if err == nil {
+			err = binary.Read(r, binary.LittleEndian, v)
+		}
+	}
+
+	for i := range bboxes {
+		read(&bboxes[i].X1)
+		read(&bboxes[i].Y1)
+		read(&bboxes[i].X2)
+		read(&bboxes[i].Y2)
+	}
+
+	return err
+}
+
+func (s *STRTree) deserialize(r *bufio.Reader) error {
+	s.tin = new(Tin)
+
+	if err := s.tin.Deserialize(r); err != nil {
+		return err
+	}
+	var numEntries uint8
+	if err := binary.Read(r, binary.LittleEndian, &numEntries); err != nil {
+		return err
+	}
+	s.Entries = int(numEntries)
+
+	if err := s.deserializeIndex(r); err != nil {
+		return err
+	}
+
+	return s.deserializeBBoxes(r)
+}
+
+func (s *STRTree) FromBytes(data []byte) error {
+	r, err := gzip.NewReader(bytes.NewReader(data))
+	if err != nil {
+		return err
+	}
+	return s.deserialize(bufio.NewReader(r))
+}
+
+func (t *Tin) Deserialize(r *bufio.Reader) error {
+
+	if err := binary.Read(r, binary.LittleEndian, &t.EPSG); err != nil {
+		return err
+	}
+
+	log.Printf("info: EPSG: %d\n", t.EPSG)
+
+	if err := t.Min.Read(r); err != nil {
+		return err
+	}
+
+	if err := t.Max.Read(r); err != nil {
+		return err
+	}
+
+	log.Printf("info: BBOX: [[%f, %f, %f], [%f, %f, %f]]\n",
+		t.Min.X, t.Min.Y, t.Min.Z,
+		t.Max.X, t.Max.Y, t.Max.Z)
+
+	var numVertices uint32
+	if err := binary.Read(r, binary.LittleEndian, &numVertices); err != nil {
+		return err
+	}
+
+	log.Printf("info: vertices: %d\n", numVertices)
+
+	vertices := make([]Vertex, numVertices)
+	t.Vertices = vertices
+
+	for i := range vertices {
+		if err := vertices[i].Read(r); err != nil {
+			return err
+		}
+	}
+
+	var numTriangles uint32
+	if err := binary.Read(r, binary.LittleEndian, &numTriangles); err != nil {
+		return err
+	}
+
+	log.Printf("info: triangles: %d\n", numTriangles)
+
+	indices := make([]int32, 3*numTriangles)
+	triangles := make([][]int32, numTriangles)
+	t.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 err
+			}
+			value := int32(v) + last
+			tri[j] = value
+			last = value
+		}
+	}
+
+	return nil
+}