|
| 1 | +package extrude_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/EliCDavis/polyform/modeling" |
| 7 | + "github.com/EliCDavis/polyform/modeling/extrude" |
| 8 | + "github.com/EliCDavis/vector/vector2" |
| 9 | + "github.com/EliCDavis/vector/vector3" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// A vertex normal is averaged across the faces that share it, so it drifts |
| 15 | +// away from any one of them. Reaching a right angle means it stopped being a |
| 16 | +// normal: ProjectFace used to hand back the ring's edge tangent, and Line |
| 17 | +// negated both flanks when only one needed it. |
| 18 | +func requireNormalsAgreeWithFaces(t *testing.T, m modeling.Mesh) { |
| 19 | + t.Helper() |
| 20 | + |
| 21 | + require.True(t, m.HasFloat3Attribute(modeling.NormalAttribute), "no normal attribute") |
| 22 | + idx := m.Indices() |
| 23 | + pos := m.Float3Attribute(modeling.PositionAttribute) |
| 24 | + nrm := m.Float3Attribute(modeling.NormalAttribute) |
| 25 | + require.Equal(t, pos.Len(), nrm.Len()) |
| 26 | + |
| 27 | + for i := 0; i < idx.Len(); i += 3 { |
| 28 | + a, b, c := pos.At(idx.At(i)), pos.At(idx.At(i+1)), pos.At(idx.At(i+2)) |
| 29 | + face := b.Sub(a).Cross(c.Sub(a)) |
| 30 | + if face.Length() == 0 { |
| 31 | + continue |
| 32 | + } |
| 33 | + face = face.Normalized() |
| 34 | + |
| 35 | + for k := 0; k < 3; k++ { |
| 36 | + vertex := idx.At(i + k) |
| 37 | + normal := nrm.At(vertex) |
| 38 | + require.NotZerof(t, normal.Length(), "vertex %d has a zero normal", vertex) |
| 39 | + assert.Positivef(t, face.Dot(normal.Normalized()), |
| 40 | + "vertex %d carries a normal at or past a right angle to its face", vertex) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestExtrudersEmitUsableNormals(t *testing.T) { |
| 46 | + square := []vector2.Float64{ |
| 47 | + vector2.New(-1., -1.), vector2.New(1., -1.), |
| 48 | + vector2.New(1., 1.), vector2.New(-1., 1.), |
| 49 | + } |
| 50 | + bent := []vector3.Float64{ |
| 51 | + vector3.New(0., 0., 0.), vector3.New(0., 2., 0.), vector3.New(1.5, 3.5, 0.), |
| 52 | + } |
| 53 | + |
| 54 | + outline, err := extrude.Outline{Shape: square, Path: bent}.Extrude() |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + for name, mesh := range map[string]modeling.Mesh{ |
| 58 | + "shape": extrude.Shape(square, bent), |
| 59 | + "closed shape": extrude.ClosedShape(square, []vector3.Float64{ |
| 60 | + vector3.New(2., 0., 0.), vector3.New(-1., 0., 1.7), vector3.New(-1., 0., -1.7), |
| 61 | + }), |
| 62 | + "polygon": extrude.Polygon(8, []extrude.ExtrusionPoint{ |
| 63 | + {Point: vector3.New(0., 0., 0.), Thickness: 1}, |
| 64 | + {Point: vector3.New(0., 2., 0.), Thickness: 1}, |
| 65 | + {Point: vector3.New(1.5, 3.5, 0.), Thickness: .7}, |
| 66 | + }), |
| 67 | + "circle": extrude.Circle{Radius: 1, Resolution: 12, Path: bent}.Extrude(), |
| 68 | + "line": extrude.Line([]extrude.LinePoint{ |
| 69 | + {Point: vector3.New(0., 0., 0.), Up: vector3.Up[float64](), Width: 1, Height: .2}, |
| 70 | + {Point: vector3.New(0., 0., 2.), Up: vector3.Up[float64](), Width: 1, Height: .2}, |
| 71 | + {Point: vector3.New(1., 0., 4.), Up: vector3.Up[float64](), Width: 1, Height: .2}, |
| 72 | + }), |
| 73 | + "screw": lathedTube(t), |
| 74 | + "outline": outline, |
| 75 | + } { |
| 76 | + t.Run(name, func(t *testing.T) { requireNormalsAgreeWithFaces(t, mesh) }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// The outline decides which way is out, so the same shape wound either way |
| 81 | +// has to produce normals pointing the same direction. |
| 82 | +func TestProjectFaceFacesOutwardEitherWinding(t *testing.T) { |
| 83 | + square := []vector2.Float64{ |
| 84 | + vector2.New(-1., -1.), vector2.New(1., -1.), |
| 85 | + vector2.New(1., 1.), vector2.New(-1., 1.), |
| 86 | + } |
| 87 | + backwards := make([]vector2.Float64, len(square)) |
| 88 | + for i, p := range square { |
| 89 | + backwards[len(square)-1-i] = p |
| 90 | + } |
| 91 | + |
| 92 | + center := vector3.New(0., 0., 0.) |
| 93 | + up := vector3.Up[float64]() |
| 94 | + perpendicular := vector3.Forward[float64]() |
| 95 | + |
| 96 | + points, normals := extrude.ProjectFace(center, up, perpendicular, square) |
| 97 | + flippedPoints, flippedNormals := extrude.ProjectFace(center, up, perpendicular, backwards) |
| 98 | + |
| 99 | + for i, p := range points { |
| 100 | + assert.Positivef(t, normals[i].Dot(p.Sub(center).Normalized()), "vertex %d points inward", i) |
| 101 | + } |
| 102 | + for i, p := range flippedPoints { |
| 103 | + assert.Positivef(t, flippedNormals[i].Dot(p.Sub(center).Normalized()), |
| 104 | + "reversed winding, vertex %d points inward", i) |
| 105 | + } |
| 106 | +} |
0 commit comments