do_ray_intersect Function

private pure function do_ray_intersect(self, ray_origin, ray_direction) result(do_intersect)

Return true if AABB is intersected by ray from origin and oriented as ray direction vector.

Arguments

Type IntentOptional AttributesName
class(aabb_object), intent(in) :: self

AABB box.

type(vector_R8P), intent(in) :: ray_origin

Ray origin.

type(vector_R8P), intent(in) :: ray_direction

Ray direction.

Return Value logical

Test result.


Source Code


Source Code

   pure function do_ray_intersect(self, ray_origin, ray_direction) result(do_intersect)
   !< Return true if AABB is intersected by ray from origin and oriented as ray direction vector.
   class(aabb_object), intent(in) :: self          !< AABB box.
   type(vector_R8P),   intent(in) :: ray_origin    !< Ray origin.
   type(vector_R8P),   intent(in) :: ray_direction !< Ray direction.
   logical                        :: do_intersect  !< Test result.
   logical                        :: must_return   !< Flag to check when to return from procedure.
   real(R8P)                      :: tmin, tmax    !< Minimum maximum ray intersections with box slabs.

   do_intersect = .false.
   must_return = .false.
   tmin = 0._R8P
   tmax = MaxR8P
   call check_slab(aabb_min=self%bmin%x, aabb_max=self%bmax%x, &
                   o=ray_origin%x, d=ray_direction%x, must_return=must_return, tmin=tmin, tmax=tmax)
   if (must_return) return
   call check_slab(aabb_min=self%bmin%y, aabb_max=self%bmax%y, &
                   o=ray_origin%y, d=ray_direction%y, must_return=must_return, tmin=tmin, tmax=tmax)
   if (must_return) return
   call check_slab(aabb_min=self%bmin%z, aabb_max=self%bmax%z, &
                   o=ray_origin%z, d=ray_direction%z, must_return=must_return, tmin=tmin, tmax=tmax)
   if (must_return) return
   ! ray intersects all 3 slabs
   do_intersect = .true.
   contains
      pure subroutine check_slab(aabb_min, aabb_max, o, d, must_return, tmin, tmax)
      !< Perform ray intersection check in a direction-split fashion over slabs.
      real(R8P), intent(in)    :: aabb_min     !< Box minimum bound in the current direction.
      real(R8P), intent(in)    :: aabb_max     !< Box maximum bound in the current direction.
      real(R8P), intent(in)    :: o            !< Ray origin in the current direction.
      real(R8P), intent(in)    :: d            !< Ray slope in the current direction.
      logical,   intent(inout) :: must_return  !< Flag to check when to return from procedure.
      real(R8P), intent(inout) :: tmin, tmax   !< Minimum maximum ray intersections with box slabs.
      real(R8P)                :: ood, t1, t2  !< Intersection coefficients.
      real(R8P)                :: tmp          !< Temporary buffer.

      if ((d) < EPS) then
         ! ray is parallel to slab, no hit if origin not within slab
         if ((o < aabb_min).or.(o > aabb_max)) then
            must_return = .true.
            return
         endif
      else
         ! compute intersection t value of ray with near and far plane of slab
         ood = 1._R8P / d
         t1 = (aabb_min - o) * ood
         t2 = (aabb_max - o) * ood
         ! make t1 be intersection with near plane, t2 with far plane
         if (t1 > t2) then
            tmp = t1
            t1 = t2
            t2 = tmp
         endif
         ! compute the intersection of slab intersection intervals
         if (t1 > tmin) tmin = t1
         if (t2 > tmax) tmax = t2
         ! exit with no collision as soon as slab intersection becomes empty
         if (tmin > tmax) then
            must_return = .true.
            return
         endif
      endif
      endsubroutine check_slab
   endfunction do_ray_intersect


aabb_assign_aabb aabb_node_assign_aabb_node aabb_tree_assign_aabb_tree add_facets add_facets add_vertex_occurrence allocate_facets analize bmax bmin build_connectivity check_normal check_vertices_occurrencies cli_parse cli_parse cli_parse cli_parse cli_parse cli_parse close_file closest_point closest_point compute_bb_from_facets compute_metrix compute_metrix compute_normal compute_normals compute_octants compute_octants compute_volume create_aabb_tree destroy destroy destroy destroy destroy distance distance distance distance distance distance_from_facets distance_from_facets do_ray_intersect do_ray_intersect do_ray_intersect edge_connection_in_other_ref facet_assign_facet file_stl_assign_file_stl first_child_node first_node flip_edge has_facets has_facets initialize initialize initialize initialize initialize is_allocated is_inside is_point_inside_polyhedron_ri is_point_inside_polyhedron_sa load_facets_number_from_file load_from_file load_from_file_ascii load_from_file_binary load_header_from_file make_normal_consistent mirror_by_matrix mirror_by_matrix mirror_by_normal mirror_by_normal nodes_number nodes_number_at_level open_file parent_node ray_intersections_number ray_intersections_number ray_intersections_number resize resize reverse_normal reverse_normals rotate_by_axis_angle rotate_by_axis_angle rotate_by_matrix rotate_by_matrix sanitize_normals save_facets_into_file_stl save_facets_into_file_stl save_geometry_tecplot_ascii save_geometry_tecplot_ascii save_geometry_tecplot_ascii save_header_into_file save_into_file save_into_file_ascii save_into_file_binary save_into_file_stl save_trailer_into_file solid_angle statistics tetrahedron_volume translate translate update_connectivity update_extents update_extents vertex vertex_global_id