12 #include <vil/vil_image_resource.h> 13 #include <vil/vil_math.h> 25 rayp = org + x[0]*dir;
26 double lon = rayp.
x(), lat = rayp.y(), elev = rayp.z();
30 int u = static_cast<int>(ud+0.5),
v = static_cast<int>(vd+0.5);
32 if(u<0||u>=ni||v<0||v>=nj){
33 if(
verbose_) std::cout <<
"warning: dem backprojection cost function - outside DEM bounds" << std::endl;
34 return std::numeric_limits<double>::max();
37 double z = (*dview_)(u,
v);
38 return (elev-z)*(elev-z);
51 if(
verbose_) std::cout <<
"WARNING! - units unknown in vpgl_backproject_dem - assume degrees meters" << std::endl;
60 unsigned nhi = ni/2, nhj = nj/2;
89 if(
verbose_) std::cout <<
"Calculating Z-range from DEM..." << std::endl;
94 auto stride_interval = static_cast<unsigned>(std::sqrt(stride_area));
97 std::vector<double> z_samples;
98 float zmin_calc=std::numeric_limits<float>::max(), zmax_calc=-zmin_calc;
99 for(
unsigned j = 0; j<nj; j+=stride_interval)
100 for(
unsigned i = 0; i<ni; i+=stride_interval){
104 z_samples.push_back(z);
107 std::sort(z_samples.begin(), z_samples.end());
109 auto ns = static_cast<double>(z_samples.size());
110 auto band_size = static_cast<unsigned>(ns*
tail_fract_);
111 for(
unsigned k = band_size; k<(ns-band_size); ++k){
112 double z = z_samples[k];
113 if(z<zmin_calc) zmin_calc = static_cast<float>(z);
114 if(z>zmax_calc) zmax_calc = z;
131 double max_z,
double min_z,
135 if(
verbose_) std::cout <<
"vpgl_backproj_dem " << image_point <<
" max_z " << max_z <<
" min_z " << min_z <<
" init_guess " << initial_guess <<
" error tol " << error_tol << std::endl;
137 double dz = (max_z - min_z);
140 if(!
vpgl_ray::ray(cam, image_point, initial_xy, max_z, dz, ray)){
141 if(
verbose_) std::cout <<
" compute camera ray failed - Fatal!" << std::endl;
147 if(std::fabs(dir.
z())<0.001){
148 if(
verbose_) std::cout <<
"Ray parallel to XY plane - Fatal!" << std::endl;
152 double t = (initial_guess.
z() - max_z)/dir.
z();
156 double lon = guess_point.
x(), lat = guess_point.
y(),elev = guess_point.
z();
160 int u = static_cast<int>(ud+0.5),
v = static_cast<int>(vd+0.5);
162 if(u<0||u>=ni||v<0||v>=nj){
163 if(
verbose_) std::cout <<
"Initial guess for DEM intersection is outside DEM bounds - Fatal!" << std::endl;
176 world_point = origin + tmin*dir;
178 if(
verbose_) std::cout <<
"success! ray/dem intersection " << world_point << std::endl;
183 double max_z,
double min_z, vnl_double_3
const& initial_guess, vnl_double_3& world_point,
188 img_pt.
set(image_point[0], image_point[1]);
189 init_guess.
set(initial_guess[0],initial_guess[1],initial_guess[2]);
190 bool good = this->
bproj_dem(cam, img_pt, max_z, min_z, init_guess, wrld_pt, error_tol);
195 world_point[0]=wrld_pt.
x(); world_point[1]=wrld_pt.
y(); world_point[2]=wrld_pt.
z();
200 vnl_double_3
const& initial_guess, vnl_double_3& world_point,
double error_tol){
203 return this->
bproj_dem(cam, image_point, max_z, min_z, initial_guess, world_point, error_tol);
210 return this->
bproj_dem(cam, image_point, max_z, min_z, initial_guess, world_point, error_tol);
static bool ray(const vpgl_camera< double > *cam, vnl_double_3 const &point_3d, vnl_double_3 &ray)
Generic camera interfaces (pointer for abstract class).
vil_image_view< float > dem_view_
vgl_ray_3d< double > ray_
dem_bproj_cost_function(vil_image_view< float > const &dem_view, vpgl_geo_camera *geo_cam, vgl_ray_3d< double > const &ray, bool verbose=false)
void img_to_global(const double i, const double j, double &lon, double &lat) const
returns the corresponding geographical coordinates for a given pixel position (i,j).
vpgl_geo_camera * geo_cam_
vpgl_geo_camera * geo_cam_
vgl_point_3d< double > geo_center_
double z_min_
bounds of the DEM.
vpgl_backproject_dem(vil_image_resource_sptr const &dem, double zmin=0.0, double zmax=-1.0)
default constructor not allowed.
void set(Type px, Type py)
std::vector< vgl_point_3d< double > > dem_corners_
A geotiff image deduced camera class.
double minimize(double ax)
const vil_image_view< float > * dview_
bool bproj_dem(const vpgl_camera< double > *cam, vnl_double_2 const &image_point, double max_z, double min_z, vnl_double_3 const &initial_guess, vnl_double_3 &world_point, double error_tol=1.0)
General camera - typically a RPC camera.
static bool init_geo_camera(vil_image_resource_sptr const &geotiff_img, const vpgl_lvcs_sptr &lvcs, vpgl_geo_camera *&camera)
uses lvcs to convert local x-y to global longitude and latitude.
vgl_point_3d< Type > origin() const
double f(vnl_vector< double > const &x) override
x is the parameter that runs along the ray, with x=0 at the ray origin.
double zmax() const
elevation bounds of the DEM (with tail removal).
Camera backproject functions involving geotiff DEMs.
void set(Type px, Type py, Type pz)
vil_image_resource_sptr dem_
double f_at_last_minimum() const
void project(const double x, const double y, const double z, double &u, double &v) const override
Implementing the generic camera interface of vpgl_camera.
Methods for computing the camera ray direction at a given 3-d point and other operations on camera ra...
vgl_vector_3d< Type > direction() const
Methods for back_projecting from cameras to 3-d geometric structures.