https://mooseframework.inl.gov
Loading...
Searching...
No Matches
AdaptiveRayContainmentCheck.h
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
10#pragma once
11
12#include "MooseMesh.h"
13#include "libmesh/point.h"
14#include "SurfaceElement.h"
15#include "SurfaceSide.h"
16#include "KDTree.h"
17#include "OrientedBoundingBox.h"
18#include "RayDirectionOptions.h"
19#include "MooseError.h"
20
21#include <array>
22#include <optional>
23
24class Ball;
25
37{
38public:
39 AdaptiveRayContainmentCheck(const std::vector<std::unique_ptr<SurfaceElement>> & bd_elements,
40 const std::vector<Point> & centroids,
41 const SurfaceGeometry::RayDirectionOptions & ray_options,
42 const Real eps_on_surface = libMesh::TOLERANCE,
43 const int leaf_max_size = 10,
44 const FileName & obb_file_name = "",
45 const FileName & ray_file_name = "",
46 const libMesh::Parallel::Communicator * comm = nullptr);
47
49 SurfaceGeometry::SurfaceSide sideness(const Point & p) const;
50
53 const Point & rayDirection() const { return _ray_direction; }
54
55private:
59
63 std::vector<Point> _projected_centroids;
64
67 std::unique_ptr<KDTree> _kd_tree;
68
71
73 const std::vector<std::unique_ptr<SurfaceElement>> & _bd_elements;
74
76 const std::vector<Point> & _centroids;
77
79 int _dim = -1;
80
82 std::size_t _num_elements = 0;
83
86
89
92
95
98
102
105
107 BoundingBox _bounds;
108
113
116 bool _build_obb = false;
117
120 Point _plane_origin = Point(0.0, 0.0, 0.0);
121
124
127
130
132 bool rayIntersectGeometry(const Point & ray_start,
133 const Point & ray_end,
134 const SurfaceElement * elem) const;
135
137 bool isOutsideBoundingBox(const Point & query_point) const;
138
140 bool isOutsideRayBBox(const Point & orig, const Point & dir, const Ball & ball) const;
141
143 bool isOutsideBoundingRegion(const Point & orig, const Point & dir, const Ball & ball) const;
144
146 BoundingBox computeGlobalBoundingBox();
147
148 // Perform Principal Component Analysis (PCA) using Singular Value Decomposition (SVD)
149 // to compute the principal directions:
150 // - _max_variance_vector: Direction with the largest variance (first principal component).
151 // - _second_variance_vector: Direction with the second-largest variance (second principal
152 // component).
153 // - _min_variance_vector: Direction with the smallest variance (typically the surface normal).
154 void preparePCASVD();
155
160
163 std::optional<SurfaceGeometry::SurfaceSide>
164 sidenessFromRayPair(const Point & p, const std::array<Point, 2> & ray_starts) const;
165
168 bool isOnSurface(const Point & p) const;
169
174 int countCrossings(const Point & ray_start,
175 const Point & ray_end,
176 const bool use_primary_direction = true) const;
177
183 template <typename CrossingTest>
184 int countFilteredCrossings(const Point & ray_start,
185 const Point & ray_end,
186 const bool use_primary_direction,
187 CrossingTest is_crossing) const;
188
193 int countCrossings2D(const Point & ray_start,
194 const Point & ray_end,
195 const bool use_primary_direction) const;
196
202 Point
203 rayStartOutsideAABB(const Point & point, const Point & unit_direction, const bool inverted) const;
204
210 Point rayStartOutsideOBB(const Point & point,
211 const Point & ray_direction,
212 const unsigned int obb_axis,
213 const bool inverted = false) const;
214
217 Point projectPointOntoPlane(const Point & point_to_project,
218 const Point & plane_point,
219 const Point & plane_normal) const;
220
223 void buildObbKdtreeAndMaxProjectedDiagonal(const Real expand_box_length);
224
226 std::vector<unsigned int> collectCandidateElementIDs(const Point & query_point) const;
227};
Ray-casting point-in-solid engine over a closed surface mesh represented as a collection of SurfaceEl...
std::vector< Point > _projected_centroids
Projected centroids of the elements in the boundary mesh.
int countFilteredCrossings(const Point &ray_start, const Point &ray_end, const bool use_primary_direction, CrossingTest is_crossing) const
Shared traversal for the 2D and 3D crossing counts: walk the candidate elements (KD-tree candidates f...
std::unique_ptr< KDTree > _kd_tree
The KDTree is constructed using the projected centroids of the elements in the boundary mesh.
Point _max_variance_vector
max variance vector
int countCrossings2D(const Point &ray_start, const Point &ray_end, const bool use_primary_direction) const
2D crossing count using a half-open side-based crossing rule: an edge is counted when its two endpoin...
Point rayStartOutsideAABB(const Point &point, const Point &unit_direction, const bool inverted) const
Ray start strictly outside the global AABB along unit_direction, for any direction.
Point projectPointOntoPlane(const Point &point_to_project, const Point &plane_point, const Point &plane_normal) const
Orthogonally project point_to_project onto the plane defined by plane_point and unit normal plane_nor...
Point _ray_direction
Ray shooting direction.
bool isOutsideRayBBox(const Point &orig, const Point &dir, const Ball &ball) const
Check if element center is outside ray bounding box.
bool rayIntersectGeometry(const Point &ray_start, const Point &ray_end, const SurfaceElement *elem) const
Ray-element intersection (e.g., ray-line for 2D, ray-triangle for 3D)
FileName _obb_file_name
The file name for the OBB.
Point _second_variance_vector
second max variance vector
Point _plane_origin
The origin of the plane used to ensure that every projected point is correctly aligned and lies on th...
Point _centroid_nodal_points
The centroid of the boundary elements' node points (prepare inside this class).
bool isOutsideBoundingRegion(const Point &orig, const Point &dir, const Ball &ball) const
Check if element center is outside ray bounding circle/sphere.
int countCrossings(const Point &ray_start, const Point &ray_end, const bool use_primary_direction=true) const
Count how many times the segment from ray_start to ray_end crosses the surface.
SurfaceGeometry::SurfaceSide sideness(const Point &p) const
Main function: Determine if a point is inside the geometry.
Real _max_projected_diag_length
The maximum diagonal length of the projected bounding box from the boundary elements.
const Point & rayDirection() const
The resolved ray direction actually used for shooting: the (normalized) user direction for a user-sel...
std::vector< unsigned int > collectCandidateElementIDs(const Point &query_point) const
Use the kd-tree to collect candidate element IDs to check intersections.
bool isOnSurface(const Point &p) const
True if p lies on the surface (within _eps_on_surface), i.e.
Point _min_variance_vector
min variance vector (only used for 3D)
BoundingBox computeGlobalBoundingBox()
Compute the global bounding box of all boundary elements.
Real _eps_on_surface
Epsilon value for checking if a point is on the surface of the geometry.
int _dim
The dimension of the embedding mesh.
const std::vector< std::unique_ptr< SurfaceElement > > & _bd_elements
pass into the constructor for the surface elements
OrientedBoundingBox _obb_bounds
The oriented bounding box (OBB).
FileName _ray_file_name
The file name for the ray.
Point rayStartOutsideOBB(const Point &point, const Point &ray_direction, const unsigned int obb_axis, const bool inverted=false) const
Computes the starting point of an OBB-based ray (auto/PCA policy) for a given query point.
int _leaf_max_size
Configures KDTree leaf node size for performance tuning.
const std::vector< Point > & _centroids
pass into the constructor for the surface element centroids
bool _build_obb
When the ray direction is auto-selected (PCA) we build an Oriented Bounding Box (OBB); a user-selecte...
std::size_t _num_elements
The number of elements in the boundary mesh.
bool isOutsideBoundingBox(const Point &query_point) const
Check if point is outside global bounding box.
std::optional< SurfaceGeometry::SurfaceSide > sidenessFromRayPair(const Point &p, const std::array< Point, 2 > &ray_starts) const
Determine sideness from a pair of opposite rays.
void buildObbKdtreeAndMaxProjectedDiagonal(const Real expand_box_length)
Constructs an oriented bounding box (OBB) using the results of PCA and the KD-tree.
void initializeRayDirection()
Finalizes the ray direction and the matching bounding box.
const libMesh::Parallel::Communicator * _comm
Communicator used only for writing the debug OBB/ray mesh files.
BoundingBox _bounds
The bounding box AABB.
bool _auto_ray_direction
Whether the ray direction is auto-selected via PCA (true) or user-selected (false).
Ball primitive: a circle in 2D or a sphere in 3D.
Definition Ball.h:35
Oriented bounding box in 2 D or 3 D.
Base class for a single surface (boundary) element of a closed surface mesh.
SurfaceSide
The side of a closed surface where a query point is located.
Definition SurfaceSide.h:21
static constexpr Real TOLERANCE
Ray-direction intent for AdaptiveRayContainmentCheck: an explicit mode plus the direction to use when...