evaluate_cylinder Function

private pure elemental function evaluate_cylinder(this, pos) result(res)

Evaluation function for Cylinder SDF.

Type Bound

cylinder

Arguments

Type IntentOptional Attributes Name
class(cylinder), intent(in) :: this
type(vector), intent(in) :: pos

vector position to evaluate SDF at

Return Value real(kind=wp)


Source Code

    pure elemental function evaluate_cylinder(this, pos) result(res)
        !! Evaluation function for Cylinder SDF.

        class(cylinder), intent(in) :: this
        !> vector position to evaluate SDF at
        type(vector),    intent(in) :: pos
        real(kind=wp) :: res

        type(vector)  :: p, ba, pa
        real(kind=wp) :: x, y, x2, y2, d, baba, paba

        p = pos .dot. this%transform

        ba = this%b - this%a
        pa = p - this%a
        baba = ba .dot. ba
        paba = pa .dot. ba
        x = length(pa * baba - ba*paba) - this%radius*baba
        y = abs(paba - baba*.5_wp) - baba*.5_wp
        x2 = x**2
        y2 = (y**2)*baba
        if(max(x, y) < 0._wp)then
            d = -min(x2, y2)
        else
            if(x > 0._wp .and. y > 0._wp)then
                d = x2 + y2
            elseif(x > 0._wp)then
                d = x2
            elseif(y > 0._wp)then
                d = y2
            else
                d = 0._wp
            end if
        end if

        res = sign(sqrt(abs(d))/baba, d)

    end function evaluate_cylinder