From 3457762d71508e1e766783a495b552cb70f32506 Mon Sep 17 00:00:00 2001 From: SimonDanisch Date: Mon, 15 Jun 2020 23:46:55 +0200 Subject: [PATCH] fix texturecoordinates fallback --- src/interfaces.jl | 19 +++++++++++++++---- test/geometrytypes.jl | 7 +++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/interfaces.jl b/src/interfaces.jl index b6670b7b..427c61f1 100644 --- a/src/interfaces.jl +++ b/src/interfaces.jl @@ -33,6 +33,8 @@ function faces(primitive, nvertices=nothing) return nothing end +texturecoordinates(primitive, nvertices=nothing) = nothing + """ Tesselation(primitive, nvertices) For abstract geometries, when we generate @@ -123,16 +125,25 @@ function decompose(NT::Normal{T}, primitive) where T end function decompose(UVT::Union{UV{T}, UVW{T}}, primitive) where T + # This is the fallback for texture coordinates if a primitive doesn't overload them + # We just take the positions and normalize them uv = texturecoordinates(primitive) if uv === nothing - return decompose(UVT, texturecoordinates(coordinates(primitive))) + # If the primitive doesn't even have coordinates, we're out of options and return + # nothing, indicating that texturecoordinates aren't implemented + positions = decompose(Point, primitive) + positions === nothing && return nothing + # Let this overlord do the work + return decompose(UVT, positions) end return collect_with_eltype(T, uv) end -function texturecoordinates(positions::AbstractVector{<:VecTypes}) - bb = Rect(positions) - return map(positions) do p +function decompose(UVT::Union{UV{T}, UVW{T}}, positions::AbstractVector{<:VecTypes}) where T + N = length(T) + positions_nd = decompose(Point{N, eltype(T)}, positions) + bb = Rect(positions_nd) # Make sure we get this as points + return map(positions_nd) do p return (p .- minimum(bb)) ./ widths(bb) end end diff --git a/test/geometrytypes.jl b/test/geometrytypes.jl index 03669359..c79dd984 100644 --- a/test/geometrytypes.jl +++ b/test/geometrytypes.jl @@ -102,6 +102,9 @@ end @test GeometryBasics.coordinates(m) ≈ positions m = normal_mesh(s)# just test that it works without explicit resolution parameter @test m isa GLNormalMesh + + muv = uv_mesh(s) + @test Rect(Point.(texturecoordinates(muv))) == FRect2D(Vec2f0(0), Vec2f0(1.0)) end end @@ -240,7 +243,7 @@ end @test !in(rect1, split1) prim = Rect(0.0, 0.0, 1.0, 1.0) - @test length(prim) == 2 + @test length(prim) == 2 @test width(prim) == 1.0 @test height(prim) == 1.0 @@ -267,7 +270,7 @@ end @test update(b, v) isa GeometryBasics.HyperRectangle{2,Float64} v = Vec(1.0, 2.0) @test update(b, v) isa GeometryBasics.HyperRectangle{2,Float64} - + p = Vec(5.0, 4.0) rect = Rect(0.0, 0.0, 1.0, 1.0) @test min_dist_dim(rect, p, 1) == 4.0