https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Ball.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
10#include "Ball.h"
11#include "LineSegment.h"
12
13#include <cmath>
14
15using libMesh::Point;
16using libMesh::Real;
17
18bool
19Ball::intersect(const LineSegment & line_segment) const
20{
21 const Point & p0 = line_segment.start();
22 const Point & p1 = line_segment.end();
23 const Point d = p1 - p0;
24 const Point f = p0 - _c;
25
26 const Real a = d.norm_sq();
27 const Real b = 2.0 * (f * d);
28 const Real c = f.norm_sq() - _r * _r;
29
30 if (a == 0.0)
31 return f.norm_sq() <= _r * _r;
32
33 const Real discriminant = b * b - 4.0 * a * c;
34 if (discriminant < 0.0)
35 return false;
36
37 const Real sqrt_disc = std::sqrt(discriminant);
38 const Real inv_denom = 1.0 / (2.0 * a);
39 const Real t1 = (-b - sqrt_disc) * inv_denom;
40 const Real t2 = (-b + sqrt_disc) * inv_denom;
41
42 return (t1 >= 0.0 && t1 <= 1.0) || (t2 >= 0.0 && t2 <= 1.0);
43}
44
45Ball
47{
48 return *this;
49}
Ball primitive: a circle in 2D or a sphere in 3D.
Definition Ball.h:35
libMesh::Real _r
Definition Ball.h:60
libMesh::Point _c
Definition Ball.h:59
bool intersect(const LineSegment &line_segment) const override
Check if a line segment intersects this ball.
Definition Ball.C:19
Ball computeBoundingBall() const override
return the ball itself
Definition Ball.C:46
The LineSegment class is used by the LineMaterialSamplerBase class and for some ray tracing stuff.
Definition LineSegment.h:31
const Point & end() const
Ending of the line segment.
Definition LineSegment.h:90
const Point & start() const
Beginning of the line segment.
Definition LineSegment.h:85
auto norm_sq() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real