OGLplus (0.52.0) a C++ wrapper for OpenGL

compile_time.hpp
1 
12 #pragma once
13 #ifndef OGLPLUS_MATH_COMPILE_TIME_1107121519_HPP
14 #define OGLPLUS_MATH_COMPILE_TIME_1107121519_HPP
15 
16 #include <type_traits>
17 
18 namespace oglplus {
19 namespace math {
20 
21 template <unsigned N>
22 struct Factorial
23 {
24  static const unsigned value = Factorial<N-1>::value*N;
25 };
26 
27 template <>
28 struct Factorial<0u>
29 {
30  static const unsigned value = 1;
31 };
32 
33 template <unsigned N, unsigned K>
34 struct Binomial
35 {
36  static_assert(N > 0, "N must be non-zero");
37  static_assert(K <= N, "K must be between 0 and N-1");
38  static const unsigned value =
39  (Factorial<N>::value) /
40  (Factorial<N-K>::value * Factorial<K>::value);
41 };
42 
43 template <typename T>
44 static T Pow(T, std::integral_constant<unsigned, 0u>)
45 {
46  return T(1);
47 }
48 
49 template <typename T, unsigned N>
50 static T Pow(T v, std::integral_constant<unsigned, N>)
51 {
52  return v * Pow<T>(v, std::integral_constant<unsigned, N-1>());
53 }
54 
55 
56 // T - interpolated type
57 // P - parameter type [0, 1]
58 // N - degree, i.e. linear = 1, quadratic = 2, cubic = 3, ...
59 template <typename T, typename P, unsigned N>
60 struct Bezier
61 {
62 private:
63  template <unsigned M, unsigned I>
64  static P _bi(
65  std::integral_constant<unsigned, M>,
66  std::integral_constant<unsigned, I>,
67  P t
68  )
69  {
70  return Binomial<M, I>::value *
71  Pow<P>(t, std::integral_constant<unsigned, I>()) *
72  Pow<P>(P(1)-t, std::integral_constant<unsigned, M-I>());
73  }
74 
75  // f(t)
76  template <unsigned I>
77  static T _f(
78  std::integral_constant<unsigned, 0> /*derivation*/,
79  std::integral_constant<unsigned, I> i,
80  const T* v,
81  P t
82  )
83  {
84  std::integral_constant<unsigned, N> n;
85  return _bi(n, i, t) * v[I];
86  }
87 
88 
89  // f'(t)
90  template <unsigned I>
91  static T _f(
92  std::integral_constant<unsigned, 1> /*derivation*/,
93  std::integral_constant<unsigned, I> i,
94  const T* v,
95  P t
96  )
97  {
98  std::integral_constant<unsigned, N-1> n_1;
99  return _bi(n_1, i, t) * N * (v[I+1] - v[I]);
100  }
101 
102 
103  // f''(t)
104  template <unsigned I>
105  static T _f(
106  std::integral_constant<unsigned, 2> /*derivation*/,
107  std::integral_constant<unsigned, I> i,
108  const T* v,
109  P t
110  )
111  {
112  std::integral_constant<unsigned, N-2> n_2;
113  return _bi(n_2, i, t) * N*(N - 1) * (v[I+2] - 2*v[I+1] + v[I]);
114  }
115 
116  template <unsigned D>
117  static T _sum(
118  std::integral_constant<unsigned, D> d,
119  std::integral_constant<unsigned, 0> i,
120  const T* v,
121  P t
122  )
123  {
124  return _f(d, i, v, t);
125  }
126 
127  template <unsigned D, unsigned I>
128  static T _sum(
129  std::integral_constant<unsigned, D> d,
130  std::integral_constant<unsigned, I> i,
131  const T* v,
132  P t
133  )
134  {
135  std::integral_constant<unsigned, I-1> i_1;
136  return _sum(d, i_1, v, t) + _f(d, i, v, t);
137  }
138 
139 public:
140  template <unsigned D>
141  static T B(
142  std::integral_constant<unsigned, D> d,
143  const T* v,
144  size_t s,
145  P t
146  )
147  {
148  OGLPLUS_FAKE_USE(s);
149  assert(s >= N);
150  std::integral_constant<unsigned, N-D> n_d;
151  return _sum(d, n_d, v, t);
152  }
153 
154  static T Position(const T* v, size_t s, P t)
155  {
156  std::integral_constant<unsigned, 0> d;
157  return B(d, v, s, t);
158  }
159 
160  static T Derivative1(const T* v, size_t s, P t)
161  {
162  std::integral_constant<unsigned, 1> d;
163  return B(d, v, s, t);
164  }
165 
166  static T Derivative2(const T* v, size_t s, P t)
167  {
168  std::integral_constant<unsigned, 2> d;
169  return B(d, v, s, t);
170  }
171 };
172 
173 // T - interpolated type
174 // W - weight type
175 // P - parameter type [0, 1]
176 // N - degree, i.e. linear = 1, quadratic = 2, cubic = 3, ...
177 template <typename T, typename W, typename P, unsigned N>
178 struct NURBS
179 {
180 private:
181  template <unsigned I>
182  static T _part(
183  std::integral_constant<unsigned, I>,
184  const T* v,
185  const W* w,
186  P t
187  )
188  {
189  return Binomial<N, I>::value *
190  Pow<P>(t, std::integral_constant<unsigned, I>()) *
191  Pow<P>(P(1)-t, std::integral_constant<unsigned, N-I>())*
192  v[I]*w[I];
193  }
194 
195  static T _sum(
196  std::integral_constant<unsigned, 0> i,
197  const T* v,
198  const W* w,
199  P t
200  )
201  {
202  return _part(i, v, w, t);
203  }
204 
205  template <unsigned I>
206  static T _sum(
207  std::integral_constant<unsigned, I> i,
208  const T* v,
209  const W* w,
210  P t
211  )
212  {
213  std::integral_constant<unsigned, I-1> i_1;
214  return _sum(i_1, v, w, t) + _part(i, v, w, t);
215  }
216  template <unsigned I>
217  static W _part(std::integral_constant<unsigned, I>, const W* w, P t)
218  {
219  return Binomial<N, I>::value *
220  Pow<P>(t, std::integral_constant<unsigned, I>()) *
221  Pow<P>(P(1)-t, std::integral_constant<unsigned, N-I>())*
222  w[I];
223  }
224 
225  static W _sum(std::integral_constant<unsigned, 0> i, const W* w, P t)
226  {
227  return _part(i, w, t);
228  }
229 
230  template <unsigned I>
231  static W _sum(std::integral_constant<unsigned, I> i, const W* w, P t)
232  {
233  std::integral_constant<unsigned, I-1> i_1;
234  return _sum(i_1, w, t) + _part(i, w, t);
235  }
236 public:
237  static T Calc(const T* v, const W* w, size_t s, P t)
238  {
239  assert(s >= N);
240  std::integral_constant<unsigned, N> n;
241  return _sum(n, v, w, t)*(W(1)/_sum(n, w, t));
242  }
243 };
244 
245 } // namespace math
246 } // namespace oglplus
247 
248 #endif // include guard

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).