Function computes intersection of line with cube
If optional argument pntIntersect contains the intersection point of the line with cube
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(tem_line_type), | intent(in) | :: | line |
line segment to check for intersection |
||
type(tem_cube_type), | intent(in) | :: | cube |
cube to intersect with |
||
real(kind=rk), | intent(out), | optional | :: | pntIntersect(3) |
intersection point if there is intersection |
function tem_lineCubeOverlap( line, cube, pntIntersect ) result(overlap)
! ---------------------------------------------------------------------------!
!> line segment to check for intersection
type(tem_line_type), intent(in) :: line
!> cube to intersect with
type(tem_cube_type), intent(in) :: cube
!> intersection point if there is intersection
real(kind=rk), optional, intent(out) :: pntIntersect(3)
logical :: overlap
! ---------------------------------------------------------------------------!
real(kind=rk) :: proj
real(kind=rk) :: loc_pntIntersect(3)
! ---------------------------------------------------------------------------!
overlap = .false.
!check whether line is intersect the cube by rayCubeOverlap test
!then check whether intersected point is within the line segment
if(rayCubeOverlap( line, cube, loc_pntIntersect )) then
!project the intersected point on the line
!and return true only if intersected point is
!within line segment length
!The point is inside the line segment if the
!projected value is >= 0 and < 1.
proj = dot_product(loc_pntIntersect-line%origin, line%vec) &
& / dot_product(line%vec, line%vec)
overlap = (proj >= 0.0_rk) .and. (proj < 1.0_rk)
endif
if(present(pntIntersect)) pntIntersect = loc_pntIntersect
end function tem_lineCubeOverlap