Camera.h 11 KB

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