Free Electron
PointSphereNearest.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __solve_PointSphereNearest_h__
8 #define __solve_PointSphereNearest_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Find nearest point on sphere
17 
18  @ingroup solve
19 *//***************************************************************************/
20 template <typename T>
22 {
23  public:
24 static T solve(const Vector<3,T>& center,const T radius,
25  const Vector<3,T>& origin,
26  Vector<3,T>& direction);
27 
28 static BWORD within(const Vector<3,T>& center,const T radius,
29  const Vector<3,T>& origin,const T distance);
30 };
31 
32 template <typename T>
33 inline T PointSphereNearest<T>::solve(const Vector<3,T>& center,T radius,
34  const Vector<3,T>& origin,Vector<3,T>& direction)
35 {
36  const Vector<3,T> to_origin=origin-center;
37  const T mag=magnitude(to_origin);
38  if(mag>T(0))
39  {
40  direction= -T(1)/mag*to_origin;
41  }
42  else
43  {
44  set(direction);
45  }
46  return mag-radius;
47 }
48 
49 template <typename T>
50 inline BWORD PointSphereNearest<T>::within(const Vector<3,T>& center,T radius,
51  const Vector<3,T>& origin,const T distance)
52 {
53  const Vector<3,T> to_origin=origin-center;
54  const T mag2=magnitudeSquared(to_origin);
55  const T sum=distance+radius;
56 
57  return (mag2<=(sum*sum));
58 }
59 
60 } /* namespace ext */
61 } /* namespace fe */
62 
63 #endif /* __solve_PointSphereNearest_h__ */
64 
kernel
Definition: namespace.dox:3
Find nearest point on sphere.
Definition: PointSphereNearest.h:21