https://mooseframework.inl.gov
Loading...
Searching...
No Matches
BoundingBoxIntersectionHelper.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11
12// libMesh includes
13#include "libmesh/bounding_box.h"
14#include "libmesh/face_quad4.h"
15#include "libmesh/cell_hex8.h"
16
17#include "TraceRayTools.h"
18#include "DebugRay.h"
19
21 const unsigned int dim)
22 : _comm(), _mesh(std::make_unique<Mesh>(_comm, dim))
23{
24 // Add the nodes that represent our bounding box
25 _mesh->add_point(Point(bbox.min()(0), bbox.min()(1), bbox.min()(2)), 0, 0);
26 _mesh->add_point(Point(bbox.max()(0), bbox.min()(1), bbox.min()(2)), 1, 0);
27 if (dim > 1)
28 {
29 _mesh->add_point(Point(bbox.max()(0), bbox.max()(1), bbox.min()(2)), 2, 0);
30 _mesh->add_point(Point(bbox.min()(0), bbox.max()(1), bbox.min()(2)), 3, 0);
31
32 if (dim == 3)
33 {
34 _mesh->add_point(Point(bbox.min()(0), bbox.min()(1), bbox.max()(2)), 4, 0);
35 _mesh->add_point(Point(bbox.max()(0), bbox.min()(1), bbox.max()(2)), 5, 0);
36 _mesh->add_point(Point(bbox.max()(0), bbox.max()(1), bbox.max()(2)), 6, 0);
37 _mesh->add_point(Point(bbox.min()(0), bbox.max()(1), bbox.max()(2)), 7, 0);
38 }
39 }
40
41 // Build the element and set the nodes accordingly
42 Elem * elem;
43 if (dim == 1)
44 elem = Elem::build(EDGE2).release();
45 else if (dim == 2)
46 elem = Elem::build(QUAD4).release();
47 else
48 elem = Elem::build(HEX8).release();
49 elem = _mesh->add_elem(elem);
50 elem->subdomain_id() = 0;
51 elem->processor_id(0);
52 elem->set_id() = 0;
53 for (unsigned int n = 0; n < _mesh->n_nodes(); ++n)
54 elem->set_node(n, _mesh->node_ptr(n));
55
56 // All done with the mesh
57 _mesh->skip_partitioning(true);
58 _mesh->prepare_for_use();
59
60 // This costs a little bit so why not cache it now
61 _hmax = elem->hmax();
62}
63
64Point
65BoundingBoxIntersectionHelper::intersection(const Point & point, const Point & direction) const
66{
67 mooseAssert(std::abs(1.0 - direction.norm()) < TOLERANCE, "Unnormalized direction");
68
69 const Elem * elem = _mesh->elem_ptr(0);
70
71 // We're going to check all possible intersections and keep the one that's furthest away
72 Point best_intersection_point = RayTracingCommon::invalid_point;
73 Real best_intersection_distance = -std::numeric_limits<Real>::max();
74
75 // For use in the intersection routines
76 Point intersection_point;
77 ElemExtrema intersected_extrema;
78 Real intersection_distance;
79 bool intersected;
80
81 // Check intersection with sides
82 for (const auto s : elem->side_index_range())
83 {
84 intersection_point = RayTracingCommon::invalid_point;
85 intersected_extrema.invalidate();
86
87 if (elem->dim() == 3) // 3D: Intersect HEX8
88 intersected = TraceRayTools::sideIntersectedByLine<Hex8>(elem,
89 point,
90 direction,
91 s,
92 _hmax,
93 intersection_point,
94 intersection_distance,
95 intersected_extrema,
96 _hmax
97#ifdef DEBUG_RAY_INTERSECTIONS
98 ,
99 false
100#endif
101 );
102 else if (elem->dim() == 2) // 2D: Intersect QUAD4
103 {
104 intersected = TraceRayTools::sideIntersectedByLine<Quad4>(elem,
105 point,
106 direction,
107 s,
108 _hmax,
109 intersection_point,
110 intersection_distance,
111 intersected_extrema,
112 _hmax
113#ifdef DEBUG_RAY_INTERSECTIONS
114 ,
115 false
116#endif
117 );
118 }
119 else // 1D: see if side point is between point and end far away
120 {
121 // Dummy end point outside of the element
122 const Point dummy_end = point + 1.01 * direction * _hmax;
123
124 intersected = TraceRayTools::isWithinSegment(point, dummy_end, elem->point(s));
125 if (intersected)
126 {
127 intersection_distance = (elem->point(s) - point).norm();
128 intersection_point = elem->point(s);
129 }
130 }
131
132 if (intersected && intersection_distance > best_intersection_distance)
133 {
134 best_intersection_distance = intersection_distance;
135 best_intersection_point = intersection_point;
136 }
137 }
138
139 return best_intersection_point;
140}
unsigned int dim
libMesh::Point intersection(const libMesh::Point &point, const libMesh::Point &direction) const
libMesh::Real _hmax
hmax for the dummy element
const std::unique_ptr< libMesh::Mesh > _mesh
Dummy mesh that contains a single element used for TraceRayTools intersection methods.
BoundingBoxIntersectionHelper(const libMesh::BoundingBox &bbox, const unsigned int dim)
Constructor.
static const libMesh::Point invalid_point(invalid_distance, invalid_distance, invalid_distance)
Identifier for an invalid point.
bool isWithinSegment(const Point &segment1, const Point &segment2, const Point &point, const Real tolerance=TRACE_TOLERANCE)
Checks whether or not a point is within a line segment.
Helper for defining if at an element's edge, vertex, or neither.
Definition ElemExtrema.h:26
void invalidate()
Invalidates the current state.
Definition ElemExtrema.h:87