r2/spatial: add Box methods
MODIFY
spatial/r2/vector.go
spatial/r2/vector.go
@@ -76,11 +76,6 @@ func Cos(p, q Vec) float64 {
return Dot(p, q) / (Norm(p) * Norm(q))
}
-// Box is a 2D bounding box.
-type Box struct {
- Min, Max Vec
-}
-
// Rotation describes a rotation in 2D.
type Rotation struct {
sin, cos float64
@@ -112,3 +107,48 @@ func (r Rotation) Rotate(p Vec) Vec {
func (r Rotation) isIdentity() bool {
return r.sin == 0 && r.cos == 1
}
+
+// minElem returns a vector with the element-wise
+// minimum components of vectors a and b.
+func minElem(a, b Vec) Vec {
+ return Vec{
+ X: math.Min(a.X, b.X),
+ Y: math.Min(a.Y, b.Y),
+ }
+}
+
+// maxElem returns a vector with the element-wise
+// maximum components of vectors a and b.
+func maxElem(a, b Vec) Vec {
+ return Vec{
+ X: math.Max(a.X, b.X),
+ Y: math.Max(a.Y, b.Y),
+ }
+}
+
+// absElem returns the vector with components set to their absolute value.
+func absElem(a Vec) Vec {
+ return Vec{
+ X: math.Abs(a.X),
+ Y: math.Abs(a.Y),
+ }
+}
+
+// mulElem returns the Hadamard product between vectors a and b.
+// v = {a.X*b.X, a.Y*b.Y, a.Z*b.Z}
+func mulElem(a, b Vec) Vec {
+ return Vec{
+ X: a.X * b.X,
+ Y: a.Y * b.Y,
+ }
+}
+
+// divElem returns the Hadamard product between vector a
+// and the inverse components of vector b.
+// v = {a.X/b.X, a.Y/b.Y, a.Z/b.Z}
+func divElem(a, b Vec) Vec {
+ return Vec{
+ X: a.X / b.X,
+ Y: a.Y / b.Y,
+ }
+}

MODIFY
spatial/r2/vector_test.go
spatial/r2/vector_test.go
@@ -8,6 +8,7 @@ import (
"math"
"testing"
+ "golang.org/x/exp/rand"
"gonum.org/v1/gonum/floats/scalar"
)
@@ -170,6 +171,7 @@ func TestNorm2(t *testing.T) {
}
func TestUnit(t *testing.T) {
+ const tol = 1e-14
for _, test := range []struct {
v, want Vec
}{
@@ -185,7 +187,7 @@ func TestUnit(t *testing.T) {
{Vec{1e4, math.MaxFloat32 - 1}, Vec{0, 1}},
} {
got := Unit(test.v)
- if !vecApproxEqual(got, test.want) {
+ if !vecApproxEqual(got, test.want, tol) {
t.Errorf(
"Unit(%v) = %v, want %v",
test.v, got, test.want,
@@ -201,6 +203,7 @@ func TestUnit(t *testing.T) {
}
func TestCos(t *testing.T) {
+ const tol = 1e-14
for _, test := range []struct {
v1, v2 Vec
want float64
@@ -211,7 +214,6 @@ func TestCos(t *testing.T) {
{Vec{1, 0}, Vec{0, 1}, 0},
{Vec{1, 0}, Vec{-1, 0}, -1},
} {
- tol := 1e-14
got := Cos(test.v1, test.v2)
if !scalar.EqualWithinAbs(got, test.want, tol) {
t.Errorf("cos(%v, %v)= %v, want %v",
@@ -222,6 +224,7 @@ func TestCos(t *testing.T) {
}
func TestRotate(t *testing.T) {
+ const tol = 1e-14
for _, test := range []struct {
v, q Vec
alpha float64
@@ -237,7 +240,7 @@ func TestRotate(t *testing.T) {
{Vec{2, 2}, Vec{2, 0}, math.Pi, Vec{2, -2}},
} {
got := Rotate(test.v, test.alpha, test.q)
- if !vecApproxEqual(got, test.want) {
+ if !vecApproxEqual(got, test.want, tol) {
t.Errorf(
"rotate(%v, %v, %v)= %v, want=%v",
test.v, test.alpha, test.q, got, test.want,
@@ -254,12 +257,26 @@ func vecIsNaNAny(v Vec) bool {
return math.IsNaN(v.X) || math.IsNaN(v.Y)
}
-func vecApproxEqual(a, b Vec) bool {
- const tol = 1e-14
+func vecApproxEqual(a, b Vec, tol float64) bool {
+ if tol == 0 {
+ return vecEqual(a, b)
+ }
if vecIsNaNAny(a) || vecIsNaNAny(b) {
return vecIsNaN(a) && vecIsNaN(b)
}
-
return scalar.EqualWithinAbs(a.X, b.X, tol) &&
scalar.EqualWithinAbs(a.Y, b.Y, tol)
}
+
+func randomVec(rnd *rand.Rand) (v Vec) {
+ v.X = (rnd.Float64() - 0.5) * 20
+ v.Y = (rnd.Float64() - 0.5) * 20
+ return v
+}
+
+func vecEqual(a, b Vec) bool {
+ if vecIsNaNAny(a) || vecIsNaNAny(b) {
+ return vecIsNaN(a) && vecIsNaN(b)
+ }
+ return a == b
+}

r2/spatial: add Box methods