2 #ifndef vpgl_generic_camera_hxx_ 3 #define vpgl_generic_camera_hxx_ 13 # include <vcl_msvc_warnings.h> 35 unsigned long nc = rays.cols();
36 unsigned long nr = rays.rows();
40 double max_dist = 0.0;
42 for (
int v = 0;
v<nr; ++
v)
43 for (
int u = 0; u<nc; ++u) {
48 max_ray_origin_ = org;
49 max_ray_direction_ = rays[
v][u].direction();
53 min_ray_origin_ = org;
54 min_ray_direction_ = rays[
v][u].direction();
62 double lv = std::log(dim)/std::log(2.0);
63 n_levels_ = static_cast<int>(lv);
64 if (dim*std::pow(0.5, static_cast<double>(n_levels_-1)) < 32.0) n_levels_--;
65 if (n_levels_<=0) n_levels_ = 1;
66 rays_.resize(n_levels_);
67 nr_.resize(n_levels_);
68 nc_.resize(n_levels_);
71 int nrlv = (nr)/2, nclv = (nc)/2;
72 for (
int lev = 1; lev<n_levels_; ++lev) {
73 rays_[lev].resize(nrlv, nclv);
74 nr_[lev]=nrlv; nc_[lev]=nclv;
75 for (
int r = 0; r<nrlv; ++r)
76 for (
int c = 0; c<nclv; ++c)
77 rays_[lev][r][c] = rays_[lev-1][2*r][2*c];
79 nrlv =(nrlv) / 2; nclv = (nclv) / 2;
86 std::vector<int> nrs, std::vector<int> ncs )
88 assert(rays.size()>0 && nrs.size()>0 && ncs.size()>0);
91 double max_dist = 0.0;
93 for (
int v = 0;
v<nrs[0]; ++
v) {
94 for (
int u = 0; u<ncs[0]; ++u) {
99 max_ray_origin_ = org;
100 max_ray_direction_ = rays[0][
v][u].direction();
104 min_ray_origin_ = org;
105 min_ray_direction_ = rays[0][
v][u].direction();
112 n_levels_ = rays.size();
121 int start_r,
int end_r,
122 int start_c,
int end_c,
123 int& nearest_r,
int& nearest_c)
const 125 assert(level>=0 && level<n_levels_);
126 assert(start_r>=0 && end_r < nr_[level]);
127 assert(start_c>=0 && end_c < nc_[level]);
128 nearest_r = 0, nearest_c = 0;
130 for (
int r = start_r; r<=end_r; ++r)
131 for (
int c = start_c; c<=end_c; ++c) {
144 int& nearest_r,
int& nearest_c)
const 146 int lev = n_levels_-1;
147 int start_r = 0, end_r = nr_[lev];
148 int start_c = 0, end_c = nc_[lev];
149 for (; lev >= 0; --lev) {
150 if (start_r<0) start_r = 0;
151 if (start_c<0) start_c = 0;
152 if (end_r>=nr_[lev]) end_r = nr_[lev]-1;
153 if (end_c>=nc_[lev]) end_c = nc_[lev]-1;
154 nearest_ray(lev, p, start_r, end_r, start_c, end_c,
155 nearest_r, nearest_c);
157 start_r = 2*nearest_r-1; start_c = 2*nearest_c-1;
158 end_r = start_r + 2; end_c = start_c +2;
160 if ( (lev > 0) && (nr_[lev-1]%2 != 0) ) end_r++;
161 if ( (lev > 0) && (nc_[lev-1]%2 != 0) ) end_c++;
171 T u = static_cast<T>(nearest_c),
v = static_cast<T>(nearest_r);
172 ray = this->ray(u,
v);
185 int nearest_c = -1, nearest_r = -1;
186 this->nearest_ray_to_point(p, nearest_r, nearest_c);
188 this->refine_ray_at_point(nearest_c, nearest_r, p, r);
205 bool valid_inter =
true;
207 std::vector<vgl_point_3d<T> > inter_pts;
208 std::vector<vgl_point_2d<T> > img_pts;
211 inter_pts.push_back(ipt);
217 if (nearest_r>0 && !horiz) {
220 if(std::fabs((ipt-inter_pts[0]).
length()) > vnl_math::eps)
222 inter_pts.push_back(ipt);
227 if (nearest_c>0 && !vert) {
230 if(std::fabs((ipt-inter_pts[0]).
length()) > vnl_math::eps)
232 inter_pts.push_back(ipt);
237 int nrght = static_cast<int>(cols())-1;
238 if (nearest_c<nrght && !vert ) {
241 if(std::fabs((ipt-inter_pts[0]).
length()) > vnl_math::eps)
243 inter_pts.push_back(ipt);
248 int nbl = static_cast<int>(rows())-1;
249 if (nearest_r<nbl && !horiz ) {
252 if(std::fabs((ipt-inter_pts[0]).
length()) > vnl_math::eps)
254 inter_pts.push_back(ipt);
260 if (!valid_inter||inter_pts.size()<3) {
261 u = static_cast<T>(nearest_c);
262 v = static_cast<T>(nearest_r);
275 T one_over_det = static_cast<T>(1)/(v0v0*v1v1 - v0v1*v0v1);
286 u = nearest_c + del.
x();
287 v = nearest_r + del.
y();
296 int nearest_c=-1, nearest_r=-1;
297 this->nearest_ray_to_point(p, nearest_r, nearest_c);
299 this->refine_projection(nearest_c, nearest_r, p, u,
v);
307 double du = static_cast<double>(u);
308 double dv = static_cast<double>(
v);
309 int nright = static_cast<int>(cols())-1;
310 int nbelow = static_cast<int>(rows())-1;
311 if( ! (du>=-0.5 && dv>=-0.5 && du<=nright+0.5 && dv<=nbelow+0.5) ) {
316 iu = du<nright ? static_cast<int>(du) : nright-1;
317 iv = dv<nbelow ? static_cast<int>(dv) : nbelow-1;
319 if ((du-iu) == 0.0 && (dv-iv) == 0.0)
320 return rays_[0][iv][iu];
323 std::vector<double> dist;
324 std::vector<vgl_ray_3d<T> > nrays;
328 double d = (1 - (dv-iv))*(1 - (du-iu));
334 double d = (1 - (dv-iv))*(1 - (iu+1-du));
341 double d = (1 - (iv+1-dv))*(1 - (du-iu));
344 if(iu<nright && iv<nbelow) {
347 nrays.push_back(rlr);
348 double d = (1 - (iv+1-dv))*(1 - (iu+1-du));
351 assert(dist.size() == 4);
353 double ox = 0.0, oy = 0.0, oz = 0.0, dx = 0.0, dy = 0.0, dz = 0.0;
354 for (
unsigned i = 0; i<nrays.size(); ++i) {
359 ox += w*org.
x(); oy += w*org.
y(); oz += w*org.
z();
360 dx += w*dir.
x(); dy += w*dir.
y(); dz += w*dir.
z();
375 for (
int r = 0; r<nr_[level]; ++r) {
376 for (
int c = 0; c<nc_[level]; ++c) {
378 std::cout <<
'(' << o.
x() <<
' ' << o.
y() <<
") ";
387 for (
int r = 0; r<nr_[level]; ++r) {
388 for (
int c = 0; c<nc_[level]; ++c) {
391 <<
"translation " << o.
x() <<
' ' << o.
y() <<
' ' 392 <<
' ' << o.
z() <<
'\n' 395 <<
" appearance DEF A1 Appearance {\n" 396 <<
" material Material\n" 398 <<
" diffuseColor " << 1 <<
' ' << 0 <<
' ' << 0 <<
'\n' 399 <<
" emissiveColor " << .3 <<
' ' << 0 <<
' ' << 0 <<
'\n' 402 <<
" geometry Sphere\n" 404 <<
" radius " << 20 <<
'\n' 414 #undef vpgl_GENERIC_CAMERA_INSTANTIATE 415 #define vpgl_GENERIC_CAMERA_INSTANTIATE(T) \ 416 template class vpgl_generic_camera<T > 419 #endif // vpgl_generic_camera_hxx_
void refine_ray_at_point(int nearest_c, int nearest_r, vgl_point_3d< T > const &p, vgl_ray_3d< T > &ray) const
refine ray.
double vgl_distance(vgl_point_2d< T >const &p1, vgl_point_2d< T >const &p2)
void refine_projection(int nearest_c, int nearest_r, vgl_point_3d< T > const &p, T &u, T &v) const
refine the projection to sub pixel.
void nearest_ray_to_point(vgl_point_3d< T > const &p, int &nearest_r, int &nearest_c) const
vgl_point_2d< T > vgl_closest_point(vgl_line_2d< T > const &l, vgl_point_2d< T > const &p)
vgl_point_3d< T > vgl_intersection(const std::vector< vgl_plane_3d< T > > &p)
void print_to_vrml(int level, std::ostream &os)
visualization.
void print_orig(int level)
debug function.
vgl_point_3d< Type > origin() const
vgl_ray_3d< T > ray(const T u, const T v) const
the ray corresponding to a given pixel.
void set(vgl_point_3d< Type > const &p0, vgl_vector_3d< Type > const &direction)
T dot_product(v const &a, v const &b)
double length(v const &a)
vgl_vector_3d< Type > direction() const
void nearest_ray(int level, vgl_point_3d< T > const &p, int start_r, int end_r, int start_c, int end_c, int &nearest_r, int &nearest_c) const
nearest ray at level.
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. Finds projection using a pyrami...