RotateWidget.cpp 12 KB

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