-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode_test.go
More file actions
607 lines (507 loc) · 14.2 KB
/
Copy pathnode_test.go
File metadata and controls
607 lines (507 loc) · 14.2 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package g3d
import (
"math"
"testing"
)
// Uses shared test helpers from math_test.go:
// epsilon, approxEqual, approxEqualVec3
// Uses shared test helper from matrix_test.go:
// approxEqualMat4
// --- NewNode defaults ---
func TestNewNodeDefaults(t *testing.T) {
n := NewNode()
if n.Position != (Vec3{}) {
t.Errorf("expected zero position, got %v", n.Position)
}
if n.Rotation != (Euler{}) {
t.Errorf("expected zero rotation, got %v", n.Rotation)
}
if n.Scale != (Vec3{1, 1, 1}) {
t.Errorf("expected scale {1,1,1}, got %v", n.Scale)
}
if !n.Visible() {
t.Error("expected visible=true")
}
if n.Parent() != nil {
t.Error("expected nil parent")
}
if n.ChildCount() != 0 {
t.Error("expected 0 children")
}
if n.Name() != "" {
t.Errorf("expected empty name, got %q", n.Name())
}
if n.UserData() != nil {
t.Error("expected nil userData")
}
}
func TestNewNodeScaleNotZero(t *testing.T) {
n := NewNode()
// Scale must be {1,1,1}, not {0,0,0}. Zero scale collapses geometry.
if n.Scale == (Vec3{}) {
t.Fatal("CRITICAL: Scale default is {0,0,0} — must be {1,1,1}")
}
}
// --- Name, Visible, UserData ---
func TestNodeSetName(t *testing.T) {
n := NewNode()
n.SetName("Cube")
if n.Name() != "Cube" {
t.Errorf("expected name Cube, got %q", n.Name())
}
}
func TestNodeSetVisible(t *testing.T) {
n := NewNode()
n.SetVisible(false)
if n.Visible() {
t.Error("expected visible=false")
}
n.SetVisible(true)
if !n.Visible() {
t.Error("expected visible=true")
}
}
func TestNodeUserData(t *testing.T) {
n := NewNode()
n.SetUserData("hello")
if n.UserData() != "hello" {
t.Errorf("expected userData hello, got %v", n.UserData())
}
}
// --- Parent-Child relationships ---
func TestNodeAddChild(t *testing.T) {
parent := NewNode()
child := NewNode()
parent.Add(child)
if child.Parent() != parent {
t.Error("child parent should be parent")
}
if parent.ChildCount() != 1 {
t.Errorf("expected 1 child, got %d", parent.ChildCount())
}
}
func TestNodeAddChildReparents(t *testing.T) {
oldParent := NewNode()
newParent := NewNode()
child := NewNode()
oldParent.Add(child)
if child.Parent() != oldParent {
t.Error("child should be under oldParent")
}
newParent.Add(child)
if child.Parent() != newParent {
t.Error("child should be reparented to newParent")
}
if oldParent.ChildCount() != 0 {
t.Error("oldParent should have 0 children after reparent")
}
if newParent.ChildCount() != 1 {
t.Error("newParent should have 1 child")
}
}
func TestNodeRemoveChild(t *testing.T) {
parent := NewNode()
child := NewNode()
parent.Add(child)
parent.Remove(child)
if child.Parent() != nil {
t.Error("child parent should be nil after remove")
}
if parent.ChildCount() != 0 {
t.Error("parent should have 0 children")
}
}
func TestNodeRemoveNonChild(t *testing.T) {
parent := NewNode()
other := NewNode()
// Should not panic.
parent.Remove(other)
if parent.ChildCount() != 0 {
t.Error("no children should have been affected")
}
}
func TestNodeAddNil(t *testing.T) {
n := NewNode()
n.Add(nil) // Should not panic.
if n.ChildCount() != 0 {
t.Error("adding nil should be a no-op")
}
}
func TestNodeAddSelf(t *testing.T) {
n := NewNode()
n.Add(n) // Should not panic or create a cycle.
if n.ChildCount() != 0 {
t.Error("adding self should be a no-op")
}
}
func TestNodeAddCircular(t *testing.T) {
a := NewNode()
b := NewNode()
c := NewNode()
a.Add(b)
b.Add(c)
// c is a descendant of a. Adding a as child of c would create a cycle.
c.Add(a)
if a.Parent() == c {
t.Error("adding ancestor as child should be prevented")
}
}
func TestNodeRemoveNil(t *testing.T) {
n := NewNode()
n.Remove(nil) // Should not panic.
}
// --- Children() returns a copy ---
func TestNodeChildrenReturnsCopy(t *testing.T) {
parent := NewNode()
child1 := NewNode()
child2 := NewNode()
parent.Add(child1)
parent.Add(child2)
children := parent.Children()
if len(children) != 2 {
t.Fatalf("expected 2 children, got %d", len(children))
}
// Mutate the returned slice — should NOT affect the node.
children[0] = nil
if parent.Children()[0] == nil {
t.Error("Children() must return a copy, not the internal slice")
}
}
func TestNodeChildrenEmptyReturnsNil(t *testing.T) {
n := NewNode()
if n.Children() != nil {
t.Error("Children() on a node with no children should return nil")
}
}
// --- Dirty flag propagation ---
func TestNodeSetPositionDirties(t *testing.T) {
n := NewNode()
_ = n.LocalMatrix() // Force clean.
_ = n.WorldMatrix()
n.SetPosition(Vec3{1, 2, 3})
if !n.localDirty {
t.Error("SetPosition should dirty localMatrix")
}
if !n.worldDirty {
t.Error("SetPosition should dirty worldMatrix")
}
}
func TestNodeSetRotationDirties(t *testing.T) {
n := NewNode()
_ = n.LocalMatrix()
_ = n.WorldMatrix()
n.SetRotation(Euler{0.1, 0, 0})
if !n.localDirty {
t.Error("SetRotation should dirty localMatrix")
}
}
func TestNodeSetScaleDirties(t *testing.T) {
n := NewNode()
_ = n.LocalMatrix()
_ = n.WorldMatrix()
n.SetScale(Vec3{2, 2, 2})
if !n.localDirty {
t.Error("SetScale should dirty localMatrix")
}
}
func TestNodeDirtyPropagesToChildren(t *testing.T) {
parent := NewNode()
child := NewNode()
grandchild := NewNode()
parent.Add(child)
child.Add(grandchild)
// Force all clean.
_ = parent.WorldMatrix()
_ = child.WorldMatrix()
_ = grandchild.WorldMatrix()
// Now dirty the parent.
parent.SetPosition(Vec3{5, 0, 0})
if !child.worldDirty {
t.Error("parent SetPosition should propagate worldDirty to child")
}
if !grandchild.worldDirty {
t.Error("parent SetPosition should propagate worldDirty to grandchild")
}
}
func TestNodeDirtyPropagationSkipsAlreadyDirty(t *testing.T) {
// When a node is already worldDirty, the propagation should early-exit
// to avoid O(N^2) re-traversal. We verify this indirectly: no panic,
// and the flags are correct.
parent := NewNode()
child := NewNode()
parent.Add(child)
// Both start dirty (from NewNode + Add). Set one clean, then re-dirty.
_ = parent.WorldMatrix()
_ = child.WorldMatrix()
parent.SetPosition(Vec3{1, 0, 0})
parent.SetPosition(Vec3{2, 0, 0}) // Second dirty should be fine.
if !child.worldDirty {
t.Error("child should be worldDirty")
}
}
// --- LocalMatrix correctness ---
func TestNodeLocalMatrixIdentity(t *testing.T) {
n := NewNode()
got := n.LocalMatrix()
want := Mat4Identity()
if got != want {
t.Errorf("default LocalMatrix should be identity\n got: %v\n want: %v", got, want)
}
}
func TestNodeLocalMatrixTranslation(t *testing.T) {
n := NewNode()
n.SetPosition(Vec3{3, 4, 5})
m := n.LocalMatrix()
// Column 3 (indices 12,13,14) holds translation.
if !approxEqual(m[12], 3, epsilon) || !approxEqual(m[13], 4, epsilon) || !approxEqual(m[14], 5, epsilon) {
t.Errorf("translation should be (3,4,5), got (%v,%v,%v)", m[12], m[13], m[14])
}
}
func TestNodeLocalMatrixScale(t *testing.T) {
n := NewNode()
n.SetScale(Vec3{2, 3, 4})
m := n.LocalMatrix()
// For pure scale, diagonal entries are (sx, sy, sz, 1).
if !approxEqual(m[0], 2, epsilon) || !approxEqual(m[5], 3, epsilon) || !approxEqual(m[10], 4, epsilon) {
t.Errorf("scale diagonal should be (2,3,4), got (%v,%v,%v)", m[0], m[5], m[10])
}
}
func TestNodeLocalMatrixRotation90Y(t *testing.T) {
n := NewNode()
n.SetRotation(Euler{0, math.Pi / 2, 0}) // 90 degrees around Y.
m := n.LocalMatrix()
// After 90-deg Y rotation:
// col0 (X-axis) should point toward +Z: (0, 0, -1)
// col2 (Z-axis) should point toward +X: (1, 0, 0)
// (Column-major: col0 = m[0..3], col2 = m[8..11])
if !approxEqual(m[0], 0, epsilon) || !approxEqual(m[2], -1, epsilon) {
t.Errorf("Y-rotation 90deg: col0 should be ~(0,0,-1), got (%v,%v,%v)", m[0], m[1], m[2])
}
if !approxEqual(m[8], 1, epsilon) || !approxEqual(m[10], 0, epsilon) {
t.Errorf("Y-rotation 90deg: col2 should be ~(1,0,0), got (%v,%v,%v)", m[8], m[9], m[10])
}
}
func TestNodeLocalMatrixTRS(t *testing.T) {
// Verify that LocalMatrix = T * R * S
n := NewNode()
n.SetPosition(Vec3{1, 2, 3})
n.SetRotation(Euler{0.3, 0.5, 0.7})
n.SetScale(Vec3{2, 3, 4})
got := n.LocalMatrix()
tr := Mat4Translate(Vec3{1, 2, 3})
rot := Mat4FromQuat(QuatFromEuler(Euler{0.3, 0.5, 0.7}))
sc := Mat4Scale(Vec3{2, 3, 4})
want := tr.Mul(rot).Mul(sc)
if !approxEqualMat4(got, want, epsilon) {
t.Errorf("LocalMatrix should equal T*R*S\n got: %v\n want: %v", got, want)
}
}
func TestNodeLocalMatrixCachedUntilDirty(t *testing.T) {
n := NewNode()
m1 := n.LocalMatrix()
m2 := n.LocalMatrix()
if m1 != m2 {
t.Error("LocalMatrix should return the same cached value")
}
if n.localDirty {
t.Error("localDirty should be false after reading LocalMatrix")
}
}
// --- WorldMatrix correctness ---
func TestNodeWorldMatrixNoParent(t *testing.T) {
n := NewNode()
n.SetPosition(Vec3{1, 2, 3})
if n.WorldMatrix() != n.LocalMatrix() {
t.Error("WorldMatrix with no parent should equal LocalMatrix")
}
}
func TestNodeWorldMatrixParentChild(t *testing.T) {
parent := NewNode()
parent.SetPosition(Vec3{10, 0, 0})
child := NewNode()
child.SetPosition(Vec3{0, 5, 0})
parent.Add(child)
wm := child.WorldMatrix()
want := parent.LocalMatrix().Mul(child.LocalMatrix())
if !approxEqualMat4(wm, want, epsilon) {
t.Errorf("WorldMatrix should be parent.World * child.Local\n got: %v\n want: %v", wm, want)
}
}
func TestNodeWorldMatrixThreeLevels(t *testing.T) {
root := NewNode()
root.SetPosition(Vec3{1, 0, 0})
mid := NewNode()
mid.SetPosition(Vec3{0, 2, 0})
root.Add(mid)
leaf := NewNode()
leaf.SetPosition(Vec3{0, 0, 3})
mid.Add(leaf)
// Expected: root.Local * mid.Local * leaf.Local
want := root.LocalMatrix().Mul(mid.LocalMatrix()).Mul(leaf.LocalMatrix())
got := leaf.WorldMatrix()
if !approxEqualMat4(got, want, epsilon) {
t.Errorf("3-level WorldMatrix mismatch\n got: %v\n want: %v", got, want)
}
}
func TestNodeWorldMatrixTranslationAccumulates(t *testing.T) {
root := NewNode()
root.SetPosition(Vec3{1, 0, 0})
child := NewNode()
child.SetPosition(Vec3{0, 2, 0})
root.Add(child)
grandchild := NewNode()
grandchild.SetPosition(Vec3{0, 0, 3})
child.Add(grandchild)
pos := grandchild.WorldPosition()
want := Vec3{1, 2, 3}
if !approxEqualVec3(pos, want, epsilon) {
t.Errorf("WorldPosition should accumulate: got %v, want %v", pos, want)
}
}
func TestNodeWorldMatrixWithScaleAndRotation(t *testing.T) {
parent := NewNode()
parent.SetScale(Vec3{2, 2, 2})
parent.SetRotation(Euler{0, math.Pi / 2, 0})
child := NewNode()
child.SetPosition(Vec3{1, 0, 0})
parent.Add(child)
wp := child.WorldPosition()
// Parent: scale(2) + rotateY(90deg). Child at (1,0,0).
// Scale: (1,0,0) -> (2,0,0). RotateY(90): (2,0,0) -> (0,0,-2).
want := Vec3{0, 0, -2}
if !approxEqualVec3(wp, want, epsilon) {
t.Errorf("WorldPosition with scale+rotation: got %v, want %v", wp, want)
}
}
func TestNodeWorldMatrixRecomputesAfterParentChange(t *testing.T) {
parent := NewNode()
parent.SetPosition(Vec3{10, 0, 0})
child := NewNode()
child.SetPosition(Vec3{0, 5, 0})
parent.Add(child)
pos1 := child.WorldPosition()
if !approxEqualVec3(pos1, Vec3{10, 5, 0}, epsilon) {
t.Fatalf("initial WorldPosition: got %v", pos1)
}
// Move parent — child's world should update.
parent.SetPosition(Vec3{20, 0, 0})
pos2 := child.WorldPosition()
if !approxEqualVec3(pos2, Vec3{20, 5, 0}, epsilon) {
t.Errorf("after parent move: got %v, want {20,5,0}", pos2)
}
}
// --- WorldPosition ---
func TestNodeWorldPosition(t *testing.T) {
n := NewNode()
n.SetPosition(Vec3{7, 8, 9})
wp := n.WorldPosition()
if !approxEqualVec3(wp, Vec3{7, 8, 9}, epsilon) {
t.Errorf("WorldPosition for root node: got %v", wp)
}
}
// --- Add/Remove edge cases ---
func TestNodeAddMultipleChildren(t *testing.T) {
parent := NewNode()
c1 := NewNode()
c2 := NewNode()
c3 := NewNode()
parent.Add(c1)
parent.Add(c2)
parent.Add(c3)
if parent.ChildCount() != 3 {
t.Errorf("expected 3 children, got %d", parent.ChildCount())
}
}
func TestNodeRemoveMiddleChild(t *testing.T) {
parent := NewNode()
c1 := NewNode()
c1.SetName("c1")
c2 := NewNode()
c2.SetName("c2")
c3 := NewNode()
c3.SetName("c3")
parent.Add(c1)
parent.Add(c2)
parent.Add(c3)
parent.Remove(c2)
if parent.ChildCount() != 2 {
t.Errorf("expected 2 children after remove, got %d", parent.ChildCount())
}
children := parent.Children()
if children[0].Name() != "c1" || children[1].Name() != "c3" {
t.Errorf("order should be [c1,c3], got [%s,%s]", children[0].Name(), children[1].Name())
}
}
func TestNodeAddSameChildTwice(t *testing.T) {
parent := NewNode()
child := NewNode()
parent.Add(child)
parent.Add(child) // Should not duplicate.
if parent.ChildCount() != 1 {
t.Errorf("expected 1 child (no duplicate), got %d", parent.ChildCount())
}
}
// --- LookAt ---
func TestNodeLookAtPositiveZ(t *testing.T) {
n := NewNode()
n.SetPosition(Vec3{0, 0, 0})
n.LookAt(Vec3{0, 0, -10})
// Looking at -Z from origin should produce roughly identity rotation
// (camera convention: -Z is forward).
wp := n.WorldPosition()
if !approxEqualVec3(wp, Vec3{0, 0, 0}, epsilon) {
t.Errorf("position should be unchanged, got %v", wp)
}
}
// --- Deep hierarchy benchmark ---
func TestNodeDeepHierarchyWorldMatrix(t *testing.T) {
// Build a 10-level chain, each with translation (1,0,0).
// Leaf WorldPosition should be (10,0,0).
const depth = 10
nodes := make([]*Node, depth)
for i := range depth {
nodes[i] = NewNode()
nodes[i].SetPosition(Vec3{1, 0, 0})
if i > 0 {
nodes[i-1].Add(nodes[i])
}
}
wp := nodes[depth-1].WorldPosition()
want := Vec3{float32(depth), 0, 0}
if !approxEqualVec3(wp, want, epsilon) {
t.Errorf("10-level chain WorldPosition: got %v, want %v", wp, want)
}
}
// --- Benchmarks ---
func BenchmarkNodeLocalMatrix(b *testing.B) {
n := NewNode()
n.SetPosition(Vec3{1, 2, 3})
n.SetRotation(Euler{0.3, 0.5, 0.7})
n.SetScale(Vec3{2, 3, 4})
for range b.N {
n.localDirty = true
_ = n.LocalMatrix()
}
}
func BenchmarkNodeWorldMatrix3Levels(b *testing.B) {
root := NewNode()
root.SetPosition(Vec3{1, 0, 0})
mid := NewNode()
mid.SetPosition(Vec3{0, 2, 0})
root.Add(mid)
leaf := NewNode()
leaf.SetPosition(Vec3{0, 0, 3})
mid.Add(leaf)
for range b.N {
root.markLocalDirty()
_ = leaf.WorldMatrix()
}
}
func BenchmarkNodeAddRemove(b *testing.B) {
parent := NewNode()
child := NewNode()
for range b.N {
parent.Add(child)
parent.Remove(child)
}
}