Camera.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. using namespace igl;
  162. // http://stackoverflow.com/a/3738696/148668
  163. if(m_angle >= IGL_CAMERA_MIN_ANGLE)
  164. {
  165. const double yScale = tan(PI*0.5 - 0.5*m_angle*PI/180.);
  166. // http://stackoverflow.com/a/14975139/148668
  167. const double xScale = yScale/m_aspect;
  168. const double far = m_at_dist + m_far;
  169. const double near = m_near;
  170. P<<
  171. xScale, 0, 0, 0,
  172. 0, yScale, 0, 0,
  173. 0, 0, -(far+near)/(far-near), -1,
  174. 0, 0, -2.*near*far/(far-near), 0;
  175. P = P.transpose().eval();
  176. }else
  177. {
  178. const double f = 0.5;
  179. const double left = -f*m_aspect;
  180. const double right = f*m_aspect;
  181. const double bottom = -f;
  182. const double top = f;
  183. const double near = m_near;
  184. const double far = m_at_dist + m_far;
  185. const double tx = (right+left)/(right-left);
  186. const double ty = (top+bottom)/(top-bottom);
  187. const double tz = (far+near)/(far-near);
  188. const double z_fix =
  189. 0.5/(m_at_dist_min_angle * tan(m_angle_min_angle/2./180.*M_PI))+
  190. (-m_at_dist+m_at_dist_min_angle)/m_at_dist_min_angle;
  191. P<<
  192. z_fix*2./(right-left), 0, 0, -tx,
  193. 0, z_fix*2./(top-bottom), 0, -ty,
  194. 0, 0, -z_fix*2./(far-near), -tz,
  195. 0, 0, 0, 1;
  196. }
  197. return P;
  198. }
  199. inline Eigen::Affine3d igl::Camera::affine() const
  200. {
  201. using namespace Eigen;
  202. Affine3d t = Affine3d::Identity();
  203. t.rotate(m_rotation_conj.conjugate());
  204. t.translate(m_translation);
  205. return t;
  206. }
  207. inline Eigen::Affine3d igl::Camera::inverse() const
  208. {
  209. using namespace Eigen;
  210. Affine3d t = Affine3d::Identity();
  211. t.translate(-m_translation);
  212. t.rotate(m_rotation_conj);
  213. return t;
  214. }
  215. inline Eigen::Vector3d igl::Camera::eye() const
  216. {
  217. using namespace Eigen;
  218. return affine() * Vector3d(0,0,0);
  219. }
  220. inline Eigen::Vector3d igl::Camera::at() const
  221. {
  222. using namespace Eigen;
  223. return affine() * (Vector3d(0,0,-1)*m_at_dist);
  224. }
  225. inline Eigen::Vector3d igl::Camera::up() const
  226. {
  227. using namespace Eigen;
  228. Affine3d t = Affine3d::Identity();
  229. t.rotate(m_rotation_conj.conjugate());
  230. return t * Vector3d(0,1,0);
  231. }
  232. inline Eigen::Vector3d igl::Camera::unit_plane() const
  233. {
  234. using namespace igl;
  235. // Distance of center pixel to eye
  236. const double d = 1.0;
  237. const double a = m_aspect;
  238. const double theta = m_angle*PI/180.;
  239. const double w =
  240. 2.*sqrt(-d*d/(a*a*pow(tan(0.5*theta),2.)-1.))*a*tan(0.5*theta);
  241. const double h = w/a;
  242. return Eigen::Vector3d(w*0.5,h*0.5,-d);
  243. }
  244. inline void igl::Camera::dolly(const Eigen::Vector3d & dv)
  245. {
  246. m_translation += dv;
  247. }
  248. inline void igl::Camera::push_away(const double s)
  249. {
  250. using namespace Eigen;
  251. using namespace igl;
  252. #ifndef NDEBUG
  253. Vector3d old_at = at();
  254. #endif
  255. const double old_at_dist = m_at_dist;
  256. m_at_dist = old_at_dist * s;
  257. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  258. assert((old_at-at()).squaredNorm() < DOUBLE_EPS);
  259. }
  260. inline void igl::Camera::dolly_zoom(const double da)
  261. {
  262. using namespace std;
  263. using namespace igl;
  264. using namespace Eigen;
  265. #ifndef NDEBUG
  266. Vector3d old_at = at();
  267. #endif
  268. const double old_angle = m_angle;
  269. m_angle += da;
  270. m_angle = min(89.,max(0.,m_angle));
  271. const double eff_angle = (IGL_CAMERA_MIN_ANGLE > m_angle ? IGL_CAMERA_MIN_ANGLE : m_angle);
  272. if(old_angle >= IGL_CAMERA_MIN_ANGLE)
  273. {
  274. // change in distance
  275. const double s =
  276. (2.*tan(old_angle/2./180.*M_PI)) /
  277. (2.*tan(eff_angle/2./180.*M_PI)) ;
  278. const double old_at_dist = m_at_dist;
  279. m_at_dist = old_at_dist * s;
  280. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  281. if(eff_angle == IGL_CAMERA_MIN_ANGLE)
  282. {
  283. m_at_dist_min_angle = m_at_dist;
  284. m_angle_min_angle = eff_angle;
  285. }
  286. assert((old_at-at()).squaredNorm() < DOUBLE_EPS);
  287. }else if(old_angle < IGL_CAMERA_MIN_ANGLE && m_angle >= IGL_CAMERA_MIN_ANGLE)
  288. {
  289. // Restore decent length
  290. const double z_fix =
  291. // There should be some factor here based on the incoming angle
  292. // (m_angle_min_angle) and outgoing angle (m_angle)... For now I set it
  293. // to 1. (assumes equality)
  294. //0.5/(m_at_dist_min_angle * tan(m_angle_min_angle/2./180.*M_PI))+
  295. 1.+(-m_at_dist+m_at_dist_min_angle)/m_at_dist_min_angle;
  296. m_at_dist = m_at_dist_min_angle / z_fix;
  297. }
  298. }
  299. inline void igl::Camera::turn_eye(const Eigen::Quaterniond & q)
  300. {
  301. using namespace Eigen;
  302. using namespace igl;
  303. Vector3d old_eye = eye();
  304. // eye should be fixed
  305. //
  306. // eye_1 = R_1 * t_1 = eye_0
  307. // t_1 = R_1' * eye_0
  308. m_rotation_conj = q.conjugate();
  309. m_translation = m_rotation_conj * old_eye;
  310. assert((old_eye - eye()).squaredNorm() < DOUBLE_EPS);
  311. }
  312. inline void igl::Camera::orbit(const Eigen::Quaterniond & q)
  313. {
  314. using namespace Eigen;
  315. using namespace igl;
  316. Vector3d old_at = at();
  317. // at should be fixed
  318. //
  319. // at_1 = R_1 * t_1 - R_1 * z = at_0
  320. // t_1 = R_1' * (at_0 + R_1 * z)
  321. m_rotation_conj = q.conjugate();
  322. m_translation =
  323. m_rotation_conj *
  324. (old_at +
  325. m_rotation_conj.conjugate() * Vector3d(0,0,1) * m_at_dist);
  326. assert((old_at - at()).squaredNorm() < DOUBLE_EPS);
  327. }
  328. inline void igl::Camera::look_at(
  329. const Eigen::Vector3d & eye,
  330. const Eigen::Vector3d & at,
  331. const Eigen::Vector3d & up)
  332. {
  333. using namespace Eigen;
  334. using namespace std;
  335. using namespace igl;
  336. // http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml
  337. // Normalize vector from at to eye
  338. Vector3d F = eye-at;
  339. m_at_dist = F.norm();
  340. F.normalize();
  341. // Project up onto plane orthogonal to F and normalize
  342. assert(up.cross(F).norm() > DOUBLE_EPS && "(eye-at) x up ≈ 0");
  343. const Vector3d proj_up = (up-(up.dot(F))*F).normalized();
  344. Quaterniond a,b;
  345. a.setFromTwoVectors(Vector3d(0,0,-1),-F);
  346. b.setFromTwoVectors(a*Vector3d(0,1,0),proj_up);
  347. m_rotation_conj = (b*a).conjugate();
  348. m_translation = m_rotation_conj * eye;
  349. //cout<<"m_at_dist: "<<m_at_dist<<endl;
  350. //cout<<"proj_up: "<<proj_up.transpose()<<endl;
  351. //cout<<"F: "<<F.transpose()<<endl;
  352. //cout<<"eye(): "<<this->eye().transpose()<<endl;
  353. //cout<<"at(): "<<this->at().transpose()<<endl;
  354. //cout<<"eye()-at(): "<<(this->eye()-this->at()).normalized().transpose()<<endl;
  355. //cout<<"eye-this->eye(): "<<(eye-this->eye()).squaredNorm()<<endl;
  356. assert( (eye-this->eye()).squaredNorm() < DOUBLE_EPS);
  357. assert((F-(this->eye()-this->at()).normalized()).squaredNorm() <
  358. DOUBLE_EPS);
  359. assert( (at-this->at()).squaredNorm() < DOUBLE_EPS);
  360. assert( (proj_up-this->up()).squaredNorm() < DOUBLE_EPS);
  361. }
  362. #endif