Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix texturecoordinates fallback #64

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions test/geometrytypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down