Camera.h 11 KB

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