Initial commit
This commit is contained in:
35
internal/util/itype/angle.go
Normal file
35
internal/util/itype/angle.go
Normal file
@ -0,0 +1,35 @@
|
||||
package itype
|
||||
|
||||
import "math"
|
||||
|
||||
// Angle wraps a angle in the form of radian float32.
|
||||
//
|
||||
// Its value lies strictly in [0, 2Pi).
|
||||
type Angle float32
|
||||
|
||||
func normalizeRadians(rad float64) float64 {
|
||||
if rad < 0 {
|
||||
return 2*math.Pi - math.Mod(-rad, 2*math.Pi)
|
||||
}
|
||||
return math.Mod(rad, 2*math.Pi)
|
||||
}
|
||||
|
||||
// Radians construct an Angle from a radian value.
|
||||
func Radians(rad float32) Angle {
|
||||
return Angle(normalizeRadians(float64(rad)))
|
||||
}
|
||||
|
||||
// Degrees construct an Angle from a degree value.
|
||||
func Degrees(deg float32) Angle {
|
||||
return Radians(deg * math.Pi / 180)
|
||||
}
|
||||
|
||||
// Radians return the value of the Angle in radians.
|
||||
func (a Angle) Radians() float32 {
|
||||
return float32(normalizeRadians(float64(a)))
|
||||
}
|
||||
|
||||
// Degrees return the value of the Angle in degrees.
|
||||
func (a Angle) Degrees() float32 {
|
||||
return float32(normalizeRadians(float64(a)) * 180 / math.Pi)
|
||||
}
|
11
internal/util/itype/dataset.go
Normal file
11
internal/util/itype/dataset.go
Normal file
@ -0,0 +1,11 @@
|
||||
package itype
|
||||
|
||||
// Dataset describes arbitrary data stored in String-Interface{} pairs.
|
||||
// This is the major way how complicated data is stored in blocks and entities.
|
||||
//
|
||||
// The empty interface should only hold these 4 types:
|
||||
// - int
|
||||
// - float64
|
||||
// - bool
|
||||
// - string
|
||||
type Dataset map[string]interface{}
|
37
internal/util/itype/direction.go
Normal file
37
internal/util/itype/direction.go
Normal file
@ -0,0 +1,37 @@
|
||||
package itype
|
||||
|
||||
type Direction int
|
||||
|
||||
const (
|
||||
XPlus Direction = iota // X+
|
||||
XMinus // X-
|
||||
YPlus // Y+
|
||||
YMinus // Y-
|
||||
ZPlus // Z+
|
||||
ZMinus // Z-
|
||||
)
|
||||
|
||||
var DirectionVeci = [6]Vec3i{
|
||||
XPlus: {1, 0, 0},
|
||||
XMinus: {-1, 0, 0},
|
||||
YPlus: {0, 1, 0},
|
||||
YMinus: {0, -1, 0},
|
||||
ZPlus: {0, 0, 1},
|
||||
ZMinus: {0, 0, -1},
|
||||
}
|
||||
var DirectionVecf = [6]Vec3f{
|
||||
XPlus: {1, 0, 0},
|
||||
XMinus: {-1, 0, 0},
|
||||
YPlus: {0, 1, 0},
|
||||
YMinus: {0, -1, 0},
|
||||
ZPlus: {0, 0, 1},
|
||||
ZMinus: {0, 0, -1},
|
||||
}
|
||||
var DirectionVecd = [6]Vec3d{
|
||||
XPlus: {1, 0, 0},
|
||||
XMinus: {-1, 0, 0},
|
||||
YPlus: {0, 1, 0},
|
||||
YMinus: {0, -1, 0},
|
||||
ZPlus: {0, 0, 1},
|
||||
ZMinus: {0, 0, -1},
|
||||
}
|
133
internal/util/itype/rect.go
Normal file
133
internal/util/itype/rect.go
Normal file
@ -0,0 +1,133 @@
|
||||
package itype
|
||||
|
||||
import "sort"
|
||||
|
||||
// Recti is a 2D rectangle with int coordinates.
|
||||
type Recti struct {
|
||||
Left, Top int
|
||||
Width, Height int
|
||||
}
|
||||
|
||||
// Rectf is a 2D rectangle with float32 coordinates.
|
||||
type Rectf struct {
|
||||
Left, Top float32
|
||||
Width, Height float32
|
||||
}
|
||||
|
||||
// Rectd is a 2D rectangle with float64 coordinates.
|
||||
type Rectd struct {
|
||||
Left, Top float64
|
||||
Width, Height float64
|
||||
}
|
||||
|
||||
// Boxi is a 3D box with int coordinates.
|
||||
type Boxi struct {
|
||||
OffX, OffY, OffZ int
|
||||
SizeX, SizeY, SizeZ int
|
||||
}
|
||||
|
||||
// Boxf is a 3D box with float32 coordinates.
|
||||
type Boxf struct {
|
||||
OffX, OffY, OffZ float32
|
||||
SizeX, SizeY, SizeZ float32
|
||||
}
|
||||
|
||||
func (b Boxf) Offset(offset Vec3f) Boxf {
|
||||
return Boxf{
|
||||
OffX: b.OffX + offset[0],
|
||||
OffY: b.OffY + offset[1],
|
||||
OffZ: b.OffZ + offset[2],
|
||||
SizeX: b.SizeX,
|
||||
SizeY: b.SizeY,
|
||||
SizeZ: b.SizeZ,
|
||||
}
|
||||
}
|
||||
|
||||
func (b Boxf) MinPoint() Vec3f {
|
||||
return Vec3f{b.OffX, b.OffY, b.OffZ}
|
||||
}
|
||||
func (b Boxf) MaxPoint() Vec3f {
|
||||
return Vec3f{b.OffX + b.SizeX, b.OffY + b.SizeY, b.OffZ + b.SizeZ}
|
||||
}
|
||||
|
||||
// Boxd is a 3D box with float64 coordinates.
|
||||
type Boxd struct {
|
||||
OffX, OffY, OffZ float64
|
||||
SizeX, SizeY, SizeZ float64
|
||||
}
|
||||
|
||||
func (b Boxd) ToFloat32() Boxf {
|
||||
return Boxf{
|
||||
OffX: float32(b.OffX),
|
||||
OffY: float32(b.OffY),
|
||||
OffZ: float32(b.OffZ),
|
||||
SizeX: float32(b.SizeX),
|
||||
SizeY: float32(b.SizeY),
|
||||
SizeZ: float32(b.SizeZ),
|
||||
}
|
||||
}
|
||||
|
||||
func (b Boxd) Offset(offset Vec3d) Boxd {
|
||||
return Boxd{
|
||||
OffX: b.OffX + offset[0],
|
||||
OffY: b.OffY + offset[1],
|
||||
OffZ: b.OffZ + offset[2],
|
||||
SizeX: b.SizeX,
|
||||
SizeY: b.SizeY,
|
||||
SizeZ: b.SizeZ,
|
||||
}
|
||||
}
|
||||
|
||||
func (b Boxd) Offsetv(x, y, z float64) Boxd {
|
||||
return Boxd{
|
||||
OffX: b.OffX + x,
|
||||
OffY: b.OffY + y,
|
||||
OffZ: b.OffZ + z,
|
||||
SizeX: b.SizeX,
|
||||
SizeY: b.SizeY,
|
||||
SizeZ: b.SizeZ,
|
||||
}
|
||||
}
|
||||
|
||||
func (b Boxd) MinPoint() Vec3d {
|
||||
return Vec3d{b.OffX, b.OffY, b.OffZ}
|
||||
}
|
||||
func (b Boxd) MaxPoint() Vec3d {
|
||||
return Vec3d{b.OffX + b.SizeX, b.OffY + b.SizeY, b.OffZ + b.SizeZ}
|
||||
}
|
||||
|
||||
func (b Boxd) Contains(point Vec3d) bool {
|
||||
return point[0] >= b.OffX && point[0] <= b.OffX+b.SizeX &&
|
||||
point[1] >= b.OffY && point[1] <= b.OffY+b.SizeY &&
|
||||
point[2] >= b.OffZ && point[2] <= b.OffZ+b.SizeZ
|
||||
}
|
||||
|
||||
func pointIntersect(n, m, p, q float64) (min, len float64) {
|
||||
if m < p || q < n { // no intersection
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
arr := []float64{n, m, p, q}
|
||||
sort.Float64s(arr)
|
||||
|
||||
return arr[1], arr[2] - arr[1]
|
||||
}
|
||||
|
||||
func (box1 Boxd) Intersects(box2 Boxd) (result Boxd) {
|
||||
a, b := pointIntersect(box1.OffX, box1.OffX+box1.SizeX, box2.OffX, box2.OffX+box2.SizeX)
|
||||
c, d := pointIntersect(box1.OffY, box1.OffY+box1.SizeY, box2.OffY, box2.OffY+box2.SizeY)
|
||||
e, f := pointIntersect(box1.OffZ, box1.OffZ+box1.SizeZ, box2.OffZ, box2.OffZ+box2.SizeZ)
|
||||
|
||||
if b == 0 || d == 0 || f == 0 {
|
||||
return Boxd{}
|
||||
} else {
|
||||
return Boxd{
|
||||
OffX: a,
|
||||
SizeX: b,
|
||||
OffY: c,
|
||||
SizeY: d,
|
||||
OffZ: e,
|
||||
SizeZ: f,
|
||||
}
|
||||
}
|
||||
}
|
147
internal/util/itype/vec.go
Normal file
147
internal/util/itype/vec.go
Normal file
@ -0,0 +1,147 @@
|
||||
package itype
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Vec2i is a two-element int vector
|
||||
type Vec2i [2]int
|
||||
|
||||
func Vec2iToFloat32(v Vec2i) Vec2f { return Vec2f{float32(v[0]), float32(v[1])} }
|
||||
|
||||
func (v Vec2i) Add(add Vec2i) Vec2i { return Vec2i{v[0] + add[0], v[1] + add[1]} }
|
||||
func (v Vec2i) MultiplyInt(mult int) Vec2i { return Vec2i{v[0] * mult, v[1] * mult} }
|
||||
|
||||
// Vec3i is a three-element int vector
|
||||
type Vec3i [3]int
|
||||
|
||||
func (v Vec3i) ToFloat32() Vec3f { return Vec3f{float32(v[0]), float32(v[1]), float32(v[2])} }
|
||||
func (v Vec3i) ToFloat64() Vec3d { return Vec3d{float64(v[0]), float64(v[1]), float64(v[2])} }
|
||||
|
||||
func (v Vec3i) Add(add Vec3i) Vec3i { return Vec3i{v[0] + add[0], v[1] + add[1], v[2] + add[2]} }
|
||||
func (v Vec3i) Addv(x, y, z int) Vec3i { return Vec3i{v[0] + x, v[1] + y, v[2] + z} }
|
||||
func (v Vec3i) MultiplyInt(mult int) Vec3i { return Vec3i{v[0] * mult, v[1] * mult, v[2] * mult} }
|
||||
|
||||
// Vec4i is a four-element int vector
|
||||
type Vec4i [4]int
|
||||
|
||||
func Vec4iToFloat32(v Vec4i) Vec4f {
|
||||
return Vec4f{float32(v[0]), float32(v[1]), float32(v[2]), float32(v[3])}
|
||||
}
|
||||
|
||||
func (v Vec4i) Add(add Vec4i) Vec4i {
|
||||
return Vec4i{
|
||||
v[0] + add[0],
|
||||
v[1] + add[1],
|
||||
v[2] + add[2],
|
||||
v[3] + add[3],
|
||||
}
|
||||
}
|
||||
func (v Vec4i) MultiplyInt(mult int) Vec4i {
|
||||
return Vec4i{
|
||||
v[0] * mult,
|
||||
v[1] * mult,
|
||||
v[2] * mult,
|
||||
v[3] * mult,
|
||||
}
|
||||
}
|
||||
|
||||
// Vec2f is a two-element float vector
|
||||
type Vec2f [2]float32
|
||||
|
||||
// Vec3f is a three-element float vector
|
||||
type Vec3f [3]float32
|
||||
|
||||
func (v Vec3f) Add(add Vec3f) Vec3f {
|
||||
return Vec3f{v[0] + add[0], v[1] + add[1], v[2] + add[2]}
|
||||
}
|
||||
|
||||
func (v Vec3f) Addv(x, y, z float32) Vec3f {
|
||||
return Vec3f{v[0] + x, v[1] + y, v[2] + z}
|
||||
}
|
||||
|
||||
func (v Vec3f) Multiply(mult float32) Vec3f {
|
||||
return Vec3f{v[0] * mult, v[1] * mult, v[2] * mult}
|
||||
}
|
||||
|
||||
func (v Vec3f) Floor() Vec3i {
|
||||
return Vec3i{
|
||||
int(math.Floor(float64(v[0]))),
|
||||
int(math.Floor(float64(v[1]))),
|
||||
int(math.Floor(float64(v[2]))),
|
||||
}
|
||||
}
|
||||
func (v Vec3f) Length() float32 {
|
||||
v0, v1, v2 := float64(v[0]), float64(v[1]), float64(v[2])
|
||||
return float32(math.Sqrt(v0*v0 + v1*v1 + v2*v2))
|
||||
}
|
||||
|
||||
func (v Vec3f) Normalize() Vec3f {
|
||||
l := v.Length()
|
||||
return Vec3f{v[0] / l, v[1] / l, v[2] / l}
|
||||
}
|
||||
|
||||
func (v Vec3f) ToFloat64() Vec3d {
|
||||
return Vec3d{float64(v[0]), float64(v[1]), float64(v[2])}
|
||||
}
|
||||
|
||||
// Vec4f is a four-element float vector
|
||||
type Vec4f [4]float32
|
||||
|
||||
// Vec2d is a two-element float64 vector
|
||||
type Vec2d [2]float64
|
||||
|
||||
// Vec3d is a three-element float64 vector
|
||||
type Vec3d [3]float64
|
||||
|
||||
func (v Vec3d) Negative() Vec3d {
|
||||
return Vec3d{-v[0], -v[1], -v[2]}
|
||||
}
|
||||
|
||||
func (v Vec3d) Add(add Vec3d) Vec3d {
|
||||
return Vec3d{v[0] + add[0], v[1] + add[1], v[2] + add[2]}
|
||||
}
|
||||
|
||||
func (v Vec3d) Addf(add Vec3f) Vec3d {
|
||||
return Vec3d{v[0] + float64(add[0]), v[1] + float64(add[1]), v[2] + float64(add[2])}
|
||||
}
|
||||
|
||||
func (v Vec3d) Addv(x, y, z float64) Vec3d {
|
||||
return Vec3d{v[0] + x, v[1] + y, v[2] + z}
|
||||
}
|
||||
|
||||
func (v Vec3d) Multiply(mult float64) Vec3d {
|
||||
return Vec3d{v[0] * mult, v[1] * mult, v[2] * mult}
|
||||
}
|
||||
|
||||
func (v1 Vec3d) Cross(v2 Vec3d) Vec3d {
|
||||
return Vec3d{v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}
|
||||
}
|
||||
|
||||
func (v1 Vec3d) Dot(v2 Vec3d) float64 {
|
||||
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]
|
||||
}
|
||||
|
||||
func (v Vec3d) Length() float64 {
|
||||
return math.Sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2])
|
||||
}
|
||||
|
||||
func (v Vec3d) Normalize() Vec3d {
|
||||
l := v.Length()
|
||||
return Vec3d{v[0] / l, v[1] / l, v[2] / l}
|
||||
}
|
||||
|
||||
func (v Vec3d) Floor() Vec3i {
|
||||
return Vec3i{
|
||||
int(math.Floor(v[0])),
|
||||
int(math.Floor(v[1])),
|
||||
int(math.Floor(v[2])),
|
||||
}
|
||||
}
|
||||
|
||||
func (v Vec3d) ToFloat32() Vec3f {
|
||||
return Vec3f{float32(v[0]), float32(v[1]), float32(v[2])}
|
||||
}
|
||||
|
||||
// Vec4d is a four-element float64 vector
|
||||
type Vec4d [4]float64
|
Reference in New Issue
Block a user