libsurf
Programmer's Documentation

uvsplinecurve.h (r6227/r5398)
1 
2 /* Copyright (C) 2015 David Eller <david@larosterna.com>
3  *
4  * Commercial License Usage
5  * Licensees holding valid commercial licenses may use this file in accordance
6  * with the terms contained in their respective non-exclusive license agreement.
7  * For further information contact david@larosterna.com .
8  *
9  * GNU General Public License Usage
10  * Alternatively, this file may be used under the terms of the GNU General
11  * Public License version 3.0 as published by the Free Software Foundation and
12  * appearing in the file gpl.txt included in the packaging of this file.
13  */
14 
15 #ifndef SURF_UVSPLINECURVE_H
16 #define SURF_UVSPLINECURVE_H
17 
18 #include "forward.h"
19 #include "abstractuvcurve.h"
20 #include <genua/splinebasis.h>
21 #include <genua/lu.h>
22 #include <genua/xcept.h>
23 
32 template <int P>
33 class UvSplineCurve : public AbstractUvCurve
34 {
35 public:
36 
38  UvSplineCurve(SurfacePtr psf = SurfacePtr())
39  : AbstractUvCurve(psf), m_evalReverse(false) {}
40 
42  const Vector & interpolate(const PointList<2> &pts)
43  {
44  assert(pts.size() > P);
45 
46  // generate arclength parametrization
47  const int np = pts.size();
48  m_ipp.resize(np);
49  for (int i=1; i<np; ++i)
50  m_ipp[i] = m_ipp[i-1] + norm(pts[i] - pts[i-1]);
51  m_ipp /= m_ipp.back();
52  m_ipp.front() = 0.0;
53  m_ipp.back() = 1.0;
54  this->interpolate(m_ipp, pts);
55  return m_ipp;
56  }
57 
59  const Vector & interpolate(const Vector &u, const PointList<2> &pts)
60  {
61  assert(pts.size() > P);
62  assert(u.size() == pts.size());
63  m_ipp = u;
64  m_basis.init(P, m_ipp);
65 
66  // bandwidth
67  const int np = pts.size();
68  const int ku(P), kl(P);
69 
70  // setup system of equations
71  SVector<P+1> b;
72  Matrix bcf(2*kl+ku+1, np), rm(np, 2);
73  for (int i=0; i<np; i++) {
74  rm(i,0) = pts[i][0];
75  rm(i,1) = pts[i][1];
76  int span = m_basis.eval(m_ipp[i], b);
77  for (int j=0; j<P+1; j++) {
78  int col = span-P+j;
79  int row = kl+ku+i-col;
80  bcf(row, col) = b[j];
81  }
82  }
83 
84  // solve for new control points
85  int stat = banded_lu_solve(kl, ku, bcf, rm);
86  if (stat != 0)
87  throw Error("Lapack: LU solve failed in "
88  "UvSplineCurve::interpolate(), INFO = "+str(stat));
89 
90  m_uvc.resize(np);
91  for (int i=0; i<np; ++i)
92  m_uvc[i] = Vct2( rm(i,0), rm(i,1) );
93  return m_ipp;
94  }
95 
97  void reverse() {
99  }
100 
102  Vct2 uveval(Real tp) const {
103  SVector<P+1> b;
104  Vct2 q;
105  Real t = m_evalReverse ? (1.0 - tp) : tp;
106  uint span = m_basis.eval(t, b);
107  for (uint i=0; i<P+1; ++i)
108  q += b[i] * m_uvc[ span-P+i ];
109  q[0] = clamp(q[0], 0.0, 1.0);
110  q[1] = clamp(q[1], 0.0, 1.0);
111  return q;
112  }
113 
115  Vct2 uvderive(Real tp, uint k) const {
116  Vct2 qd;
117  Matrix fu(k+1, P+1);
118  Real t = m_evalReverse ? (1.0 - tp) : tp;
119  Real sgn = m_evalReverse ? -1.0 : 1.0;
120  uint span = m_basis.derive(t, k, fu);
121  for (uint i=0; i<P+1; ++i)
122  qd += sgn * fu(k,i) * m_uvc[ span-P+i ];
123  return qd;
124  }
125 
127  void uvtgline(Real tp, Vct2 &q, Vct2 &dq) const {
128  SMatrix<2,P+1> b;
129  Real t = m_evalReverse ? (1.0 - tp) : tp;
130  Real sgn = m_evalReverse ? -1.0 : 1.0;
131  uint span = m_basis.derive(t, b);
132  q = 0.0;
133  dq = 0.0;
134  for (uint i=0; i<P+1; ++i) {
135  const Vct2 & cp( m_uvc[ span-P+i ] );
136  q += b(0,i) * cp;
137  dq += sgn * b(1,i) * cp;
138  }
139  q[0] = clamp(q[0], 0.0, 1.0);
140  q[1] = clamp(q[1], 0.0, 1.0);
141  }
142 
143 protected:
144 
146  void splitSpline(Real u, UvSplineCurve<P> &chi) {
147  m_basis.split(u, m_uvc, chi.m_basis, chi.m_uvc);
148  }
149 
150 protected:
151 
154 
157 
159  Vector m_ipp;
160 
163 };
164 
165 #endif // UVSPLINECURVE_H
uint size() const
uint derive(Real u, SMatrix< K, N > &b) const
bool m_evalReverse
direction flag
Definition: uvsplinecurve.h:162
int banded_lu_solve(int kl, int ku, MatrixType &a, MatrixType &b)
Parameter-space spline curve with compile-time degree.
Definition: forward.h:220
Curve in (u,v) domain of parametric surface.
Definition: abstractuvcurve.h:31
const Vector & init(uint deg, uint nuk, const Real knots[], const int mtp[])
PointList< 2 > m_uvc
control points in (u,v) space
Definition: uvsplinecurve.h:156
Real eval(int i, Real u) const
void uvtgline(Real tp, Vct2 &q, Vct2 &dq) const
evaluate point and first derivative in (u,v) space
Definition: uvsplinecurve.h:127
Vct2 uvderive(Real tp, uint k) const
evaluate derivative in (u,v) space
Definition: uvsplinecurve.h:115
void reverse()
reverse direction
Definition: uvsplinecurve.h:97
const Vector & interpolate(const PointList< 2 > &pts)
contruct so that points are interpolated exactly
Definition: uvsplinecurve.h:42
const Vector & interpolate(const Vector &u, const PointList< 2 > &pts)
contruct so that points are interpolated exactly
Definition: uvsplinecurve.h:59
SplineBasis m_basis
spline basis
Definition: uvsplinecurve.h:153
void resize(uint n)
void split(Real u, CpArray &cpts, SplineBasis &hib, CpArray &hcp)
void splitSpline(Real u, UvSplineCurve< P > &chi)
split curve at u, assign right/high part to chi
Definition: uvsplinecurve.h:146
Vct2 uveval(Real tp) const
evaluate in (u,v) space
Definition: uvsplinecurve.h:102
Vector m_ipp
parameterization of the interpolated points
Definition: uvsplinecurve.h:159
Generated on Wed Jan 19 2022 03:03:15 for libsurf by   doxygen 1.8.5