Camera.h 11 KB

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