Camera.h 11 KB

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