Free Electron
RayCurveIntersect.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_RayCurveIntersect_h__
8 #define __solve_RayCurveIntersect_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Find intersection between ray and curve with radius
17 
18  The radius tolerance can be adjusted by distance, useful for picking.
19  The radius0 is at distance 0. The radius0 is at distance 1.
20 
21  @ingroup solve
22 
23 *//***************************************************************************/
24 template <typename T>
26 {
27  public:
28 static T solve(const Vector<3,T>* a_pVertex,const U32 a_vertCount,
29  const T a_radiusAt0,const T a_radiusAt1,
30  const Vector<3,T>& a_origin,
31  const Vector<3,T>& a_direction,
32  T& a_along,Vector<3,T>& a_intersection);
33 };
34 
35 // direction must be normalized
36 template <typename T>
38  const Vector<3,T>* a_pVertex,const U32 a_vertCount,
39  const T a_radiusAt0,const T a_radiusAt1,
40  const Vector<3,T>& a_origin, const Vector<3,T>& a_direction,
41  T& a_along,Vector<3,T>& a_intersection)
42 {
43  T bestMag= -1.0;
44  T realIndex=0.0;
45  for(U32 m=0;m<a_vertCount-1;m++)
46  {
47  const SpatialVector center=0.5*(a_pVertex[m]+a_pVertex[m+1]);
48  const Real distance=magnitude(center-a_origin);
49  const Real radius=a_radiusAt0+(a_radiusAt1-a_radiusAt0)*distance;
50 
51  SpatialVector oneIntersection;
52  T oneAlong;
53  T oneMag=RayCylinderIntersect<T>::solve(a_pVertex[m],
54  a_pVertex[m+1]-a_pVertex[m],radius,
55  a_origin,a_direction,oneAlong,oneIntersection);
56  if(oneMag>=0.0 && (bestMag<0.0 || oneMag<bestMag))
57  {
58  a_intersection=oneIntersection;
59  realIndex=m+oneAlong;
60  bestMag=oneMag;
61  }
62  }
63 
64  a_along=(bestMag<0.0)? -1.0: (realIndex)/T(a_vertCount-1);
65 
66  return bestMag;
67 }
68 
69 } /* namespace ext */
70 } /* namespace fe */
71 
72 #endif /* __solve_RayCurveIntersect_h__ */
kernel
Definition: namespace.dox:3
Find intersection between ray and cylinder.
Definition: RayCylinderIntersect.h:26
Find intersection between ray and curve with radius.
Definition: RayCurveIntersect.h:25