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
15bool
16Ball::intersect(const LineSegment & line_segment) const
17{
18 const Point & p0 = line_segment.start();
19 const Point & p1 = line_segment.end();
20 const Point d = p1 - p0;
21 const Point f = p0 - _c;
22
23 const Real a = d.norm_sq();
24 const Real b = 2.0 * (f * d);
25 const Real c = f.norm_sq() - _r * _r;
26
27 if (a == 0.0)
28 return f.norm_sq() <= _r * _r;
29
30 const Real discriminant = b * b - 4.0 * a * c;
31 if (discriminant < 0.0)
32 return false;
33
34 const Real sqrt_disc = std::sqrt(discriminant);
35 const Real inv_denom = 1.0 / (2.0 * a);
36 const Real t1 = (-b - sqrt_disc) * inv_denom;
37 const Real t2 = (-b + sqrt_disc) * inv_denom;
38
39 return (t1 >= 0.0 && t1 <= 1.0) || (t2 >= 0.0 && t2 <= 1.0);
40}
41
42Ball
44{
45 return *this;
46}
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:16
Ball computeBoundingBall() const override
return the ball itself
Definition Ball.C:43
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