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