1
0
forked from MTSR/mapserver

coords and tests

This commit is contained in:
Thomas Rudin 2019-01-09 10:52:51 +01:00
parent 869e7b28d6
commit a72886c5ad
3 changed files with 67 additions and 15 deletions

View File

@ -1,4 +1,4 @@
package db
package coords
//https://bitbucket.org/s_l_teichmann/mtsatellite/src/e1bf980a2b278c570b3f44f9452c9c087558acb3/common/coords.go?at=default&fileviewer=file-view-default
const (
@ -10,17 +10,17 @@ const (
)
func CoordToPlain(x, y, z int) int64 {
return int64(z)<<(2*numBitsPerComponent) +
int64(y)<<numBitsPerComponent +
int64(x)
func CoordToPlain(c MapBlockCoords) int64 {
return int64(c.Z)<<(2*numBitsPerComponent) +
int64(c.Y)<<numBitsPerComponent +
int64(c.X)
}
func unsignedToSigned(i int16) int16 {
func unsignedToSigned(i int16) int {
if i < maxPositive {
return i
return int(i)
}
return i - maxPositive*2
return int(i - maxPositive*2)
}
// To match C++ code.
@ -32,11 +32,12 @@ func pythonModulo(i int16) int16 {
return modulo - -i&mask
}
func PlainToCoord(i int64) (int, int, int) {
x := unsignedToSigned(pythonModulo(int16(i)))
i = (i - int64(x)) >> numBitsPerComponent
y := unsignedToSigned(pythonModulo(int16(i)))
i = (i - int64(x)) >> numBitsPerComponent
z := unsignedToSigned(pythonModulo(int16(i)))
return int(x), int(y), int(z)
func PlainToCoord(i int64) MapBlockCoords {
c := MapBlockCoords{}
c.X = unsignedToSigned(pythonModulo(int16(i)))
i = (i - int64(c.X)) >> numBitsPerComponent
c.Y = unsignedToSigned(pythonModulo(int16(i)))
i = (i - int64(c.Y)) >> numBitsPerComponent
c.Z = unsignedToSigned(pythonModulo(int16(i)))
return c
}

42
coords/convert_test.go Normal file
View File

@ -0,0 +1,42 @@
package coords
import (
"testing"
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init(){
log = logrus.WithFields(logrus.Fields{"prefix": "coords/convert_test"})
}
func testCoordConvert(t *testing.T, mb MapBlockCoords){
log.WithFields(logrus.Fields{"coords":mb}).Info("MapblockCoords")
p := CoordToPlain(mb)
log.WithFields(logrus.Fields{"plain":p}).Info("MapblockCoords")
mb2 := PlainToCoord(p)
log.WithFields(logrus.Fields{"coords2":mb2}).Info("MapblockCoords")
if mb.X != mb2.X {
t.Fatal("X mismatch")
}
if mb.Y != mb2.Y {
t.Fatal("Y mismatch")
}
if mb.Z != mb2.Z {
t.Fatal("Z mismatch")
}
}
func TestConvertPlainMapBlock(t *testing.T){
testCoordConvert(t, NewMapBlockCoords(10, 0, -10))
testCoordConvert(t, NewMapBlockCoords(-2048, 2047, -10))
testCoordConvert(t, NewMapBlockCoords(-3, 0, 2047)) //0...2047
}

9
coords/mapblockcoords.go Normal file
View File

@ -0,0 +1,9 @@
package coords
type MapBlockCoords struct {
X,Y,Z int
}
func NewMapBlockCoords(x,y,z int) MapBlockCoords {
return MapBlockCoords{X:x, Y:y, Z:z}
}