Przeglądaj źródła

Add unit tests for points

implement_v2
Wouter Horlings 4 miesięcy temu
rodzic
commit
4679dd8a22
16 zmienionych plików z 154 dodań i 29 usunięć
  1. +1
    -1
      pkg/path/path.go
  2. +1
    -1
      pkg/path/splines.go
  3. +1
    -1
      pkg/pattern/pattern.go
  4. +2
    -2
      pkg/point/absolute_point.go
  5. +71
    -0
      pkg/point/absolute_point_test.go
  6. +2
    -3
      pkg/point/between_point.go
  7. +48
    -0
      pkg/point/between_point_test.go
  8. +4
    -3
      pkg/point/extend_point.go
  9. +1
    -1
      pkg/point/point.go
  10. +1
    -1
      pkg/point/relative_point.go
  11. +5
    -0
      pkg/position/position.go
  12. +1
    -1
      pkg/template/line.go
  13. +2
    -2
      pkg/template/point.go
  14. +6
    -6
      pkg/template/point_test.go
  15. +4
    -3
      pkg/util/id_test.go
  16. +4
    -4
      templates/tailored_shirt_block.yaml

+ 1
- 1
pkg/path/path.go Wyświetl plik

@@ -20,7 +20,7 @@ func (p *Polygon) Through() []point.Point {

// NewPolygon returns a new [Polygon].
func NewPolygon(points []point.Point, style Style, id util.ID) *Polygon {
return &Polygon{points: points, style: style}
return &Polygon{points: points, style: style, id: id}
}

// WithStyle updates the style of the Polygon.


+ 1
- 1
pkg/path/splines.go Wyświetl plik

@@ -2,10 +2,10 @@ package path

import (
"fmt"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"log/slog"

"git.wtrh.nl/patterns/gopatterns/pkg/point"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"github.com/tdewolff/canvas"
splines "gitlab.com/Achilleshiel/gosplines"
)


+ 1
- 1
pkg/pattern/pattern.go Wyświetl plik

@@ -3,11 +3,11 @@ package pattern

import (
"fmt"
"git.wtrh.nl/patterns/gopatterns/pkg/util"

"git.wtrh.nl/patterns/gopatterns/pkg/dimensions"
"git.wtrh.nl/patterns/gopatterns/pkg/pattern/text"
"git.wtrh.nl/patterns/gopatterns/pkg/point"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"github.com/tdewolff/canvas"
"golang.org/x/image/font/gofont/goregular"
"gopkg.in/Knetic/govaluate.v3"


+ 2
- 2
pkg/point/absolute_point.go Wyświetl plik

@@ -19,7 +19,7 @@ type AbsolutePoint struct {

// Matrix calculates and returns the [canvas.Matrix] of a point.
func (a *AbsolutePoint) Matrix() canvas.Matrix {
return canvas.Identity.Translate(a.position.Vector.Values()).Rotate(a.position.Rotation)
return canvas.Identity.Translate(a.position.Vector.Values()).Rotate(a.position.RotationD())
}

// ID returns the point ID.
@@ -69,7 +69,7 @@ func (a *AbsolutePoint) SetDraw() {

// UnsetDraw indicates that the point should not be drawn.
func (a *AbsolutePoint) UnsetDraw() {
a.draw = true
a.draw = false
}

// Hide returns if the point must remain hidden.


+ 71
- 0
pkg/point/absolute_point_test.go Wyświetl plik

@@ -0,0 +1,71 @@
package point_test

import (
"math"
"testing"

"git.wtrh.nl/patterns/gopatterns/pkg/point"
"git.wtrh.nl/patterns/gopatterns/pkg/position"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"git.wtrh.nl/patterns/gopatterns/pkg/vector"
"github.com/stretchr/testify/require"
"github.com/tdewolff/canvas"
)

func EqualMatrix(t *testing.T, expected, actual canvas.Matrix, delta float64) {
t.Helper()

require.InDeltaSlice(t, expected[0][:], actual[0][:], delta)
require.InDeltaSlice(t, expected[1][:], actual[1][:], delta)
}

func TestAbsolutePoint_Matrix(t *testing.T) {
rotation := math.Pi/2 + 2
newPoint := point.NewAbsolutePoint(1, 2, rotation, "1")
actual := newPoint.Matrix()

expected := canvas.Matrix{
{math.Cos(rotation), -math.Sin(rotation), 1.0},
{math.Sin(rotation), math.Cos(rotation), 2.0},
}

EqualMatrix(t, actual, expected, 1e-10)
}

func TestAbsolutePoint_ID(t *testing.T) {
newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value")
require.Equal(t, util.ID("this is a test value"), newPoint.ID())
}

func TestAbsolutePoint_Name(t *testing.T) {
require.Equal(t, "this is a test value", point.NewAbsolutePoint(1, 2, 3, "this is a test value").Name())
}

func TestAbsolutePoint_Position(t *testing.T) {
actual := point.NewAbsolutePoint(1, 2, 3, "this is a test value").Position()
expected := position.Position{
Vector: vector.Vector{X: 1, Y: 2},
Rotation: 3,
}

require.Equal(t, expected, actual)
}

func TestAbsolutePoint_Draw(t *testing.T) {
newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value")
require.False(t, newPoint.Draw())

newPoint.SetDraw()
require.True(t, newPoint.Draw())

newPoint.UnsetDraw()
require.False(t, newPoint.Draw())
}

func TestAbsolutePoint_Hide(t *testing.T) {
newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value")
require.False(t, newPoint.Hide())

newPoint.SetHide()
require.True(t, newPoint.Hide())
}

+ 2
- 3
pkg/point/between_point.go Wyświetl plik

@@ -1,10 +1,10 @@
package point

import (
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"math"

"git.wtrh.nl/patterns/gopatterns/pkg/position"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"git.wtrh.nl/patterns/gopatterns/pkg/vector"
"github.com/tdewolff/canvas"
)
@@ -53,8 +53,7 @@ func (b *BetweenPoint) Vector() vector.Vector {

// Matrix calculates and returns the [canvas.Matrix] of a point.
func (b *BetweenPoint) Matrix() canvas.Matrix {
return b.p.Matrix().Translate(b.inBetween().Values()).
Rotate((b.p.Vector().AngleBetween(b.q.Vector()) - math.Pi/2) * 180 / math.Pi)
return canvas.Identity.Translate(b.Vector().Values()).Rotate(b.Position().RotationD())
}

// ID returns the point ID.


+ 48
- 0
pkg/point/between_point_test.go Wyświetl plik

@@ -0,0 +1,48 @@
package point_test

import (
"math"
"testing"

"git.wtrh.nl/patterns/gopatterns/pkg/point"
"git.wtrh.nl/patterns/gopatterns/pkg/position"
"git.wtrh.nl/patterns/gopatterns/pkg/position/testutil"
"git.wtrh.nl/patterns/gopatterns/pkg/vector"
"github.com/tdewolff/canvas"
)

func TestBetweenPoint_Position(t *testing.T) {
p1 := point.NewAbsolutePoint(0, 0, 0, "1")
p2 := point.NewAbsolutePoint(1, 1, 0, "2")
p3 := point.NewBetweenPoint(p1, p2, 0.5, "3")
testutil.EqualPosition(t, position.Position{
Vector: vector.Vector{
X: 0.5,
Y: 0.5,
},
Rotation: -math.Pi / 4,
}, p3.Position(), 1e-10)

matrix := p3.Matrix()
EqualMatrix(t, canvas.Matrix{
{math.Cos(-math.Pi / 4), -math.Sin(-math.Pi / 4), 0.5},
{math.Sin(-math.Pi / 4), math.Cos(-math.Pi / 4), 0.5},
}, matrix, 1e-10)

p4 := point.NewAbsolutePoint(0, 1, 0, "4")
p5 := point.NewBetweenPoint(p3, p4, 0.5, "5")

testutil.EqualPosition(t, position.Position{
Vector: vector.Vector{
X: 0.25,
Y: 0.75,
},
Rotation: math.Pi / 4,
}, p5.Position(), 1e-10)

matrix2 := p5.Matrix()
EqualMatrix(t, canvas.Matrix{
{math.Cos(math.Pi / 4), -math.Sin(math.Pi / 4), 0.25},
{math.Sin(math.Pi / 4), math.Cos(math.Pi / 4), 0.75},
}, matrix2, 1e-10)
}

+ 4
- 3
pkg/point/extend_point.go Wyświetl plik

@@ -1,10 +1,10 @@
package point

import (
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"math"

"git.wtrh.nl/patterns/gopatterns/pkg/position"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"git.wtrh.nl/patterns/gopatterns/pkg/vector"
"github.com/tdewolff/canvas"
)
@@ -52,8 +52,9 @@ func (b *ExtendPoint) Vector() vector.Vector {

// Matrix calculates and returns the [canvas.Matrix] of a point.
func (b *ExtendPoint) Matrix() canvas.Matrix {
return b.to.Matrix().Translate(b.extendedVector().Values()).
Rotate((b.from.Vector().AngleBetween(b.to.Vector()) - math.Pi/2) * 180 / math.Pi)
return canvas.Identity.Translate(b.Position().Vector.Values()).Rotate(b.Position().RotationD())
//return b.to.Matrix().Translate(b.extendedVector().Values()).
// Rotate((b.from.Vector().AngleBetween(b.to.Vector()) - math.Pi/2) * 180 / math.Pi)
}

// ID returns the point ID.


+ 1
- 1
pkg/point/point.go Wyświetl plik

@@ -53,7 +53,7 @@ func Draw(c *canvas.Canvas, point Point, face *canvas.FontFace, debug bool) {
c.RenderPath(path, style, m)

text := canvas.NewTextLine(face, point.Name(), canvas.Bottom)
c.RenderText(text, m.Translate(2, -4))
c.RenderText(text, m.Translate(2, -4).Rotate(-point.Position().RotationD()))

if debug {
yStyle := canvas.Style{


+ 1
- 1
pkg/point/relative_point.go Wyświetl plik

@@ -1,10 +1,10 @@
package point

import (
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"math"

"git.wtrh.nl/patterns/gopatterns/pkg/position"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"git.wtrh.nl/patterns/gopatterns/pkg/vector"
"github.com/tdewolff/canvas"
)


+ 5
- 0
pkg/position/position.go Wyświetl plik

@@ -22,6 +22,11 @@ func (p Position) Add(q Position) Position {
}
}

// RotationD returns the rotation angle of the position in degrees.
func (p Position) RotationD() float64 {
return p.Rotation * 180 / math.Pi
}

// Distance returns the distance between two positions.
func (p Position) Distance(q Position) float64 {
return p.Vector.Distance(q.Vector)


+ 1
- 1
pkg/template/line.go Wyświetl plik

@@ -2,11 +2,11 @@ package template

import (
"errors"
"git.wtrh.nl/patterns/gopatterns/pkg/util"

"git.wtrh.nl/patterns/gopatterns/pkg/path"
"git.wtrh.nl/patterns/gopatterns/pkg/pattern"
"git.wtrh.nl/patterns/gopatterns/pkg/point"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
)

var ErrLineNotFound = errors.New("required path not found")


+ 2
- 2
pkg/template/point.go Wyświetl plik

@@ -3,13 +3,13 @@ package template
import (
"errors"
"fmt"
"git.wtrh.nl/patterns/gopatterns/pkg/path"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"maps"
"math"
"strconv"

"git.wtrh.nl/patterns/gopatterns/pkg/path"
"git.wtrh.nl/patterns/gopatterns/pkg/point"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"gopkg.in/Knetic/govaluate.v3"
)



+ 6
- 6
pkg/template/point_test.go Wyświetl plik

@@ -2,7 +2,6 @@ package template_test

import (
_ "embed"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"math"
"testing"

@@ -10,6 +9,7 @@ import (
"git.wtrh.nl/patterns/gopatterns/pkg/position"
"git.wtrh.nl/patterns/gopatterns/pkg/position/testutil"
"git.wtrh.nl/patterns/gopatterns/pkg/template"
"git.wtrh.nl/patterns/gopatterns/pkg/util"
"git.wtrh.nl/patterns/gopatterns/pkg/vector"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
@@ -231,11 +231,11 @@ func TestFunctions(t *testing.T) {
tests := map[string]struct {
result float64
}{
"distance": {result: 5},
"angle": {result: math.Atan2(3, 4)},
"yDistance": {result: 3},
"xDistance": {result: 4},
"lineLength": {result: 12},
"distance": {result: 5},
"angle": {result: math.Atan2(3, 4)},
"yDistance": {result: 3},
"xDistance": {result: 4},
"lineLength": {result: 12},
"lineLength2": {result: 4},
}
temp := &template.Template{}


+ 4
- 3
pkg/util/id_test.go Wyświetl plik

@@ -1,9 +1,10 @@
package util_test

import (
"testing"

"git.wtrh.nl/patterns/gopatterns/pkg/util"
"github.com/stretchr/testify/require"
"testing"
)

func TestID(t *testing.T) {
@@ -20,7 +21,7 @@ func TestID(t *testing.T) {
},
"1.test": {
panel: "1",
name:"test",
name: "test",
},
}
for testName, tt := range tests {
@@ -30,4 +31,4 @@ func TestID(t *testing.T) {
require.Equal(t, tt.name, id.Name())
})
}
}
}

+ 4
- 4
templates/tailored_shirt_block.yaml Wyświetl plik

@@ -367,8 +367,8 @@ panels:
8a:
between:
from: 4
to: 0
offset: 0.25
to: 9a
offset: 0.5
8:
relativeTo: 8a
position:
@@ -384,9 +384,9 @@ panels:
x: -12.5
10a:
between:
from: 4
from: 9a
to: 0
offset: 0.75
offset: 0.5
10:
relativeTo: 10a
position:


Ładowanie…
Anuluj
Zapisz