MouseController.h 15 KB

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