2 #ifndef vpgl_camera_convert_cxx_ 3 #define vpgl_camera_convert_cxx_ 13 # include <vcl_msvc_warnings.h> 39 static std::vector<double>
40 pvector(
const double x,
const double y,
const double z)
43 std::vector<double> pv(20);
67 static std::vector<double>
68 power_vector_dx(
const double x,
const double y,
const double z)
71 std::vector<double> pv(20, 0.0);
85 static std::vector<double>
86 power_vector_dy(
const double x,
const double y,
const double z)
89 std::vector<double> pv(20, 0.0);
103 static std::vector<double>
104 power_vector_dz(
const double x,
const double y,
const double z)
107 std::vector<double> pv(20, 0.0);
122 static double pval(std::vector<double>
const& pv, std::vector<double>
const& coef)
125 for (
unsigned i = 0; i<20; ++i)
126 sum += pv[i]*coef[i];
137 double x0 = world_center.
x(), y0 = world_center.
y(), z0 = world_center.
z();
147 std::vector<std::vector<double> > coeffs = rat_cam.
coefficients();
148 std::vector<double> neu_u = coeffs[0];
149 std::vector<double> den_u = coeffs[1];
150 std::vector<double> neu_v = coeffs[2];
151 std::vector<double> den_v = coeffs[3];
155 double dmax_u = nmax_u, nmax_v = nmax_u,dmax_v = nmax_u;
156 for (
unsigned i = 0; i<20; ++i)
158 if (std::fabs(neu_u[i])>nmax_u)
159 nmax_u=std::fabs(neu_u[i]);
160 if (std::fabs(den_u[i])>dmax_u)
161 dmax_u=std::fabs(den_u[i]);
162 if (std::fabs(neu_v[i])>nmax_v)
163 nmax_v=std::fabs(neu_v[i]);
164 if (std::fabs(den_v[i])>dmax_v)
165 dmax_v=std::fabs(den_v[i]);
168 double norm_u = nmax_u, norm_v = nmax_v;
169 if (norm_u<dmax_u) norm_u = dmax_u;
170 if (norm_v<dmax_v) norm_v = dmax_v;
171 for (
unsigned i = 0; i<20; ++i)
173 neu_u[i] /= norm_u; den_u[i] /= norm_u;
174 neu_v[i] /= norm_v; den_v[i] /= norm_v;
179 std::vector<double> pv0 = pvector(nx0,ny0,nz0);
180 double neu_u_0 = pval(pv0,neu_u), den_u_0 = pval(pv0,den_u);
181 double neu_v_0 = pval(pv0,neu_v), den_v_0 = pval(pv0,den_v);
182 std::vector<double> pv_dx0 = power_vector_dx(nx0,ny0,nz0);
183 double neu_u_dx0 = pval(pv_dx0,neu_u), den_u_dx0 = pval(pv_dx0,den_u);
184 double neu_v_dx0 = pval(pv_dx0,neu_v), den_v_dx0 = pval(pv_dx0,den_v);
185 std::vector<double> pv_dy0 = power_vector_dy(nx0,ny0,nz0);
186 double neu_u_dy0 = pval(pv_dy0,neu_u), den_u_dy0 = pval(pv_dy0,den_u);
187 double neu_v_dy0 = pval(pv_dy0,neu_v), den_v_dy0 = pval(pv_dy0,den_v);
188 std::vector<double> pv_dz0 = power_vector_dz(nx0,ny0,nz0);
189 double neu_u_dz0 = pval(pv_dz0,neu_u), den_u_dz0 = pval(pv_dz0,den_u);
190 double neu_v_dz0 = pval(pv_dz0,neu_v), den_v_dz0 = pval(pv_dz0,den_v);
194 C[0][0]=neu_u_dx0; C[0][1]=neu_u_dy0; C[0][2]=neu_u_dz0; C[0][3]=neu_u_0;
195 C[1][0]=den_u_dx0; C[1][1]=den_u_dy0; C[1][2]=den_u_dz0; C[1][3]=den_u_0;
196 C[2][0]=neu_v_dx0; C[2][1]=neu_v_dy0; C[2][2]=neu_v_dz0; C[2][3]=neu_v_0;
197 C[3][0]=den_v_dx0; C[3][1]=den_v_dy0; C[3][2]=den_v_dz0; C[3][3]=den_v_0;
204 std::cout <<
"Center of projection\n" << nv <<
'\n' 205 <<
"Residual\n" << C*nv <<
'\n';
208 double ndu = std::sqrt(den_u_dx0*den_u_dx0 + den_u_dy0*den_u_dy0 +
209 den_u_dz0*den_u_dz0);
210 double ndv = std::sqrt(den_v_dx0*den_v_dx0 + den_v_dy0*den_v_dy0 +
211 den_v_dz0*den_v_dz0);
214 if (ndu/std::fabs(den_u_0)<1.0e-10||ndv/std::fabs(den_v_0)<1.0e-10)
216 std::cout <<
"Camera is nearly affine - approximation not implemented!\n";
221 for (
unsigned i = 0; i<3; ++i)
225 M[2][i]=(C[1][i]/ndu + C[3][i]/ndv)/2;
228 std::cout <<
"M matrix\n" << M <<
'\n';
231 for (
int i = 0; i < 3; ++i )
232 for (
int j = 0; j < 3; ++j )
233 Mf(i,j) = M(2-j,2-i);
238 for (
int i = 0; i < 3; ++i ) {
239 for (
int j = 0; j < 3; ++j ) {
240 Qf(i,j) = q(2-j,2-i);
241 Rf(i,j) = r(2-j,2-i);
244 std::cout <<
"Flipped Rotation\n" << Qf <<
'\n' 245 <<
"Flipped Upper Triangular\n" << Rf <<
'\n';
249 std::cout <<
"UnFlipped Rotation\n" << uq <<
'\n' 250 <<
"UnFlipped Upper Triangular\n" << ur <<
'\n' 251 <<
"Det uq " << vnl_det<double>(uq) <<
'\n';
254 for (
unsigned i = 0; i<3; ++i) {
259 std::cout <<
"Denominators\n" 265 for (
unsigned i = 0; i<3; ++i)
270 for (
unsigned r = 0; r<3; ++r)
271 for (
unsigned c = 0; c<3; ++c)
272 pmatrix[r][c] = M[r][c];
273 for (
unsigned r = 0; r<3; ++r)
274 pmatrix[r][3] = p3[r];
282 Kr[0][0]=uscale; Kr[0][2]=uoff;
283 Kr[1][1]=vscale; Kr[1][2]=voff;
286 std::cout <<
"Kr\n" << Kr <<
'\n';
288 for (
int i = 0; i < 3; ++i )
289 for (
int j = 0; j < 3; ++j )
290 KRf(i,j) = KR(2-j,2-i);
295 for (
int i = 0; i < 3; ++i ) {
296 for (
int j = 0; j < 3; ++j ) {
297 krQf(i,j) = krq(2-j,2-i);
298 krRf(i,j) = krr(2-j,2-i);
301 std::cout <<
"Flipped Rotation (KR)\n" << krQf <<
'\n' 302 <<
"Flipped Upper Triangular (KR)\n" << krRf <<
'\n';
304 int r0pos = krRf(0,0) > 0 ? 1 : -1;
305 int r1pos = krRf(1,1) > 0 ? 1 : -1;
306 int r2pos = krRf(2,2) > 0 ? 1 : -1;
307 int diag[3] = { r0pos, r1pos, r2pos };
309 for (
int i = 0; i < 3; ++i ) {
310 for (
int j = 0; j < 3; ++j ) {
311 K1(i,j) = diag[j]*krRf(i,j);
312 R1(i,j) = diag[i]*krQf(i,j);
316 std::cout <<
"K1\n" << K1 <<
'\n' 317 <<
"R1\n" << R1 <<
'\n' 318 <<
"Det R1 " << vnl_det<double>(R1) <<
'\n';
324 T[0][0]=1.0; T[1][1]=1.0; T[2][2]=1.0; T[3][3]=1.0;
325 T[0][3] = -nx0; T[1][3]= -ny0; T[2][3]=-nz0;
326 pmatrix = Kr*pmatrix*T;
328 std::cout <<
"P Matrix\n" << pmatrix <<
'\n';
348 T.
set(0,0,1/xscale); T.
set(1,1,1/yscale); T.
set(2,2,1/zscale);
349 T.
set(0,3, -xoff/xscale); T.
set(1,3, -yoff/yscale);
350 T.
set(2,3, -zoff/zscale);
371 auto ni = static_cast<unsigned>(2*sou.
scale());
372 auto nj = static_cast<unsigned>(2*sov.
scale());
382 double xmin = minp.
x(), ymin = minp.
y(), zmin = minp.
z();
383 double xrange = maxp.
x()-xmin, yrange = maxp.
y()-ymin,
384 zrange = maxp.
z()-zmin;
385 if (xrange<0||yrange<0||zrange<0)
389 std::vector<vgl_point_3d<double> > world_pts;
390 unsigned count = 0, ntrials = 0;
394 double rx = xrange*(std::rand()/(RAND_MAX+1.0));
395 double ry = yrange*(std::rand()/(RAND_MAX+1.0));
396 double rz = zrange*(std::rand()/(RAND_MAX+1.0));
399 if (ip.
x()<0||ip.
x()>ni||ip.
y()<0||ip.
y()>nj)
401 world_pts.push_back(wp);
404 std::cout <<
"Ntrials " << ntrials <<
'\n';
407 std::vector<vgl_point_3d<double> > norm_world_pts;
408 std::vector<vgl_point_2d<double> > image_pts, norm_image_pts;
409 auto N = static_cast<unsigned int>(world_pts.size());
410 for (
unsigned i = 0; i<N; ++i)
414 image_pts.push_back(ip);
416 norm_image_pts.push_back(nip);
420 norm_world_pts.push_back(nwp);
436 std::cout << camera <<
'\n';
444 camera.set_calibration(kk);
447 double err_max = 0, err_min = 1e10;
449 for (
unsigned i = 0; i<N; ++i)
453 camera.project(nwp.
x(), nwp.
y(), nwp.
z(), U, V);
455 double error = std::sqrt((ip.
x()-U)*(ip.
x()-U) + (ip.
y()-V)*(ip.
y()-V));
456 if ( error > err_max )
459 max_pt = world_pts[i];
464 min_pt = world_pts[i];
467 std::cout <<
"Max Error = " << err_max <<
" at " << max_pt <<
'\n' 468 <<
"Min Error = " << err_min <<
" at " << min_pt <<
'\n' 469 <<
"final cam\n" << camera <<
'\n';
481 double lon_low = approximation_volume.
min_x();
482 double lat_low = approximation_volume.
min_y();
484 double lon_high = approximation_volume.
max_x();
485 double lat_high = approximation_volume.
max_y();
486 assert( lat_low < lat_high && lon_low < lon_high );
488 vpgl_lvcs lvcs_converter( lat_low, lon_low,
489 .5*(approximation_volume.
min_z()+approximation_volume.
max_z()),
493 double min_lx=1e99, min_ly=1e99, min_lz=1e99, max_lx=0, max_ly=0, max_lz=0;
494 for (
int cx = 0; cx < 2; ++cx ) {
495 for (
int cy = 0; cy < 2; ++cy ) {
496 for (
int cz = 0; cz < 2; ++cz ) {
498 approximation_volume.
min_y()*cy + approximation_volume.
max_y()*(1-cy),
499 approximation_volume.
min_z()*cz + approximation_volume.
max_z()*(1-cz) );
500 double lcx, lcy, lcz;
503 if ( cx == 0 && cy == 0 && cz == 0 ) {
504 min_lx = wc_loc.
x(); max_lx = wc_loc.
x();
505 min_ly = wc_loc.
y(); max_ly = wc_loc.
y();
506 min_lz = wc_loc.
z(); max_lz = wc_loc.
z();
509 if ( wc_loc.
x() < min_lx ) min_lx = wc_loc.
x();
510 if ( wc_loc.
y() < min_ly ) min_ly = wc_loc.
y();
511 if ( wc_loc.
z() < min_lz ) min_lz = wc_loc.
z();
512 if ( wc_loc.
x() > max_lx ) max_lx = wc_loc.
x();
513 if ( wc_loc.
y() > max_ly ) max_ly = wc_loc.
y();
514 if ( wc_loc.
z() > max_lz ) max_lz = wc_loc.
z();
518 double dlx = max_lx-min_lx, dly = max_ly-min_ly, dlz = max_lz-min_lz;
521 norm_trans.
set(0,0,2/dlx); norm_trans.
set(1,1,2/dly); norm_trans.
set(2,2,2/dlz);
522 norm_trans.
set(0,3, -1-2*min_lx/dlx );
523 norm_trans.
set(1,3, -1-2*min_ly/dly);
524 norm_trans.
set(2,3, -1-2*min_lz/dlz);
538 auto ni = static_cast<unsigned>(2*sou.
scale());
539 auto nj = static_cast<unsigned>(2*sov.
scale());
543 double xmin = minp.
x(), ymin = minp.y(), zmin = minp.z();
544 double xrange = maxp.
x()-xmin,
545 yrange = maxp.
y()-ymin,
546 zrange = maxp.
z()-zmin;
547 if (xrange<0||yrange<0||zrange<0)
551 std::vector<vgl_point_3d<double> > world_pts;
552 unsigned count = 0, ntrials = 0;
556 double rx = xrange*(std::rand()/(RAND_MAX+1.0));
557 double ry = yrange*(std::rand()/(RAND_MAX+1.0));
558 double rz = zrange*(std::rand()/(RAND_MAX+1.0));
561 if (ip.
x()<0||ip.
x()>ni||ip.
y()<0||ip.
y()>nj)
563 world_pts.push_back(wp);
566 std::cout <<
"Ntrials " << ntrials <<
'\n';
569 std::vector<vgl_point_3d<double> > norm_world_pts;
570 std::vector<vgl_point_2d<double> > image_pts, norm_image_pts;
571 auto N = static_cast<unsigned int>(world_pts.size());
572 for (
unsigned i = 0; i<N; ++i)
576 image_pts.push_back(ip);
578 norm_image_pts.push_back(nip);
581 double lcx, lcy, lcz;
586 assert( std::fabs(nwp.
x()) <= 1
587 && std::fabs(nwp.
y()) <= 1
588 && std::fabs(nwp.
z()) <= 1 );
589 norm_world_pts.emplace_back(nwp );
605 std::cout << camera <<
'\n';
613 camera.set_calibration(kk);
616 double err_max = 0, err_min = 1e10;
618 for (
unsigned i = 0; i<N; ++i)
622 camera.project(nwp.
x(), nwp.
y(), nwp.
z(), U, V);
624 double error = std::sqrt((ip.
x()-U)*(ip.
x()-U) + (ip.
y()-V)*(ip.
y()-V));
625 if ( error > err_max )
628 max_pt = world_pts[i];
633 min_pt = world_pts[i];
636 std::cout <<
"Max Error = " << err_max <<
" at " << max_pt <<
'\n' 637 <<
"Min Error = " << err_min <<
" at " << min_pt <<
'\n' 638 <<
"final cam\n" << camera <<
'\n';
654 auto nrays = static_cast<unsigned int>(ray_nbrs.size());
655 if (nrays!=4)
return false;
668 double ox = 0.5*(org0.
x()+ temp.
x());
669 double oy = 0.5*(org0.
y()+ temp.
y());
670 double oz = 0.5*(org0.
z()+ temp.
z());
673 intrp_ray.
set(iorg, idir);
679 double mid_u,
double mid_v,
684 double& org_tol,
double& dir_tol)
693 ip.
set(mid_u+1.0, mid_v+1.0);
697 double gsd = (low_pt-low_pt_pix).
length();
699 double tfact = 0.001;
703 double ang =
angle(dir0, dir1);
727 auto nrays = static_cast<unsigned int>(ray_nbrs.size());
728 if (nrays!=4)
return false;
734 org01 = r01.origin();
736 org11 = r11.origin();
741 interp_rays[0] = ray;
746 interp_rays[1].
set(iorg, idir);
749 iorg = org00+ (org10-org00)*0.5;
750 idir = 0.5*dir00 + 0.5*dir10;
751 interp_rays[2].
set(iorg, idir);
754 iorg = org00+0.25*(org01-org00) + 0.25*(org10-org00)+ 0.25*(org11-org00);
755 idir = 0.25*dir00 + 0.25*dir01+ 0.25*dir10+0.25*dir11;
756 interp_rays[3].
set(iorg, idir);
797 double el_low = zoff-zscl/2;
798 double el_high = zoff+zscl/2;
801 double x,y, z_low, z_high;
805 return convert(rat_cam, ni, nj, gen_cam, z_low, z_high, level);
821 double local_z_min,
double local_z_max,
unsigned level)
833 double lv = std::log(dim)/std::log(2.0);
834 int n_levels = static_cast<int>(lv+1.0);
835 if (dim*std::pow(0.5, static_cast<double>(n_levels-1)) < 3.0) n_levels--;
838 std::vector<int> nr(n_levels,0), nc(n_levels,0);
840 std::vector<double> scl(n_levels,1.0);
841 std::vector<vbl_array_2d<vgl_ray_3d<double> > > ray_pyr(n_levels);
842 ray_pyr[0].resize(nj, ni);
844 nr[0]=nj; nc[0]=ni; scl[0]=1.0;
845 std::cout<<
"(ni,nj)"<<ni<<
","<<nj<<std::endl;
846 int di = (ni+1)/2 +1,
849 for (
int i=1; i<n_levels; ++i)
851 std::cout<<
"(di,dj)"<<di<<
","<<dj<<std::endl;
852 ray_pyr[i].resize(dj,di);
863 double org_tol = 0.0;
864 double ang_tol = 0.0;
865 double max_org_err = 0.0, max_ang_err = 0.0;
866 if (!ray_tol(rat_cam, ni/2.0, nj/2.0, high,
867 org, low, endpt, org_tol, ang_tol))
869 bool need_interp =
true;
870 int lev = n_levels-1;
871 for (; lev>=0&&need_interp; --lev) {
873 for (
int j =0; j<nr[lev]; ++j) {
874 int sj = static_cast<int>(scl[lev]*j);
877 for (
int i =0;i<nc[lev]; ++i)
879 int si = static_cast<int>(scl[lev]*i);
886 if (lev < n_levels-1) {
887 double rel_scale = scl[lev]/scl[lev+1];
888 int i_above = static_cast<int>(rel_scale * i);
889 int j_above = static_cast<int>(rel_scale * j);
891 prev_org = ray_pyr[lev+1][j_above][i_above].origin();
894 double ray_len = (local_z_min - prev_org.z()) / prev_dir.
z();
895 prev_endpt = prev_org + (prev_dir * ray_len);
897 constexpr
double error_tol = 0.25;
904 ray_pyr[lev][j][i].
set(org, dir);
912 max_org_err = 0.0; max_ang_err = 0.0;
913 std::vector<vgl_ray_3d<double> > ray_nbrs(4);
915 for (
int j =1; (j<nr[lev]-1)&&!need_interp; ++j) {
916 for (
int i =1;(i<nc[lev]-1)&&!need_interp; ++i) {
925 ray_nbrs[0]=ray_pyr[lev][j-1][i];
926 ray_nbrs[1]=ray_pyr[lev][j][i-1];
927 ray_nbrs[2]=ray_pyr[lev][j][i+1];
928 ray_nbrs[3]=ray_pyr[lev][j+1][i];
931 if (!interp_ray(ray_nbrs, intp_ray))
935 if (dorg>max_org_err) max_org_err = dorg;
936 if (dang>max_ang_err) max_ang_err = dang;
937 need_interp = max_org_err>org_tol || max_ang_err>ang_tol;
939 std::cout<<lev<<
", "<<i<<
","<<j<<std::endl;
945 for (++lev; lev>0; --lev) {
946 unsigned int ncr = nc[lev];
947 unsigned int nrb = nr[lev];
950 std::vector<vgl_ray_3d<double> > ray_nbrs(4);
951 std::vector<vgl_ray_3d<double> > interp_rays(4);
952 for (
unsigned int j = 0; j<nrb; ++j)
953 for (
unsigned int i = 0; i<ncr; ++i) {
954 ray_nbrs[0] = clev[j][i];
955 ray_nbrs[1] = clev[j][i];
956 ray_nbrs[2] = clev[j][i];
957 ray_nbrs[3] = clev[j][i];
958 if (i+1<ncr) ray_nbrs[1] = clev[j][i+1];
959 if (j+1<nrb) ray_nbrs[2] = clev[j+1][i];
960 if (i+1<ncr && j+1<nrb) ray_nbrs[3] = clev[j+1][i+1];
963 if (2*i<nlev.
cols() && 2*j<nlev.
rows())
965 nlev[2*j][2*i] =interp_rays[0];
966 if (2*i+1<nlev.
cols()) nlev[2*j][2*i+1] =interp_rays[1];
967 if (2*j+1<nlev.
rows()) nlev[2*j+1][2*i] =interp_rays[2];
968 if (2*i+1<nlev.
cols() && 2*j+1<nlev.
rows()) nlev[2*j+1][2*i+1]=interp_rays[3];
972 if ((
int)level < n_levels) {
983 int ni,
int nj,
int offseti,
int offsetj,
984 double local_z_min,
double local_z_max,
985 int n_levels,std::vector<int> nr, std::vector<int> nc,
995 double org_tol = 0.0;
996 double ang_tol = 0.0;
997 double max_org_err = 0.0, max_ang_err = 0.0;
998 if (!ray_tol(rat_cam,offseti+ ni/2.0, offsetj +nj/2.0, high,
999 org, low, endpt, org_tol, ang_tol))
1002 bool need_interp =
true;
1003 int lev = n_levels-1;
1005 for (; lev>=0&&need_interp; --lev) {
1007 for (
int j =0; j<nr[lev]; ++j) {
1008 int sj = offsetj+static_cast<int>(scl[lev]*j);
1012 for (
int i =0;i<nc[lev]; ++i)
1014 int si = offseti+ static_cast<int>(scl[lev]*i);
1022 if (lev < n_levels-1) {
1023 double rel_scale = scl[lev]/scl[lev+1];
1024 int i_above = static_cast<int>(rel_scale * i);
1025 int j_above = static_cast<int>(rel_scale * j);
1026 prev_org = ray_pyr[lev+1][j_above][i_above].origin();
1029 double ray_len = (local_z_min - prev_org.
z()) / prev_dir.
z();
1030 prev_endpt = prev_org + (prev_dir * ray_len);
1032 constexpr
double error_tol = 0.5;
1038 ray_pyr[lev][j][i].
set(org, dir);
1044 need_interp =
false;
1045 max_org_err = 0.0; max_ang_err = 0.0;
1046 std::vector<vgl_ray_3d<double> > ray_nbrs(4);
1048 for (
int j =1; (j<nr[lev]-1)&&!need_interp; ++j) {
1049 for (
int i =1;(i<nc[lev]-1)&&!need_interp; ++i) {
1058 ray_nbrs[0]=ray_pyr[lev][j-1][i];
1059 ray_nbrs[1]=ray_pyr[lev][j][i-1];
1060 ray_nbrs[2]=ray_pyr[lev][j][i+1];
1061 ray_nbrs[3]=ray_pyr[lev][j+1][i];
1064 if (!interp_ray(ray_nbrs, intp_ray))
1068 if (dorg>max_org_err) max_org_err = dorg;
1069 if (dang>max_ang_err) max_ang_err = dang;
1070 need_interp = max_org_err>org_tol || max_ang_err>ang_tol;
1073 std::cout<<lev<<
", "<<i<<
","<<j<<std::endl;
1080 for (++lev; lev>0; --lev) {
1081 unsigned int ncr = nc[lev];
1082 unsigned int nrb = nr[lev];
1085 std::vector<vgl_ray_3d<double> > ray_nbrs(4);
1086 std::vector<vgl_ray_3d<double> > interp_rays(4);
1087 for (
unsigned int j = 0; j<nrb; ++j) {
1088 for (
unsigned int i = 0; i<ncr; ++i) {
1089 ray_nbrs[0] = clev[j][i];
1090 ray_nbrs[1] = clev[j][i];
1091 ray_nbrs[2] = clev[j][i];
1092 ray_nbrs[3] = clev[j][i];
1093 if (i+1<ncr) ray_nbrs[1] = clev[j][i+1];
1094 if (j+1<nrb) ray_nbrs[2] = clev[j+1][i];
1095 if (i+1<ncr && j+1<nrb) ray_nbrs[3] = clev[j+1][i+1];
1098 if (2*i<nlev.
cols() && 2*j<nlev.
rows())
1100 nlev[2*j][2*i] =interp_rays[0];
1101 if (2*i+1<nlev.
cols()) nlev[2*j][2*i+1] =interp_rays[1];
1102 if (2*j+1<nlev.
rows()) nlev[2*j+1][2*i] =interp_rays[2];
1103 if (2*i+1<nlev.
cols() && 2*j+1<nlev.
rows()) nlev[2*j+1][2*i+1]=interp_rays[3];
1115 double local_z_min,
double local_z_max,
unsigned level)
1125 unsigned int mindim = gni < gnj ? gni : gnj;
1126 unsigned int factor = 256;
1127 unsigned int n_levels = 6;
1129 while( mindim < factor )
1134 std::cout<<
"."<<std::endl;
1136 unsigned int numi = gni/factor;
1137 unsigned int numj = gnj/factor;
1141 std::vector<int> nr(n_levels,0), nc(n_levels,0);
1144 std::vector<unsigned int> scl(n_levels);
1145 unsigned int running_scale = 1;
1146 std::vector<vbl_array_2d<vgl_ray_3d<double> > > ray_pyr(n_levels);
1147 for(
unsigned int i = 0; i < n_levels; i++)
1149 nr[i] = factor/running_scale + 1;
1150 nc[i] = factor/running_scale + 1;
1151 ray_pyr[i].resize(nr[i], nc[i]);
1153 scl[i]=running_scale;
1157 for (
unsigned int bigi=0 ; bigi<=numi;bigi++)
1159 unsigned int ni = factor;
1160 unsigned int offseti = bigi*factor;
1162 offseti = gni-factor;
1163 for (
unsigned int bigj=0 ; bigj<=numj;bigj++)
1165 unsigned int nj = factor;
1166 unsigned int offsetj = bigj*factor;
1168 offsetj = gnj-factor;
1171 if(!
vpgl_generic_camera_convert::pyramid_est(rat_cam,ni,nj,offseti,offsetj,local_z_min, local_z_max,n_levels,nr, nc,scl,ray_pyr ))
1173 for(
unsigned int i = 0; i < factor; i++)
1175 for(
unsigned int j = 0; j < factor; j++)
1177 if(j+offsetj < gnj && i+offseti <gni )
1178 finalrays[j+offsetj][i+offseti] = ray_pyr[0][j][i];
1183 if (level < n_levels) {
1195 double local_z_min,
double local_z_max,
unsigned )
1205 for(
unsigned i = 0 ; i < gni; i++)
1207 for(
unsigned j = 0 ; j < gnj; j++)
1210 constexpr
double error_tol = 0.5;
1216 finalrays[j][i].set(org, dir);
1231 double scale = (level < 32) ?
double(1L<<level) : std::pow(2.0,static_cast<double>(level));
1232 for (
int j = 0; j<nj; ++j)
1233 for (
int i = 0; i<ni; ++i) {
1234 ipt.
set(i*scale, j*scale, 1.0);
1249 double scale = (level < 32) ?
double(1L<<level) : std::pow(2.0,static_cast<double>(level));
1250 for (
int j = 0; j<nj; ++j)
1251 for (
int i = 0; i<ni; ++i) {
1252 ipt.
set(i*scale, j*scale, 1.0);
1267 double scale = (level < 32) ?
double(1L<<level) : std::pow(2.0,static_cast<double>(level));
1268 for (
int j = -margin; j<nj+margin; ++j)
1269 for (
int i = -margin; i<ni+margin; ++i) {
1270 ipt.
set(i*scale, j*scale, 1.0);
1272 rays[j+margin][i+margin]=ray;
1287 double scale = (level < 32) ?
double(1L<<level) : std::pow(2.0,static_cast<double>(level));
1301 for (
unsigned j=0; j<3; ++j) {
1315 for (
int j = 0; j<nj; ++j) {
1316 for (
int i = 0; i<ni; ++i) {
1320 org.
set(org_vnl[0], org_vnl[1], org_vnl[2]);
1321 rays[j][i].set(org, dir);
1360 double scale = (level < 32) ?
double(1L<<level) : std::pow(2.0,static_cast<double>(level));
1368 for (
int j = 0; j<nj; ++j)
1369 for (
int i = 0; i<ni; ++i) {
1372 org.
set(x, y, height);
1373 rays[j][i].set(org, dir);
1384 double scale = (level < 32) ?
double(1L<<level) : std::pow(2.0,static_cast<double>(level));
1389 for (
int j = 0; j<nj; ++j)
1390 for (
int i = 0; i<ni; ++i) {
1393 org.
set(x, y, height);
1394 rays[j][i].set(org, dir);
1404 unsigned int num_points)
1407 const double width = region_of_interest.
width();
1408 const double height = region_of_interest.
height();
1409 const double depth = region_of_interest.
depth();
1410 double min_x = region_of_interest.
min_x();
1411 double min_y = region_of_interest.
min_y();
1412 double min_z = region_of_interest.
min_z();
1414 std::vector< vgl_point_2d<double> > image_pts;
1415 std::vector< vgl_point_3d<double> > world_pts;
1416 for (
unsigned i=0; i<num_points; ++i) {
1417 double x = rng.
drand64()*width + min_x;
1418 double y = rng.
drand64()*depth + min_y;
1419 double z = rng.
drand64()*height + min_z;
1420 world_pts.emplace_back(x,y,z);
1423 image_pts.emplace_back(u,v);
1433 #endif // HAS_GEOTIFF 1435 #endif // vpgl_camera_convert_cxx_ void project(const T x, const T y, const T z, T &u, T &v) const override
The generic camera interface. u represents image column, v image row.
vnl_matrix_ref< T > as_ref()
A geographic coordinate system.
static bool convert_bruteforce(vpgl_local_rational_camera< double > const &rat_cam, int gni, int gnj, vpgl_generic_camera< double > &gen_cam, double local_z_min, double local_z_max, unsigned level)
double drand64(double a, double b)
T normalize(const T value) const
static bool convert(vpgl_local_rational_camera< double > const &camera_in, vgl_box_3d< double > const ®ion_of_interest, vpgl_affine_camera< double > &camera_out, unsigned int num_points=10000)
Convert from rational camera using a local Euclidean coordinate system.
vnl_vector< T > nullvector() const
void global_to_local(const double lon, const double lat, const double gz, cs_names cs_name, double &lx, double &ly, double &lz, AngUnits output_ang_unit=DEG, LenUnits output_len_unit=METERS)
Converts pointin, given in a global coord system described by global_cs_name, to pointout in the loca...
vgl_h_matrix_3d & set(unsigned int row_index, unsigned int col_index, T value)
Several routines for converting camera types.
static bool convert(vpgl_rational_camera< double > const &rat_cam, vgl_box_3d< double > const &approximation_volume, vpgl_perspective_camera< double > &camera, vgl_h_matrix_3d< double > &norm_trans)
Convert from a rational camera.
vnl_matrix< T > const & Q() const
vnl_matrix< T > inverse() const
vnl_matrix< T > const & R() const
vpgl_scale_offset< T > scl_off(const coor_index coor_index) const
get a specific scale_offset.
static bool convert_local(vpgl_rational_camera< double > const &rat_cam, vgl_box_3d< double > const &approximation_volume, vpgl_perspective_camera< double > &camera, vgl_h_matrix_3d< double > &norm_trans)
Convert from rational camera using a local Euclidean coordinate system.
void backproject(const double u, const double v, double &x, double &y, double &z)
backprojects an image point into local coordinates (based on lvcs_).
static bool convert(vpgl_rational_camera< double > const &rat_cam, vgl_point_3d< double > const &world_center, vpgl_proj_camera< double > &camera)
Find a projective camera that best approximates the rational camera at the world center (lon (deg),...
virtual bool set_matrix(const vnl_matrix_fixed< T, 3, 4 > &new_camera_matrix)
Setters mirror the constructors and return true if the setting was successful.
vgl_point_3d< Type > min_point() const
static bool convert_with_margin(vpgl_perspective_camera< double > const &per_cam, int ni, int nj, vpgl_generic_camera< double > &gen_cam, int margin, unsigned level=0)
Several routines for computing different kinds of cameras from world-point correspondences.
static vgl_h_matrix_3d< double > norm_trans(vpgl_rational_camera< double > const &rat_cam)
An auxiliary matrix that transforms (normalizes) world points prior to projection by a projective cam...
Methods for projecting geometric structures onto the image.
vnl_matrix_fixed< T, 3, 3 > get_matrix() const
Get the calibration matrix.
vnl_matrix_fixed & fill(T)
std::vector< std::vector< T > > coefficients() const
get the rational polynomial coefficients in a vcl array.
vgl_ray_3d< T > backproject_ray(const vgl_point_2d< T > &image_point) const
Finite ray backprojection.
virtual vgl_ray_3d< T > backproject_ray(const vgl_homg_point_2d< T > &image_point) const
Find the 3d ray that goes through the camera center and the provided image point.
void set(T px, T py, T pw=(T) 1)
vgl_point_3d< Type > origin() const
This class implements the perspective camera class as described in Hartley & Zisserman as a finite ca...
vgl_h_matrix_3d & set_identity()
vgl_homg_point_3d< T > camera_center() const override
Find the 3d coordinates of the center of the camera. Will be an ideal point with the sense of the ray...
void orient_ray_direction(vgl_vector_3d< T > const &look_dir)
flip the ray direction so that dot product with look_dir is positive.
T scale(const coor_index coor_index) const
get a specific scale value.
static bool compute(const std::vector< vgl_point_2d< double > > &image_pts, const std::vector< vgl_point_3d< double > > &world_pts, vpgl_affine_camera< double > &camera)
Compute from two sets of corresponding points.
const vnl_matrix_fixed< T, 3, 4 > & get_matrix() const
Return a copy of the camera matrix.
vgl_point_3d< Type > max_point() const
static bool convert(vpgl_local_rational_camera< double > const &rat_cam, int ni, int nj, vpgl_generic_camera< double > &gen_cam, unsigned level=0)
Convert a local rational camera to a generic camera.
static bool pyramid_est(vpgl_local_rational_camera< double > const &rat_cam, int ni, int nj, int offseti, int offsetj, double local_z_min, double local_z_max, int n_levels, std::vector< int > nr, std::vector< int > nc, std::vector< unsigned int > scl, std::vector< vbl_array_2d< vgl_ray_3d< double > > > &ray_pyr)
void set(T vx, T vy, T vz)
void set(vgl_point_3d< Type > const &p0, vgl_vector_3d< Type > const &direction)
static vgl_ray_3d< double > interp_pair(vgl_ray_3d< double > const &r0, vgl_ray_3d< double > const &r1, double n_grid)
interpolate a span of rays base on a linear interpolation. n_grid is the step distance from r1....
double angle(vgl_ray_3d< Type > const &r0, vgl_ray_3d< Type > const &r1)
vgl_homg_plane_3d< T > principal_plane() const override
Find the world plane perpendicular to the camera rays at viewing distance from the origin.
void set(Type px, Type py, Type pz)
void set_viewing_distance(T dist)
set a finite viewing distance to allow the methods below to return finite objects.
static bool compute(const std::vector< vgl_point_2d< double > > &image_pts, const std::vector< vgl_point_3d< double > > &world_pts, const vpgl_calibration_matrix< double > &K, vpgl_perspective_camera< double > &camera)
Compute from two sets of corresponding points.
T offset(const coor_index coor_index) const
get a specific offset value.
double length(v const &a)
Solve min(R,s) ||X-s(RY+t)||, where R is a rotation matrix, X,Y are 3-d points, s is a scalar and t i...
static bool bproj_plane(const vpgl_camera< double > *cam, vnl_double_2 const &image_point, vnl_double_4 const &plane, vnl_double_3 const &initial_guess, vnl_double_3 &world_point, double error_tol=0.05, double relative_diameter=1.0)
Generic camera interfaces (pointer for abstract class).
vgl_vector_3d< Type > direction() const
Methods for back_projecting from cameras to 3-d geometric structures.
static bool upsample_rays(std::vector< vgl_ray_3d< double > > const &ray_nbrs, vgl_ray_3d< double > const &ray, std::vector< vgl_ray_3d< double > > &interp_rays)
interpolate rays to fill next higher resolution pyramid layer.
void project(const T x, const T y, const T z, T &u, T &v) const override
The generic camera interface. u represents image column, v image row.