libsurf
Programmer's Documentation

uvmapping.h (r6227/r5768)
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_UVMAPPING_H
16 #define SURF_UVMAPPING_H
17 
18 #include "forward.h"
19 #include <genua/splinebasis.h>
20 #include <genua/dmatrix.h>
21 #include <genua/smallqr.h>
22 
23 
34 class UvMapping
35 {
36 public:
37 
39  UvMapping() {}
40 
42  UvMapping(const Surface & srf, const Vector & up, const Vector & vp);
43 
45  void init(const Surface & srf, const Vector & up, const Vector & vp);
46 
48  void boundaries(Real & tmin, Real & tmax) const;
49 
51  bool empty() const {return (m_ctp.size() == 0);}
52 
54  void dump(const std::string &fname) const;
55 
57  Real eval(Real u, Real v) const {
58  SVector<SDEG+1> bu, bv;
59  const int uspan = m_ubas.eval(u, bu);
60  const int vspan = m_vbas.eval(v, bv);
61  Real t = 0.0;
62  for (int i=0; i<SDEG+1; ++i)
63  for (int j=0; j<SDEG+1; ++j)
64  t += bu[i] * bv[j] * m_ctp( uspan+i-SDEG, vspan+j-SDEG );
65  return t;
66  }
67 
69  Vct2 eval(const Vct2 & uv) const {
70  Real s = uv[0];
71  Real t = eval(uv[0], uv[1]);
72  return Vct2(s, t);
73  }
74 
76  Vct2 gradient(Real u, Real v) const {
77  SMatrix<2,SDEG+1> bu, bv;
78  const int uspan = m_ubas.derive(u, bu);
79  const int vspan = m_vbas.derive(v, bv);
80  Real tu(0.0), tv(0.0);
81  for (int i=0; i<SDEG+1; ++i) {
82  for (int j=0; j<SDEG+1; ++j) {
83  Real c = m_ctp( uspan+i-SDEG, vspan+j-SDEG );
84  tu += bu(1,i) * bv(0,j) * c;
85  tv += bu(0,i) * bv(1,j) * c;
86  }
87  }
88  return Vct2(tu,tv);
89  }
90 
92  Vct2 uvStep(const Vct2 & uv, const Vct2 & dst) const {
93  Vct2 duv, g = gradient(uv[0], uv[1]);
94  duv[0] = dst[0];
95  duv[1] = (dst[1] - duv[0]*g[0]) / g[1];
96  return duv;
97  }
98 
100  Vct2 invert(const Vct2 & st, Real tol = 1e-9) const;
101 
103  Vct2 lookup(const Vct2 &st) const;
104 
106  bool invert(const Vct2 & st, Vct2 & uv, Real tol = 1e-9) const;
107 
109  template <int ND>
110  static Vct2 mappingCriteria(const SVector<ND> &Su, const SVector<ND> &Sv) {
111  Real squ = sq(Su);
112  Real sqv = sq(Sv);
113  Real c1 = sqv / squ; assert(c1 > 0);
114  Real t1 = dot(Su,Sv);
115  Real c2 = sq(t1) / (squ*sqv); assert(c2 < 1);
116  Real cr1 = sign(t1) * sqrt(c2 / (1-c2));
117  Real cr2 = sqrt(c1 / (1-c2));
118  return Vct2( cr1, cr2 );
119  }
120 
122  void plane(const Surface &srf, Real u, Real v, Vct3 &Ss, Vct3 &St) const;
123 
125  void clear();
126 
127 private:
128 
130  static void downSample(const Vector & upp, Vector & up);
131 
133  static void upSample(const Vector &knots, Vector &p);
134 
136  void fitSpline(const Surface &srf);
137 
139  bool denseFitSpline(const Surface &srf);
140 
142  bool sparseFitSpline(const Surface &srf);
143 
145  bool lengthRatioMapping(const Surface &srf);
146 
148  void buildLookupTable();
149 
151  Real vlookup(Real t, uint jcol) const;
152 
153 private:
154 
155  enum {SDEG = 2};
156 
159 
161  Matrix m_ctp;
162 
164  Vector m_ucol;
165 
167  Vector m_vrow;
168 
170  Matrix m_tval;
171 
173  static uint s_max_neval;
174 };
175 
176 #endif // UVMAPPING_H
Vct2 uvStep(const Vct2 &uv, const Vct2 &dst) const
solve for step in uv for a given change in st
Definition: uvmapping.h:92
Matrix m_ctp
control points for forward mapping (u,v) -&gt; t
Definition: uvmapping.h:161
void init(const Surface &srf, const Vector &up, const Vector &vp)
construct mapping for surface srf avaluated at given parameter grid
Definition: uvmapping.cpp:86
uint derive(Real u, SMatrix< K, N > &b) const
void boundaries(Real &tmin, Real &tmax) const
determine min/max boundaries for t
Definition: uvmapping.cpp:37
static Vct2 mappingCriteria(const SVector< ND > &Su, const SVector< ND > &Sv)
compute the mapping criteria from surface derivatives
Definition: uvmapping.h:110
Parameter-space mapping.
Definition: uvmapping.h:34
Vct2 eval(const Vct2 &uv) const
convenience interface
Definition: uvmapping.h:69
UvMapping()
undefined mapping
Definition: uvmapping.h:39
bool sparseFitSpline(const Surface &srf)
use Tim Davis&#39; sparse QR for spline fitting
Definition: uvmapping.cpp:528
Vct2 lookup(const Vct2 &st) const
obtain an initial guess for the inverse mapping (s,t) -&gt; (u,v)
Definition: uvmapping.cpp:326
void buildLookupTable()
generate the inverse mapping
Definition: uvmapping.cpp:296
SplineBasis m_ubas
spline basis for scalar 2D mapping
Definition: uvmapping.h:158
Vct2 invert(const Vct2 &st, Real tol=1e-9) const
compute (u,v) for known (s,t) without initial guess
Definition: uvmapping.cpp:342
static uint s_max_neval
used to limit the number of evaluation points for map definition
Definition: uvmapping.h:173
void dump(const std::string &fname) const
dump mapping to file (debugging)
Definition: uvmapping.cpp:533
bool denseFitSpline(const Surface &srf)
use dense QR from LAPACK for spline fitting
Definition: uvmapping.cpp:144
Real eval(int i, Real u) const
bool empty() const
check whether mapping is initialized
Definition: uvmapping.h:51
Real eval(Real u, Real v) const
evaluate mapping t &lt;- (u,v) while implicitly, s &lt;- u
Definition: uvmapping.h:57
static void upSample(const Vector &knots, Vector &p)
utility to generate sampling vector from knots
Definition: uvmapping.cpp:66
void plane(const Surface &srf, Real u, Real v, Vct3 &Ss, Vct3 &St) const
compute derivatives of surface coordinates S w.r.t. s,t
Definition: uvmapping.cpp:376
Vector m_ucol
support for inverse lookup : spacing in s/u (columns of m_tval)
Definition: uvmapping.h:164
static void downSample(const Vector &upp, Vector &up)
down-sample map evaluation grid
Definition: uvmapping.cpp:48
Surface interface.
Definition: surface.h:37
Vector m_vrow
support for inverse lookup : spacing in v (rows of m_tval)
Definition: uvmapping.h:167
Matrix m_tval
values of t for inverse lookup (s,t) -&gt; v
Definition: uvmapping.h:170
Real vlookup(Real t, uint jcol) const
lookup a value of t in column j, return interpolated v
Definition: uvmapping.cpp:309
void clear()
clear data
Definition: uvmapping.cpp:558
void fitSpline(const Surface &srf)
solve least-squares problem, assumes spline bases are initialized
Definition: uvmapping.cpp:127
Vct2 gradient(Real u, Real v) const
evaluate derivatives of mapping t &lt;- (u,v)
Definition: uvmapping.h:76
bool lengthRatioMapping(const Surface &srf)
generate a constant length-based mapping, ignore skew (fallback)
Definition: uvmapping.cpp:243
Generated on Wed Jan 19 2022 03:03:15 for libsurf by   doxygen 1.8.5