aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/surface_sphere_test.go (5629B)


      1 package main
      2 
      3 import (
      4 	"ao/server/assets"
      5 	"math"
      6 	"sync"
      7 	"testing"
      8 )
      9 
     10 func TestSurfaceSphereUsesFrontFaceEdgesAndVertices(t *testing.T) {
     11 	tree := &surfaceTree{
     12 		vertices: [][3]float32{{0, 0, 0}, {0, 0, 2}, {0, 2, 0}},
     13 		groups:   []surfaceGroup{{bounds: [6]float32{0, 0, 0, 0, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}}},
     14 		nodes:    []surfaceNode{{groups: []uint32{0}}},
     15 	}
     16 
     17 	for _, scenario := range []struct {
     18 		name     string
     19 		center   [3]float32
     20 		radius   float32
     21 		hit      bool
     22 		point    [3]float32
     23 		distance float32
     24 	}{
     25 		{"face", [3]float32{-.25, .5, .5}, .5, true, [3]float32{0, .5, .5}, .25},
     26 		{"edge", [3]float32{-.25, -.25, .5}, .5, true, [3]float32{0, 0, .5}, .35355338},
     27 		{"vertex", [3]float32{-.25, -.25, -.25}, .5, true, [3]float32{}, .4330127},
     28 		{"tangent", [3]float32{-.5, .5, .5}, .5, true, [3]float32{0, .5, .5}, .5},
     29 		{"on plane", [3]float32{0, .5, .5}, .5, true, [3]float32{0, .5, .5}, 0},
     30 		{"back face", [3]float32{.25, .5, .5}, .5, false, [3]float32{}, 0},
     31 		{"beyond radius", [3]float32{-.5001, .5, .5}, .5, false, [3]float32{}, 0},
     32 		{"outside edge", [3]float32{-.25, -.5, .5}, .5, false, [3]float32{}, 0},
     33 	} {
     34 		t.Run(scenario.name, func(t *testing.T) {
     35 			contact := tree.sphere(scenario.center, scenario.radius)
     36 			if contact.hit != scenario.hit || contact.point != scenario.point || math.Abs(float64(contact.distance-scenario.distance)) > .000001 {
     37 				t.Fatal("native sphere contact", contact, scenario)
     38 			}
     39 			if contact.hit && contact.normal != [3]float32{-1, 0, 0} {
     40 				t.Fatal("edge/vertex contact must return the triangle face normal", contact.normal)
     41 			}
     42 		})
     43 	}
     44 }
     45 
     46 func TestSurfaceSphereSharesLazyCoefficientsAcrossQueries(t *testing.T) {
     47 	tree := &surfaceTree{
     48 		vertices: [][3]float32{{0, 0, 0}, {0, 0, 2}, {0, 2, 0}},
     49 		groups:   []surfaceGroup{{bounds: [6]float32{0, 0, 0, 0, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}}},
     50 		nodes:    []surfaceNode{{groups: []uint32{0}}},
     51 	}
     52 	var workers sync.WaitGroup
     53 	contacts := make(chan surfaceContact, 16)
     54 
     55 	for range 16 {
     56 		workers.Add(1)
     57 		go func() {
     58 			defer workers.Done()
     59 			contacts <- tree.sphere([3]float32{-.25, .5, .5}, .5)
     60 		}()
     61 	}
     62 	workers.Wait()
     63 	close(contacts)
     64 
     65 	for contact := range contacts {
     66 		if !contact.hit || contact.distance != .25 || contact.point != [3]float32{0, .5, .5} || contact.normal != [3]float32{-1, 0, 0} {
     67 			t.Fatal("concurrent immutable surface query", contact)
     68 		}
     69 	}
     70 }
     71 
     72 func TestSurfaceSpherePreservesGroupAndKDTraversalTies(t *testing.T) {
     73 	vertices := [][3]float32{{0, -2, -2}, {0, -2, 2}, {0, 2, -2}, {-2, -2, 0}, {-2, 2, 0}, {2, -2, 0}}
     74 	groups := []surfaceGroup{
     75 		{bounds: [6]float32{0, -2, -2, 0, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}},
     76 		{bounds: [6]float32{-2, -2, 0, 2, 2, 0}, triangles: [][3]uint32{{3, 4, 5}}},
     77 	}
     78 
     79 	for _, scenario := range []struct {
     80 		name   string
     81 		nodes  []surfaceNode
     82 		center [3]float32
     83 		normal [3]float32
     84 	}{
     85 		{"first group", []surfaceNode{{groups: []uint32{0, 1}}}, [3]float32{-.25, 0, -.25}, [3]float32{-1, 0, 0}},
     86 		{"reversed groups", []surfaceNode{{groups: []uint32{1, 0}}}, [3]float32{-.25, 0, -.25}, [3]float32{0, 0, -1}},
     87 		{"upper on split", []surfaceNode{{axis: 1, children: [2]int{1, 2}}, {groups: []uint32{0}}, {groups: []uint32{1}}}, [3]float32{-.25, 0, -.25}, [3]float32{0, 0, -1}},
     88 		{"lower below split", []surfaceNode{{axis: 1, children: [2]int{1, 2}}, {groups: []uint32{0}}, {groups: []uint32{1}}}, [3]float32{-.25, -.01, -.25}, [3]float32{-1, 0, 0}},
     89 	} {
     90 		t.Run(scenario.name, func(t *testing.T) {
     91 			tree := &surfaceTree{vertices: vertices, groups: append([]surfaceGroup(nil), groups...), nodes: scenario.nodes}
     92 			contact := tree.sphere(scenario.center, .5)
     93 			if !contact.hit || contact.distance != .25 || contact.normal != scenario.normal {
     94 				t.Fatal("authored traversal must decide equal-distance contacts", contact)
     95 			}
     96 		})
     97 	}
     98 }
     99 
    100 func TestSurfaceSpherePrefersFaceWithinSquaredDistanceTolerance(t *testing.T) {
    101 	tree := &surfaceTree{
    102 		vertices: [][3]float32{{0, -2, 0}, {0, 2, 0}, {0, -2, -2}, {.0001, -2, -2}, {.0001, -2, 2}, {.0001, 2, -2}},
    103 		groups:   []surfaceGroup{{bounds: [6]float32{0, -2, -2, .0001, 2, 2}, triangles: [][3]uint32{{0, 1, 2}, {3, 4, 5}}}},
    104 		nodes:    []surfaceNode{{groups: []uint32{0}}},
    105 	}
    106 
    107 	contact := tree.sphere([3]float32{-.25, -.5, .005}, .5)
    108 
    109 	if !contact.hit || math.Abs(float64(contact.point[0]-.0001)) > .0000001 || math.Abs(float64(contact.point[2]-.005)) > .0000001 || math.Abs(float64(contact.distance-.2501)) > .0000001 {
    110 		t.Fatal("near-tied face should replace the slightly closer edge within its group", contact)
    111 	}
    112 }
    113 
    114 func TestSurfaceSphereFindsShippedBuildingAtNativeStop(t *testing.T) {
    115 	database, err := readGameDatabase(assets.Files)
    116 	if err != nil {
    117 		t.Fatal(err)
    118 	}
    119 	t.Cleanup(func() { database.file.Close() })
    120 	payload, version, err := database.read(1000013, 46203264)
    121 	if err != nil {
    122 		t.Fatal(err)
    123 	}
    124 	surface, err := decodeSceneSurface(payload, version)
    125 	if err != nil {
    126 		t.Fatal(err)
    127 	}
    128 	radius := float32(.25075000524520874)
    129 
    130 	stopped := surface.primary.sphere([3]float32{971.701416, 16.40999985 + radius, 519.643555}, radius)
    131 	penetrating := surface.primary.sphere([3]float32{971.72, 16.40999985 + radius, 519.66}, radius)
    132 
    133 	if stopped.hit {
    134 		t.Fatal("native separated actor should lie outside the authored wall sphere", stopped)
    135 	}
    136 	if !penetrating.hit || penetrating.distance >= radius || penetrating.normal[1] != 0 {
    137 		t.Fatal("a small step beyond the measured stop must contact the shipped upright wall", penetrating)
    138 	}
    139 	t.Logf("penetrating wall contact: point=%v normal=%v distance=%v", penetrating.point, penetrating.normal, penetrating.distance)
    140 }