MouseController.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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_MOUSECONTROLLER_H
  9. #define IGL_MOUSECONTROLLER_H
  10. // Needs to be included before others
  11. #include <Eigen/StdVector>
  12. #include <igl/RotateWidget.h>
  13. #include <Eigen/Core>
  14. #include <Eigen/Geometry>
  15. #include <vector>
  16. // Class for control a skeletal FK rig with the mouse.
  17. namespace igl
  18. {
  19. class MouseController
  20. {
  21. public:
  22. typedef Eigen::VectorXi VectorXb;
  23. // Propogate selection to descendants so that selected bones and their
  24. // subtrees are all selected.
  25. //
  26. // Input:
  27. // S #S list of whether selected
  28. // P #S list of bone parents
  29. // Output:
  30. // T #S list of whether selected
  31. static inline void propogate_to_descendants_if(
  32. const VectorXb & S,
  33. const Eigen::VectorXi & P,
  34. VectorXb & T);
  35. // Create a matrix of colors for the selection and their descendants.
  36. //
  37. // Inputs:
  38. // selection #S list of whether a bone is selected
  39. // selected_color color for selected bones
  40. // unselected_color color for unselected bones
  41. // Outputs:
  42. // C #P by 4 list of colors
  43. static inline void color_if(
  44. const VectorXb & S,
  45. const Eigen::Vector4f & selected_color,
  46. const Eigen::Vector4f & unselected_color,
  47. Eigen::MatrixXf & C);
  48. private:
  49. // m_is_selecting whether currently selecting
  50. // m_selection #m_rotations list of whether a bone is selected
  51. // m_down_x x-coordinate of mouse location at down
  52. // m_down_y y-coordinate 〃
  53. // m_drag_x x-coordinate of mouse location at drag
  54. // m_drag_y y-coordinate 〃
  55. // m_widget rotation widget for selected bone
  56. // m_width width of containing window
  57. // m_height height 〃
  58. // m_rotations list of rotations for each bone
  59. // m_rotations_at_selection list of rotations for each bone at time of
  60. // selection
  61. // m_fk_rotations_at_selection list of rotations for each bone at time of
  62. // selection
  63. // m_root_enabled Whether root is enabled
  64. bool m_is_selecting;
  65. VectorXb m_selection;
  66. int m_down_x,m_down_y,m_drag_x,m_drag_y;
  67. int m_width,m_height;
  68. igl::RotateWidget m_widget;
  69. Eigen::Quaterniond m_widget_rot_at_selection;
  70. typedef std::vector<
  71. Eigen::Quaterniond,
  72. Eigen::aligned_allocator<Eigen::Quaterniond> > RotationList;
  73. RotationList
  74. m_rotations,m_rotations_at_selection,m_fk_rotations_at_selection;
  75. bool m_root_enabled;
  76. public:
  77. MouseController();
  78. // Returns const reference to m_selection
  79. inline const VectorXb & selection() const{return m_selection;};
  80. // 〃 m_is_selecting
  81. inline const bool & is_selecting() const{return m_is_selecting;}
  82. inline const bool & is_widget_down() const{return m_widget.is_down();}
  83. // 〃 m_rotations
  84. inline const RotationList & rotations() const{return m_rotations;}
  85. // Returns non-const reference to m_root_enabled
  86. inline bool & root_enabled(){ return m_root_enabled;}
  87. inline void reshape(const int w, const int h);
  88. // Process down, drag, up mouse events
  89. //
  90. // Inputs:
  91. // x x-coordinate of mouse click with respect to container
  92. // y y-coordinate 〃
  93. // Returns true if accepted (action taken).
  94. inline bool down(const int x, const int y);
  95. inline bool drag(const int x, const int y);
  96. inline bool up(const int x, const int y);
  97. // Draw selection box and widget
  98. inline void draw() const;
  99. // Set `m_selection` based on the last drag selection and initialize
  100. // widget.
  101. //
  102. // Inputs:
  103. // C #C by dim list of joint positions at rest
  104. // BE #BE by 2 list of bone indices at rest
  105. // P #P list of bone parents
  106. inline void set_selection_from_last_drag(
  107. const Eigen::MatrixXd & C,
  108. const Eigen::MatrixXi & BE,
  109. const Eigen::VectorXi & P,
  110. const Eigen::VectorXi & RP);
  111. // Set from explicit selection
  112. inline void set_selection(
  113. const Eigen::VectorXi & S,
  114. const Eigen::MatrixXd & C,
  115. const Eigen::MatrixXi & BE,
  116. const Eigen::VectorXi & P,
  117. const Eigen::VectorXi & RP);
  118. // Set size of skeleton
  119. //
  120. // Inputs:
  121. // n number of bones
  122. inline void set_size(const int n);
  123. // Resets m_rotation elements to identity
  124. inline void reset_rotations();
  125. inline void reset_selected_rotations();
  126. inline bool set_rotations(const RotationList & vQ);
  127. // Sets all entries in m_selection to false
  128. inline void clear_selection();
  129. // Returns true iff some element in m_selection is true
  130. inline bool any_selection() const;
  131. public:
  132. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  133. };
  134. }
  135. // Implementation
  136. #include <igl/line_segment_in_rectangle.h>
  137. #include <igl/draw_rectangular_marquee.h>
  138. #include <igl/project.h>
  139. #include <igl/forward_kinematics.h>
  140. #include <igl/matlab_format.h>
  141. #include <igl/any_of.h>
  142. #include <iostream>
  143. #include <algorithm>
  144. #include <functional>
  145. inline void igl::MouseController::propogate_to_descendants_if(
  146. const VectorXb & S,
  147. const Eigen::VectorXi & P,
  148. VectorXb & T)
  149. {
  150. using namespace std;
  151. const int n = S.rows();
  152. assert(P.rows() == n);
  153. // dynamic programming
  154. T = S;
  155. vector<bool> seen(n,false);
  156. // Recursively look up chain and see if ancestor is selected
  157. const function<bool(int)> look_up = [&](int e) -> bool
  158. {
  159. if(e==-1)
  160. {
  161. return false;
  162. }
  163. if(!seen[e])
  164. {
  165. seen[e] = true;
  166. T(e) |= look_up(P(e));
  167. }
  168. return T(e);
  169. };
  170. for(int e = 0;e<n;e++)
  171. {
  172. if(!seen[e])
  173. {
  174. T(e) = look_up(e);
  175. }
  176. }
  177. }
  178. inline void igl::MouseController::color_if(
  179. const VectorXb & S,
  180. const Eigen::Vector4f & selected_color,
  181. const Eigen::Vector4f & unselected_color,
  182. Eigen::MatrixXf & C)
  183. {
  184. C.resize(S.rows(),4);
  185. for(int e=0;e<S.rows();e++)
  186. {
  187. C.row(e) = S(e)?selected_color:unselected_color;
  188. }
  189. }
  190. inline igl::MouseController::MouseController():
  191. m_is_selecting(false),
  192. m_selection(),
  193. m_down_x(-1),m_down_y(-1),m_drag_x(-1),m_drag_y(-1),
  194. m_width(-1),m_height(-1),
  195. m_widget(),
  196. m_widget_rot_at_selection(),
  197. m_rotations(),
  198. m_rotations_at_selection(),
  199. m_root_enabled(true)
  200. {
  201. }
  202. inline void igl::MouseController::reshape(const int w, const int h)
  203. {
  204. m_width = w;
  205. m_height = h;
  206. }
  207. inline bool igl::MouseController::down(const int x, const int y)
  208. {
  209. using namespace std;
  210. using namespace igl;
  211. m_down_x = m_drag_x =x;
  212. m_down_y = m_drag_y =y;
  213. const bool widget_down = any_selection() && m_widget.down(x,m_height-y);
  214. if(!widget_down)
  215. {
  216. m_is_selecting = true;
  217. }
  218. return m_is_selecting || widget_down;
  219. }
  220. inline bool igl::MouseController::drag(const int x, const int y)
  221. {
  222. using namespace std;
  223. using namespace Eigen;
  224. m_drag_x = x;
  225. m_drag_y = y;
  226. if(m_is_selecting)
  227. {
  228. return m_is_selecting;
  229. }else
  230. {
  231. if(!m_widget.drag(x,m_height-y))
  232. {
  233. return false;
  234. }
  235. assert(any_selection());
  236. assert(m_selection.size() == (int)m_rotations.size());
  237. for(int e = 0;e<m_selection.size();e++)
  238. {
  239. if(m_selection(e))
  240. {
  241. // Let:
  242. // w.θr = w.θ ⋅ w.θ₀*
  243. // w.θr takes (absolute) frame of w.θ₀ to w.θ:
  244. // w.θ = w.θr ⋅ w.θ₀
  245. // Define:
  246. // w.θ₀ = θfk ⋅ θx,
  247. // the absolute rotation of the x axis to the deformed bone at
  248. // selection. Likewise,
  249. // w.θ = θfk' ⋅ θx,
  250. // the current absolute rotation of the x axis to the deformed bone.
  251. // Define recursively:
  252. // θfk = θfk(p) ⋅ Θr,
  253. // then because we're only changeing this relative rotation
  254. // θfk' = θfk(p) ⋅ Θr ⋅ θr* ⋅ θr'
  255. // θfk' = θfk ⋅ θr* ⋅ θr'
  256. // w.θ ⋅ θx* = θfk ⋅ θr* ⋅ θr'
  257. // θr ⋅ θfk* ⋅ w.θ ⋅ θx* = θr'
  258. // θr ⋅ θfk* ⋅ w.θr ⋅ w.θ₀ ⋅ θx* = θr'
  259. // θr ⋅ θfk* ⋅ w.θr ⋅ θfk ⋅θx ⋅ θx* = θr'
  260. // θr ⋅ θfk* ⋅ w.θr ⋅ θfk = θr'
  261. // which I guess is the right multiply change after being changed to
  262. // the bases of θfk, the rotation of the bone relative to its rest
  263. // frame.
  264. //
  265. const Quaterniond & frame = m_fk_rotations_at_selection[e];
  266. m_rotations[e] =
  267. m_rotations_at_selection[e] *
  268. frame.conjugate() *
  269. (m_widget.rot*m_widget_rot_at_selection.conjugate()) *
  270. frame;
  271. }
  272. }
  273. return true;
  274. }
  275. }
  276. inline bool igl::MouseController::up(const int x, const int y)
  277. {
  278. m_is_selecting = false;
  279. m_widget.up(x,m_height-y);
  280. return false;
  281. }
  282. inline void igl::MouseController::draw() const
  283. {
  284. using namespace igl;
  285. if(any_selection())
  286. {
  287. m_widget.draw();
  288. }
  289. if(m_is_selecting)
  290. {
  291. // Remember settings
  292. GLboolean dt;
  293. glGetBooleanv(GL_DEPTH_TEST,&dt);
  294. int old_vp[4];
  295. glGetIntegerv(GL_VIEWPORT,old_vp);
  296. // True screen space
  297. glViewport(0,0,m_width,m_height);
  298. glMatrixMode(GL_PROJECTION);
  299. glPushMatrix();
  300. glLoadIdentity();
  301. gluOrtho2D(0,m_width,0,m_height);
  302. glMatrixMode(GL_MODELVIEW);
  303. glPushMatrix();
  304. glLoadIdentity();
  305. glDisable(GL_DEPTH_TEST);
  306. draw_rectangular_marquee(
  307. m_down_x,
  308. m_height-m_down_y,
  309. m_drag_x,
  310. m_height-m_drag_y);
  311. // Restore settings
  312. glMatrixMode(GL_PROJECTION);
  313. glPopMatrix();
  314. glMatrixMode(GL_MODELVIEW);
  315. glPopMatrix();
  316. glViewport(old_vp[0],old_vp[1],old_vp[2],old_vp[3]);
  317. dt?glEnable(GL_DEPTH_TEST):glDisable(GL_DEPTH_TEST);
  318. }
  319. }
  320. inline void igl::MouseController::set_selection_from_last_drag(
  321. const Eigen::MatrixXd & C,
  322. const Eigen::MatrixXi & BE,
  323. const Eigen::VectorXi & P,
  324. const Eigen::VectorXi & RP)
  325. {
  326. using namespace Eigen;
  327. using namespace std;
  328. using namespace igl;
  329. m_rotations_at_selection = m_rotations;
  330. assert(BE.rows() == P.rows());
  331. m_selection = VectorXb::Zero(BE.rows());
  332. // m_rotation[e] is the relative rotation stored at bone e (as seen by the
  333. // joint traveling with its parent)
  334. // vQ[e] is the absolute rotation of a bone at rest to its current position:
  335. // vQ[e] = vQ[p(e)] * m_rotation[e]
  336. vector<Quaterniond,aligned_allocator<Quaterniond> > vQ;
  337. vector<Vector3d> vT;
  338. forward_kinematics(C,BE,P,m_rotations,vQ,vT);
  339. // Loop over deformed bones
  340. for(int e = 0;e<BE.rows();e++)
  341. {
  342. Affine3d a = Affine3d::Identity();
  343. a.translate(vT[e]);
  344. a.rotate(vQ[e]);
  345. Vector3d s = a * (Vector3d)C.row(BE(e,0));
  346. Vector3d d = a * (Vector3d)C.row(BE(e,1));
  347. Vector3d projs = project(s);
  348. Vector3d projd = project(d);
  349. m_selection(e) = line_segment_in_rectangle(
  350. projs.head(2),projd.head(2),
  351. Vector2d(m_down_x,m_height-m_down_y),
  352. Vector2d(m_drag_x,m_height-m_drag_y));
  353. }
  354. return set_selection(m_selection,C,BE,P,RP);
  355. }
  356. inline void igl::MouseController::set_selection(
  357. const Eigen::VectorXi & S,
  358. const Eigen::MatrixXd & C,
  359. const Eigen::MatrixXi & BE,
  360. const Eigen::VectorXi & P,
  361. const Eigen::VectorXi & RP)
  362. {
  363. using namespace igl;
  364. using namespace Eigen;
  365. using namespace std;
  366. vector<Quaterniond,aligned_allocator<Quaterniond> > & vQ =
  367. m_fk_rotations_at_selection;
  368. vector<Vector3d> vT;
  369. forward_kinematics(C,BE,P,m_rotations,vQ,vT);
  370. if(&m_selection != &S)
  371. {
  372. m_selection = S;
  373. }
  374. assert(m_selection.rows() == BE.rows());
  375. assert(BE.rows() == P.rows());
  376. assert(BE.rows() == RP.rows());
  377. // Zero-out S up a path of ones from e
  378. auto propagate = [&](const int e, const VectorXb & S, VectorXb & N)
  379. {
  380. if(S(e))
  381. {
  382. int f = e;
  383. while(true)
  384. {
  385. int p = P(f);
  386. if(p==-1||!S(p))
  387. {
  388. break;
  389. }
  390. N(f) = false;
  391. f = p;
  392. }
  393. }
  394. };
  395. VectorXb prev_selection = m_selection;
  396. // Combine upward, group rigid parts, repeat
  397. while(true)
  398. {
  399. // Spread selection accross rigid pieces
  400. VectorXb SRP(VectorXb::Zero(RP.maxCoeff()+1));
  401. for(int e = 0;e<BE.rows();e++)
  402. {
  403. SRP(RP(e)) |= m_selection(e);
  404. }
  405. for(int e = 0;e<BE.rows();e++)
  406. {
  407. m_selection(e) = SRP(RP(e));
  408. }
  409. // Clear selections below m_selection ancestors
  410. VectorXb new_selection = m_selection;
  411. for(int e = 0;e<P.rows();e++)
  412. {
  413. propagate(e,m_selection,new_selection);
  414. }
  415. m_selection = new_selection;
  416. if(m_selection==prev_selection)
  417. {
  418. break;
  419. }
  420. prev_selection = m_selection;
  421. }
  422. // Now selection should contain just bone roots of m_selection subtrees
  423. if(any_of(m_selection))
  424. {
  425. // Taking average
  426. m_widget.pos.setConstant(0);
  427. m_widget_rot_at_selection.coeffs().setConstant(0);
  428. m_widget.rot.coeffs().array().setConstant(0);
  429. Quaterniond cur_rot(0,0,0,0);
  430. int num_selection = 0;
  431. // Compute average widget for selection
  432. for(int e = 0;e<BE.rows();e++)
  433. {
  434. if(m_selection(e))
  435. {
  436. Vector3d s = C.row(BE(e,0));
  437. Vector3d d = C.row(BE(e,1));
  438. auto b = (d-s).transpose().eval();
  439. {
  440. Affine3d a = Affine3d::Identity();
  441. a.translate(vT[e]);
  442. a.rotate(vQ[e]);
  443. m_widget.pos += a*s;
  444. }
  445. // Rotation of x axis to this bone
  446. Quaterniond rot_at_bind;
  447. rot_at_bind.setFromTwoVectors(Vector3d(1,0,0),b);
  448. const Quaterniond abs_rot = vQ[e] * rot_at_bind;
  449. m_widget_rot_at_selection.coeffs() += abs_rot.coeffs();
  450. num_selection++;
  451. }
  452. }
  453. // Take average
  454. m_widget.pos.array() /= (double)num_selection;
  455. m_widget_rot_at_selection.coeffs().array() /= (double)num_selection;
  456. m_widget_rot_at_selection.normalize();
  457. m_widget.rot = m_widget_rot_at_selection;
  458. }
  459. m_widget.m_is_enabled = true;
  460. for(int s = 0;s<m_selection.rows();s++)
  461. {
  462. // a root is selected then disable.
  463. if(!m_root_enabled && m_selection(s) && P(s) == -1)
  464. {
  465. m_widget.m_is_enabled = false;
  466. break;
  467. }
  468. }
  469. }
  470. inline void igl::MouseController::set_size(const int n)
  471. {
  472. using namespace Eigen;
  473. clear_selection();
  474. m_rotations.clear();
  475. m_rotations.resize(n,Quaterniond::Identity());
  476. m_selection = VectorXb::Zero(n);
  477. }
  478. inline void igl::MouseController::reset_rotations()
  479. {
  480. using namespace Eigen;
  481. using namespace std;
  482. fill(m_rotations.begin(),m_rotations.end(),Quaterniond::Identity());
  483. // cop out. just clear selection
  484. clear_selection();
  485. }
  486. inline void igl::MouseController::reset_selected_rotations()
  487. {
  488. using namespace Eigen;
  489. for(int e = 0;e<m_selection.size();e++)
  490. {
  491. if(m_selection(e))
  492. {
  493. m_rotations[e] = Quaterniond::Identity();
  494. }
  495. }
  496. }
  497. inline bool igl::MouseController::set_rotations(const RotationList & vQ)
  498. {
  499. if(vQ.size() != m_rotations.size())
  500. {
  501. return false;
  502. }
  503. assert(!any_selection());
  504. m_rotations = vQ;
  505. return true;
  506. }
  507. inline void igl::MouseController::clear_selection()
  508. {
  509. m_selection.setConstant(false);
  510. }
  511. inline bool igl::MouseController::any_selection() const
  512. {
  513. return igl::any_of(m_selection);
  514. }
  515. #endif