quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
tbasisfun.h
Go to the documentation of this file.
1#ifndef HAVE_TBASISFUN_H
2#define HAVE_TBASISFUN_H
3
4#include <algorithm>
5#include <iostream>
6#include <iterator>
7#include <limits>
8
9namespace bspline {
10
11 template<typename FLT>
12 FLT
13 ratio (const FLT num, const FLT denom) {
14 if (denom != FLT(0.0)) {
15 return num / denom;
16 } else if (num != FLT(0.0)) {
17 throw ("division by zero!");
18 } else {
19 return FLT(1.0);
20 }
21 };
22
25
30
31 template<typename INT, typename FLT, typename ITERATOR>
32 FLT
33 onebasisfun (FLT const u, INT const p, ITERATOR const Ubegin, ITERATOR const Uend) {
34 FLT N{0.0};
35 const FLT Umin = *(std::min_element (Ubegin, Uend));
36 const FLT Umax = *(std::max_element (Ubegin, Uend));
37
38 if ((u <= Umin) || ( u > Umax)) {
39 N = 0.0;
40 return N;
41 } else if (p == 0) {
42 N = 1.0;
43 return N;
44 } else if (p == 1) {
45 if (u < *std::next(Ubegin, 1)) {
46 N = ratio ((u - *Ubegin),
47 (*std::next(Ubegin, 1) - *Ubegin));
48 return N;
49 } else {
50 N = ratio ((*std::next(Ubegin, 2) - u),
51 (*std::next(Ubegin, 2) - *std::next(Ubegin, 1)));
52 return N;
53 }
54 } else if (p == 2) {
55 const FLT ln = u - *Ubegin;
56 const FLT dn = *std::next(Ubegin, 3) - u;
57 const FLT ld = *std::next(Ubegin, 2) - *Ubegin;
58 const FLT dd = *std::next(Ubegin, 3) - *std::next(Ubegin, 1);
59 if (u < *std::next(Ubegin, 1)) {
60 N = ratio (ln*ln, (ld * (*std::next(Ubegin, 1) - *Ubegin)));
61 return N;
62 } else if (u > *std::next(Ubegin, 2)) {
63 N = ratio (dn*dn,
64 (dd * (*std::next(Ubegin, 3) - *std::next(Ubegin, 2))));
65 return N;
66 } else {
67 if (ld > FLT(0.0)) {
68 N += ratio (ln * (*std::next(Ubegin, 2) - u),
69 ((*std::next(Ubegin, 2) - *std::next(Ubegin, 1)) * ld));
70 }
71 if (dd > FLT(0.0)) {
72 N += ratio (dn * (u - *std::next(Ubegin, 1)),
73 ((*std::next(Ubegin, 2) - *std::next(Ubegin, 1)) * dd));
74 }
75 return N;
76 }
77 }
78
79 const FLT ln = u - *Ubegin;
80 const FLT ld = *std::next(Uend, - 2) - *Ubegin;
81 if (ld > FLT(0.0)) {
82 N += ln * onebasisfun (u, p-1, Ubegin, std::next(Uend, - 1)) / ld;
83 }
84
85 const FLT dn = *std::next (Uend, - 1) - u;
86 const FLT dd = *std::next (Uend, - 1) - *std::next (Ubegin , 1);
87 if (dd > FLT(0.0)) {
88 N += dn * onebasisfun (u, p-1, std::next (Ubegin, 1), Uend) / dd;
89 }
90
91 return N;
92 };
93
95
100 template<typename INT, typename FLT, typename ITERATOR>
101 FLT
102 onebasisfunder (FLT u, INT p, ITERATOR Ubegin, ITERATOR Uend)
103 {
104
105 FLT Nder{0.0};
106 const FLT Umin = *(std::min_element (Ubegin, Uend));
107 const FLT Umax = *(std::max_element (Ubegin, Uend));
108
109 if ((u <= Umin) || ( u > Umax)) {
110 Nder = FLT(0.0);
111 return Nder;
112 } else if (p == 0) {
113 Nder = FLT(0.0);
114 return Nder;
115 } else {
116
117 const FLT ld = *std::next (Uend, -2) - *Ubegin;
118 if (ld != FLT(0.0)) {
119 Nder += p * onebasisfun (u, p-1, Ubegin, std::next (Uend, -1)) / ld;
120 }
121
122 const FLT dd = *std::next (Uend, -1) - *std::next (Ubegin, 1);
123 if (dd != FLT(0.0)) {
124 Nder -= p * onebasisfun (u, p-1, std::next (Ubegin, 1), Uend) / dd;
125 }
126
127 }
128
129 return Nder;
130 };
131
132 template<typename INT, typename FLT, typename ITERATOR>
133 FLT
134 onebasisfun2d (FLT const u, FLT const v, INT const pu, INT const pv,
135 ITERATOR const Ubegin, ITERATOR const Uend,
136 ITERATOR const Vbegin, ITERATOR const Vend) {
137 FLT N = onebasisfun (u, pu, Ubegin, Uend);
138 N *= onebasisfun (v, pv, Vbegin, Vend);
139 return N;
140 }
141
142}
143
144#endif
145
146
Definition: tbasisfun.h:9
FLT onebasisfun2d(FLT const u, FLT const v, INT const pu, INT const pv, ITERATOR const Ubegin, ITERATOR const Uend, ITERATOR const Vbegin, ITERATOR const Vend)
Definition: tbasisfun.h:134
FLT onebasisfunder(FLT u, INT p, ITERATOR Ubegin, ITERATOR Uend)
function to compute the derivative of a BSpline basis function.
Definition: tbasisfun.h:102
FLT onebasisfun(FLT const u, INT const p, ITERATOR const Ubegin, ITERATOR const Uend)
function to compute the value of a BSpline basis function at a given point.
Definition: tbasisfun.h:33
FLT ratio(const FLT num, const FLT denom)
Definition: tbasisfun.h:13