view src/Grids/geometry.jl @ 1966:478b233999c5 feature/grids/geometry_functions

Add tests and better constructors for Line
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 12 Feb 2025 15:33:48 +0100
parents 6859089b361e
children 669361a8195a
line wrap: on
line source

struct Line{PT}
    p::PT
    tangent::PT

    Line{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent)
end

function Line(p, t)
    S = length(p)
    T = promote_type(eltype(p), eltype(t))

    PT = SVector{S,T}
    return Line{PT}(
        convert(PT, p),
        convert(PT, t),
    )
end

function Line(p::Tuple, t::Tuple)
    p = promote(p...)
    t = promote(t...)

    return Line(SVector(p), SVector(t))
end

(c::Line)(s) = c.p + s*c.tangent


struct LineSegment{PT}
    a::PT
    b::PT
end

(c::LineSegment)(s) = (1-s)*c.a + s*c.b


function linesegments(ps...)
    return [LineSegment(ps[i], ps[i+1]) for i ∈ 1:length(ps)-1]
end


function polygon_edges(ps...)
    n = length(ps)
    return [LineSegment(ps[i], ps[mod1(i+1,n)]) for i ∈ eachindex(ps)]
end

struct Circle{T,PT}
    c::PT
    r::T
end

function (C::Circle)(θ)
    (;c, r) = C
    c + r*@SVector[cos(θ), sin(θ)]
end

struct TransfiniteInterpolationSurface{T1,T2,T3,T4}
    c₁::T1
    c₂::T2
    c₃::T3
    c₄::T4
end

function (s::TransfiniteInterpolationSurface)(u,v)
    c₁, c₂, c₃, c₄ = s.c₁, s.c₂, s.c₃, s.c₄
    P₀₀ = c₁(0)
    P₁₀ = c₂(0)
    P₁₁ = c₃(0)
    P₀₁ = c₄(0)
    return (1-v)*c₁(u) + u*c₂(v) + v*c₃(1-u) + (1-u)*c₄(1-v) - (
        (1-u)*(1-v)*P₀₀ + u*(1-v)*P₁₀ + u*v*P₁₁ + (1-u)*v*P₀₁
    )
end

function (s::TransfiniteInterpolationSurface)(ξ̄::AbstractArray)
    s(ξ̄...)
end

# TODO: Implement jacobian() for the different mapping helpers