Free Electron
RayDiskIntersect.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_RayDiskIntersect_h__
8 #define __solve_RayDiskIntersect_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Find intersection between ray and circular solid
17 
18  @ingroup solve
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,
26  const Vector<3,T>& origin,
27  const Vector<3,T>& direction,
28  Vector<3,T>& intersection);
29 static void resolveContact(const Vector<3,T>& center,
30  const Vector<3,T>& facing,
31  const T radius,
32  const Vector<3,T>& origin,
33  const Vector<3,T>& direction,
34  const T range,
35  const Vector<3,T>& intersection,
36  Vector<3,T>& normal);
37 };
38 
39 // direction must be normalized
40 template <typename T>
41 inline T RayDiskIntersect<T>::solve(const Vector<3,T>& center,
42  const Vector<3,T>& facing,T radius,
43  const Vector<3,T>& origin, const Vector<3,T>& direction,
44  Vector<3,T>& intersection)
45 {
46 // feLog("center %s facing %s radius %.6G\n",c_print(center),radius);
47 // feLog("origin: %s\n",c_print(origin));
48 // feLog("direction: %s\n",c_print(direction));
49 
50  const T incident=dot(facing,direction);
51  if(incident==0.0f)
52  {
53  return T(-1);
54  }
55  const T range=(dot(facing,center)-dot(facing,origin))/incident;
56 
57  intersection=origin+direction*range;
58 // feLog("intersection: %s sq %.6G vs %.6G\n",c_print(intersection),
59 // magnitudeSquared(intersection-center),radius*radius);
60 
61  if(magnitudeSquared(intersection-center)>radius*radius)
62  {
63  return T(-1);
64  }
65 
66  return range;
67 }
68 
69 // direction must be normalized
70 template <typename T>
72  const Vector<3,T>& center,
73  const Vector<3,T>& facing,
74  const T radius,
75  const Vector<3,T>& origin,
76  const Vector<3,T>& direction,
77  const T range,
78  const Vector<3,T>& intersection,
79  Vector<3,T>& normal)
80 {
81  normal=facing;
82 }
83 
84 } /* namespace ext */
85 } /* namespace fe */
86 
87 #endif /* __solve_RayDiskIntersect_h__ */
88 
Find intersection between ray and circular solid.
Definition: RayDiskIntersect.h:21
kernel
Definition: namespace.dox:3