RotateWidget.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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_OPENGL2_ROTATE_WIDGET_H
  9. #define IGL_OPENGL2_ROTATE_WIDGET_H
  10. #include <Eigen/Geometry>
  11. #include <Eigen/Core>
  12. #include <vector>
  13. #include "../material_colors.h"
  14. namespace igl
  15. {
  16. namespace opengl2
  17. {
  18. // 3D Rotate tool widget similar to Maya's. Works best if field of view angle
  19. // is less than ~25.
  20. class RotateWidget
  21. {
  22. // If a is true then use A else use desaturated A
  23. static inline void glColor4fv(const bool a, const Eigen::Vector4f & A);
  24. public:
  25. inline static Eigen::Quaterniond axis_q(const int a);
  26. inline static Eigen::Vector3d view_direction(const int x, const int y);
  27. inline static Eigen::Vector3d view_direction(const Eigen::Vector3d & pos);
  28. Eigen::Vector3d pos;
  29. Eigen::Quaterniond rot,down_rot;
  30. Eigen::Vector2d down_xy,drag_xy,down_dir;
  31. Eigen::Vector3d udown,udrag;
  32. double outer_radius_on_screen;
  33. double outer_over_inner;
  34. bool m_is_enabled;
  35. enum DownType
  36. {
  37. DOWN_TYPE_X = 0,
  38. DOWN_TYPE_Y = 1,
  39. DOWN_TYPE_Z = 2,
  40. DOWN_TYPE_OUTLINE = 3,
  41. DOWN_TYPE_TRACKBALL = 4,
  42. DOWN_TYPE_NONE = 5,
  43. NUM_DOWN_TYPES = 6
  44. } down_type, selected_type;
  45. inline RotateWidget();
  46. // Vector from origin to mouse click "Unprojected" onto plane with depth of
  47. // origin and scale to so that outer radius is 1
  48. //
  49. // Inputs:
  50. // x mouse x position
  51. // y mouse y position
  52. // Returns vector
  53. inline Eigen::Vector3d unproject_onto(const int x, const int y) const;
  54. // Shoot ray from mouse click to sphere
  55. //
  56. // Inputs:
  57. // x mouse x position
  58. // y mouse y position
  59. // Outputs:
  60. // hit position of hit
  61. // Returns true only if there was a hit
  62. inline bool intersect(
  63. const int x,
  64. const int y,
  65. Eigen::Vector3d & hit) const;
  66. inline double unprojected_inner_radius() const;
  67. inline bool down(const int x, const int y);
  68. inline bool drag(const int x, const int y);
  69. inline bool up(const int x, const int y);
  70. inline bool is_down() const;
  71. inline void draw() const;
  72. inline void draw_guide() const;
  73. public:
  74. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  75. };
  76. }
  77. }
  78. // Implementation
  79. #include "../opengl/OpenGL_convenience.h"
  80. #include "../PI.h"
  81. #include "../EPS.h"
  82. #include "../ray_sphere_intersect.h"
  83. #include "../mat_to_quat.h"
  84. #include "../trackball.h"
  85. #include "project.h"
  86. #include "unproject.h"
  87. #include <iostream>
  88. #include <cassert>
  89. inline void igl::opengl2::RotateWidget::glColor4fv(
  90. const bool a,
  91. const Eigen::Vector4f & A)
  92. {
  93. if(a)
  94. {
  95. ::glColor4fv(A.data());
  96. }else
  97. {
  98. Eigen::Vector4f B;
  99. const double f = 0.95; // desaturate by 95%
  100. const double L = 0.3*A(0) + 0.6*A(1) + 0.1*A(2);
  101. B.head(3) = A.head(3).array() + f*(L-A.head(3).array());
  102. B(3) = A(3);
  103. ::glColor4fv(B.data());
  104. }
  105. }
  106. inline Eigen::Quaterniond igl::opengl2::RotateWidget::axis_q(const int a)
  107. {
  108. assert(a<3 && a>=0);
  109. const Eigen::Quaterniond axes[3] = {
  110. Eigen::Quaterniond(Eigen::AngleAxisd(igl::PI*0.5,Eigen::Vector3d(0,1,0))),
  111. Eigen::Quaterniond(Eigen::AngleAxisd(igl::PI*0.5,Eigen::Vector3d(1,0,0))),
  112. Eigen::Quaterniond::Identity()};
  113. return axes[a];
  114. }
  115. inline Eigen::Vector3d igl::opengl2::RotateWidget::view_direction(const int x, const int y)
  116. {
  117. using namespace Eigen;
  118. const Vector3d win_s(x,y,0), win_d(x,y,1);
  119. const Vector3d s = unproject(win_s);
  120. const Vector3d d = unproject(win_d);
  121. return d-s;
  122. }
  123. inline Eigen::Vector3d igl::opengl2::RotateWidget::view_direction(const Eigen::Vector3d & pos)
  124. {
  125. using namespace Eigen;
  126. const Vector3d ppos = project(pos);
  127. return view_direction(ppos(0),ppos(1));
  128. }
  129. inline igl::opengl2::RotateWidget::RotateWidget():
  130. pos(0,0,0),
  131. rot(Eigen::Quaterniond::Identity()),
  132. down_rot(rot),
  133. down_xy(-1,-1),drag_xy(-1,-1),
  134. outer_radius_on_screen(91.),
  135. outer_over_inner(1.13684210526),
  136. m_is_enabled(true),
  137. down_type(DOWN_TYPE_NONE),
  138. selected_type(DOWN_TYPE_NONE)
  139. {
  140. }
  141. inline Eigen::Vector3d igl::opengl2::RotateWidget::unproject_onto(
  142. const int x,
  143. const int y) const
  144. {
  145. using namespace Eigen;
  146. // KNOWN BUG: This projects to same depths as pos. I think what we actually
  147. // want is The intersection with the plane perpendicular to the view
  148. // direction at pos. If the field of view angle is small then this difference
  149. // is negligible.
  150. //const Vector3d ppos = project(pos);
  151. //const Vector3d uxy = unproject( Vector3d(x,y,ppos(2)));
  152. // http://en.wikipedia.org/wiki/Line-plane_intersection
  153. //
  154. // Hrrmmm. There's still something wrong here if the ball's in the corner of
  155. // the screen. Am I somehow not accounting for perspective correctly?
  156. //
  157. // Q: What about just projecting the circle's equation and solving for the
  158. // distance?
  159. const Vector3d l0 = unproject(Vector3d(x,y,0));
  160. const Vector3d l = unproject(Vector3d(x,y,1))-l0;
  161. const Vector3d n = view_direction(pos);
  162. const double t = (pos-l0).dot(n)/l.dot(n);
  163. const Vector3d uxy = l0+t*l;
  164. return (uxy-pos)/unprojected_inner_radius()*outer_over_inner*outer_over_inner;
  165. }
  166. inline bool igl::opengl2::RotateWidget::intersect(
  167. const int x,
  168. const int y,
  169. Eigen::Vector3d & hit) const
  170. {
  171. using namespace Eigen;
  172. Vector3d view = view_direction(x,y);
  173. const Vector3d ppos = project(pos);
  174. Vector3d uxy = unproject(Vector3d(x,y,ppos(2)));
  175. double t0,t1;
  176. if(!ray_sphere_intersect(uxy,view,pos,unprojected_inner_radius(),t0,t1))
  177. {
  178. return false;
  179. }
  180. hit = uxy+t0*view;
  181. return true;
  182. }
  183. inline double igl::opengl2::RotateWidget::unprojected_inner_radius() const
  184. {
  185. using namespace Eigen;
  186. Vector3d off,ppos,ppos_off,pos_off;
  187. project(pos,ppos);
  188. ppos_off = ppos;
  189. ppos_off(0) += outer_radius_on_screen/outer_over_inner;
  190. unproject(ppos_off,pos_off);
  191. return (pos-pos_off).norm();
  192. }
  193. inline bool igl::opengl2::RotateWidget::down(const int x, const int y)
  194. {
  195. using namespace Eigen;
  196. using namespace std;
  197. if(!m_is_enabled)
  198. {
  199. return false;
  200. }
  201. down_type = DOWN_TYPE_NONE;
  202. selected_type = DOWN_TYPE_NONE;
  203. down_xy = Vector2d(x,y);
  204. drag_xy = down_xy;
  205. down_rot = rot;
  206. Vector3d ppos = project(pos);
  207. const double r = (ppos.head(2) - down_xy).norm();
  208. const double thresh = 3;
  209. if(fabs(r - outer_radius_on_screen)<thresh)
  210. {
  211. udown = unproject_onto(x,y);
  212. udrag = udown;
  213. down_type = DOWN_TYPE_OUTLINE;
  214. selected_type = DOWN_TYPE_OUTLINE;
  215. // project mouse to same depth as pos
  216. return true;
  217. }else if(r < outer_radius_on_screen/outer_over_inner+thresh*0.5)
  218. {
  219. Vector3d hit;
  220. const bool is_hit = intersect(down_xy(0),down_xy(1),hit);
  221. if(!is_hit)
  222. {
  223. //cout<<"~~~!is_hit"<<endl;
  224. }
  225. auto on_meridian = [&](
  226. const Vector3d & hit,
  227. const Quaterniond & rot,
  228. const Quaterniond & m,
  229. Vector3d & pl_hit) -> bool
  230. {
  231. // project onto rotate plane
  232. pl_hit = hit-pos;
  233. pl_hit = (m.conjugate()*rot.conjugate()*pl_hit).eval();
  234. pl_hit(2) = 0;
  235. pl_hit = (rot*m*pl_hit).eval();
  236. pl_hit.normalize();
  237. pl_hit *= unprojected_inner_radius();
  238. pl_hit += pos;
  239. return (project(pl_hit).head(2)-project(hit).head(2)).norm()<2*thresh;
  240. };
  241. udown = (hit-pos).normalized()/outer_radius_on_screen;
  242. udrag = udown;
  243. for(int a = 0;a<3;a++)
  244. {
  245. Vector3d pl_hit;
  246. if(on_meridian(hit,rot,Quaterniond(axis_q(a)),pl_hit))
  247. {
  248. udown = (pl_hit-pos).normalized()/outer_radius_on_screen;
  249. udrag = udown;
  250. down_type = DownType(DOWN_TYPE_X+a);
  251. selected_type = down_type;
  252. {
  253. Vector3d dir3 = axis_q(a).conjugate()*down_rot.conjugate()*(hit-pos);
  254. dir3 = AngleAxisd(-PI*0.5,Vector3d(0,0,1))*dir3;
  255. dir3 = (rot*axis_q(a)*dir3).eval();
  256. down_dir = (project((hit+dir3).eval())-project(hit)).head(2);
  257. down_dir.normalize();
  258. //// flip y because y coordinate is going to be given backwards in
  259. //// drag()
  260. //down_dir(1) *= -1;
  261. }
  262. return true;
  263. }
  264. }
  265. //assert(is_hit);
  266. down_type = DOWN_TYPE_TRACKBALL;
  267. selected_type = DOWN_TYPE_TRACKBALL;
  268. return true;
  269. }else
  270. {
  271. return false;
  272. }
  273. }
  274. inline bool igl::opengl2::RotateWidget::drag(const int x, const int y)
  275. {
  276. using namespace std;
  277. using namespace Eigen;
  278. if(!m_is_enabled)
  279. {
  280. return false;
  281. }
  282. drag_xy = Vector2d(x,y);
  283. switch(down_type)
  284. {
  285. case DOWN_TYPE_NONE:
  286. return false;
  287. default:
  288. {
  289. const Quaterniond & q = axis_q(down_type-DOWN_TYPE_X);
  290. const double dtheta = -(drag_xy - down_xy).dot(down_dir)/
  291. outer_radius_on_screen/outer_over_inner*PI/2.;
  292. Quaterniond dq(AngleAxisd(dtheta,down_rot*q*Vector3d(0,0,1)));
  293. rot = dq * down_rot;
  294. udrag = dq * udown;
  295. return true;
  296. }
  297. case DOWN_TYPE_OUTLINE:
  298. {
  299. Vector3d ppos = project(pos);
  300. // project mouse to same depth as pos
  301. udrag = unproject_onto(x,y);
  302. const Vector2d A = down_xy - ppos.head(2);
  303. const Vector2d B = drag_xy - ppos.head(2);
  304. const double dtheta = atan2(A(0)*B(1)-A(1)*B(0),A(0)*B(0)+A(1)*B(1));
  305. Vector3d n = view_direction(pos).normalized();
  306. Quaterniond dq(AngleAxisd(dtheta,-n));
  307. //Vector3d n = udrag.cross(udown).normalized();
  308. //Quaterniond dq(AngleAxisd(fabs(dtheta),-n));
  309. rot = dq * down_rot;
  310. }
  311. return true;
  312. case DOWN_TYPE_TRACKBALL:
  313. {
  314. Vector3d ppos = project(pos);
  315. const double r = (double)outer_radius_on_screen/outer_over_inner*2.0;
  316. //const int h = w;
  317. Vector4i vp;
  318. glGetIntegerv(GL_VIEWPORT,vp.data());
  319. const int h = vp(3);
  320. Quaterniond dq;
  321. trackball(
  322. r,r,
  323. 1,
  324. Quaterniond::Identity(),
  325. double( down_xy(0)-ppos(0) )+r/2.,
  326. double((h-down_xy(1))-(h-ppos(1)))+r/2.,
  327. double( x-ppos(0) )+r/2.,
  328. double( (h-y)-(h-ppos(1)))+r/2.,
  329. dq);
  330. // We've computed change in rotation according to this view:
  331. // R = mv * r, R' = rot * (mv * r)
  332. // But we only want new value for r:
  333. // R' = mv * r'
  334. // mv * r' = rot * (mv * r)
  335. // r' = mv* * rot * mv * r
  336. Matrix4d mv;
  337. glGetDoublev(GL_MODELVIEW_MATRIX,mv.data());
  338. Quaterniond scene_rot;
  339. // Convert modelview matrix to quaternion
  340. mat4_to_quat(mv.data(),scene_rot.coeffs().data());
  341. scene_rot.normalize();
  342. rot = scene_rot.conjugate() * dq * scene_rot * down_rot;
  343. }
  344. return true;
  345. }
  346. }
  347. inline bool igl::opengl2::RotateWidget::up(const int /*x*/, const int /*y*/)
  348. {
  349. // even if disabled process up
  350. down_type = DOWN_TYPE_NONE;
  351. return false;
  352. }
  353. inline bool igl::opengl2::RotateWidget::is_down() const
  354. {
  355. return down_type != DOWN_TYPE_NONE;
  356. }
  357. inline void igl::opengl2::RotateWidget::draw() const
  358. {
  359. using namespace Eigen;
  360. using namespace std;
  361. glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_DEPTH_BUFFER_BIT | GL_LINE_BIT);
  362. glDisable(GL_CLIP_PLANE0);
  363. glDisable(GL_LIGHTING);
  364. glDisable(GL_DEPTH_TEST);
  365. glLineWidth(2.0);
  366. double r = unprojected_inner_radius();
  367. Vector3d view = view_direction(pos).normalized();
  368. auto draw_circle = [&](const bool cull)
  369. {
  370. Vector3d view = view_direction(pos).normalized();
  371. glBegin(GL_LINES);
  372. const double th_step = (2.0*igl::PI/100.0);
  373. for(double th = 0;th<2.0*igl::PI+th_step;th+=th_step)
  374. {
  375. Vector3d a(cos(th),sin(th),0.0);
  376. Vector3d b(cos(th+th_step),sin(th+th_step),0.0);
  377. if(!cull || (0.5*(a+b)).dot(view)<FLOAT_EPS)
  378. {
  379. glVertex3dv(a.data());
  380. glVertex3dv(b.data());
  381. }
  382. }
  383. glEnd();
  384. };
  385. glPushMatrix();
  386. glTranslated(pos(0),pos(1),pos(2));
  387. glScaled(r,r,r);
  388. // Draw outlines
  389. {
  390. glPushMatrix();
  391. glColor4fv(m_is_enabled,MAYA_GREY);
  392. Quaterniond q;
  393. q.setFromTwoVectors(Vector3d(0,0,1),view);
  394. glMultMatrixd(Affine3d(q).matrix().data());
  395. draw_circle(false);
  396. glScaled(outer_over_inner,outer_over_inner,outer_over_inner);
  397. if(selected_type == DOWN_TYPE_OUTLINE)
  398. {
  399. glColor4fv(m_is_enabled,MAYA_YELLOW);
  400. }else
  401. {
  402. glColor4fv(m_is_enabled,MAYA_CYAN);
  403. }
  404. draw_circle(false);
  405. glPopMatrix();
  406. }
  407. // Draw quartiles
  408. {
  409. glPushMatrix();
  410. glMultMatrixd(Affine3d(rot).matrix().data());
  411. if(selected_type == DOWN_TYPE_Z)
  412. {
  413. glColor4fv(m_is_enabled,MAYA_YELLOW);
  414. }else
  415. {
  416. glColor4fv(m_is_enabled,MAYA_BLUE);
  417. }
  418. draw_circle(true);
  419. if(selected_type == DOWN_TYPE_Y)
  420. {
  421. glColor4fv(m_is_enabled,MAYA_YELLOW);
  422. }else
  423. {
  424. glColor4fv(m_is_enabled,MAYA_GREEN);
  425. }
  426. glRotated(90.0,1.0,0.0,0.0);
  427. draw_circle(true);
  428. if(selected_type == DOWN_TYPE_X)
  429. {
  430. glColor4fv(m_is_enabled,MAYA_YELLOW);
  431. }else
  432. {
  433. glColor4fv(m_is_enabled,MAYA_RED);
  434. }
  435. glRotated(90.0,0.0,1.0,0.0);
  436. draw_circle(true);
  437. glPopMatrix();
  438. }
  439. glColor4fv(m_is_enabled,MAYA_GREY);
  440. draw_guide();
  441. glPopMatrix();
  442. glPopAttrib();
  443. };
  444. inline void igl::opengl2::RotateWidget::draw_guide() const
  445. {
  446. using namespace Eigen;
  447. using namespace std;
  448. glPushAttrib(
  449. GL_DEPTH_BUFFER_BIT |
  450. GL_ENABLE_BIT |
  451. GL_POLYGON_BIT |
  452. GL_POINT_BIT |
  453. GL_TRANSFORM_BIT |
  454. GL_STENCIL_BUFFER_BIT |
  455. GL_LIGHTING_BIT);
  456. // http://www.codeproject.com/Articles/23444/A-Simple-OpenGL-Stipple-Polygon-Example-EP_OpenGL_
  457. const GLubyte halftone[] = {
  458. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  459. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  460. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  461. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  462. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  463. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  464. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  465. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  466. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  467. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  468. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  469. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  470. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  471. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  472. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  473. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};
  474. switch(down_type)
  475. {
  476. case DOWN_TYPE_NONE:
  477. case DOWN_TYPE_TRACKBALL:
  478. goto finish;
  479. case DOWN_TYPE_OUTLINE:
  480. glScaled(outer_over_inner,outer_over_inner,outer_over_inner);
  481. break;
  482. default:
  483. break;
  484. }
  485. {
  486. const Vector3d nudown(udown.normalized()),
  487. nudrag(udrag.normalized());
  488. glPushMatrix();
  489. glDisable(GL_CULL_FACE);
  490. glDisable(GL_POINT_SMOOTH);
  491. glPointSize(5.);
  492. glBegin(GL_POINTS);
  493. glVertex3dv(nudown.data());
  494. glVertex3d(0,0,0);
  495. glVertex3dv(nudrag.data());
  496. glEnd();
  497. glBegin(GL_LINE_STRIP);
  498. glVertex3dv(nudown.data());
  499. glVertex3d(0,0,0);
  500. glVertex3dv(nudrag.data());
  501. glEnd();
  502. glEnable(GL_POLYGON_STIPPLE);
  503. glPolygonStipple(halftone);
  504. glBegin(GL_TRIANGLE_FAN);
  505. glVertex3d(0,0,0);
  506. Quaterniond dq = rot * down_rot.conjugate();
  507. //dq.setFromTwoVectors(nudown,nudrag);
  508. for(double t = 0;t<1;t+=0.1)
  509. {
  510. const Vector3d p = Quaterniond::Identity().slerp(t,dq) * nudown;
  511. glVertex3dv(p.data());
  512. }
  513. glVertex3dv(nudrag.data());
  514. glEnd();
  515. glPopMatrix();
  516. }
  517. finish:
  518. glPopAttrib();
  519. }
  520. #endif