RotateWidget.h 16 KB

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