ldpk
tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled.h
Go to the documentation of this file.
1 #ifndef tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled_sdv
2 #define tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled_sdv
3 
4 #include <ldpk/ldpk_ldp_builtin.h>
5 #include <ldpk/ldpk_generic_anamorphic_distortion.h>
6 #include <ldpk/ldpk_rotation_extender.h>
7 #include <ldpk/ldpk_squeeze_extender.h>
8 #include <ldpk/ldpk_linear_extender.h>
9 
10 #ifndef M_PI
11 #define M_PI 3.1415926535
12 #endif
13 
17 
18 template <class VEC2,class MAT2>
20  {
21 private:
22  typedef VEC2 vec2_type;
23  typedef MAT2 mat2_type;
25 
26 // Wir kombinieren hier anamorphische Verzeichung Grad 4
27 // mit einer Rotation und einem Squeeze. Das ist der anamorphe Anteil
33 
35  ldpk::linear_extender<vec2_type,mat2_type> _rot_sqx_sqy_rescale_pa;
37 
38  static const char* _para[14];
39 
40  bool decypher(const char* name,int& i)
41  {
42  typedef base_type bt;
43  int n;
44  getNumParameters(n);
45  for(i = 0;i < n;++i)
46  {
47  if(0 == strcmp(name,_para[i]))
48  {
49  return true;
50  }
51  }
52  return false;
53  }
54  bool initializeParameters()
55  {
56  typedef base_type bt;
57  bt::check_builtin_parameters();
58  _pa.set_sq(bt::pa());
59 // This method is the last one invoked, before the object can be used,
60 // therefore we have to prepare the concatenated extenders here.
61  _rot_sqx_sqy_rescale_pa.set(_rotation,_squeeze_x,_squeeze_y,_rescale,_pa);
62  if(_squeeze_x.get_sq() == 0)
63  { std::cerr << "tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled::initializeParameters, error: Squeeze-X is 0." << std::endl; }
64  if(_squeeze_y.get_sq() == 0)
65  { std::cerr << "tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled::initializeParameters, error: Squeeze-Y is 0." << std::endl; }
66  _pa_rescale_rot.set(_pa,_rescale,_rotation);
67  _anamorphic.prepare();
68  return true;
69  }
70  bool getNumParameters(int& n)
71  {
72  n = 14;
73  return true;
74  }
75  bool getParameterName(int i,char* identifier)
76  {
77  strcpy(identifier,_para[i]);
78  return true;
79  }
80  bool setParameterValue(const char *identifier,double v)
81  {
82  typedef base_type bt;
83  int i;
84 // Does the base class know the parameter?
85  if(bt::set_builtin_parameter_value(identifier,v))
86  {
87  return true;
88  }
89  if(!decypher(identifier,i))
90  {
91  return false;
92  }
93  if(i < 10)
94  {
95  if(_anamorphic.get_coeff(i) != v)
96  { bt::no_longer_uptodate_lut(); }
97  _anamorphic.set_coeff(i,v);
98  }
99  else if(i == 10)
100  {
101  if(_rotation.get_phi() != v / 180.0 * M_PI)
102  { bt::no_longer_uptodate_lut(); }
103  _rotation.set_phi(v / 180.0 * M_PI);
104  }
105  else if(i == 11)
106  {
107  if(_squeeze_x.get_sq() != v)
108  { bt::no_longer_uptodate_lut(); }
109  _squeeze_x.set_sq(v);
110  }
111  else if(i == 12)
112  {
113  if(_squeeze_y.get_sq() != v)
114  { bt::no_longer_uptodate_lut(); }
115  _squeeze_y.set_sq(v);
116  }
117  else if(i == 13)
118  {
119  if(_rescale.get_sq() != v)
120  { bt::no_longer_uptodate_lut(); }
121  _rescale.set_sq(v);
122  }
123 
124  return true;
125  }
126 // slightly faster version
127  virtual bool undistort(double x0,double y0,double &x1,double &y1)
128  {
129  typedef base_type bt;
130  vec2_type q = bt::map_dn_to_unit(
131  _rot_sqx_sqy_rescale_pa.eval(
132  _anamorphic.eval(
133  _pa_rescale_rot.eval_inv(
134  bt::map_unit_to_dn(vec2_type(x0,y0))))));
135  x1 = q[0];
136  y1 = q[1];
137  return true;
138  }
139 /*
140 // original version
141  virtual bool undistort(double x0,double y0,double &x1,double &y1)
142  {
143  vec2_type q = bt::map_dn_to_unit(
144  _rotation.eval(
145  _squeeze_x.eval(
146  _squeeze_y.eval(
147  _rescale.eval(
148  _anamorphic.eval(
149  _rotation.eval_inv(
150  _rescale.eval_inv(
151  bt::map_unit_to_dn(vec2_type(x0,y0))))))))));
152  x1 = q[0];
153  y1 = q[1];
154  return true;
155  }
156 */
157  virtual bool distort(double x0,double y0,double &x1,double &y1)
158  {
159  typedef base_type bt;
160 // The distort-method without initial values is not constant by semantics,
161 // since it may cause an update of the lookup-tables. Implementing a Nuke node
162 // it turned out that we need to prevent threads from trying so simultaneously.
163 // By the following double check of is_uptodate_lut() we keep the mutex lock
164 // out of our frequently called distort stuff (for performance reasons) and
165 // prevent threads from updating without need.
166  if(!bt::is_uptodate_lut())
167  {
168  bt::lock();
169  if(!bt::is_uptodate_lut())
170  {
171  bt::update_lut();
172  }
173  bt::unlock();
174  }
175 
176 // Get initial value from lookup-table
177  vec2_type qs = bt::get_lut().get_initial_value(vec2_type(x0,y0));
178 // vec2_type qs(x0,y0);
179 // Call version of distort with initial value.
180  vec2_type q = bt::map_dn_to_unit(
181  _pa_rescale_rot.eval(
182  _anamorphic.map_inverse(
183  _rot_sqx_sqy_rescale_pa.eval_inv(
184  bt::map_unit_to_dn(vec2_type(x0,y0))),
185  _rot_sqx_sqy_rescale_pa.eval_inv(
186  bt::map_unit_to_dn(qs)))));
187  x1 = q[0];
188  y1 = q[1];
189  return true;
190  }
191 /*
192 // original version
193  virtual bool distort(double x0,double y0,double &x1,double &y1)
194  {
195  typedef base_type bt;
196 // The distort-method without initial values is not constant by semantics,
197 // since it may cause an update of the lookup-tables. Implementing a Nuke node
198 // it turned out that we need to prevent threads from trying so simultaneously.
199 // By the following double check of is_uptodate_lut() we keep the mutex lock
200 // out of our frequently called distort stuff (for performance reasons) and
201 // prevent threads from updating without need.
202  if(!bt::is_uptodate_lut())
203  {
204  pthread_mutex_lock(&_mutex);
205  if(!bt::is_uptodate_lut())
206  {
207  bt::update_lut();
208  }
209  pthread_mutex_unlock(&_mutex);
210  }
211 
212 // Get initial value from lookup-table
213  vec2_type qs = bt::get_lut().get_initial_value(vec2_type(x0,y0));
214 // vec2_type qs(x0,y0);
215 // Call version of distort with initial value.
216  vec2_type q = bt::map_dn_to_unit(
217  _rescale.eval(
218  _rotation.eval(
219  _anamorphic.map_inverse(
220  _rescale.eval_inv(
221  _squeeze_y.eval_inv(
222  _squeeze_x.eval_inv(
223  _rotation.eval_inv(
224  bt::map_unit_to_dn(vec2_type(x0,y0)))))),
225  _rescale.eval_inv(
226  _squeeze_y.eval_inv(
227  _squeeze_x.eval_inv(
228  _rotation.eval_inv(
229  bt::map_unit_to_dn(qs)))))))));
230  x1 = q[0];
231  y1 = q[1];
232  return true;
233  }
234 */
235  virtual bool distort(double x0,double y0,double x1_start,double y1_start,double &x1,double &y1)
236  {
237  typedef base_type bt;
238  vec2_type q = bt::map_dn_to_unit(
239  _pa_rescale_rot.eval(
240  _anamorphic.map_inverse(
241  _rot_sqx_sqy_rescale_pa.eval_inv(
242  bt::map_unit_to_dn(vec2_type(x0,y0))),
243  _rot_sqx_sqy_rescale_pa.eval_inv(
244  bt::map_unit_to_dn(vec2_type(x1_start,y1_start))))));
245  x1 = q[0];
246  y1 = q[1];
247  return true;
248  }
249 /*
250 // original version
251  virtual bool distort(double x0,double y0,double x1_start,double y1_start,double &x1,double &y1)
252  {
253  vec2_type q = bt::map_dn_to_unit(
254  _rescale.eval(
255  _rotation.eval(
256  _anamorphic.map_inverse(
257  _rescale.eval_inv(
258  _squeeze_y.eval_inv(
259  _squeeze_x.eval_inv(
260  _rotation.eval_inv(
261  bt::map_unit_to_dn(vec2_type(x0,y0)))))),
262  _rescale.eval_inv(
263  _squeeze_y.eval_inv(
264  _squeeze_x.eval_inv(
265  _rotation.eval_inv(
266  bt::map_unit_to_dn(vec2_type(x1_start,y1_start))))))))));
267  x1 = q[0];
268  y1 = q[1];
269  return true;
270  }
271 */
272 public:
273 // Mutex initialized and destroyed in baseclass.
275  { }
277  { }
278  bool getModelName(char *name)
279  {
280 #ifdef LDPK_COMPILE_AS_PLUGIN_SDV
281  strcpy(name,"3DE4 Anamorphic - Rescaled, Degree 4 [Plugin]");
282 #else
283  strcpy(name,"3DE4 Anamorphic - Rescaled, Degree 4");
284 #endif
285  return true;
286  }
287  bool getParameterType(const char* identifier,tde4_ldp_ptype& ptype)
288  {
289  typedef base_type bt;
290  int i;
291  if(bt::get_builtin_parameter_type(identifier,ptype)) return true;
292  if(!decypher(identifier,i)) return false;
293  ptype = TDE4_LDP_ADJUSTABLE_DOUBLE;
294  return true;
295  }
296  bool getParameterDefaultValue(const char* identifier,double& v)
297  {
298  typedef base_type bt;
299  int i;
300  if(!decypher(identifier,i)) return false;
301  if(i < 11)
302  {
303  v = 0.0;
304  }
305  else if((i == 11) || (i == 12))
306  {
307  v = 1.0;
308  }
309  else if(i == 13)
310  {
311  v = 1.0;
312  }
313  return true;
314  }
315  bool getParameterRange(const char* identifier,double& a,double& b)
316  {
317  typedef base_type bt;
318  int i;
319  if(!decypher(identifier,i)) return false;
320  if(i < 10)
321  {
322  a = -0.5;
323  b = 0.5;
324  }
325  else if(i == 10)
326  {
327 // Lens Rotation in degree.
328  a = -2.0;
329  b = +2.0;
330  }
331  else if((i == 11) || (i == 12))
332  {
333 // Squeeze X/Y
334  a = 0.9;
335  b = 1.1;
336  }
337  else if(i == 13)
338  {
339  a = 0.25;
340  b = 4.0;
341  }
342  else
343  {
344  std::cerr << "getParameterRange: i out of range" << std::endl;
345  }
346  return true;
347  }
349  bool getJacobianMatrix(double x0,double y0,double& m00,double& m01,double& m10,double& m11)
350  {
351  typedef base_type bt;
352  mat2_type m = _rot_sqx_sqy_rescale_pa.get_mat()
353  * _anamorphic.jacobi(
354  _pa_rescale_rot.eval_inv(
355  bt::map_unit_to_dn(vec2_type(x0,y0))))
356  * _pa_rescale_rot.get_mat_inv();
357  mat2_type u2d(bt::w_fb_cm() / bt::r_fb_cm(),0.0,0.0,bt::h_fb_cm() / bt::r_fb_cm());
358  mat2_type d2u(bt::r_fb_cm() / bt::w_fb_cm(),0.0,0.0,bt::r_fb_cm() / bt::h_fb_cm());
359  m = d2u * m * u2d;
360  m00 = m[0][0];m01 = m[0][1];m10 = m[1][0];m11 = m[1][1];
361  return true;
362  }
363  };
364 
365 template <class VEC2,class MAT2>
367  "Cx02 - Degree 2","Cy02 - Degree 2",
368  "Cx22 - Degree 2","Cy22 - Degree 2",
369 
370  "Cx04 - Degree 4","Cy04 - Degree 4",
371  "Cx24 - Degree 4","Cy24 - Degree 4",
372  "Cx44 - Degree 4","Cy44 - Degree 4",
373 
374  "Lens Rotation","Squeeze-X","Squeeze-Y",
375  "Rescale"
376  };
377 
378 #endif
bool getParameterRange(const char *identifier, double &a, double &b)
returns range for adjustable double parameters...
Definition: tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled.h:315
virtual mat2_type jacobi(const vec2_type &p_dn) const
Jacobi-Matrix. The result is a matrix g_{ij} = d/dp_j f(p)_i, where f represents the undistort-functi...
Definition: ldpk_generic_distortion_base.h:109
vec2_type eval(const vec2_type &p) const
eval() is per definition removal of lens distortion (undistort).
Definition: ldpk_linear_extender.h:65
vec2_type eval(const vec2_type &p) const
Same as method instead of operator.
Definition: ldpk_generic_distortion_base.h:104
void set_coeff(int i, double q)
Set coefficient as demanded by base class.
Definition: ldpk_generic_anamorphic_distortion.h:77
double get_phi() const
Getter.
Definition: ldpk_rotation_extender.h:36
bool getModelName(char *name)
returns a name for the model as to show up in the GUI (maximum length of "name": 100 bytes)...
Definition: tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled.h:278
const mat2_type & get_mat_inv() const
The inverse matrix for this extender.
Definition: ldpk_linear_extender.h:77
void set(const mat2_type &m)
Passing the matrix.
Definition: ldpk_linear_extender.h:24
bool getParameterType(const char *identifier, tde4_ldp_ptype &ptype)
returns type of given parameter... The method should return false, if the parameter addressed by iden...
Definition: tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled.h:287
Definition: tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled.h:19
This class handles the built-in parameter and the lookup table. You may find it useful for your own d...
Definition: ldpk_ldp_builtin.h:31
const mat2_type & get_mat() const
The matrix for this extender.
Definition: ldpk_linear_extender.h:74
virtual vec2_type map_inverse(const vec2_type &q) const
Inverse mapping by solving the fixed point equation without providing initial values. Virtual, because the derived class might use some smart data structure for calculating an initial value.
Definition: ldpk_generic_distortion_base.h:122
bool getJacobianMatrix(double x0, double y0, double &m00, double &m01, double &m10, double &m11)
Tested against difference quotients.
Definition: tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled.h:349
vec2_type eval_inv(const vec2_type &q) const
eval_inv() is applying lens distortion (distort)
Definition: ldpk_linear_extender.h:68
double get_coeff(int i) const
Get coefficient as demanded by base class.
Definition: ldpk_generic_anamorphic_distortion.h:71
bool getParameterDefaultValue(const char *identifier, double &v)
returns default value for given parameter (maximum length of "char *v": 1000 bytes)......
Definition: tde4_ldp_anamorphic_deg_4_rotate_squeeze_xy_rescaled.h:296
void set_phi(double phi)
The rotation extender has one parameter called phi (in radians).
Definition: ldpk_rotation_extender.h:29