RotateWidget.h 15 KB

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