RotateWidget.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. using namespace igl;
  117. const Vector3d win_s(x,y,0), win_d(x,y,1);
  118. const Vector3d s = unproject(win_s);
  119. const Vector3d d = unproject(win_d);
  120. return d-s;
  121. }
  122. inline Eigen::Vector3d igl::RotateWidget::view_direction(const Eigen::Vector3d & pos)
  123. {
  124. using namespace Eigen;
  125. using namespace igl;
  126. const Vector3d ppos = project(pos);
  127. return view_direction(ppos(0),ppos(1));
  128. }
  129. inline igl::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::RotateWidget::unproject_onto(
  142. const int x,
  143. const int y) const
  144. {
  145. using namespace Eigen;
  146. using namespace igl;
  147. // KNOWN BUG: This projects to same depths as pos. I think what we actually
  148. // want is The intersection with the plane perpendicular to the view
  149. // direction at pos. If the field of view angle is small then this difference
  150. // is negligible.
  151. //const Vector3d ppos = project(pos);
  152. //const Vector3d uxy = unproject( Vector3d(x,y,ppos(2)));
  153. // http://en.wikipedia.org/wiki/Line-plane_intersection
  154. //
  155. // Hrrmmm. There's still something wrong here if the ball's in the corner of
  156. // the screen. Am I somehow not accounting for perspective correctly?
  157. //
  158. // Q: What about just projecting the circle's equation and solving for the
  159. // distance?
  160. const Vector3d l0 = unproject(Vector3d(x,y,0));
  161. const Vector3d l = unproject(Vector3d(x,y,1))-l0;
  162. const Vector3d n = view_direction(pos);
  163. const double t = (pos-l0).dot(n)/l.dot(n);
  164. const Vector3d uxy = l0+t*l;
  165. return (uxy-pos)/unprojected_inner_radius()*outer_over_inner*outer_over_inner;
  166. }
  167. inline bool igl::RotateWidget::intersect(
  168. const int x,
  169. const int y,
  170. Eigen::Vector3d & hit) const
  171. {
  172. using namespace Eigen;
  173. using namespace igl;
  174. Vector3d view = view_direction(x,y);
  175. const Vector3d ppos = project(pos);
  176. Vector3d uxy = unproject(Vector3d(x,y,ppos(2)));
  177. double t0,t1;
  178. if(!ray_sphere_intersect(uxy,view,pos,unprojected_inner_radius(),t0,t1))
  179. {
  180. return false;
  181. }
  182. hit = uxy+t0*view;
  183. return true;
  184. }
  185. inline double igl::RotateWidget::unprojected_inner_radius() const
  186. {
  187. using namespace Eigen;
  188. using namespace igl;
  189. Vector3d off,ppos,ppos_off,pos_off;
  190. project(pos,ppos);
  191. ppos_off = ppos;
  192. ppos_off(0) += outer_radius_on_screen/outer_over_inner;
  193. unproject(ppos_off,pos_off);
  194. return (pos-pos_off).norm();
  195. }
  196. inline bool igl::RotateWidget::down(const int x, const int y)
  197. {
  198. using namespace Eigen;
  199. using namespace igl;
  200. using namespace std;
  201. if(!m_is_enabled)
  202. {
  203. return false;
  204. }
  205. down_type = DOWN_TYPE_NONE;
  206. selected_type = DOWN_TYPE_NONE;
  207. down_xy = Vector2d(x,y);
  208. drag_xy = down_xy;
  209. down_rot = rot;
  210. Vector3d ppos = project(pos);
  211. const double r = (ppos.head(2) - down_xy).norm();
  212. const double thresh = 3;
  213. if(fabs(r - outer_radius_on_screen)<thresh)
  214. {
  215. udown = unproject_onto(x,y);
  216. udrag = udown;
  217. down_type = DOWN_TYPE_OUTLINE;
  218. selected_type = DOWN_TYPE_OUTLINE;
  219. // project mouse to same depth as pos
  220. return true;
  221. }else if(r < outer_radius_on_screen/outer_over_inner+thresh*0.5)
  222. {
  223. Vector3d hit;
  224. const bool is_hit = intersect(down_xy(0),down_xy(1),hit);
  225. if(!is_hit)
  226. {
  227. //cout<<"~~~!is_hit"<<endl;
  228. }
  229. auto on_meridian = [&](
  230. const Vector3d & hit,
  231. const Quaterniond & rot,
  232. const Quaterniond & m,
  233. Vector3d & pl_hit) -> bool
  234. {
  235. // project onto rotate plane
  236. pl_hit = hit-pos;
  237. pl_hit = (m.conjugate()*rot.conjugate()*pl_hit).eval();
  238. pl_hit(2) = 0;
  239. pl_hit = (rot*m*pl_hit).eval();
  240. pl_hit.normalize();
  241. pl_hit *= unprojected_inner_radius();
  242. pl_hit += pos;
  243. return (project(pl_hit).head(2)-project(hit).head(2)).norm()<2*thresh;
  244. };
  245. udown = (hit-pos).normalized()/outer_radius_on_screen;
  246. udrag = udown;
  247. for(int a = 0;a<3;a++)
  248. {
  249. Vector3d pl_hit;
  250. if(on_meridian(hit,rot,Quaterniond(axis_q(a)),pl_hit))
  251. {
  252. udown = (pl_hit-pos).normalized()/outer_radius_on_screen;
  253. udrag = udown;
  254. down_type = DownType(DOWN_TYPE_X+a);
  255. selected_type = down_type;
  256. {
  257. Vector3d dir3 = axis_q(a).conjugate()*down_rot.conjugate()*(hit-pos);
  258. dir3 = AngleAxisd(-PI*0.5,Vector3d(0,0,1))*dir3;
  259. dir3 = (rot*axis_q(a)*dir3).eval();
  260. down_dir = (project((hit+dir3).eval())-project(hit)).head(2);
  261. down_dir.normalize();
  262. //// flip y because y coordinate is going to be given backwards in
  263. //// drag()
  264. //down_dir(1) *= -1;
  265. }
  266. return true;
  267. }
  268. }
  269. //assert(is_hit);
  270. down_type = DOWN_TYPE_TRACKBALL;
  271. selected_type = DOWN_TYPE_TRACKBALL;
  272. return true;
  273. }else
  274. {
  275. return false;
  276. }
  277. }
  278. inline bool igl::RotateWidget::drag(const int x, const int y)
  279. {
  280. using namespace igl;
  281. using namespace std;
  282. using namespace Eigen;
  283. if(!m_is_enabled)
  284. {
  285. return false;
  286. }
  287. drag_xy = Vector2d(x,y);
  288. switch(down_type)
  289. {
  290. case DOWN_TYPE_NONE:
  291. return false;
  292. default:
  293. {
  294. const Quaterniond & q = axis_q(down_type-DOWN_TYPE_X);
  295. const double dtheta = -(drag_xy - down_xy).dot(down_dir)/
  296. outer_radius_on_screen/outer_over_inner*PI/2.;
  297. Quaterniond dq(AngleAxisd(dtheta,down_rot*q*Vector3d(0,0,1)));
  298. rot = dq * down_rot;
  299. udrag = dq * udown;
  300. return true;
  301. }
  302. case DOWN_TYPE_OUTLINE:
  303. {
  304. Vector3d ppos = project(pos);
  305. // project mouse to same depth as pos
  306. udrag = unproject_onto(x,y);
  307. const Vector2d A = down_xy - ppos.head(2);
  308. const Vector2d B = drag_xy - ppos.head(2);
  309. const double dtheta = atan2(A(0)*B(1)-A(1)*B(0),A(0)*B(0)+A(1)*B(1));
  310. Vector3d n = view_direction(pos).normalized();
  311. Quaterniond dq(AngleAxisd(dtheta,-n));
  312. //Vector3d n = udrag.cross(udown).normalized();
  313. //Quaterniond dq(AngleAxisd(fabs(dtheta),-n));
  314. rot = dq * down_rot;
  315. }
  316. return true;
  317. case DOWN_TYPE_TRACKBALL:
  318. {
  319. Vector3d ppos = project(pos);
  320. const double r = (double)outer_radius_on_screen/outer_over_inner*2.0;
  321. //const int h = w;
  322. Vector4i vp;
  323. glGetIntegerv(GL_VIEWPORT,vp.data());
  324. const int h = vp(3);
  325. Quaterniond dq;
  326. trackball(
  327. r,r,
  328. 1,
  329. Quaterniond::Identity(),
  330. double( down_xy(0)-ppos(0) )+r/2.,
  331. double((h-down_xy(1))-(h-ppos(1)))+r/2.,
  332. double( x-ppos(0) )+r/2.,
  333. double( (h-y)-(h-ppos(1)))+r/2.,
  334. dq);
  335. // We've computed change in rotation according to this view:
  336. // R = mv * r, R' = rot * (mv * r)
  337. // But we only want new value for r:
  338. // R' = mv * r'
  339. // mv * r' = rot * (mv * r)
  340. // r' = mv* * rot * mv * r
  341. Matrix4d mv;
  342. glGetDoublev(GL_MODELVIEW_MATRIX,mv.data());
  343. Quaterniond scene_rot;
  344. // Convert modelview matrix to quaternion
  345. mat4_to_quat(mv.data(),scene_rot.coeffs().data());
  346. scene_rot.normalize();
  347. rot = scene_rot.conjugate() * dq * scene_rot * down_rot;
  348. }
  349. return true;
  350. }
  351. }
  352. inline bool igl::RotateWidget::up(const int /*x*/, const int /*y*/)
  353. {
  354. // even if disabled process up
  355. down_type = DOWN_TYPE_NONE;
  356. return false;
  357. }
  358. inline bool igl::RotateWidget::is_down() const
  359. {
  360. return down_type != DOWN_TYPE_NONE;
  361. }
  362. inline void igl::RotateWidget::draw() const
  363. {
  364. using namespace Eigen;
  365. using namespace std;
  366. using namespace igl;
  367. glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_DEPTH_BUFFER_BIT | GL_LINE_BIT);
  368. glDisable(GL_CLIP_PLANE0);
  369. glDisable(GL_LIGHTING);
  370. glDisable(GL_DEPTH_TEST);
  371. glLineWidth(2.0);
  372. double r = unprojected_inner_radius();
  373. Vector3d view = view_direction(pos).normalized();
  374. auto draw_circle = [&](const bool cull)
  375. {
  376. Vector3d view = view_direction(pos).normalized();
  377. glBegin(GL_LINES);
  378. const double th_step = (2.0*igl::PI/100.0);
  379. for(double th = 0;th<2.0*igl::PI+th_step;th+=th_step)
  380. {
  381. Vector3d a(cos(th),sin(th),0.0);
  382. Vector3d b(cos(th+th_step),sin(th+th_step),0.0);
  383. if(!cull || (0.5*(a+b)).dot(view)<FLOAT_EPS)
  384. {
  385. glVertex3dv(a.data());
  386. glVertex3dv(b.data());
  387. }
  388. }
  389. glEnd();
  390. };
  391. glPushMatrix();
  392. glTranslated(pos(0),pos(1),pos(2));
  393. glScaled(r,r,r);
  394. // Draw outlines
  395. {
  396. glPushMatrix();
  397. glColor4fv(m_is_enabled,MAYA_GREY);
  398. Quaterniond q;
  399. q.setFromTwoVectors(Vector3d(0,0,1),view);
  400. glMultMatrixd(Affine3d(q).matrix().data());
  401. draw_circle(false);
  402. glScaled(outer_over_inner,outer_over_inner,outer_over_inner);
  403. if(selected_type == DOWN_TYPE_OUTLINE)
  404. {
  405. glColor4fv(m_is_enabled,MAYA_YELLOW);
  406. }else
  407. {
  408. glColor4fv(m_is_enabled,MAYA_CYAN);
  409. }
  410. draw_circle(false);
  411. glPopMatrix();
  412. }
  413. // Draw quartiles
  414. {
  415. glPushMatrix();
  416. glMultMatrixd(Affine3d(rot).matrix().data());
  417. if(selected_type == DOWN_TYPE_Z)
  418. {
  419. glColor4fv(m_is_enabled,MAYA_YELLOW);
  420. }else
  421. {
  422. glColor4fv(m_is_enabled,MAYA_BLUE);
  423. }
  424. draw_circle(true);
  425. if(selected_type == DOWN_TYPE_Y)
  426. {
  427. glColor4fv(m_is_enabled,MAYA_YELLOW);
  428. }else
  429. {
  430. glColor4fv(m_is_enabled,MAYA_GREEN);
  431. }
  432. glRotated(90.0,1.0,0.0,0.0);
  433. draw_circle(true);
  434. if(selected_type == DOWN_TYPE_X)
  435. {
  436. glColor4fv(m_is_enabled,MAYA_YELLOW);
  437. }else
  438. {
  439. glColor4fv(m_is_enabled,MAYA_RED);
  440. }
  441. glRotated(90.0,0.0,1.0,0.0);
  442. draw_circle(true);
  443. glPopMatrix();
  444. }
  445. glColor4fv(m_is_enabled,MAYA_GREY);
  446. draw_guide();
  447. glPopMatrix();
  448. glPopAttrib();
  449. };
  450. inline void igl::RotateWidget::draw_guide() const
  451. {
  452. using namespace Eigen;
  453. using namespace std;
  454. using namespace igl;
  455. glPushAttrib(
  456. GL_DEPTH_BUFFER_BIT |
  457. GL_ENABLE_BIT |
  458. GL_POLYGON_BIT |
  459. GL_POINT_BIT |
  460. GL_TRANSFORM_BIT |
  461. GL_STENCIL_BUFFER_BIT |
  462. GL_LIGHTING_BIT);
  463. // http://www.codeproject.com/Articles/23444/A-Simple-OpenGL-Stipple-Polygon-Example-EP_OpenGL_
  464. const GLubyte halftone[] = {
  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. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  475. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  476. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  477. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  478. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  479. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
  480. 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};
  481. switch(down_type)
  482. {
  483. case DOWN_TYPE_NONE:
  484. case DOWN_TYPE_TRACKBALL:
  485. goto finish;
  486. case DOWN_TYPE_OUTLINE:
  487. glScaled(outer_over_inner,outer_over_inner,outer_over_inner);
  488. break;
  489. default:
  490. break;
  491. }
  492. {
  493. const Vector3d nudown(udown.normalized()),
  494. nudrag(udrag.normalized());
  495. glPushMatrix();
  496. glDisable(GL_CULL_FACE);
  497. glDisable(GL_POINT_SMOOTH);
  498. glPointSize(5.);
  499. glBegin(GL_POINTS);
  500. glVertex3dv(nudown.data());
  501. glVertex3d(0,0,0);
  502. glVertex3dv(nudrag.data());
  503. glEnd();
  504. glBegin(GL_LINE_STRIP);
  505. glVertex3dv(nudown.data());
  506. glVertex3d(0,0,0);
  507. glVertex3dv(nudrag.data());
  508. glEnd();
  509. glEnable(GL_POLYGON_STIPPLE);
  510. glPolygonStipple(halftone);
  511. glBegin(GL_TRIANGLE_FAN);
  512. glVertex3d(0,0,0);
  513. Quaterniond dq = rot * down_rot.conjugate();
  514. //dq.setFromTwoVectors(nudown,nudrag);
  515. for(double t = 0;t<1;t+=0.1)
  516. {
  517. const Vector3d p = Quaterniond::Identity().slerp(t,dq) * nudown;
  518. glVertex3dv(p.data());
  519. }
  520. glVertex3dv(nudrag.data());
  521. glEnd();
  522. glPopMatrix();
  523. }
  524. finish:
  525. glPopAttrib();
  526. }
  527. #endif