coords and tests
This commit is contained in:
parent
869e7b28d6
commit
a72886c5ad
@ -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
|
//https://bitbucket.org/s_l_teichmann/mtsatellite/src/e1bf980a2b278c570b3f44f9452c9c087558acb3/common/coords.go?at=default&fileviewer=file-view-default
|
||||||
const (
|
const (
|
||||||
@ -10,17 +10,17 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func CoordToPlain(x, y, z int) int64 {
|
func CoordToPlain(c MapBlockCoords) int64 {
|
||||||
return int64(z)<<(2*numBitsPerComponent) +
|
return int64(c.Z)<<(2*numBitsPerComponent) +
|
||||||
int64(y)<<numBitsPerComponent +
|
int64(c.Y)<<numBitsPerComponent +
|
||||||
int64(x)
|
int64(c.X)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unsignedToSigned(i int16) int16 {
|
func unsignedToSigned(i int16) int {
|
||||||
if i < maxPositive {
|
if i < maxPositive {
|
||||||
return i
|
return int(i)
|
||||||
}
|
}
|
||||||
return i - maxPositive*2
|
return int(i - maxPositive*2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// To match C++ code.
|
// To match C++ code.
|
||||||
@ -32,11 +32,12 @@ func pythonModulo(i int16) int16 {
|
|||||||
return modulo - -i&mask
|
return modulo - -i&mask
|
||||||
}
|
}
|
||||||
|
|
||||||
func PlainToCoord(i int64) (int, int, int) {
|
func PlainToCoord(i int64) MapBlockCoords {
|
||||||
x := unsignedToSigned(pythonModulo(int16(i)))
|
c := MapBlockCoords{}
|
||||||
i = (i - int64(x)) >> numBitsPerComponent
|
c.X = unsignedToSigned(pythonModulo(int16(i)))
|
||||||
y := unsignedToSigned(pythonModulo(int16(i)))
|
i = (i - int64(c.X)) >> numBitsPerComponent
|
||||||
i = (i - int64(x)) >> numBitsPerComponent
|
c.Y = unsignedToSigned(pythonModulo(int16(i)))
|
||||||
z := unsignedToSigned(pythonModulo(int16(i)))
|
i = (i - int64(c.Y)) >> numBitsPerComponent
|
||||||
return int(x), int(y), int(z)
|
c.Z = unsignedToSigned(pythonModulo(int16(i)))
|
||||||
|
return c
|
||||||
}
|
}
|
42
coords/convert_test.go
Normal file
42
coords/convert_test.go
Normal 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
9
coords/mapblockcoords.go
Normal 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}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user