2 #ifndef vpgl_perspective_camera_hxx_ 3 #define vpgl_perspective_camera_hxx_ 26 #include <vul/vul_file.h> 27 #include <vul/vul_file_iterator.h> 31 # include <vcl_msvc_warnings.h> 41 camera_center_.set( (T)0, (T)0, (T)0 );
52 K_( K ), camera_center_( camera_center ), R_(std::move( R ))
63 K_( K ), R_(std::move( R ))
74 camera_center_(that.camera_center_),
109 if ( !wp_homg.
ideal() )
110 wp.
set( wp_homg.
x()/wp_homg.
w(), wp_homg.
y()/wp_homg.
w(), wp_homg.
z()/wp_homg.
w() );
112 wp.
set( camera_center_.x()+wp_homg.
x(),
113 camera_center_.y()+wp_homg.
y(),
114 camera_center_.z()+wp_homg.
z() );
116 wp = camera_center_ + ( camera_center_-wp );
146 T
dot = world_point.
x()*
l.a() + world_point.
y()*
l.b() +
147 world_point.
z()*
l.c() + world_point.
w()*
l.d();
148 if (world_point.
w() < (T)0)
dot = ((T)(-1))*
dot;
165 camera_center_ = camera_center;
175 camera_center_.
set(cv.
x(), cv.
y(), cv.
z());
191 vgl_vector_3d<T> c(camera_center_.x(), camera_center_.y(),camera_center_.z());
209 bool singularity = std::fabs(std::fabs(static_cast<double>(dp))-1.0)<1e-08;
210 assert(!singularity);
214 if (std::fabs(dot_product<T>(u,z)-T(1))<1e-5)
223 else if (std::fabs(dot_product<T>(u,z)-T(-1))<1e-5)
240 T r[] = { x.
x(), x.
y(), x.
z(),
242 z.
x(), z.
y(), z.
z() };
257 for (
int i = 0; i < 3; i++ )
259 Pnew(0,3) = -camera_center_.x();
260 Pnew(1,3) = -camera_center_.y();
261 Pnew(2,3) = -camera_center_.z();
264 this->set_matrix(K_.get_matrix()*R_.as_matrix()*Pnew);
278 if ( det == (T)0 )
return false;
294 for (
int i = 0; i < 3; i++ )
295 for (
int j = 0; j < 3; j++ )
296 Hf(i,j) = H(2-j,2-i);
301 for (
int i = 0; i < 3; i++ ) {
302 for (
int j = 0; j < 3; j++ ) {
303 Qf(i,j) = q(2-j,2-i);
304 Rf(i,j) = r(2-j,2-i);
310 int r0pos = Rf(0,0) > 0 ? 1 : -1;
311 int r1pos = Rf(1,1) > 0 ? 1 : -1;
312 int r2pos = Rf(2,2) > 0 ? 1 : -1;
313 int diag[3] = { r0pos, r1pos, r2pos };
315 for (
int i = 0; i < 3; i++ ) {
316 for (
int j = 0; j < 3; j++ ) {
317 K1(i,j) = diag[j]*Rf(i,j);
318 R1(i,j) = diag[i]*Qf(i,j);
425 template <
class Type>
435 << t.
x() <<
' ' << t.
y() <<
' ' << t.
z() <<
'\n';
440 template <
class Type>
459 template <
class Type>
462 std::ofstream os(cam_path.c_str());
464 std::cout <<
"unable to open output stream in vpgl_proj_camera<T>::save(.)\n";
472 template <
class Type>
476 str <<
"Transform {\n" 477 <<
"translation " << cent.
x() <<
' ' << cent.
y() <<
' ' 478 <<
' ' << cent.
z() <<
'\n' 481 <<
" appearance Appearance{\n" 482 <<
" material Material\n" 484 <<
" diffuseColor " << 1 <<
' ' << 1.0 <<
' ' << 0.0 <<
'\n' 485 <<
" transparency " << 0.0 <<
'\n' 488 <<
" geometry Sphere\n" 490 <<
" radius " << rad <<
'\n' 496 std::cout<<
"principal axis :" <<r<<std::endl;
503 std::cout<<
"quaternion "<<axis<<
" angle "<<q.
angle()<<
"\n\n";
504 double ang = q.
angle();
505 str <<
"Transform {\n" 506 <<
" translation " << cent.
x()+6*rad*r.
x() <<
' ' << cent.
y()+6*rad*r.
y()
507 <<
' ' << cent.
z()+6*rad*r.
z() <<
'\n' 508 <<
" rotation " << axis[0] <<
' ' << axis[1] <<
' ' << axis[2] <<
' ' << ang <<
'\n' 511 <<
" appearance Appearance{\n" 512 <<
" material Material\n" 514 <<
" diffuseColor 1 0 0\n" 515 <<
" transparency 0\n" 518 <<
" geometry Cylinder\n" 520 <<
" radius "<<rad/3<<
'\n' 521 <<
" height " << 12*rad <<
'\n' 533 std::vector<vpgl_perspective_camera<T> > camlist;
534 if (!vul_file::is_directory(dir.c_str()) ) {
535 std::cerr <<
"cameras_from_directory: " << dir <<
" is not a directory\n";
540 std::string camglob=dir+
"/*";
541 vul_file_iterator file_it(camglob.c_str());
542 std::vector<std::string> cam_files;
544 std::string camName(file_it());
545 cam_files.push_back(camName);
548 std::sort(cam_files.begin(), cam_files.end());
551 for (
auto & cam_file : cam_files)
553 std::ifstream ifs(cam_file.c_str());
555 if (!ifs.is_open()) {
556 std::cerr <<
"Failed to open file " << cam_file <<
'\n';
560 camlist.push_back(pcam);
574 return std::acos((
trace-1.0)/2.0);
608 std::vector<vgl_ray_3d<T> > corner_rays;
617 #undef vpgl_PERSPECTIVE_CAMERA_INSTANTIATE 618 #define vpgl_PERSPECTIVE_CAMERA_INSTANTIATE(T) \ 619 template class vpgl_perspective_camera<T >; \ 620 template bool vpgl_perspective_decomposition(const vnl_matrix_fixed<T,3,4>& camera_matrix, \ 621 vpgl_perspective_camera<T >& p_camera ); \ 622 template vpgl_perspective_camera<T > vpgl_align_down(const vpgl_perspective_camera<T >& p0, \ 623 const vpgl_perspective_camera<T >& p1 ); \ 624 template vpgl_perspective_camera<T > vpgl_align_up(const vpgl_perspective_camera<T >& p0, \ 625 const vpgl_perspective_camera<T >& p1 ); \ 626 template vpgl_perspective_camera<T > postmultiply(const vpgl_perspective_camera<T >& in_cam, \ 627 const vgl_h_matrix_3d<T >& euclid_trans); \ 628 template double vpgl_persp_cam_distance(const vpgl_perspective_camera<T >& cam1, \ 629 const vpgl_perspective_camera<T >& cam2); \ 630 template vgl_vector_3d<T> vpgl_persp_cam_base_line_vector(const vpgl_perspective_camera<T>& cam1, \ 631 const vpgl_perspective_camera<T>& cam2); \ 632 template vgl_rotation_3d<T> vpgl_persp_cam_relative_orientation(const vpgl_perspective_camera<T>& cam1, \ 633 const vpgl_perspective_camera<T>& cam2); \ 634 template void vrml_write(std::ostream &s, const vpgl_perspective_camera<T >&, double rad); \ 635 template std::vector<vpgl_perspective_camera<T > > cameras_from_directory(std::string dir, T); \ 636 template vgl_frustum_3d<T> frustum(vpgl_perspective_camera<T> const& cam, T d_near, T d_far); \ 637 template std::ostream& operator<<(std::ostream&, const vpgl_perspective_camera<T >&); \ 638 template std::istream& operator>>(std::istream&, vpgl_perspective_camera<T >&) 641 #endif // vpgl_perspective_camera_hxx_ static vpgl_perspective_camera< T > postmultiply(const vpgl_perspective_camera< T > &in_cam, const vgl_h_matrix_3d< T > &euclid_trans)
Post-multiply this perspective camera with a 3-d Euclidean transformation.
vnl_matrix_ref< T > as_ref()
A class for the perspective camera model.
void set_rotation(const vgl_rotation_3d< T > &R)
std::istream & operator >>(std::istream &s, vpgl_perspective_camera< Type > &p)
Read camera from stream.
vgl_point_2d< T > principal_point() const
vnl_matrix_fixed< T, 3, 3 > as_matrix() const
vnl_vector_fixed< T, num_rows > get_column(unsigned col) const
vgl_homg_line_3d_2_points< T > backproject(const vgl_homg_point_2d< T > &image_point) const override
Finite backprojection.
vgl_frustum_3d< T > frustum(vpgl_perspective_camera< T > const &cam, T d_near, T d_far)
compute the frustrum of the camera view cone. The near plane.
vgl_h_matrix_3d & set_translation(T tx, T ty, T tz)
vgl_vector_3d< T > principal_axis() const
Compute the principal axis.
void vrml_write(std::ostream &str, vpgl_perspective_camera< Type > const &p, double rad)
Write vpgl_perspective_camera to a vrml file.
vpgl_perspective_camera< T > vpgl_align_down(const vpgl_perspective_camera< T > &p0, const vpgl_perspective_camera< T > &p1)
Changes the coordinate system of camera p1 such that the same change would transform p0 to K[I|0].
A class representing the "K" matrix of a perspective camera matrix as described in.
T vnl_trace(vnl_matrix< T > const &M)
vgl_vector_3d< Type > direction() const
void set_camera_center(const vgl_point_3d< T > &camera_center)
vnl_matrix< T > const & Q() const
vnl_vector_ref< T > as_ref()
vnl_matrix< T > const & R() const
void save(std::string cam_path) override
Save in ascii format.
const vpgl_calibration_matrix< T > & get_calibration() const
const vgl_rotation_3d< T > & get_rotation() const
vnl_vector_fixed< T, 3 > axis() const
vgl_vector_3d< T > vpgl_persp_cam_base_line_vector(const vpgl_perspective_camera< T > &cam1, const vpgl_perspective_camera< T > &cam2)
void set_calibration(const vpgl_calibration_matrix< T > &K)
Setters and getters.
vgl_rotation_3d< T > vpgl_persp_cam_relative_orientation(const vpgl_perspective_camera< T > &cam1, const vpgl_perspective_camera< T > &cam2)
compute rotation such that principal_vector1 = R*principal_vector2.
VNL_EXPORT T vnl_det(T const *row0, T const *row1)
double dot(const double *v1, const double *v2, unsigned n)
void look_at(const vgl_homg_point_3d< T > &point, const vgl_vector_3d< T > &up=vgl_vector_3d< T >(0, 0, 1))
Rotate the camera about its center such that it looks at the given point.
vnl_matrix< T > extract(unsigned r, unsigned c, unsigned top=0, unsigned left=0) const
vgl_h_matrix_3d & set_rotation_matrix(vnl_matrix_fixed< T, 3, 3 > const &R)
void recompute_matrix()
Recalculate the 3x4 camera matrix from the parameters.
vgl_point_3d< Type > point1() const
double vpgl_persp_cam_distance(const vpgl_perspective_camera< T > &cam1, const vpgl_perspective_camera< T > &cam2)
const vgl_point_3d< T > & get_camera_center() const
bool vpgl_perspective_decomposition(const vnl_matrix_fixed< T, 3, 4 > &camera_matrix, vpgl_perspective_camera< T > &p_camera)
Decompose camera into parameter blocks.
vgl_ray_3d< T > backproject_ray(const vgl_point_2d< T > &image_point) const
Finite ray backprojection.
std::ostream & operator<<(std::ostream &s, const vpgl_local_rational_camera< T > &p)
Write to stream.
vpgl_perspective_camera< T > vpgl_align_up(const vpgl_perspective_camera< T > &p0, const vpgl_perspective_camera< T > &p1)
Changes the coordinate system of camera p1 such that the same change would transform K[I|0] to p0.
This class implements the perspective camera class as described in Hartley & Zisserman as a finite ca...
vgl_h_matrix_3d & set_identity()
bool is_behind_camera(const vgl_homg_point_3d< T > &world_point) const
Determine whether the given point lies in front of the principal plane.
vgl_homg_point_3d< T > get_translation() const
void set(T vx, T vy, T vz)
vpgl_perspective_camera()
Default constructor.
vpgl_proj_camera< T > * clone(void) const override
Clone ‘this’: creation of a new object and initialization.
bool ideal(Type tol=(Type) 0) const
T dot_product(v const &a, v const &b)
void set_translation(const vgl_vector_3d< T > &t)
void set(T px, T py, T pz)
bool is_euclidean() const
vgl_rotation_3d< T > transpose() const
std::vector< vpgl_perspective_camera< T > > cameras_from_directory(std::string dir, T)
Return a list of camera's, loaded from the (name sorted) files from the given directory.
T cross_product(v const &a, v const &b)
vgl_h_matrix_3d< T > get_upper_3x3() const
vgl_vector_3d< T > get_translation() const