Free Electron
PointDiskNearest.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 __geometry_PointDiskNearest_h__
8 #define __geometry_PointDiskNearest_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Find point nearest to a circular solid
17 
18  @ingroup geometry
19 *//***************************************************************************/
20 template <typename T>
22 {
23  public:
24 static T solve(const Vector<3,T>& center,const Vector<3,T>& facing,
25  const T radius,const Vector<3,T>& origin,
26  Vector<3,T>& direction,Vector<3,T>& intersection);
27 };
28 
29 template <typename T>
30 inline T PointDiskNearest<T>::solve(const Vector<3,T>& center,
31  const Vector<3,T>& facing,T radius,
32  const Vector<3,T>& origin,Vector<3,T>& direction,
33  Vector<3,T>& intersection)
34 {
35 // feLog("center %s facing %s radius %.6G\n",
36 // c_print(center),c_print(facing),radius);
37 // feLog("origin: %s\n",c_print(origin));
38 // feLog("direction: %s\n",c_print(direction));
39 
40  const Vector<3,T> to_origin=origin-center;
41  const T displacement=dot(facing,to_origin);
42  intersection=origin-displacement*facing;
43  const Vector<3,T> radial=intersection-center;
44  const T outward2=magnitudeSquared(radial);
45 // feLog("displacement %.6G radial %s\n",displacement,c_print(radial));
46 // feLog("intersection: %s sq %.6G vs %.6G\n",c_print(intersection),
47 // outward2,radius*radius);
48 
49  if(outward2<=radius*radius)
50  {
51  direction= -facing;
52  return displacement;
53  }
54 
55  intersection=center+radius/sqrt(outward2)*radial;
56  direction=intersection-origin;
57  T mag=magnitude(direction);
58  direction*=T(1)/mag;
59  return mag;
60 }
61 
62 } /* namespace ext */
63 } /* namespace fe */
64 
65 #endif /* __geometry_PointDiskNearest_h__ */
66 
67 
kernel
Definition: namespace.dox:3
Find point nearest to a circular solid.
Definition: PointDiskNearest.h:21