1 #ifndef ldp_builtin_sdv 2 #define ldp_builtin_sdv 5 #include <ldpk/tde4_ld_plugin.h> 15 para_not_a_builtin = -1,
16 para_focal_length = 0,
17 para_filmback_width = 1,
18 para_filmback_height = 2,
19 para_lens_center_offset_x = 3,
20 para_lens_center_offset_y = 4,
21 para_pixel_aspect = 5,
22 para_custom_focus_distance = 6
30 template <
class VEC2,
class MAT2>
34 typedef VEC2 vec2_type;
35 typedef MAT2 mat2_type;
39 static const char* _para_builtin[7];
41 double _value_builtin[7];
72 static int compute_extremal_parameters_of_spline(
const vec2_type& qa,
const vec2_type& qb,
73 const vec2_type& dqa,
const vec2_type& dqb,
77 vec2_type c = -3.0 * qa + 3.0 * qb - 2.0 * dqa - dqb;
78 vec2_type d = 2.0 * qa - 2.0 * qb + dqa + dqb;
82 for(
int i = 0;i < 2;++i)
86 double d_mul_3 = 3.0 * d[i];
87 disc[i] = (c[i] * c[i]) / (d_mul_3 * d_mul_3) - b[i] / d_mul_3;
92 double t,r = sqrt(disc[i]);
93 t = -c[i] / d_mul_3 + r;
94 if((t > 0.0) && (t < 1.0))
95 { t_out[i_out++] = t; }
96 t = -c[i] / d_mul_3 - r;
97 if((t > 0.0) && (t < 1.0))
98 { t_out[i_out++] = t; }
104 double t = b[i] / (-2.0 * c[i]);
105 if((t > 0.0) && (t < 1.0))
106 { t_out[i_out++] = t; }
111 template <
int I>
void extend_box_by_spline(
const vec2_type& pa,
const vec2_type& pb,
112 const vec2_type& qa,
const vec2_type& qb,
113 const mat2_type& ja,
const mat2_type& jb,
117 vec2_type dqa = ja * (pb - pa);
118 vec2_type dqb = jb * (pb - pa);
119 int n_out = compute_extremal_parameters_of_spline(qa,qb,dqa,dqb,t_out);
120 for(
int i = 0;i < n_out;++i)
122 if((t_out[i] <= 0.0) || (t_out[i] >= 1.0))
123 { std::cerr <<
"ldpk::ldp_builtin: extend_box_by_spline: implementation error, needs fix." << std::endl; }
125 pt = (1.0 - t_out[i]) * pa + t_out[i] * pb;
126 qt = apply_mapping<I>(pt);
131 template <
int I> mat2_type get_jacobian_matrix(
const vec2_type& p,
const vec2_type& q)
147 template <
int I> vec2_type apply_mapping(
const vec2_type& p)
153 { distort(p[0],p[1],q[0],q[1]); }
156 template <
int I>
void get_bounding_box(
double xa_in,
double ya_in,
double xb_in,
double yb_in,
157 double& xa_out,
double& ya_out,
double& xb_out,
double& yb_out,
160 double dx_in = xb_in - xa_in,dy_in = yb_in - ya_in;
167 pb = vec2_type(xa_in,ya_in);
168 qb = apply_mapping<I>(pb);
169 jb = get_jacobian_matrix<I>(pb,qb);
171 for(
int ix = 1;ix <= nx;++ix)
173 double x_in = xa_in + dx_in * double(ix) / nx;
176 pa = pb;qa = qb;ja = jb;
177 pb = vec2_type(x_in,y_in);
178 qb = apply_mapping<I>(pb);
179 jb = get_jacobian_matrix<I>(pb,qb);
180 extend_box_by_spline<I>(pa,pb,qa,qb,ja,jb,bbox);
184 pb = vec2_type(xa_in,yb_in);
185 qb = apply_mapping<I>(pb);
186 jb = get_jacobian_matrix<I>(pb,qb);
188 for(
int ix = 1;ix <= nx;++ix)
190 double x_in = xa_in + dx_in * double(ix) / nx;
193 pa = pb;qa = qb;ja = jb;
194 pb = vec2_type(x_in,y_in);
195 qb = apply_mapping<I>(pb);
196 jb = get_jacobian_matrix<I>(pb,qb);
197 extend_box_by_spline<I>(pa,pb,qa,qb,ja,jb,bbox);
201 pb = vec2_type(xa_in,ya_in);
202 qb = apply_mapping<I>(pb);
203 jb = get_jacobian_matrix<I>(pb,qb);
205 for(
int iy = 1;iy <= ny;++iy)
208 double y_in = ya_in + dy_in * double(iy) / ny;
210 pa = pb;qa = qb;ja = jb;
211 pb = vec2_type(x_in,y_in);
212 qb = apply_mapping<I>(pb);
213 jb = get_jacobian_matrix<I>(pb,qb);
214 extend_box_by_spline<I>(pa,pb,qa,qb,ja,jb,bbox);
218 pb = vec2_type(xb_in,ya_in);
219 qb = apply_mapping<I>(pb);
220 jb = get_jacobian_matrix<I>(pb,qb);
222 for(
int iy = 1;iy <= ny;++iy)
225 double y_in = ya_in + dy_in * double(iy) / ny;
227 pa = pb;qa = qb;ja = jb;
228 pb = vec2_type(x_in,y_in);
229 qb = apply_mapping<I>(pb);
230 jb = get_jacobian_matrix<I>(pb,qb);
231 extend_box_by_spline<I>(pa,pb,qa,qb,ja,jb,bbox);
234 xa_out = bbox.a()[0];
235 ya_out = bbox.a()[1];
236 xb_out = bbox.b()[0];
237 yb_out = bbox.b()[1];
244 pthread_mutex_t _mutex;
253 {
return _uptodate_lut; }
257 { _uptodate_lut =
false; }
260 { _uptodate_lut = u; }
279 if(find_builtin_parameter_by_name(identifier) != para_not_a_builtin)
281 ptype = TDE4_LDP_DOUBLE;
293 { std::cerr <<
"ldp_builtin: filmback width is 0." << std::endl; }
295 { std::cerr <<
"ldp_builtin: filmback height is 0." << std::endl; }
297 { std::cerr <<
"ldp_builtin: focal length is 0." << std::endl; }
299 { std::cerr <<
"ldp_builtin: pixel aspect is 0." << std::endl; }
300 _r_fb_cm = sqrt(w_fb_cm() * w_fb_cm() + h_fb_cm() * h_fb_cm()) / 2.0;
303 vec2_type map_unit_to_dn(
const vec2_type& p_unit)
const 305 vec2_type p_cm((p_unit[0] - 1.0/2.0) * w_fb_cm() - x_lco_cm(),(p_unit[1] - 1.0/2.0) * h_fb_cm() - y_lco_cm());
306 return p_cm / r_fb_cm();
308 vec2_type map_dn_to_unit(
const vec2_type& p_dn)
const 310 vec2_type p_cm(p_dn * r_fb_cm());
311 p_cm += vec2_type(w_fb_cm() / 2 + x_lco_cm(),h_fb_cm() / 2 + y_lco_cm());
312 return vec2_type(p_cm[0] / w_fb_cm(),p_cm[1] / h_fb_cm());
317 EnterCriticalSection(&_critsec);
319 pthread_mutex_lock(&this->_mutex);
325 LeaveCriticalSection(&_critsec);
327 pthread_mutex_unlock(&this->_mutex);
334 InitializeCriticalSection(&_critsec);
336 int r = pthread_mutex_init(&_mutex,NULL);
338 { std::cerr <<
"ldpk::ldp_builtin::pthread_mutex_init: " << strerror(r) << std::endl; }
343 { _value_builtin[i] = 0; }
348 DeleteCriticalSection(&_critsec);
350 int r = pthread_mutex_destroy(&_mutex);
352 { std::cerr <<
"ldpk::ldp_builtin::pthread_mutex_destroy: " << strerror(r) << std::endl; }
358 double w_fb_cm()
const 359 {
return _value_builtin[para_filmback_width]; }
360 double h_fb_cm()
const 361 {
return _value_builtin[para_filmback_height]; }
362 double x_lco_cm()
const 363 {
return _value_builtin[para_lens_center_offset_x]; }
364 double y_lco_cm()
const 365 {
return _value_builtin[para_lens_center_offset_y]; }
367 {
return _value_builtin[para_focal_length]; }
369 {
return _value_builtin[para_pixel_aspect]; }
371 {
return _value_builtin[para_custom_focus_distance]; }
373 double r_fb_cm()
const 384 virtual bool distort_gnomonic(
double x0,
double y0,
double &x1,
double &y1)
385 {
return distort(x0,y0,x1,y1); }
386 virtual bool distort_gnomonic(
double x0,
double y0,
double x1_start ,
double y1_start,
double &x1,
double &y1)
387 {
return distort(x0,y0,x1_start,y1_start,x1,y1); }
391 virtual void getBoundingBoxUndistort(
double xa_in,
double ya_in,
double xb_in,
double yb_in,
double& xa_out,
double& ya_out,
double& xb_out,
double& yb_out,
int nx,
int ny)
392 { get_bounding_box<1>(xa_in,ya_in,xb_in,yb_in,xa_out,ya_out,xb_out,yb_out,nx,ny); }
393 virtual void getBoundingBoxDistort(
double xa_in,
double ya_in,
double xb_in,
double yb_in,
double& xa_out,
double& ya_out,
double& xb_out,
double& yb_out,
int nx,
int ny)
394 { get_bounding_box<0>(xa_in,ya_in,xb_in,yb_in,xa_out,ya_out,xb_out,yb_out,nx,ny); }
399 template <
class VEC2,
class MAT2>
401 "tde4_focal_length_cm",
402 "tde4_filmback_width_cm",
403 "tde4_filmback_height_cm",
404 "tde4_lens_center_offset_x_cm",
405 "tde4_lens_center_offset_y_cm",
407 "tde4_custom_focus_distance_cm" 409 template <
class VEC2,
class MAT2>
414 if(strcmp(_para_builtin[i],identifier) == 0)
419 return para_not_a_builtin;
421 template <
class VEC2,
class MAT2>
424 int i = find_builtin_parameter_by_name(identifier);
427 if(_value_builtin[i] != v)
431 _value_builtin[i] = v;
435 template <
class VEC2,
class MAT2>
445 distort_gnomonic(p[0],p[1],qs[0],qs[1],q[0],q[1]);
virtual bool undistort(double x0, double y0, double &x1, double &y1)=0
warp/unwarp 2D points...
virtual void getBoundingBoxDistort(double xa_in, double ya_in, double xb_in, double yb_in, double &xa_out, double &ya_out, double &xb_out, double &yb_out, int nx, int ny)
Iterate around the specified box, distort the points and compute the bounding box.
Definition: ldpk_ldp_builtin.h:393
bool next()
False means no value to be generated, loop is done. See example.
Definition: ldpk_lookup_table.h:203
virtual void update_lut()
Update the lookup table. The derived class will do this, if necessary, when distort() without initial...
Definition: ldpk_ldp_builtin.h:436
void no_longer_uptodate_lut()
This class and the derived class mark the lookup table as obsolete when some parameter was changed...
Definition: ldpk_ldp_builtin.h:256
int get_num_builtin_parameters() const
There are seven built-in parameters.
Definition: ldpk_ldp_builtin.h:271
virtual void getBoundingBoxUndistort(double xa_in, double ya_in, double xb_in, double yb_in, double &xa_out, double &ya_out, double &xb_out, double &yb_out, int nx, int ny)
Bounding box.
Definition: ldpk_ldp_builtin.h:391
CRITICAL_SECTION _critsec
The Mutex, used in derived classes, initialized in constructor.
Definition: ldpk_ldp_builtin.h:242
The namespace of (most of the) things related to the Lens Distortion Plugin Kit.
Definition: ldpk.h:19
A simple box class for double precision points in 2d. We will extend this as needed.
Definition: ldpk_vec2d.h:192
Lens Distortion Plugin Base Class.
Definition: tde4_ld_plugin.h:29
void reset()
Use reset() and next() to build a loop for generating positions. See example.
Definition: ldpk_lookup_table.h:191
para_builtin_enum
An enum-type for built-in parameters. Also used to address elements in parameter name array...
Definition: ldpk_ldp_builtin.h:13
void init(int nx, int ny)
Initialize a grid with nx * ny sample points. Implies reset().
Definition: ldpk_lookup_table.h:72
void set_uptodate_lut(bool u)
If a derived class computes the LUT, it must have write-access to this flag.
Definition: ldpk_ldp_builtin.h:259
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
void check_builtin_parameters()
This method should be invoked by the derived classes in initializeParameters(). It write error messag...
Definition: ldpk_ldp_builtin.h:290
vec2_type get_current_initial_value() const
Appropriate initial value for calculating warp at current position obtained with get_p_current_fov()...
Definition: ldpk_lookup_table.h:235
vec2_type get_p_current_fov() const
Current position in normalized FOV-coordinates. This is the position you want to warp next...
Definition: ldpk_lookup_table.h:109
const ldpk::lookup_table< vec2_type > & get_lut() const
The derived class has constant access, since it needs to consult it.
Definition: ldpk_ldp_builtin.h:265
virtual bool undistort_gnomonic(double x0, double y0, double &x1, double &y1)
Model function Our base class presents the model functions along with the projection. Internally, we have to carefully keep apart the model function and the projection. For most distortion models, this is no problem, yet for fisheye models it is, so all non-gnomonic distortion classes will have to overwrite these.
Definition: ldpk_ldp_builtin.h:382
Double-valued vector and matrix class.
virtual bool getJacobianMatrix(double x0, double y0, double &m00, double &m01, double &m10, double &m11)
calculate the Jacobian matrix of the undistort()-Method. Overwrite this, if you know the Jacobian for...
Definition: tde4_ld_plugin.h:132
bool set_builtin_parameter_value(const char *identifier, double v)
The return values indicates, if 'identifier' refers to a built-in parameter.
Definition: ldpk_ldp_builtin.h:422
void set_q_current_fov(const vec2_type &q_fov)
Set value for given index pair. Index pairs out of bounds will raise an exception. Use this method to insert the warped position from warping p_current_fov.
Definition: ldpk_lookup_table.h:115
bool get_builtin_parameter_type(const char *identifier, tde4_ldp_ptype &ptype) const
The return values indicates, if 'identifier' refers to a built-in parameter. Currently, all built-in parameters are double-valued.
Definition: ldpk_ldp_builtin.h:277
bool is_uptodate_lut() const
The derived class uses this in order to check if the lookup table needs an update.
Definition: ldpk_ldp_builtin.h:252