intersectCone Function

public function intersectCone(orig, dir, t, centre, radius, height)

calculates where a line, with origin:orig and direction:dir hits a cone, radius:radius and height:height with centre:centre. centre is the point under the apex at the cone's base. returns true if intersection exists returns t, the paramertised parameter of the line equation adapted from scratchapixel and pbrt need to check z height after moving ray if not this is an infinte cone cone lies height ways along z-axis

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 cone

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

Radius of the cones base

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

Height of the cone

Return Value logical


Source Code

    logical function intersectCone(orig, dir, t, centre, radius, height)
    !! calculates where a line, with origin:orig and direction:dir hits a cone, radius:radius and height:height with centre:centre.
    !! centre is the point under the apex at the cone's base.
    !! returns true if intersection exists
    !! returns t, the paramertised parameter of the line equation
    !! adapted from scratchapixel and pbrt
    !! need to check z height after moving ray
    !! if not this is an infinte cone
    !! cone lies height ways along z-axis

        !> Direction vector of the ray
        type(vector),  intent(IN)  :: dir
        !> origin of the ray
        type(vector),  intent(IN)  :: orig
        !> Centre of the cone
        type(vector),  intent(IN)  :: centre
        !> distance from orig to the intersection point
        real(kind=wp), intent(OUT) :: t
        !> Radius of the cones base
        real(kind=wp), intent(IN)  :: radius
        !> Height of the cone
        real(kind=wp), intent(IN)  :: height

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

        intersectCone = .false.
        k = radius / height
        k = k**2

        L = orig - centre
        a = dir%x**2 + dir%y**2 - (k*dir%z**2)
        b = 2._wp*((dir%x * L%x) + (dir%y * L%y) - (k*dir%z * (L%z - height)))
        c = L%x**2 + L%y**2 - (k*(L%z - height)**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

        intersectCone = .true.
        return

    end function intersectCone