JWS C++ Library
C++ language utility library
|
00001 /* 00002 * This work is licensed under a Creative Commons 00003 * Attribution-Noncommercial-Share Alike 3.0 United States License. 00004 * 00005 * http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 00006 * 00007 * You are free: 00008 * 00009 * to Share - to copy, distribute, display, and perform the work 00010 * to Remix - to make derivative works 00011 * 00012 * Under the following conditions: 00013 * 00014 * Attribution. You must attribute the work in the manner specified by the 00015 * author or licensor (but not in any way that suggests that they endorse you 00016 * or your use of the work). 00017 * 00018 * Noncommercial. You may not use this work for commercial purposes. 00019 * 00020 * Share Alike. If you alter, transform, or build upon this work, you may 00021 * distribute the resulting work only under the same or similar license to 00022 * this one. 00023 * 00024 * For any reuse or distribution, you must make clear to others the license 00025 * terms of this work. The best way to do this is by including this header. 00026 * 00027 * Any of the above conditions can be waived if you get permission from the 00028 * copyright holder. 00029 * 00030 * Apart from the remix rights granted under this license, nothing in this 00031 * license impairs or restricts the author's moral rights. 00032 */ 00033 00034 00049 #ifndef CAMERA_H 00050 #define CAMERA_H 00051 00052 00053 #include <jwsc++/config.h> 00054 00055 #include <iostream> 00056 00057 #if defined JWSCXX_HAVE_OPENGL 00058 #include <GL/gl.h> 00059 #elif defined JWSCXX_HAVE_OPENGL_FRAMEWORK 00060 #include <OpenGL/gl.h> 00061 #endif 00062 00063 #include <jwsc/vector/vector.h> 00064 #include <jwsc/matrix/matrix.h> 00065 #include <jwsc/image/image.h> 00066 00067 #include <jwsc++/base/exception.h> 00068 #include <jwsc++/base/cloneable.h> 00069 #include <jwsc++/base/readable.h> 00070 #include <jwsc++/base/writeable.h> 00071 00072 00073 namespace jwscxx { 00074 namespace graphics { 00075 00076 00084 class Camera_f : public jwscxx::base::Cloneable, 00085 public jwscxx::base::Writeable, 00086 public jwscxx::base::Readable 00087 { 00088 public: 00089 00091 Camera_f 00092 ( 00093 float focal_pt_x, 00094 float focal_pt_y, 00095 float focal_pt_z, 00096 float ref_pt_x, 00097 float ref_pt_y, 00098 float ref_pt_z, 00099 float up_x, 00100 float up_y, 00101 float up_z, 00102 float aperture, 00103 float clip_near, 00104 float clip_far 00105 ) 00106 throw (jwscxx::base::Arg_error); 00107 00108 00110 Camera_f 00111 ( 00112 const jwsc::Vector_f* focal_pt, 00113 const jwsc::Vector_f* ref_pt, 00114 const jwsc::Vector_f* up, 00115 float aperture, 00116 float clip_near, 00117 float clip_far 00118 ) 00119 throw (jwscxx::base::Arg_error); 00120 00121 00123 Camera_f(const Camera_f& c); 00124 00125 00127 Camera_f(const char* fname) throw (jwscxx::base::Arg_error, 00128 jwscxx::base::IO_error); 00129 00130 00132 Camera_f(std::istream& in) throw (jwscxx::base::Arg_error, 00133 jwscxx::base::IO_error); 00134 00135 00137 virtual ~Camera_f(); 00138 00139 00141 virtual Camera_f& operator= (const Camera_f& c); 00142 00143 00145 virtual Camera_f* clone() const; 00146 00147 00149 const jwsc::Vector_f* get_u() const; 00150 00151 00153 const jwsc::Vector_f* get_v() const; 00154 00155 00157 const jwsc::Vector_f* get_n() const; 00158 00159 00161 const jwsc::Vector_f* get_prp() const; 00162 00163 00165 const jwsc::Vector_f* get_vrp() const; 00166 00167 00169 float get_clipping_near() const; 00170 00171 00173 float get_clipping_far() const; 00174 00175 00177 float get_focal_length() const; 00178 00179 00181 float get_aperture() const; 00182 00183 00188 float get_half_focal_height() const; 00189 00190 00195 virtual void set_f_with_new_aperture_using_prp(float f) 00196 throw (jwscxx::base::Arg_error); 00197 00198 00203 virtual void set_f_with_same_aperture_using_prp(float f) 00204 throw (jwscxx::base::Arg_error); 00205 00206 00211 virtual void set_f_with_new_aperture_using_vrp(float f) 00212 throw (jwscxx::base::Arg_error); 00213 00214 00219 virtual void set_f_with_same_aperture_using_vrp(float f) 00220 throw (jwscxx::base::Arg_error); 00221 00222 00224 virtual void set_aperture(float aperture) 00225 throw (jwscxx::base::Arg_error); 00226 00227 00229 virtual void set_clipping(float clip_near, float clip_far) 00230 throw (jwscxx::base::Arg_error); 00231 00232 00234 virtual void rotate(float angle, float x, float y, float z) 00235 throw (jwscxx::base::Arg_error); 00236 00237 00239 virtual void rotate(float angle, const jwsc::Vector_f* r) 00240 throw (jwscxx::base::Arg_error); 00241 00242 00244 virtual void translate(float x, float y, float z); 00245 00246 00248 virtual void translate(const jwsc::Vector_f* t) 00249 throw (jwscxx::base::Arg_error); 00250 00251 00256 virtual void look_at 00257 ( 00258 float focal_pt_x, 00259 float focal_pt_y, 00260 float focal_pt_z, 00261 float ref_pt_x, 00262 float ref_pt_y, 00263 float ref_pt_z, 00264 float up_x, 00265 float up_y, 00266 float up_z 00267 ) 00268 throw (jwscxx::base::Arg_error); 00269 00270 00275 void look_at 00276 ( 00277 const jwsc::Vector_f* focal_pt, 00278 const jwsc::Vector_f* ref_pt, 00279 const jwsc::Vector_f* up 00280 ) 00281 throw (jwscxx::base::Arg_error); 00282 00283 00285 virtual void set_gl_modelview() const; 00286 00287 00289 virtual void set_gl_projection() const; 00290 00291 00296 void get_image_pt_on_focal_plane 00297 ( 00298 jwsc::Vector_f** pt_out, 00299 float x, 00300 float y 00301 ); 00302 00303 00308 void get_image_pt_on_focal_plane 00309 ( 00310 jwsc::Vector_f** pt_out, 00311 const jwsc::Vector_f* pt_in 00312 ) 00313 throw (jwscxx::base::Arg_error); 00314 00315 00320 void get_image_pt_on_near_clipping_plane 00321 ( 00322 jwsc::Vector_f** pt_out, 00323 float x, 00324 float y 00325 ); 00326 00327 00332 void get_image_pt_on_near_clipping_plane 00333 ( 00334 jwsc::Vector_f** pt_out, 00335 const jwsc::Vector_f* pt_in 00336 ) 00337 throw (jwscxx::base::Arg_error); 00338 00339 00344 void get_image_pt_on_far_clipping_plane 00345 ( 00346 jwsc::Vector_f** pt_out, 00347 float x, 00348 float y 00349 ); 00350 00351 00356 void get_image_pt_on_far_clipping_plane 00357 ( 00358 jwsc::Vector_f** pt_out, 00359 const jwsc::Vector_f* pt_in 00360 ) 00361 throw (jwscxx::base::Arg_error); 00362 00363 00368 static float get_gl_viewport_aspect(); 00369 00370 00372 static float get_gl_viewport_width(); 00373 00374 00376 static float get_gl_viewport_height(); 00377 00378 00380 static void capture_gl_view(jwsc::Image_f** img_out); 00381 00382 00384 static void capture_gl_view(const char* fname) 00385 throw (jwscxx::base::IO_error); 00386 00387 00389 static void capture_gl_view(const char* fname_base, uint32_t N) 00390 throw (jwscxx::base::IO_error); 00391 00392 00394 virtual void read(std::istream& in) throw (jwscxx::base::IO_error, 00395 jwscxx::base::Arg_error); 00396 00397 00399 virtual void write(std::ostream& out) const 00400 throw (jwscxx::base::IO_error); 00401 00402 00403 protected: 00404 00406 jwsc::Vector_f* vrp; 00407 00409 jwsc::Vector_f* prp; 00410 00412 jwsc::Vector_f* u; 00413 00415 jwsc::Vector_f* v; 00416 00418 jwsc::Vector_f* n; 00419 00421 float aperture; 00422 00424 float focal_length; 00425 00427 float half_focal_height; 00428 00430 float half_near_height; 00431 00433 float near; 00434 00436 float far; 00437 00439 jwsc::Matrix_f* R_phi; 00440 00442 jwsc::Matrix_f* R_theta; 00443 00445 jwsc::Matrix_f* R_psi; 00446 00448 jwsc::Matrix_f* R; 00449 }; 00450 00451 00452 00453 00461 class Camera_d : public jwscxx::base::Cloneable, 00462 public jwscxx::base::Writeable, 00463 public jwscxx::base::Readable 00464 { 00465 public: 00466 00468 Camera_d 00469 ( 00470 double focal_pt_x, 00471 double focal_pt_y, 00472 double focal_pt_z, 00473 double ref_pt_x, 00474 double ref_pt_y, 00475 double ref_pt_z, 00476 double up_x, 00477 double up_y, 00478 double up_z, 00479 double aperture, 00480 double clip_near, 00481 double clip_far 00482 ) 00483 throw (jwscxx::base::Arg_error); 00484 00485 00487 Camera_d 00488 ( 00489 const jwsc::Vector_d* focal_pt, 00490 const jwsc::Vector_d* ref_pt, 00491 const jwsc::Vector_d* up, 00492 double aperture, 00493 double clip_near, 00494 double clip_far 00495 ) 00496 throw (jwscxx::base::Arg_error); 00497 00498 00500 Camera_d(const Camera_d& c); 00501 00502 00504 Camera_d(const char* fname) throw (jwscxx::base::Arg_error, 00505 jwscxx::base::IO_error); 00506 00507 00509 Camera_d(std::istream& in) throw (jwscxx::base::Arg_error, 00510 jwscxx::base::IO_error); 00511 00512 00514 virtual ~Camera_d(); 00515 00516 00518 virtual Camera_d& operator= (const Camera_d& c); 00519 00520 00522 virtual Camera_d* clone() const; 00523 00524 00526 const jwsc::Vector_d* get_u() const; 00527 00528 00530 const jwsc::Vector_d* get_v() const; 00531 00532 00538 const jwsc::Vector_d* get_n() const; 00539 00540 00542 const jwsc::Vector_d* get_prp() const; 00543 00544 00546 const jwsc::Vector_d* get_vrp() const; 00547 00548 00550 double get_clipping_near() const; 00551 00552 00554 double get_clipping_far() const; 00555 00556 00558 double get_focal_length() const; 00559 00560 00562 double get_aperture() const; 00563 00564 00569 double get_half_focal_height() const; 00570 00571 00576 virtual void set_f_with_new_aperture_using_prp(double f) 00577 throw (jwscxx::base::Arg_error); 00578 00579 00584 virtual void set_f_with_same_aperture_using_prp(double f) 00585 throw (jwscxx::base::Arg_error); 00586 00587 00592 virtual void set_f_with_new_aperture_using_vrp(double f) 00593 throw (jwscxx::base::Arg_error); 00594 00595 00600 virtual void set_f_with_same_aperture_using_vrp(double f) 00601 throw (jwscxx::base::Arg_error); 00602 00603 00605 virtual void set_aperture(double aperture) 00606 throw (jwscxx::base::Arg_error); 00607 00608 00610 virtual void set_clipping(double clip_near, double clip_far) 00611 throw (jwscxx::base::Arg_error); 00612 00613 00615 virtual void rotate(double angle, double x, double y, double z) 00616 throw (jwscxx::base::Arg_error); 00617 00618 00620 virtual void rotate(double angle, const jwsc::Vector_d* r) 00621 throw (jwscxx::base::Arg_error); 00622 00623 00625 virtual void translate(double x, double y, double z); 00626 00627 00629 virtual void translate(const jwsc::Vector_d* t) 00630 throw (jwscxx::base::Arg_error); 00631 00632 00637 virtual void look_at 00638 ( 00639 double focal_pt_x, 00640 double focal_pt_y, 00641 double focal_pt_z, 00642 double ref_pt_x, 00643 double ref_pt_y, 00644 double ref_pt_z, 00645 double up_x, 00646 double up_y, 00647 double up_z 00648 ) 00649 throw (jwscxx::base::Arg_error); 00650 00651 00656 void look_at 00657 ( 00658 const jwsc::Vector_d* focal_pt, 00659 const jwsc::Vector_d* ref_pt, 00660 const jwsc::Vector_d* up 00661 ) 00662 throw (jwscxx::base::Arg_error); 00663 00664 00666 virtual void set_gl_modelview() const; 00667 00668 00670 virtual void set_gl_projection() const; 00671 00672 00677 void get_image_pt_on_focal_plane 00678 ( 00679 jwsc::Vector_d** pt_out, 00680 double x, 00681 double y 00682 ); 00683 00684 00689 void get_image_pt_on_focal_plane 00690 ( 00691 jwsc::Vector_d** pt_out, 00692 const jwsc::Vector_d* pt_in 00693 ) 00694 throw (jwscxx::base::Arg_error); 00695 00696 00701 void get_image_pt_on_near_clipping_plane 00702 ( 00703 jwsc::Vector_d** pt_out, 00704 double x, 00705 double y 00706 ); 00707 00708 00713 void get_image_pt_on_near_clipping_plane 00714 ( 00715 jwsc::Vector_d** pt_out, 00716 const jwsc::Vector_d* pt_in 00717 ) 00718 throw (jwscxx::base::Arg_error); 00719 00720 00725 void get_image_pt_on_far_clipping_plane 00726 ( 00727 jwsc::Vector_d** pt_out, 00728 double x, 00729 double y 00730 ); 00731 00732 00737 void get_image_pt_on_far_clipping_plane 00738 ( 00739 jwsc::Vector_d** pt_out, 00740 const jwsc::Vector_d* pt_in 00741 ) 00742 throw (jwscxx::base::Arg_error); 00743 00744 00749 static double get_gl_viewport_aspect(); 00750 00751 00753 static double get_gl_viewport_width(); 00754 00755 00757 static double get_gl_viewport_height(); 00758 00759 00761 static void capture_gl_view(jwsc::Image_f** img_out); 00762 00763 00765 static void capture_gl_view(const char* fname) 00766 throw (jwscxx::base::IO_error); 00767 00768 00770 static void capture_gl_view(const char* fname_base, uint32_t N) 00771 throw (jwscxx::base::IO_error); 00772 00773 00775 virtual void read(std::istream& in) throw (jwscxx::base::IO_error, 00776 jwscxx::base::Arg_error); 00777 00778 00780 virtual void write(std::ostream& out) const 00781 throw (jwscxx::base::IO_error); 00782 00783 00784 protected: 00785 00787 jwsc::Vector_d* vrp; 00788 00790 jwsc::Vector_d* prp; 00791 00793 jwsc::Vector_d* u; 00794 00796 jwsc::Vector_d* v; 00797 00799 jwsc::Vector_d* n; 00800 00802 double aperture; 00803 00805 double focal_length; 00806 00808 double half_focal_height; 00809 00811 double half_near_height; 00812 00814 double near; 00815 00817 double far; 00818 00820 jwsc::Matrix_d* R_phi; 00821 00823 jwsc::Matrix_d* R_theta; 00824 00826 jwsc::Matrix_d* R_psi; 00827 00829 jwsc::Matrix_d* R; 00830 }; 00831 00832 00833 00834 00842 class Stereo_camera_f : public Camera_f 00843 { 00844 public: 00845 00847 Stereo_camera_f 00848 ( 00849 float focal_pt_x, 00850 float focal_pt_y, 00851 float focal_pt_z, 00852 float ref_pt_x, 00853 float ref_pt_y, 00854 float ref_pt_z, 00855 float up_x, 00856 float up_y, 00857 float up_z, 00858 float aperture, 00859 float clip_near, 00860 float clip_far 00861 ) 00862 throw (jwscxx::base::Arg_error); 00863 00864 00866 Stereo_camera_f 00867 ( 00868 const jwsc::Vector_f* focal_pt, 00869 const jwsc::Vector_f* ref_pt, 00870 const jwsc::Vector_f* up, 00871 float aperture, 00872 float clip_near, 00873 float clip_far 00874 ) 00875 throw (jwscxx::base::Arg_error); 00876 00877 00879 Stereo_camera_f(const Stereo_camera_f& c); 00880 00881 00883 Stereo_camera_f(const char* fname) throw (jwscxx::base::Arg_error, 00884 jwscxx::base::IO_error); 00885 00886 00888 Stereo_camera_f(std::istream& in) throw (jwscxx::base::Arg_error, 00889 jwscxx::base::IO_error); 00890 00891 00893 virtual ~Stereo_camera_f(); 00894 00895 00897 virtual Stereo_camera_f& operator= (const Stereo_camera_f& c); 00898 00899 00901 virtual Stereo_camera_f* clone() const; 00902 00903 00905 const jwsc::Vector_f* get_left_prp() const; 00906 00907 00909 const jwsc::Vector_f* get_right_prp() const; 00910 00911 00916 const jwsc::Vector_f* get_left_vrp() const; 00917 00918 00923 const jwsc::Vector_f* get_right_vrp() const; 00924 00925 00927 void set_aperture(float aperture) throw (jwscxx::base::Arg_error); 00928 00929 00934 void set_f_with_new_aperture_using_prp(float f) 00935 throw (jwscxx::base::Arg_error); 00936 00937 00942 void set_f_with_same_aperture_using_prp(float f) 00943 throw (jwscxx::base::Arg_error); 00944 00945 00950 void set_f_with_new_aperture_using_vrp(float f) 00951 throw (jwscxx::base::Arg_error); 00952 00953 00958 void set_f_with_same_aperture_using_vrp(float f) 00959 throw (jwscxx::base::Arg_error); 00960 00961 00962 00964 void set_clipping(float clip_near, float clip_far) 00965 throw (jwscxx::base::Arg_error); 00966 00967 00969 void rotate(float angle, float x, float y, float z) 00970 throw (jwscxx::base::Arg_error); 00971 00972 00974 void rotate(float angle, const jwsc::Vector_f* r) 00975 throw (jwscxx::base::Arg_error); 00976 00977 00979 void translate(float x, float y, float z); 00980 00981 00983 void translate(const jwsc::Vector_f* t) 00984 throw (jwscxx::base::Arg_error); 00985 00986 00991 void look_at 00992 ( 00993 float focal_pt_x, 00994 float focal_pt_y, 00995 float focal_pt_z, 00996 float ref_pt_x, 00997 float ref_pt_y, 00998 float ref_pt_z, 00999 float up_x, 01000 float up_y, 01001 float up_z 01002 ) 01003 throw (jwscxx::base::Arg_error); 01004 01005 01007 void set_left_gl_modelview(); 01008 01009 01011 void set_right_gl_modelview(); 01012 01013 01015 void set_left_gl_projection(); 01016 01017 01019 void set_right_gl_projection(); 01020 01021 01023 void read(std::istream& in) throw (jwscxx::base::IO_error, 01024 jwscxx::base::Arg_error); 01025 01026 01028 void write(std::ostream& out) const throw (jwscxx::base::IO_error); 01029 01030 01031 protected: 01032 01034 jwsc::Vector_f* left_vrp; 01035 01037 jwsc::Vector_f* right_vrp; 01038 01040 jwsc::Vector_f* left_prp; 01041 01043 jwsc::Vector_f* right_prp; 01044 01046 float separation; 01047 01052 float ndfl; 01053 }; 01054 01055 01056 01057 01065 class Stereo_camera_d : public Camera_d 01066 { 01067 public: 01068 01070 Stereo_camera_d 01071 ( 01072 double focal_pt_x, 01073 double focal_pt_y, 01074 double focal_pt_z, 01075 double ref_pt_x, 01076 double ref_pt_y, 01077 double ref_pt_z, 01078 double up_x, 01079 double up_y, 01080 double up_z, 01081 double aperture, 01082 double clip_near, 01083 double clip_far 01084 ) 01085 throw (jwscxx::base::Arg_error); 01086 01087 01089 Stereo_camera_d 01090 ( 01091 const jwsc::Vector_d* focal_pt, 01092 const jwsc::Vector_d* ref_pt, 01093 const jwsc::Vector_d* up, 01094 double aperture, 01095 double clip_near, 01096 double clip_far 01097 ) 01098 throw (jwscxx::base::Arg_error); 01099 01100 01102 Stereo_camera_d(const Stereo_camera_d& c); 01103 01104 01106 Stereo_camera_d(const char* fname) throw (jwscxx::base::Arg_error, 01107 jwscxx::base::IO_error); 01108 01109 01111 Stereo_camera_d(std::istream& in) throw (jwscxx::base::Arg_error, 01112 jwscxx::base::IO_error); 01113 01114 01116 virtual ~Stereo_camera_d(); 01117 01118 01120 virtual Stereo_camera_d& operator= (const Stereo_camera_d& c); 01121 01122 01124 virtual Stereo_camera_d* clone() const; 01125 01126 01128 const jwsc::Vector_d* get_left_prp() const; 01129 01130 01132 const jwsc::Vector_d* get_right_prp() const; 01133 01134 01139 const jwsc::Vector_d* get_left_vrp() const; 01140 01141 01146 const jwsc::Vector_d* get_right_vrp() const; 01147 01148 01150 void set_aperture(double aperture) throw (jwscxx::base::Arg_error); 01151 01152 01157 void set_f_with_new_aperture_using_prp(double f) 01158 throw (jwscxx::base::Arg_error); 01159 01160 01165 void set_f_with_same_aperture_using_prp(double f) 01166 throw (jwscxx::base::Arg_error); 01167 01168 01173 void set_f_with_new_aperture_using_vrp(double f) 01174 throw (jwscxx::base::Arg_error); 01175 01176 01181 void set_f_with_same_aperture_using_vrp(double f) 01182 throw (jwscxx::base::Arg_error); 01183 01184 01185 01187 void set_clipping(double clip_near, double clip_far) 01188 throw (jwscxx::base::Arg_error); 01189 01190 01192 void rotate(double angle, double x, double y, double z) 01193 throw (jwscxx::base::Arg_error); 01194 01195 01197 void rotate(double angle, const jwsc::Vector_d* r) 01198 throw (jwscxx::base::Arg_error); 01199 01200 01202 void translate(double x, double y, double z); 01203 01204 01206 void translate(const jwsc::Vector_d* t) 01207 throw (jwscxx::base::Arg_error); 01208 01209 01214 void look_at 01215 ( 01216 double focal_pt_x, 01217 double focal_pt_y, 01218 double focal_pt_z, 01219 double ref_pt_x, 01220 double ref_pt_y, 01221 double ref_pt_z, 01222 double up_x, 01223 double up_y, 01224 double up_z 01225 ) 01226 throw (jwscxx::base::Arg_error); 01227 01228 01230 void set_left_gl_modelview(); 01231 01232 01234 void set_right_gl_modelview(); 01235 01236 01238 void set_left_gl_projection(); 01239 01240 01242 void set_right_gl_projection(); 01243 01244 01246 void read(std::istream& in) throw (jwscxx::base::IO_error, 01247 jwscxx::base::Arg_error); 01248 01249 01251 void write(std::ostream& out) const throw (jwscxx::base::IO_error); 01252 01253 01254 protected: 01255 01257 jwsc::Vector_d* left_vrp; 01258 01260 jwsc::Vector_d* right_vrp; 01261 01263 jwsc::Vector_d* left_prp; 01264 01266 jwsc::Vector_d* right_prp; 01267 01269 double separation; 01270 01275 double ndfl; 01276 }; 01277 01278 01279 }} 01280 01281 01282 #endif