Free Electron
Spline.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_Spline_h__
8 #define __geometry_Spline_h__
9 
10 #define FE_CARDINALSCALE (0.5f) // (0.5) tangent length in cardinal spline
11 
12 namespace fe
13 {
14 namespace ext
15 {
16 /**************************************************************************//**
17  @brief A stateless namespace for assorted spline functions
18 
19  @ingroup geometry
20 
21  The Cardinal spline was introduced by I.J. Schoenberg in 1973.
22 
23  The Cardinal spline guarantees control-point intersection.
24  It assumes fairly evenly spaced samples.
25 
26  A Catmull-Rom spline appears to be a Cardinal spline with the
27  "tension" adjustment fixed at 0.5.
28 *//***************************************************************************/
29 template <typename T,typename U=T>
30 class Spline
31 {
32  public:
33 
34  /// Compute f(t) for 0<=t<=1 where f(-1)=p0, f(0)=p1, f(1)=p2, f(2)=p3
35 static T Cardinal2D(U t,T p0,T p1,T p2,T p3);
36  /// Compute 1st derivitive of f(t)
37 static T Cardinal2D_d1(U t,T p0,T p1,T p2,T p3);
38  /// Compute 2nd derivitive of f(t)
39 static T Cardinal2D_d2(U t,T p0,T p1,T p2,T p3);
40 
41 static FE_DL_EXPORT T Basis2D(U a_t,I32 a_count,
42  const T* a_pKnot,const T* a_pControl);
43 };
44 
45 /******************************************************************************
46  letting f(-1) = p0
47  f( 0) = p1
48  f( 1) = p2
49  f( 2) = p3
50 
51  returns f(t) for 0.0 < t < 1.0
52 
53  value = [t^3 t^2 t 1] / -a 2-a a-2 a \ / p0 \
54  | 2a a-3 3-2a -a | | p1 |
55  | -a 0 a 0 | | p2 |
56  \ 0 1 0 0 / \ p3 /
57 
58  = p0 (-at^3 + 2at^2 - at)
59  + p1 (2t^3 - at^3 - 3t^2 + at^2 + 1)
60  + p2 (-2t^3 + at^3 + 3t^2 - 2at^2 + at)
61  + p3 (at^3 - at^2)
62 
63  d1 = p0 (-3at^2 + 4at - a)
64  + p1 (6t^2 - 3at^2 - 6t + 2at)
65  + p2 (-6t^2 + 3at^2 + 6t - 4at + a)
66  + p3 (3at^2 - 2at)
67 
68  d2 = p0 (-6at + 4a)
69  + p1 (12t - 6at - 6 + 2a)
70  + p2 (-12t + 6at + 6 - 4a)
71  + p3 (6at - 2a)
72 
73 ******************************************************************************/
74 template <typename T,typename U>
75 T Spline<T,U>::Cardinal2D(U t,T p0,T p1,T p2,T p3)
76 {
77  const U t2=t*t;
78  const U t3=t2*t;
79  const U at=FE_CARDINALSCALE*t;
80  const U at2=at*t;
81  const U at3=at2*t;
82  const U t3_2=2.0f*t3;
83  const U at2_2=2.0f*at2;
84  const U tt2=3.0f*t2;
85  const U mid=t3_2-at3-tt2;
86 
87  return p0*(at2_2-at3-at)+
88  p1*(mid+at2+1)+
89  p2*(at-at2_2-mid)+
90  p3*(at3-at2);
91 }
92 
93 template <typename T,typename U>
94 T Spline<T,U>::Cardinal2D_d1(U t,T p0,T p1,T p2,T p3)
95 {
96  const U at=FE_CARDINALSCALE*t;
97  const U t2_6=6.0f*t*t;
98  const U at2_3=3.0f*at*t;
99  const U at_2=2.0f*at;
100  const U at_4=4.0f*at;
101  const U t_6=6.0f*t;
102  const U mid=t2_6-at2_3-t_6;
103 
104  return p0*(at_4-at2_3-FE_CARDINALSCALE)+
105  p1*(mid+at_2)+
106  p2*(FE_CARDINALSCALE-mid-at_4)+
107  p3*(at2_3-at_2);
108 }
109 
110 template <typename T,typename U>
111 T Spline<T,U>::Cardinal2D_d2(U t,T p0,T p1,T p2,T p3)
112 {
113  const U t_12=12.0f*t;
114  const U at_6=6.0f*t*FE_CARDINALSCALE;
115  const U a_2=2.0f*FE_CARDINALSCALE;
116  const U a_4=4.0f*FE_CARDINALSCALE;
117  const U mid=t_12-at_6-6;
118 
119  return p0*(a_4-at_6)+
120  p1*(mid+a_2)+
121  p2*(-mid-a_4)+
122  p3*(at_6-a_2);
123 }
124 
125 } /* namespace ext */
126 } /* namespace fe */
127 
128 #endif /* __geometry_Spline_h__*/
kernel
Definition: namespace.dox:3
static T Cardinal2D(U t, T p0, T p1, T p2, T p3)
Compute f(t) for 0<=t<=1 where f(-1)=p0, f(0)=p1, f(1)=p2, f(2)=p3.
Definition: Spline.h:75
A stateless namespace for assorted spline functions.
Definition: Spline.h:30
static T Cardinal2D_d2(U t, T p0, T p1, T p2, T p3)
Compute 2nd derivitive of f(t)
Definition: Spline.h:111
static T Cardinal2D_d1(U t, T p0, T p1, T p2, T p3)
Compute 1st derivitive of f(t)
Definition: Spline.h:94