Camera.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_CAMERA_H
  9. #define IGL_CAMERA_H
  10. // you're idiot, M$!
  11. #if defined(_WIN32)
  12. #undef far
  13. #undef near
  14. #endif
  15. #include <Eigen/Geometry>
  16. #include <Eigen/Core>
  17. #define IGL_CAMERA_MIN_ANGLE 5.0
  18. namespace igl
  19. {
  20. // A simple camera class. The camera stores projection parameters (field of
  21. // view angle, aspect ratio, near and far clips) as well as a rigid
  22. // tranformation *of the camera as if it were also a scene object*. Thus, the
  23. // **inverse** of this rigid transformation is the modelview transformation.
  24. class Camera
  25. {
  26. public:
  27. // On windows you might need: -fno-delayed-template-parsing
  28. //static constexpr double IGL_CAMERA_MIN_ANGLE = 5.;
  29. // m_angle Field of view angle in degrees {45}
  30. // m_aspect Aspect ratio {1}
  31. // m_near near clipping plane {1e-2}
  32. // m_far far clipping plane {100}
  33. // m_at_dist distance of looking at point {1}
  34. // m_rotation_conj Conjugate of rotation part of rigid transformation of
  35. // camera {identity}. Note: we purposefully store the conjugate because
  36. // this is what TW_TYPE_QUAT4D is expecting.
  37. // m_translation Translation part of rigid transformation of camera
  38. // {(0,0,1)}
  39. double m_angle, m_aspect, m_near, m_far, m_at_dist;
  40. Eigen::Quaterniond m_rotation_conj;
  41. Eigen::Vector3d m_translation;
  42. private:
  43. // m_at_dist_min_angle m_at_dist from last time m_angle set to <= IGL_CAMERA_MIN_ANGLE
  44. double m_at_dist_min_angle;
  45. double m_angle_min_angle;
  46. // // m_last_positive_m_angle
  47. // // m_last_positive_m_angle_m_at_dist
  48. // double m_last_positive_m_angle,m_last_positive_m_angle_m_at_dist;
  49. public:
  50. inline Camera();
  51. inline virtual ~Camera(){}
  52. // Return projection matrix that takes relative camera coordinates and
  53. // transforms it to viewport coordinates
  54. //
  55. // Note:
  56. //
  57. // if(m_angle > 0)
  58. // {
  59. // gluPerspective(m_angle,m_aspect,m_near,m_at_dist+m_far);
  60. // }else
  61. // {
  62. // gluOrtho(-0.5*aspect,0.5*aspect,-0.5,0.5,m_at_dist+m_near,m_far);
  63. // }
  64. //
  65. // Is equivalent to
  66. //
  67. // glMultMatrixd(projection().data());
  68. //
  69. inline Eigen::Matrix4d projection() const;
  70. // Return an Affine transformation (rigid actually) that takes a world 3d coordinate and
  71. // transforms it into the relative camera coordinates.
  72. inline Eigen::Affine3d affine() const;
  73. // Return an Affine transformation (rigid actually) that takes relative
  74. // coordinates and tramsforms them into world 3d coordinates.
  75. //
  76. // Note:
  77. //
  78. // gluLookAt(
  79. // eye()(0), eye()(1), eye()(2),
  80. // at()(0), at()(1), at()(2),
  81. // up()(0), up()(1), up()(2));
  82. //
  83. // Is equivalent to
  84. //
  85. // glMultMatrixd(camera.affine().matrix().data());
  86. //
  87. // See also: affine, eye, at, up
  88. inline Eigen::Affine3d inverse() const;
  89. // Returns world coordinates position of center or "eye" of camera.
  90. inline Eigen::Vector3d eye() const;
  91. // Returns world coordinate position of a point "eye" is looking at.
  92. inline Eigen::Vector3d at() const;
  93. // Returns world coordinate unit vector of "up" vector
  94. inline Eigen::Vector3d up() const;
  95. // Return top right corner of unit plane in relative coordinates, that is
  96. // (w/2,h/2,1)
  97. inline Eigen::Vector3d unit_plane() const;
  98. // Move dv in the relative coordinate frame of the camera (move the FPS)
  99. //
  100. // Inputs:
  101. // dv (x,y,z) displacement vector
  102. //
  103. inline void dolly(const Eigen::Vector3d & dv);
  104. // "Scale zoom": Move `eye`, but leave `at`
  105. //
  106. // Input:
  107. // s amount to scale distance to at
  108. inline void push_away(const double s);
  109. // Aka "Hitchcock", "Vertigo", "Spielberg" or "Trombone" zoom:
  110. // simultaneously dolly while changing angle so that `at` not only stays
  111. // put in relative coordinates but also projected coordinates. That is
  112. //
  113. // Inputs:
  114. // da change in angle in degrees
  115. inline void dolly_zoom(const double da);
  116. // Turn around eye so that rotation is now q
  117. //
  118. // Inputs:
  119. // q new rotation as quaternion
  120. inline void turn_eye(const Eigen::Quaterniond & q);
  121. // Orbit around at so that rotation is now q
  122. //
  123. // Inputs:
  124. // q new rotation as quaternion
  125. inline void orbit(const Eigen::Quaterniond & q);
  126. // Rotate and translate so that camera is situated at "eye" looking at "at"
  127. // with "up" pointing up.
  128. //
  129. // Inputs:
  130. // eye (x,y,z) coordinates of eye position
  131. // at (x,y,z) coordinates of at position
  132. // up (x,y,z) coordinates of up vector
  133. inline void look_at(
  134. const Eigen::Vector3d & eye,
  135. const Eigen::Vector3d & at,
  136. const Eigen::Vector3d & up);
  137. // Needed any time Eigen Structures are used as class members
  138. // http://eigen.tuxfamily.org/dox-devel/group__TopicStructHavingEigenMembers.html
  139. public:
  140. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  141. };
  142. }
  143. // Implementation
  144. #include "PI.h"
  145. #include "EPS.h"
  146. #include <cmath>
  147. #include <iostream>
  148. #include <cassert>
  149. inline igl::Camera::Camera():
  150. m_angle(45.0),m_aspect(1),m_near(1e-2),m_far(100),m_at_dist(1),
  151. m_rotation_conj(1,0,0,0),
  152. m_translation(0,0,1),
  153. m_at_dist_min_angle(m_at_dist),
  154. m_angle_min_angle(m_angle)
  155. {
  156. }
  157. inline Eigen::Matrix4d igl::Camera::projection() const
  158. {
  159. Eigen::Matrix4d P;
  160. using namespace std;
  161. // http://stackoverflow.com/a/3738696/148668
  162. if(m_angle >= IGL_CAMERA_MIN_ANGLE)
  163. {
  164. const double yScale = tan(PI*0.5 - 0.5*m_angle*PI/180.);
  165. // http://stackoverflow.com/a/14975139/148668
  166. const double xScale = yScale/m_aspect;
  167. const double far = m_at_dist + m_far;
  168. const double near = m_near;
  169. P<<
  170. xScale, 0, 0, 0,
  171. 0, yScale, 0, 0,
  172. 0, 0, -(far+near)/(far-near), -1,
  173. 0, 0, -2.*near*far/(far-near), 0;
  174. P = P.transpose().eval();
  175. }else
  176. {
  177. const double f = 0.5;
  178. const double left = -f*m_aspect;
  179. const double right = f*m_aspect;
  180. const double bottom = -f;
  181. const double top = f;
  182. const double near = m_near;
  183. const double far = m_at_dist + m_far;
  184. const double tx = (right+left)/(right-left);
  185. const double ty = (top+bottom)/(top-bottom);
  186. const double tz = (far+near)/(far-near);
  187. const double z_fix =
  188. 0.5/(m_at_dist_min_angle * tan(m_angle_min_angle/2./180.*M_PI))+
  189. (-m_at_dist+m_at_dist_min_angle)/m_at_dist_min_angle;
  190. P<<
  191. z_fix*2./(right-left), 0, 0, -tx,
  192. 0, z_fix*2./(top-bottom), 0, -ty,
  193. 0, 0, -z_fix*2./(far-near), -tz,
  194. 0, 0, 0, 1;
  195. }
  196. return P;
  197. }
  198. inline Eigen::Affine3d igl::Camera::affine() const
  199. {
  200. using namespace Eigen;
  201. Affine3d t = Affine3d::Identity();
  202. t.rotate(m_rotation_conj.conjugate());
  203. t.translate(m_translation);
  204. return t;
  205. }
  206. inline Eigen::Affine3d igl::Camera::inverse() const
  207. {
  208. using namespace Eigen;
  209. Affine3d t = Affine3d::Identity();
  210. t.translate(-m_translation);
  211. t.rotate(m_rotation_conj);
  212. return t;
  213. }
  214. inline Eigen::Vector3d igl::Camera::eye() const
  215. {
  216. using namespace Eigen;
  217. return affine() * Vector3d(0,0,0);
  218. }
  219. inline Eigen::Vector3d igl::Camera::at() const
  220. {
  221. using namespace Eigen;
  222. return affine() * (Vector3d(0,0,-1)*m_at_dist);
  223. }
  224. inline Eigen::Vector3d igl::Camera::up() const
  225. {
  226. using namespace Eigen;
  227. Affine3d t = Affine3d::Identity();
  228. t.rotate(m_rotation_conj.conjugate());
  229. return t * Vector3d(0,1,0);
  230. }
  231. inline Eigen::Vector3d igl::Camera::unit_plane() const
  232. {
  233. // Distance of center pixel to eye
  234. const double d = 1.0;
  235. const double a = m_aspect;
  236. const double theta = m_angle*PI/180.;
  237. const double w =
  238. 2.*sqrt(-d*d/(a*a*pow(tan(0.5*theta),2.)-1.))*a*tan(0.5*theta);
  239. const double h = w/a;
  240. return Eigen::Vector3d(w*0.5,h*0.5,-d);
  241. }
  242. inline void igl::Camera::dolly(const Eigen::Vector3d & dv)
  243. {
  244. m_translation += dv;
  245. }
  246. inline void igl::Camera::push_away(const double s)
  247. {
  248. using namespace Eigen;
  249. #ifndef NDEBUG
  250. Vector3d old_at = at();
  251. #endif
  252. const double old_at_dist = m_at_dist;
  253. m_at_dist = old_at_dist * s;
  254. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  255. assert((old_at-at()).squaredNorm() < DOUBLE_EPS);
  256. }
  257. inline void igl::Camera::dolly_zoom(const double da)
  258. {
  259. using namespace std;
  260. using namespace Eigen;
  261. #ifndef NDEBUG
  262. Vector3d old_at = at();
  263. #endif
  264. const double old_angle = m_angle;
  265. m_angle += da;
  266. m_angle = min(89.,max(0.,m_angle));
  267. const double eff_angle = (IGL_CAMERA_MIN_ANGLE > m_angle ? IGL_CAMERA_MIN_ANGLE : m_angle);
  268. if(old_angle >= IGL_CAMERA_MIN_ANGLE)
  269. {
  270. // change in distance
  271. const double s =
  272. (2.*tan(old_angle/2./180.*M_PI)) /
  273. (2.*tan(eff_angle/2./180.*M_PI)) ;
  274. const double old_at_dist = m_at_dist;
  275. m_at_dist = old_at_dist * s;
  276. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  277. if(eff_angle == IGL_CAMERA_MIN_ANGLE)
  278. {
  279. m_at_dist_min_angle = m_at_dist;
  280. m_angle_min_angle = eff_angle;
  281. }
  282. assert((old_at-at()).squaredNorm() < DOUBLE_EPS);
  283. }else if(old_angle < IGL_CAMERA_MIN_ANGLE && m_angle >= IGL_CAMERA_MIN_ANGLE)
  284. {
  285. // Restore decent length
  286. const double z_fix =
  287. // There should be some factor here based on the incoming angle
  288. // (m_angle_min_angle) and outgoing angle (m_angle)... For now I set it
  289. // to 1. (assumes equality)
  290. //0.5/(m_at_dist_min_angle * tan(m_angle_min_angle/2./180.*M_PI))+
  291. 1.+(-m_at_dist+m_at_dist_min_angle)/m_at_dist_min_angle;
  292. m_at_dist = m_at_dist_min_angle / z_fix;
  293. }
  294. }
  295. inline void igl::Camera::turn_eye(const Eigen::Quaterniond & q)
  296. {
  297. using namespace Eigen;
  298. Vector3d old_eye = eye();
  299. // eye should be fixed
  300. //
  301. // eye_1 = R_1 * t_1 = eye_0
  302. // t_1 = R_1' * eye_0
  303. m_rotation_conj = q.conjugate();
  304. m_translation = m_rotation_conj * old_eye;
  305. assert((old_eye - eye()).squaredNorm() < DOUBLE_EPS);
  306. }
  307. inline void igl::Camera::orbit(const Eigen::Quaterniond & q)
  308. {
  309. using namespace Eigen;
  310. Vector3d old_at = at();
  311. // at should be fixed
  312. //
  313. // at_1 = R_1 * t_1 - R_1 * z = at_0
  314. // t_1 = R_1' * (at_0 + R_1 * z)
  315. m_rotation_conj = q.conjugate();
  316. m_translation =
  317. m_rotation_conj *
  318. (old_at +
  319. m_rotation_conj.conjugate() * Vector3d(0,0,1) * m_at_dist);
  320. assert((old_at - at()).squaredNorm() < DOUBLE_EPS);
  321. }
  322. inline void igl::Camera::look_at(
  323. const Eigen::Vector3d & eye,
  324. const Eigen::Vector3d & at,
  325. const Eigen::Vector3d & up)
  326. {
  327. using namespace Eigen;
  328. using namespace std;
  329. // http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml
  330. // Normalize vector from at to eye
  331. Vector3d F = eye-at;
  332. m_at_dist = F.norm();
  333. F.normalize();
  334. // Project up onto plane orthogonal to F and normalize
  335. assert(up.cross(F).norm() > DOUBLE_EPS && "(eye-at) x up ≈ 0");
  336. const Vector3d proj_up = (up-(up.dot(F))*F).normalized();
  337. Quaterniond a,b;
  338. a.setFromTwoVectors(Vector3d(0,0,-1),-F);
  339. b.setFromTwoVectors(a*Vector3d(0,1,0),proj_up);
  340. m_rotation_conj = (b*a).conjugate();
  341. m_translation = m_rotation_conj * eye;
  342. //cout<<"m_at_dist: "<<m_at_dist<<endl;
  343. //cout<<"proj_up: "<<proj_up.transpose()<<endl;
  344. //cout<<"F: "<<F.transpose()<<endl;
  345. //cout<<"eye(): "<<this->eye().transpose()<<endl;
  346. //cout<<"at(): "<<this->at().transpose()<<endl;
  347. //cout<<"eye()-at(): "<<(this->eye()-this->at()).normalized().transpose()<<endl;
  348. //cout<<"eye-this->eye(): "<<(eye-this->eye()).squaredNorm()<<endl;
  349. assert( (eye-this->eye()).squaredNorm() < DOUBLE_EPS);
  350. assert((F-(this->eye()-this->at()).normalized()).squaredNorm() <
  351. DOUBLE_EPS);
  352. assert( (at-this->at()).squaredNorm() < DOUBLE_EPS);
  353. assert( (proj_up-this->up()).squaredNorm() < DOUBLE_EPS);
  354. }
  355. #endif