Free Electron
PointCylinderNearest.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_PointCylinderNearest_h__
8 #define __geometry_PointCylinderNearest_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Find point nearest to a cylindrical solid
17 
18  @ingroup geometry
19 *//***************************************************************************/
20 template <typename T>
22 {
23  public:
24 static T solve(const Vector<3,T>& base,const Vector<3,T>& axis,
25  const T radius,const Vector<3,T>& origin,
26  Vector<3,T>& direction,T& along,Vector<3,T>& intersection);
27 };
28 
29 template <typename T>
31  const Vector<3,T>& base,const Vector<3,T>& axis,const T radius,
32  const Vector<3,T>& origin, Vector<3,T>& direction,
33  T& along,Vector<3,T>& intersection)
34 {
35  const T length=magnitude(axis);
36  const Vector<3,T> unitAxis=axis*(length>T(0)? T(1)/length: T(1));
37  const Vector<3,T> towards=origin-base;
38  along=dot(towards,unitAxis);
39 
40 #if FALSE
41  feLog("\nbase %s axis %s radius %.6G\n",
42  c_print(base),c_print(axis),radius);
43  feLog("origin: %s\n",c_print(origin));
44 
45  feLog("along: %.6G/%.6G\n",along,length);
46 #endif
47 
48  Vector<3,T> center=base+along*unitAxis;
49  Vector<3,T> radial=origin-center;
50  const T radial_mag=magnitude(radial);
51  if(radial_mag>T(0))
52  {
53  radial*=radius/radial_mag;
54  }
55  if(along<T(0))
56  {
57  along=T(0);
58  center=base;
59  }
60  else if(along>length)
61  {
62  along=length;
63  center=base+axis;
64  }
65 
66  intersection=center+radial;
67  direction=intersection-origin;
68  T mag=magnitude(direction);
69  if(mag>T(0))
70  {
71  direction*=T(1)/mag;
72  }
73 
74 #if FALSE
75  feLog("intersection: %s\n",c_print(intersection));
76  feLog("direction: %s\n",c_print(direction));
77 #endif
78 
79  return mag;
80 }
81 
82 } /* namespace ext */
83 } /* namespace fe */
84 
85 #endif /* __geometry_PointCylinderNearest_h__ */
kernel
Definition: namespace.dox:3
Find point nearest to a cylindrical solid.
Definition: PointCylinderNearest.h:21