-
Notifications
You must be signed in to change notification settings - Fork 3
/
impl3_test.go
126 lines (123 loc) · 2.83 KB
/
impl3_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ui
import (
"context"
"github.com/Yeicor/sdfx-ui/internal"
"github.com/deadsy/sdfx/sdf"
v3 "github.com/deadsy/sdfx/vec/v3"
"image"
"math"
"sync"
"testing"
)
func BenchmarkDevRenderer3_Render(b *testing.B) {
s, _ := sdf.ArcSpiral2D(1.0, 20.0, 0.25*sdf.Pi, 8*sdf.Tau, 1.0)
s3, _ := sdf.ExtrudeRounded3D(s, 4, 1)
impl := newDevRenderer3(s3)
b.ReportAllocs()
state := internal.RendererState{
ResInv: 8,
Bb: s.BoundingBox(),
}
fullRender := image.NewRGBA(image.Rect(0, 0, 1920/state.ResInv, 1080/state.ResInv))
lock1 := &sync.RWMutex{}
lock2 := &sync.RWMutex{}
b.ResetTimer()
for n := 0; n < b.N; n++ {
err := impl.Render(&internal.RenderArgs{Ctx: context.Background(), State: &state, StateLock: lock1, CachedRenderLock: lock2, FullRender: fullRender})
if err != nil {
b.Fatal(err)
}
}
}
func Test_collideRayBb(t *testing.T) {
type args struct {
origin v3.Vec
dir v3.Vec
bb sdf.Box3
}
tests := []struct {
name string
args args
want float64
}{
{
name: "Basic",
args: args{
origin: v3.Vec{Z: -2},
dir: v3.Vec{Z: 1},
bb: sdf.Box3{
Min: v3.Vec{X: -1, Y: -1, Z: -1},
Max: v3.Vec{X: 1, Y: 1, Z: 1},
},
},
want: 1,
},
{
name: "Sideways",
args: args{
origin: v3.Vec{X: -2, Y: -2, Z: -2},
dir: v3.Vec{X: 1, Y: 1, Z: 1}.Normalize(),
bb: sdf.Box3{
Min: v3.Vec{X: -1, Y: -1, Z: -1},
Max: v3.Vec{X: 1, Y: 1, Z: 1},
},
},
want: v3.Vec{X: 1, Y: 1, Z: 1}.Length(),
},
{
name: "Backwards",
args: args{
origin: v3.Vec{X: 2, Y: 2, Z: 2},
dir: v3.Vec{X: 1, Y: 1, Z: 1}.Normalize(),
bb: sdf.Box3{
Min: v3.Vec{X: -1, Y: -1, Z: -1},
Max: v3.Vec{X: 1, Y: 1, Z: 1},
},
},
want: -v3.Vec{X: 1, Y: 1, Z: 1}.Length(),
},
{
name: "Inside",
args: args{
origin: v3.Vec{X: 0.1, Y: 0.1, Z: 0.1},
dir: v3.Vec{X: 1, Y: 1, Z: 1}.Normalize(),
bb: sdf.Box3{
Min: v3.Vec{X: -1, Y: -1, Z: -1},
Max: v3.Vec{X: 1, Y: 1, Z: 1},
},
},
want: v3.Vec{X: 0.9, Y: 0.9, Z: 0.9}.Length(),
},
{
name: "Inside2",
args: args{
origin: v3.Vec{X: 0.1, Y: 0.1, Z: 0.1},
dir: v3.Vec{X: -1, Y: -1, Z: -1}.Normalize(),
bb: sdf.Box3{
Min: v3.Vec{X: -1, Y: -1, Z: -1},
Max: v3.Vec{X: 1, Y: 1, Z: 1},
},
},
want: v3.Vec{X: -1.1, Y: -1.1, Z: -1.1}.Length(),
},
{
name: "No hit",
args: args{
origin: v3.Vec{X: 10, Y: 0, Z: 0},
dir: v3.Vec{X: 1, Y: 1, Z: 1}.Normalize(),
bb: sdf.Box3{
Min: v3.Vec{X: -1, Y: -1, Z: -1},
Max: v3.Vec{X: 1, Y: 1, Z: 1},
},
},
want: -15.588457268119893,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := collideRayBb(tt.args.origin, tt.args.dir, tt.args.bb); math.Abs(got-tt.want) > 1e-12 {
t.Errorf("collideRayBb() = %v, want %v", got, tt.want)
}
})
}
}