Free Electron
PointCurveNearest.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_PointCurveNearest_h__
8 #define __geometry_PointCurveNearest_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Find point nearest to a curve with radius
17 
18  @ingroup geometry
19 *//***************************************************************************/
20 template <typename T>
22 {
23  public:
24 static T solve(const Vector<3,T>* a_pVertex,const U32 a_vertCount,
25  const T a_radius,
26  const Vector<3,T>& a_origin,Vector<3,T>& a_direction,
27  T& a_along,Vector<3,T>& a_intersection);
28 };
29 
30 template <typename T>
32  const Vector<3,T>* a_pVertex,const U32 a_vertCount,
33  const T a_radius,
34  const Vector<3,T>& a_origin, Vector<3,T>& a_direction,
35  T& a_along,Vector<3,T>& a_intersection)
36 {
37 #if FALSE
38  feLog("PointCurveNearest< %s >::solve %s radius %.6G\n",
39  FE_TYPESTRING(T).c_str(),
40  c_print(a_origin),a_radius);
41  for(U32 m=0;m<a_vertCount;m++)
42  {
43  feLog(" %d/%d %s\n",m,a_vertCount,c_print(a_pVertex[m]));
44  }
45 #endif
46 
47  if(a_vertCount<2)
48  {
49  return T(-1);
50  }
51 
52  T bestAlong=0.0;
53  T bestMag= -1.0;
54  I32 bestIndex=0;
55  for(U32 m=0;m<a_vertCount-1;m++)
56  {
57  const Vector<3,T> segment=a_pVertex[m+1]-a_pVertex[m];
58  Vector<3,T> oneIntersection;
59  T oneAlong;
60  T oneMag=PointCylinderNearest<T>::solve(a_pVertex[m],segment,
61  a_radius,a_origin,a_direction,oneAlong,oneIntersection);
62  if(oneMag>=0.0 && (bestMag<0.0 || oneMag<bestMag))
63  {
64  const T length=magnitude(segment);
65  bestAlong=oneAlong*(length>0.0? 1.0/length: 1.0);
66  a_intersection=oneIntersection;
67  bestMag=oneMag;
68  bestIndex=m;
69  }
70 
71 #if FALSE
72  feLog(" %d/%d mag %.6G along %.6G at %s\n",m,a_vertCount,
73  oneMag,oneAlong,c_print(oneIntersection));
74 #endif
75 
76  }
77 
78  FEASSERT(bestAlong>=0.0);
79  FEASSERT(bestAlong<=1.0);
80 
81  a_along=(bestMag<0.0)? 0.0: (bestIndex+bestAlong)/(a_vertCount-1.0);
82 
83 #if FALSE
84  feLog(" bestMag %.6G along %.6G \n",bestMag,a_along);
85 #endif
86 
87  return bestMag;
88 }
89 
90 } /* namespace ext */
91 } /* namespace fe */
92 
93 #endif /* __geometry_PointCurveNearest_h__ */
kernel
Definition: namespace.dox:3
Find point nearest to a curve with radius.
Definition: PointCurveNearest.h:21
Find point nearest to a cylindrical solid.
Definition: PointCylinderNearest.h:21