Camera.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_orthographic whether to use othrographic projection {false}
  35. // m_rotation_conj Conjugate of rotation part of rigid transformation of
  36. // camera {identity}. Note: we purposefully store the conjugate because
  37. // this is what TW_TYPE_QUAT4D is expecting.
  38. // m_translation Translation part of rigid transformation of camera
  39. // {(0,0,1)}
  40. double m_angle, m_aspect, m_near, m_far, m_at_dist;
  41. bool m_orthographic;
  42. Eigen::Quaterniond m_rotation_conj;
  43. Eigen::Vector3d m_translation;
  44. public:
  45. inline Camera();
  46. inline virtual ~Camera(){}
  47. // Return projection matrix that takes relative camera coordinates and
  48. // transforms it to viewport coordinates
  49. //
  50. // Note:
  51. //
  52. // if(m_angle > 0)
  53. // {
  54. // gluPerspective(m_angle,m_aspect,m_near,m_at_dist+m_far);
  55. // }else
  56. // {
  57. // gluOrtho(-0.5*aspect,0.5*aspect,-0.5,0.5,m_at_dist+m_near,m_far);
  58. // }
  59. //
  60. // Is equivalent to
  61. //
  62. // glMultMatrixd(projection().data());
  63. //
  64. inline Eigen::Matrix4d projection() const;
  65. // Return an Affine transformation (rigid actually) that takes a world 3d coordinate and
  66. // transforms it into the relative camera coordinates.
  67. inline Eigen::Affine3d affine() const;
  68. // Return an Affine transformation (rigid actually) that takes relative
  69. // coordinates and tramsforms them into world 3d coordinates.
  70. //
  71. // Note:
  72. //
  73. // gluLookAt(
  74. // eye()(0), eye()(1), eye()(2),
  75. // at()(0), at()(1), at()(2),
  76. // up()(0), up()(1), up()(2));
  77. //
  78. // Is equivalent to
  79. //
  80. // glMultMatrixd(camera.affine().matrix().data());
  81. //
  82. // See also: affine, eye, at, up
  83. inline Eigen::Affine3d inverse() const;
  84. // Returns world coordinates position of center or "eye" of camera.
  85. inline Eigen::Vector3d eye() const;
  86. // Returns world coordinate position of a point "eye" is looking at.
  87. inline Eigen::Vector3d at() const;
  88. // Returns world coordinate unit vector of "up" vector
  89. inline Eigen::Vector3d up() const;
  90. // Return top right corner of unit plane in relative coordinates, that is
  91. // (w/2,h/2,1)
  92. inline Eigen::Vector3d unit_plane() const;
  93. // Move dv in the relative coordinate frame of the camera (move the FPS)
  94. //
  95. // Inputs:
  96. // dv (x,y,z) displacement vector
  97. //
  98. inline void dolly(const Eigen::Vector3d & dv);
  99. // "Scale zoom": Move `eye`, but leave `at`
  100. //
  101. // Input:
  102. // s amount to scale distance to at
  103. inline void push_away(const double s);
  104. // Aka "Hitchcock", "Vertigo", "Spielberg" or "Trombone" zoom:
  105. // simultaneously dolly while changing angle so that `at` not only stays
  106. // put in relative coordinates but also projected coordinates. That is
  107. //
  108. // Inputs:
  109. // da change in angle in degrees
  110. inline void dolly_zoom(const double da);
  111. // Turn around eye so that rotation is now q
  112. //
  113. // Inputs:
  114. // q new rotation as quaternion
  115. inline void turn_eye(const Eigen::Quaterniond & q);
  116. // Orbit around at so that rotation is now q
  117. //
  118. // Inputs:
  119. // q new rotation as quaternion
  120. inline void orbit(const Eigen::Quaterniond & q);
  121. // Rotate and translate so that camera is situated at "eye" looking at "at"
  122. // with "up" pointing up.
  123. //
  124. // Inputs:
  125. // eye (x,y,z) coordinates of eye position
  126. // at (x,y,z) coordinates of at position
  127. // up (x,y,z) coordinates of up vector
  128. inline void look_at(
  129. const Eigen::Vector3d & eye,
  130. const Eigen::Vector3d & at,
  131. const Eigen::Vector3d & up);
  132. // Needed any time Eigen Structures are used as class members
  133. // http://eigen.tuxfamily.org/dox-devel/group__TopicStructHavingEigenMembers.html
  134. public:
  135. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  136. };
  137. }
  138. // Implementation
  139. #include "PI.h"
  140. #include "EPS.h"
  141. #include <cmath>
  142. #include <iostream>
  143. #include <cassert>
  144. inline igl::Camera::Camera():
  145. m_angle(45.0),m_aspect(1),m_near(1e-2),m_far(100),m_at_dist(1),
  146. m_orthographic(false),
  147. m_rotation_conj(1,0,0,0),
  148. m_translation(0,0,1)
  149. {
  150. }
  151. inline Eigen::Matrix4d igl::Camera::projection() const
  152. {
  153. Eigen::Matrix4d P;
  154. using namespace std;
  155. const double far = m_at_dist + m_far;
  156. const double near = m_near;
  157. // http://stackoverflow.com/a/3738696/148668
  158. if(m_orthographic)
  159. {
  160. const double f = 0.5;
  161. const double left = -f*m_aspect;
  162. const double right = f*m_aspect;
  163. const double bottom = -f;
  164. const double top = f;
  165. const double tx = (right+left)/(right-left);
  166. const double ty = (top+bottom)/(top-bottom);
  167. const double tz = (far+near)/(far-near);
  168. const double z_fix = 0.5 /m_at_dist / tan(m_angle*0.5 * (M_PI/180.) );
  169. P<<
  170. z_fix*2./(right-left), 0, 0, -tx,
  171. 0, z_fix*2./(top-bottom), 0, -ty,
  172. 0, 0, -z_fix*2./(far-near), -tz,
  173. 0, 0, 0, 1;
  174. }else
  175. {
  176. const double yScale = tan(PI*0.5 - 0.5*m_angle*PI/180.);
  177. // http://stackoverflow.com/a/14975139/148668
  178. const double xScale = yScale/m_aspect;
  179. P<<
  180. xScale, 0, 0, 0,
  181. 0, yScale, 0, 0,
  182. 0, 0, -(far+near)/(far-near), -1,
  183. 0, 0, -2.*near*far/(far-near), 0;
  184. P = P.transpose().eval();
  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. // Distance of center pixel to eye
  224. const double d = 1.0;
  225. const double a = m_aspect;
  226. const double theta = m_angle*PI/180.;
  227. const double w =
  228. 2.*sqrt(-d*d/(a*a*pow(tan(0.5*theta),2.)-1.))*a*tan(0.5*theta);
  229. const double h = w/a;
  230. return Eigen::Vector3d(w*0.5,h*0.5,-d);
  231. }
  232. inline void igl::Camera::dolly(const Eigen::Vector3d & dv)
  233. {
  234. m_translation += dv;
  235. }
  236. inline void igl::Camera::push_away(const double s)
  237. {
  238. using namespace Eigen;
  239. #ifndef NDEBUG
  240. Vector3d old_at = at();
  241. #endif
  242. const double old_at_dist = m_at_dist;
  243. m_at_dist = old_at_dist * s;
  244. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  245. assert((old_at-at()).squaredNorm() < DOUBLE_EPS);
  246. }
  247. inline void igl::Camera::dolly_zoom(const double da)
  248. {
  249. using namespace std;
  250. using namespace Eigen;
  251. #ifndef NDEBUG
  252. Vector3d old_at = at();
  253. #endif
  254. const double old_angle = m_angle;
  255. if(old_angle + da < IGL_CAMERA_MIN_ANGLE)
  256. {
  257. m_orthographic = true;
  258. }else if(old_angle + da > IGL_CAMERA_MIN_ANGLE)
  259. {
  260. m_orthographic = false;
  261. }
  262. if(!m_orthographic)
  263. {
  264. m_angle += da;
  265. m_angle = min(89.,max(IGL_CAMERA_MIN_ANGLE,m_angle));
  266. // change in distance
  267. const double s =
  268. (2.*tan(old_angle/2./180.*M_PI)) /
  269. (2.*tan(m_angle/2./180.*M_PI)) ;
  270. const double old_at_dist = m_at_dist;
  271. m_at_dist = old_at_dist * s;
  272. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  273. assert((old_at-at()).squaredNorm() < DOUBLE_EPS);
  274. }
  275. }
  276. inline void igl::Camera::turn_eye(const Eigen::Quaterniond & q)
  277. {
  278. using namespace Eigen;
  279. Vector3d old_eye = eye();
  280. // eye should be fixed
  281. //
  282. // eye_1 = R_1 * t_1 = eye_0
  283. // t_1 = R_1' * eye_0
  284. m_rotation_conj = q.conjugate();
  285. m_translation = m_rotation_conj * old_eye;
  286. assert((old_eye - eye()).squaredNorm() < DOUBLE_EPS);
  287. }
  288. inline void igl::Camera::orbit(const Eigen::Quaterniond & q)
  289. {
  290. using namespace Eigen;
  291. Vector3d old_at = at();
  292. // at should be fixed
  293. //
  294. // at_1 = R_1 * t_1 - R_1 * z = at_0
  295. // t_1 = R_1' * (at_0 + R_1 * z)
  296. m_rotation_conj = q.conjugate();
  297. m_translation =
  298. m_rotation_conj *
  299. (old_at +
  300. m_rotation_conj.conjugate() * Vector3d(0,0,1) * m_at_dist);
  301. assert((old_at - at()).squaredNorm() < DOUBLE_EPS);
  302. }
  303. inline void igl::Camera::look_at(
  304. const Eigen::Vector3d & eye,
  305. const Eigen::Vector3d & at,
  306. const Eigen::Vector3d & up)
  307. {
  308. using namespace Eigen;
  309. using namespace std;
  310. // http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml
  311. // Normalize vector from at to eye
  312. Vector3d F = eye-at;
  313. m_at_dist = F.norm();
  314. F.normalize();
  315. // Project up onto plane orthogonal to F and normalize
  316. assert(up.cross(F).norm() > DOUBLE_EPS && "(eye-at) x up ≈ 0");
  317. const Vector3d proj_up = (up-(up.dot(F))*F).normalized();
  318. Quaterniond a,b;
  319. a.setFromTwoVectors(Vector3d(0,0,-1),-F);
  320. b.setFromTwoVectors(a*Vector3d(0,1,0),proj_up);
  321. m_rotation_conj = (b*a).conjugate();
  322. m_translation = m_rotation_conj * eye;
  323. //cout<<"m_at_dist: "<<m_at_dist<<endl;
  324. //cout<<"proj_up: "<<proj_up.transpose()<<endl;
  325. //cout<<"F: "<<F.transpose()<<endl;
  326. //cout<<"eye(): "<<this->eye().transpose()<<endl;
  327. //cout<<"at(): "<<this->at().transpose()<<endl;
  328. //cout<<"eye()-at(): "<<(this->eye()-this->at()).normalized().transpose()<<endl;
  329. //cout<<"eye-this->eye(): "<<(eye-this->eye()).squaredNorm()<<endl;
  330. assert( (eye-this->eye()).squaredNorm() < DOUBLE_EPS);
  331. //assert((F-(this->eye()-this->at()).normalized()).squaredNorm() <
  332. // DOUBLE_EPS);
  333. assert( (at-this->at()).squaredNorm() < DOUBLE_EPS);
  334. //assert( (proj_up-this->up()).squaredNorm() < DOUBLE_EPS);
  335. }
  336. #endif