Camera.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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
  66. // takes relative coordinates and tramsforms them into world 3d
  67. // coordinates: moves the camera into the scene.
  68. inline Eigen::Affine3d affine() const;
  69. // Return an Affine transformation (rigid actually) that puts the takes a
  70. // world 3d coordinate and transforms it into the relative camera
  71. // coordinates: moves the scene in front of the camera.
  72. //
  73. // Note:
  74. //
  75. // gluLookAt(
  76. // eye()(0), eye()(1), eye()(2),
  77. // at()(0), at()(1), at()(2),
  78. // up()(0), up()(1), up()(2));
  79. //
  80. // Is equivalent to
  81. //
  82. // glMultMatrixd(camera.inverse().matrix().data());
  83. //
  84. // See also: affine, eye, at, up
  85. inline Eigen::Affine3d inverse() const;
  86. // Returns world coordinates position of center or "eye" of camera.
  87. inline Eigen::Vector3d eye() const;
  88. // Returns world coordinate position of a point "eye" is looking at.
  89. inline Eigen::Vector3d at() const;
  90. // Returns world coordinate unit vector of "up" vector
  91. inline Eigen::Vector3d up() const;
  92. // Return top right corner of unit plane in relative coordinates, that is
  93. // (w/2,h/2,1)
  94. inline Eigen::Vector3d unit_plane() const;
  95. // Move dv in the relative coordinate frame of the camera (move the FPS)
  96. //
  97. // Inputs:
  98. // dv (x,y,z) displacement vector
  99. //
  100. inline void dolly(const Eigen::Vector3d & dv);
  101. // "Scale zoom": Move `eye`, but leave `at`
  102. //
  103. // Input:
  104. // s amount to scale distance to at
  105. inline void push_away(const double s);
  106. // Aka "Hitchcock", "Vertigo", "Spielberg" or "Trombone" zoom:
  107. // simultaneously dolly while changing angle so that `at` not only stays
  108. // put in relative coordinates but also projected coordinates. That is
  109. //
  110. // Inputs:
  111. // da change in angle in degrees
  112. inline void dolly_zoom(const double da);
  113. // Turn around eye so that rotation is now q
  114. //
  115. // Inputs:
  116. // q new rotation as quaternion
  117. inline void turn_eye(const Eigen::Quaterniond & q);
  118. // Orbit around at so that rotation is now q
  119. //
  120. // Inputs:
  121. // q new rotation as quaternion
  122. inline void orbit(const Eigen::Quaterniond & q);
  123. // Rotate and translate so that camera is situated at "eye" looking at "at"
  124. // with "up" pointing up.
  125. //
  126. // Inputs:
  127. // eye (x,y,z) coordinates of eye position
  128. // at (x,y,z) coordinates of at position
  129. // up (x,y,z) coordinates of up vector
  130. inline void look_at(
  131. const Eigen::Vector3d & eye,
  132. const Eigen::Vector3d & at,
  133. const Eigen::Vector3d & up);
  134. // Needed any time Eigen Structures are used as class members
  135. // http://eigen.tuxfamily.org/dox-devel/group__TopicStructHavingEigenMembers.html
  136. public:
  137. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  138. };
  139. }
  140. // Implementation
  141. #include "PI.h"
  142. #include "EPS.h"
  143. #include <cmath>
  144. #include <iostream>
  145. #include <cassert>
  146. inline igl::Camera::Camera():
  147. m_angle(45.0),m_aspect(1),m_near(1e-2),m_far(100),m_at_dist(1),
  148. m_orthographic(false),
  149. m_rotation_conj(1,0,0,0),
  150. m_translation(0,0,1)
  151. {
  152. }
  153. inline Eigen::Matrix4d igl::Camera::projection() const
  154. {
  155. Eigen::Matrix4d P;
  156. using namespace std;
  157. const double far = m_at_dist + m_far;
  158. const double near = m_near;
  159. // http://stackoverflow.com/a/3738696/148668
  160. if(m_orthographic)
  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 tx = (right+left)/(right-left);
  168. const double ty = (top+bottom)/(top-bottom);
  169. const double tz = (far+near)/(far-near);
  170. const double z_fix = 0.5 /m_at_dist / tan(m_angle*0.5 * (M_PI/180.) );
  171. P<<
  172. z_fix*2./(right-left), 0, 0, -tx,
  173. 0, z_fix*2./(top-bottom), 0, -ty,
  174. 0, 0, -z_fix*2./(far-near), -tz,
  175. 0, 0, 0, 1;
  176. }else
  177. {
  178. const double yScale = tan(PI*0.5 - 0.5*m_angle*PI/180.);
  179. // http://stackoverflow.com/a/14975139/148668
  180. const double xScale = yScale/m_aspect;
  181. P<<
  182. xScale, 0, 0, 0,
  183. 0, yScale, 0, 0,
  184. 0, 0, -(far+near)/(far-near), -1,
  185. 0, 0, -2.*near*far/(far-near), 0;
  186. P = P.transpose().eval();
  187. }
  188. return P;
  189. }
  190. inline Eigen::Affine3d igl::Camera::affine() const
  191. {
  192. using namespace Eigen;
  193. Affine3d t = Affine3d::Identity();
  194. t.rotate(m_rotation_conj.conjugate());
  195. t.translate(m_translation);
  196. return t;
  197. }
  198. inline Eigen::Affine3d igl::Camera::inverse() const
  199. {
  200. using namespace Eigen;
  201. Affine3d t = Affine3d::Identity();
  202. t.translate(-m_translation);
  203. t.rotate(m_rotation_conj);
  204. return t;
  205. }
  206. inline Eigen::Vector3d igl::Camera::eye() const
  207. {
  208. using namespace Eigen;
  209. return affine() * Vector3d(0,0,0);
  210. }
  211. inline Eigen::Vector3d igl::Camera::at() const
  212. {
  213. using namespace Eigen;
  214. return affine() * (Vector3d(0,0,-1)*m_at_dist);
  215. }
  216. inline Eigen::Vector3d igl::Camera::up() const
  217. {
  218. using namespace Eigen;
  219. Affine3d t = Affine3d::Identity();
  220. t.rotate(m_rotation_conj.conjugate());
  221. return t * Vector3d(0,1,0);
  222. }
  223. inline Eigen::Vector3d igl::Camera::unit_plane() const
  224. {
  225. // Distance of center pixel to eye
  226. const double d = 1.0;
  227. const double a = m_aspect;
  228. const double theta = m_angle*PI/180.;
  229. const double w =
  230. 2.*sqrt(-d*d/(a*a*pow(tan(0.5*theta),2.)-1.))*a*tan(0.5*theta);
  231. const double h = w/a;
  232. return Eigen::Vector3d(w*0.5,h*0.5,-d);
  233. }
  234. inline void igl::Camera::dolly(const Eigen::Vector3d & dv)
  235. {
  236. m_translation += dv;
  237. }
  238. inline void igl::Camera::push_away(const double s)
  239. {
  240. using namespace Eigen;
  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 Eigen;
  253. #ifndef NDEBUG
  254. Vector3d old_at = at();
  255. #endif
  256. const double old_angle = m_angle;
  257. if(old_angle + da < IGL_CAMERA_MIN_ANGLE)
  258. {
  259. m_orthographic = true;
  260. }else if(old_angle + da > IGL_CAMERA_MIN_ANGLE)
  261. {
  262. m_orthographic = false;
  263. }
  264. if(!m_orthographic)
  265. {
  266. m_angle += da;
  267. m_angle = min(89.,max(IGL_CAMERA_MIN_ANGLE,m_angle));
  268. // change in distance
  269. const double s =
  270. (2.*tan(old_angle/2./180.*M_PI)) /
  271. (2.*tan(m_angle/2./180.*M_PI)) ;
  272. const double old_at_dist = m_at_dist;
  273. m_at_dist = old_at_dist * s;
  274. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  275. assert((old_at-at()).squaredNorm() < DOUBLE_EPS);
  276. }
  277. }
  278. inline void igl::Camera::turn_eye(const Eigen::Quaterniond & q)
  279. {
  280. using namespace Eigen;
  281. Vector3d old_eye = eye();
  282. // eye should be fixed
  283. //
  284. // eye_1 = R_1 * t_1 = eye_0
  285. // t_1 = R_1' * eye_0
  286. m_rotation_conj = q.conjugate();
  287. m_translation = m_rotation_conj * old_eye;
  288. assert((old_eye - eye()).squaredNorm() < DOUBLE_EPS);
  289. }
  290. inline void igl::Camera::orbit(const Eigen::Quaterniond & q)
  291. {
  292. using namespace Eigen;
  293. Vector3d old_at = at();
  294. // at should be fixed
  295. //
  296. // at_1 = R_1 * t_1 - R_1 * z = at_0
  297. // t_1 = R_1' * (at_0 + R_1 * z)
  298. m_rotation_conj = q.conjugate();
  299. m_translation =
  300. m_rotation_conj *
  301. (old_at +
  302. m_rotation_conj.conjugate() * Vector3d(0,0,1) * m_at_dist);
  303. assert((old_at - at()).squaredNorm() < DOUBLE_EPS);
  304. }
  305. inline void igl::Camera::look_at(
  306. const Eigen::Vector3d & eye,
  307. const Eigen::Vector3d & at,
  308. const Eigen::Vector3d & up)
  309. {
  310. using namespace Eigen;
  311. using namespace std;
  312. // http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml
  313. // Normalize vector from at to eye
  314. Vector3d F = eye-at;
  315. m_at_dist = F.norm();
  316. F.normalize();
  317. // Project up onto plane orthogonal to F and normalize
  318. assert(up.cross(F).norm() > DOUBLE_EPS && "(eye-at) x up ≈ 0");
  319. const Vector3d proj_up = (up-(up.dot(F))*F).normalized();
  320. Quaterniond a,b;
  321. a.setFromTwoVectors(Vector3d(0,0,-1),-F);
  322. b.setFromTwoVectors(a*Vector3d(0,1,0),proj_up);
  323. m_rotation_conj = (b*a).conjugate();
  324. m_translation = m_rotation_conj * eye;
  325. //cout<<"m_at_dist: "<<m_at_dist<<endl;
  326. //cout<<"proj_up: "<<proj_up.transpose()<<endl;
  327. //cout<<"F: "<<F.transpose()<<endl;
  328. //cout<<"eye(): "<<this->eye().transpose()<<endl;
  329. //cout<<"at(): "<<this->at().transpose()<<endl;
  330. //cout<<"eye()-at(): "<<(this->eye()-this->at()).normalized().transpose()<<endl;
  331. //cout<<"eye-this->eye(): "<<(eye-this->eye()).squaredNorm()<<endl;
  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