RotateWidget.h 15 KB

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