Debian Codegraph @

avogadro/rendering/bsplinegeometry.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "bsplinegeometry.h"
7
8 namespace Avogadro::Rendering {
9
10 BSplineGeometry::BSplineGeometry() : CurveGeometry() {}
11 BSplineGeometry::BSplineGeometry(bool flat) : CurveGeometry(flat) {}
12
13 float B(float i, float k, float t, float knot)
14 {
15 float ti = knot * i; // t_i
16 float ti1 = knot * (i + 1.0f); // t_(i+1)
17 if (k == 1) {
18 if (ti <= t && t < ti1) {
19 return 1.0f;
20 } else {
21 return 0.0f;
22 }
23 }
24
25 float tik = knot * (i + k); // t_(i+k)
26 float tik1 = knot * (i + k - 1.0f); // t_(i+k-1)
27
28 float a1 = (t - ti) / (tik1 - ti);
29 float a2 = B(i, k - 1, t, knot);
30
31 float b1 = (tik - t) / (tik - ti1);
32 float b2 = B(i + 1, k - 1, t, knot);
33 return a1 * a2 + b1 * b2;
34 }
35
36 Vector3f BSplineGeometry::computeCurvePoint(
37 float t, const std::list<Point*>& points) const
38 {
39 // degree: linear = 1, quadratic = 2, cubic = 3
40 float k = 3.0f;
41 // how many points in either way for approximation
42 const int lookahead = 10;
43 // #knot segments = #control points + #degree + 1
44 float m = 2 * lookahead + k + 1.0f;
45 float knot = 1.0f / m;
46 Vector3f Q = Vector3f::Zero();
47 float i = 0.0f;
48 auto it = points.begin();
49 const auto end = points.end();
50 int size = static_cast<int>(points.size());
51 // start from a lookbehind distance rather than at the beginning
52 int startIndex = (size * t) - lookahead;
53 if (startIndex < 0)
54 startIndex = 0;
55 else if (startIndex > size - 2 * lookahead)
56 startIndex = size - 2 * lookahead;
57 float t2 = (t - startIndex / (float)size) * size / (2 * lookahead);
58 for (; startIndex > 0 && it != end; --startIndex, ++it) {}
59 // only read a certain number of elements from here
60 size_t count = 2 * lookahead;
61 for (; count && it != end; --count, ++it) {
62 Q += (*it)->pos * B(i, k, t2, knot);
63 i += 1.0f;
64 }
65 return Q;
66 }
67
68 } // namespace Avogadro::Rendering