Camera.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. };
  138. }
  139. // Implementation
  140. #include "PI.h"
  141. #include "EPS.h"
  142. #include <cmath>
  143. #include <iostream>
  144. #include <cassert>
  145. inline igl::Camera::Camera():
  146. m_angle(45.0),m_aspect(1),m_near(1e-2),m_far(100),m_at_dist(1),
  147. m_rotation_conj(1,0,0,0),
  148. m_translation(0,0,1),
  149. m_at_dist_min_angle(m_at_dist),
  150. m_angle_min_angle(m_angle)
  151. {
  152. }
  153. inline Eigen::Matrix4d igl::Camera::projection() const
  154. {
  155. Eigen::Matrix4d P;
  156. using namespace std;
  157. using namespace igl;
  158. // http://stackoverflow.com/a/3738696/148668
  159. if(m_angle >= IGL_CAMERA_MIN_ANGLE)
  160. {
  161. const double yScale = tan(PI*0.5 - 0.5*m_angle*PI/180.);
  162. // http://stackoverflow.com/a/14975139/148668
  163. const double xScale = yScale/m_aspect;
  164. const double far = m_at_dist + m_far;
  165. const double near = m_near;
  166. P<<
  167. xScale, 0, 0, 0,
  168. 0, yScale, 0, 0,
  169. 0, 0, -(far+near)/(far-near), -1,
  170. 0, 0, -2.*near*far/(far-near), 0;
  171. P = P.transpose().eval();
  172. }else
  173. {
  174. const double f = 0.5;
  175. const double left = -f*m_aspect;
  176. const double right = f*m_aspect;
  177. const double bottom = -f;
  178. const double top = f;
  179. const double near = m_near;
  180. const double far = m_at_dist + m_far;
  181. const double tx = (right+left)/(right-left);
  182. const double ty = (top+bottom)/(top-bottom);
  183. const double tz = (far+near)/(far-near);
  184. const double z_fix =
  185. 0.5/(m_at_dist_min_angle * tan(m_angle_min_angle/2./180.*M_PI))+
  186. (-m_at_dist+m_at_dist_min_angle)/m_at_dist_min_angle;
  187. P<<
  188. z_fix*2./(right-left), 0, 0, -tx,
  189. 0, z_fix*2./(top-bottom), 0, -ty,
  190. 0, 0, -z_fix*2./(far-near), -tz,
  191. 0, 0, 0, 1;
  192. }
  193. return P;
  194. }
  195. inline Eigen::Affine3d igl::Camera::affine() const
  196. {
  197. using namespace Eigen;
  198. Affine3d t = Affine3d::Identity();
  199. t.rotate(m_rotation_conj.conjugate());
  200. t.translate(m_translation);
  201. return t;
  202. }
  203. inline Eigen::Affine3d igl::Camera::inverse() const
  204. {
  205. using namespace Eigen;
  206. Affine3d t = Affine3d::Identity();
  207. t.translate(-m_translation);
  208. t.rotate(m_rotation_conj);
  209. return t;
  210. }
  211. inline Eigen::Vector3d igl::Camera::eye() const
  212. {
  213. using namespace Eigen;
  214. return affine() * Vector3d(0,0,0);
  215. }
  216. inline Eigen::Vector3d igl::Camera::at() const
  217. {
  218. using namespace Eigen;
  219. return affine() * (Vector3d(0,0,-1)*m_at_dist);
  220. }
  221. inline Eigen::Vector3d igl::Camera::up() const
  222. {
  223. using namespace Eigen;
  224. Affine3d t = Affine3d::Identity();
  225. t.rotate(m_rotation_conj.conjugate());
  226. return t * Vector3d(0,1,0);
  227. }
  228. inline Eigen::Vector3d igl::Camera::unit_plane() const
  229. {
  230. using namespace igl;
  231. // Distance of center pixel to eye
  232. const double d = 1.0;
  233. const double a = m_aspect;
  234. const double theta = m_angle*PI/180.;
  235. const double w =
  236. 2.*sqrt(-d*d/(a*a*pow(tan(0.5*theta),2.)-1.))*a*tan(0.5*theta);
  237. const double h = w/a;
  238. return Eigen::Vector3d(w*0.5,h*0.5,-d);
  239. }
  240. inline void igl::Camera::dolly(const Eigen::Vector3d & dv)
  241. {
  242. m_translation += dv;
  243. }
  244. inline void igl::Camera::push_away(const double s)
  245. {
  246. using namespace Eigen;
  247. using namespace igl;
  248. #ifndef NDEBUG
  249. Vector3d old_at = at();
  250. #endif
  251. const double old_at_dist = m_at_dist;
  252. m_at_dist = old_at_dist * s;
  253. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  254. assert((old_at-at()).squaredNorm() < DOUBLE_EPS);
  255. }
  256. inline void igl::Camera::dolly_zoom(const double da)
  257. {
  258. using namespace std;
  259. using namespace igl;
  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. using namespace igl;
  299. Vector3d old_eye = eye();
  300. // eye should be fixed
  301. //
  302. // eye_1 = R_1 * t_1 = eye_0
  303. // t_1 = R_1' * eye_0
  304. m_rotation_conj = q.conjugate();
  305. m_translation = m_rotation_conj * old_eye;
  306. assert((old_eye - eye()).squaredNorm() < DOUBLE_EPS);
  307. }
  308. inline void igl::Camera::orbit(const Eigen::Quaterniond & q)
  309. {
  310. using namespace Eigen;
  311. using namespace igl;
  312. Vector3d old_at = at();
  313. // at should be fixed
  314. //
  315. // at_1 = R_1 * t_1 - R_1 * z = at_0
  316. // t_1 = R_1' * (at_0 + R_1 * z)
  317. m_rotation_conj = q.conjugate();
  318. m_translation =
  319. m_rotation_conj *
  320. (old_at +
  321. m_rotation_conj.conjugate() * Vector3d(0,0,1) * m_at_dist);
  322. assert((old_at - at()).squaredNorm() < DOUBLE_EPS);
  323. }
  324. inline void igl::Camera::look_at(
  325. const Eigen::Vector3d & eye,
  326. const Eigen::Vector3d & at,
  327. const Eigen::Vector3d & up)
  328. {
  329. using namespace Eigen;
  330. using namespace std;
  331. using namespace igl;
  332. // http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml
  333. // Normalize vector from at to eye
  334. Vector3d F = eye-at;
  335. m_at_dist = F.norm();
  336. F.normalize();
  337. // Project up onto plane orthogonal to F and normalize
  338. assert(up.cross(F).norm() > DOUBLE_EPS && "(eye-at) x up ≈ 0");
  339. const Vector3d proj_up = (up-(up.dot(F))*F).normalized();
  340. Quaterniond a,b;
  341. a.setFromTwoVectors(Vector3d(0,0,-1),-F);
  342. b.setFromTwoVectors(a*Vector3d(0,1,0),proj_up);
  343. m_rotation_conj = (b*a).conjugate();
  344. m_translation = m_rotation_conj * eye;
  345. //cout<<"m_at_dist: "<<m_at_dist<<endl;
  346. //cout<<"proj_up: "<<proj_up.transpose()<<endl;
  347. //cout<<"F: "<<F.transpose()<<endl;
  348. //cout<<"eye(): "<<this->eye().transpose()<<endl;
  349. //cout<<"at(): "<<this->at().transpose()<<endl;
  350. //cout<<"eye()-at(): "<<(this->eye()-this->at()).normalized().transpose()<<endl;
  351. //cout<<"eye-this->eye(): "<<(eye-this->eye()).squaredNorm()<<endl;
  352. assert( (eye-this->eye()).squaredNorm() < DOUBLE_EPS);
  353. assert((F-(this->eye()-this->at()).normalized()).squaredNorm() <
  354. DOUBLE_EPS);
  355. assert( (at-this->at()).squaredNorm() < DOUBLE_EPS);
  356. assert( (proj_up-this->up()).squaredNorm() < DOUBLE_EPS);
  357. }
  358. #endif