intersectCylinder Function

public function intersectCylinder(orig, dir, t, centre, radius)

calculates where a line, with origin:orig and direction:dir hits a cylinder, centre:centre and radius:radius This solves for an infinitely long cylinder centered on the z axis with radius radius returns true if intersection exists returns t, the paramertised parameter of the line equation adapted from scratchapixel need to check z height after moving ray if not this is an infinite cylinder

Arguments

Type IntentOptional Attributes Name
type(vector), intent(in) :: orig

origin of the ray

type(vector), intent(in) :: dir

Direction vector of the ray

real(kind=wp), intent(out) :: t

distance from orig to the intersection point

type(vector), intent(in) :: centre

Centre of the cylinder

real(kind=wp), intent(in) :: radius

radius of the cylinder

Return Value logical


Source Code

    logical function intersectCylinder(orig, dir, t, centre, radius)
    !! calculates where a line, with origin:orig and direction:dir hits a cylinder, centre:centre and radius:radius
    !! This solves for an infinitely long cylinder centered on the z axis with radius radius
    !! returns true if intersection exists
    !! returns t, the paramertised parameter of the line equation
    !! adapted from scratchapixel
    !! need to check z height after moving ray
    !! if not this is an infinite cylinder
        !> Direction vector of the ray
        type(vector),  intent(IN)  :: dir
        !> origin of the ray
        type(vector),  intent(IN)  :: orig
        !> Centre of the cylinder
        type(vector),  intent(IN)  :: centre
        !> distance from orig to the intersection point
        real(kind=wp), intent(OUT) :: t
        !> radius of the cylinder
        real(kind=wp), intent(IN)  :: radius

        type(vector)  :: L
        real(kind=wp) :: t0, t1, a, b, c, tmp

        intersectCylinder = .false.

        L = orig - centre
        a = dir%x**2 + dir%y**2
        b = 2._wp * (dir%x * L%x + dir%y * L%y)
        c = L%x**2 + L%y**2 - radius**2

        if(.not. solveQuadratic(a, b, c, t0, t1))return

        if(t0 > t1)then
            tmp = t1
            t1 = t0
            t0 = tmp
        end if
        if(t0 < 0._wp)then
            t0 = t1
            if(t0 < 0._wp)return
        end if

        t = t0
        intersectCylinder = .true.
        return
    end function intersectCylinder