RotateWidget.h 13 KB

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