example.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. #include <igl/Camera.h>
  2. #include <igl/MouseController.h>
  3. #include <igl/REDRUM.h>
  4. #include <igl/ReAntTweakBar.h>
  5. #include <igl/STR.h>
  6. #include <igl/barycenter.h>
  7. #include <igl/bbw/bbw.h>
  8. #include <igl/bone_parents.h>
  9. #include <igl/boundary_conditions.h>
  10. #include <igl/boundary_facets.h>
  11. #include <igl/centroid.h>
  12. #include <igl/cgal/remesh_self_intersections.h>
  13. #include <igl/colon.h>
  14. #include <igl/draw_beach_ball.h>
  15. #include <igl/draw_floor.h>
  16. #include <igl/draw_mesh.h>
  17. #include <igl/draw_skeleton_3d.h>
  18. #include <igl/draw_skeleton_vector_graphics.h>
  19. #include <igl/forward_kinematics.h>
  20. #include <igl/get_seconds.h>
  21. #include <igl/lbs_matrix.h>
  22. #include <igl/material_colors.h>
  23. #include <igl/normalize_row_sums.h>
  24. #include <igl/pathinfo.h>
  25. #include <igl/per_face_normals.h>
  26. #include <igl/quat_to_mat.h>
  27. #include <igl/readDMAT.h>
  28. #include <igl/readTGF.h>
  29. #include <igl/read_triangle_mesh.h>
  30. #include <igl/remove_unreferenced.h>
  31. #include <igl/report_gl_error.h>
  32. #include <igl/snap_to_canonical_view_quat.h>
  33. #include <igl/snap_to_fixed_up.h>
  34. #include <igl/tetgen/mesh_with_skeleton.h>
  35. #include <igl/tetgen/tetrahedralize.h>
  36. #include <igl/trackball.h>
  37. #include <igl/two_axis_valuator_fixed_up.h>
  38. #include <igl/winding_number.h>
  39. #include <igl/writeDMAT.h>
  40. #include <igl/writeOBJ.h>
  41. #include <igl/writeMESH.h>
  42. #include <igl/writeOFF.h>
  43. #include <igl/writeTGF.h>
  44. #include <igl/next_filename.h>
  45. #include <igl/volume.h>
  46. #include <Eigen/Core>
  47. #include <Eigen/Geometry>
  48. #ifdef __APPLE__
  49. #include <GLUT/glut.h>
  50. #ifndef GLUT_ACTIVE_COMMAND
  51. # define GLUT_ACTIVE_COMMAND 9
  52. #endif
  53. #include <Carbon/Carbon.h>
  54. #else
  55. #include <GL/glut.h>
  56. #endif
  57. #include <string>
  58. #include <vector>
  59. #include <queue>
  60. #include <stack>
  61. #include <iostream>
  62. #include <iomanip>
  63. #define VERBOSE
  64. enum SkelStyleType
  65. {
  66. SKEL_STYLE_TYPE_3D = 0,
  67. SKEL_STYLE_TYPE_VECTOR_GRAPHICS = 1,
  68. NUM_SKEL_STYLE_TYPE = 2
  69. }skel_style;
  70. Eigen::MatrixXd V,N,W,M;
  71. Eigen::Vector3d Vmid;
  72. double bbd = 1.0;
  73. Eigen::MatrixXi F;
  74. igl::Camera camera;
  75. Eigen::MatrixXd C;
  76. Eigen::MatrixXi BE;
  77. Eigen::VectorXi P,RP;
  78. struct State
  79. {
  80. igl::MouseController mouse;
  81. Eigen::MatrixXf colors;
  82. } s;
  83. bool wireframe = false;
  84. // See README for descriptions
  85. enum RotationType
  86. {
  87. ROTATION_TYPE_IGL_TRACKBALL = 0,
  88. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  89. NUM_ROTATION_TYPES = 2,
  90. } rotation_type = ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP;
  91. std::stack<State> undo_stack;
  92. std::stack<State> redo_stack;
  93. bool is_rotating = false;
  94. bool centroid_is_visible = true;
  95. int down_x,down_y;
  96. igl::Camera down_camera;
  97. std::string output_weights_filename,output_pose_prefix;
  98. struct CameraAnimation
  99. {
  100. bool is_animating = false;
  101. double DURATION = 0.5;
  102. double start_time = 0;
  103. Eigen::Quaterniond from_quat,to_quat;
  104. } canim;
  105. typedef std::vector<
  106. Eigen::Quaterniond,
  107. Eigen::aligned_allocator<Eigen::Quaterniond> > RotationList;
  108. struct PoseAnimation
  109. {
  110. bool is_animating = false;
  111. double DURATION = 2;
  112. double start_time = 0;
  113. RotationList pose;
  114. } panim;
  115. int width,height;
  116. Eigen::Vector4f light_pos(-0.1,-0.1,0.9,0);
  117. #define REBAR_NAME "temp.rbr"
  118. igl::ReTwBar rebar;
  119. void push_undo()
  120. {
  121. undo_stack.push(s);
  122. // Clear
  123. redo_stack = std::stack<State>();
  124. }
  125. // No-op setter, does nothing
  126. void TW_CALL no_op(const void * /*value*/, void * /*clientData*/)
  127. {
  128. }
  129. void TW_CALL set_rotation_type(const void * value, void * clientData)
  130. {
  131. using namespace Eigen;
  132. using namespace std;
  133. using namespace igl;
  134. const RotationType old_rotation_type = rotation_type;
  135. rotation_type = *(const RotationType *)(value);
  136. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  137. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  138. {
  139. push_undo();
  140. canim.from_quat = camera.m_rotation_conj;
  141. snap_to_fixed_up(canim.from_quat,canim.to_quat);
  142. // start animation
  143. canim.start_time = get_seconds();
  144. canim.is_animating = true;
  145. }
  146. }
  147. void TW_CALL get_rotation_type(void * value, void *clientData)
  148. {
  149. RotationType * rt = (RotationType *)(value);
  150. *rt = rotation_type;
  151. }
  152. void reshape(int width, int height)
  153. {
  154. ::width = width;
  155. ::height = height;
  156. glViewport(0,0,width,height);
  157. // Send the new window size to AntTweakBar
  158. TwWindowSize(width, height);
  159. camera.m_aspect = (double)width/(double)height;
  160. s.mouse.reshape(width,height);
  161. }
  162. void push_scene()
  163. {
  164. using namespace igl;
  165. using namespace std;
  166. glMatrixMode(GL_PROJECTION);
  167. glPushMatrix();
  168. glLoadIdentity();
  169. gluPerspective(camera.m_angle,camera.m_aspect,camera.m_near,camera.m_far);
  170. glMatrixMode(GL_MODELVIEW);
  171. glPushMatrix();
  172. glLoadIdentity();
  173. gluLookAt(
  174. camera.eye()(0), camera.eye()(1), camera.eye()(2),
  175. camera.at()(0), camera.at()(1), camera.at()(2),
  176. camera.up()(0), camera.up()(1), camera.up()(2));
  177. }
  178. void push_object()
  179. {
  180. using namespace igl;
  181. glPushMatrix();
  182. glScaled(2./bbd,2./bbd,2./bbd);
  183. glTranslated(-Vmid(0),-Vmid(1),-Vmid(2));
  184. }
  185. void pop_object()
  186. {
  187. glPopMatrix();
  188. }
  189. void pop_scene()
  190. {
  191. glMatrixMode(GL_PROJECTION);
  192. glPopMatrix();
  193. glMatrixMode(GL_MODELVIEW);
  194. glPopMatrix();
  195. }
  196. // Set up double-sided lights
  197. void lights()
  198. {
  199. using namespace std;
  200. using namespace Eigen;
  201. glEnable(GL_LIGHTING);
  202. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  203. glEnable(GL_LIGHT0);
  204. glEnable(GL_LIGHT1);
  205. float WHITE[4] = {0.8,0.8,0.8,1.};
  206. float GREY[4] = {0.4,0.4,0.4,1.};
  207. float BLACK[4] = {0.,0.,0.,1.};
  208. Vector4f pos = light_pos;
  209. glLightfv(GL_LIGHT0,GL_AMBIENT,GREY);
  210. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  211. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  212. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  213. pos(0) *= -1;
  214. pos(1) *= -1;
  215. pos(2) *= -1;
  216. glLightfv(GL_LIGHT1,GL_AMBIENT,GREY);
  217. glLightfv(GL_LIGHT1,GL_DIFFUSE,WHITE);
  218. glLightfv(GL_LIGHT1,GL_SPECULAR,BLACK);
  219. glLightfv(GL_LIGHT1,GL_POSITION,pos.data());
  220. }
  221. void display()
  222. {
  223. using namespace igl;
  224. using namespace std;
  225. using namespace Eigen;
  226. const float back[4] = {0.75, 0.75, 0.75,0};
  227. glClearColor(back[0],back[1],back[2],0);
  228. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  229. if(canim.is_animating)
  230. {
  231. double t = (get_seconds() - canim.start_time)/canim.DURATION;
  232. if(t > 1)
  233. {
  234. t = 1;
  235. canim.is_animating = false;
  236. }
  237. Quaterniond q = canim.from_quat.slerp(t,canim.to_quat).normalized();
  238. camera.orbit(q.conjugate());
  239. }
  240. glEnable(GL_DEPTH_TEST);
  241. glDepthFunc(GL_LEQUAL);
  242. glEnable(GL_NORMALIZE);
  243. lights();
  244. push_scene();
  245. // Draw a nice floor
  246. glEnable(GL_DEPTH_TEST);
  247. glPushMatrix();
  248. const double floor_offset =
  249. -2./bbd*(V.col(1).maxCoeff()-Vmid(1));
  250. glTranslated(0,floor_offset,0);
  251. const float GREY[4] = {0.5,0.5,0.6,1.0};
  252. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  253. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  254. glEnable(GL_CULL_FACE);
  255. glCullFace(GL_BACK);
  256. draw_floor(GREY,DARK_GREY);
  257. glDisable(GL_CULL_FACE);
  258. glPopMatrix();
  259. push_object();
  260. const auto & draw_skeleton = [](const MatrixXd & T)
  261. {
  262. switch(skel_style)
  263. {
  264. default:
  265. case SKEL_STYLE_TYPE_3D:
  266. {
  267. draw_skeleton_3d(C,BE,T,s.colors,bbd*0.5);
  268. break;
  269. }
  270. case SKEL_STYLE_TYPE_VECTOR_GRAPHICS:
  271. draw_skeleton_vector_graphics(C,BE,T);
  272. break;
  273. }
  274. };
  275. // Set material properties
  276. glDisable(GL_COLOR_MATERIAL);
  277. glMaterialfv(GL_FRONT, GL_AMBIENT,GOLD_AMBIENT);
  278. glMaterialfv(GL_FRONT, GL_DIFFUSE,GOLD_DIFFUSE);
  279. glMaterialfv(GL_FRONT, GL_SPECULAR,GOLD_SPECULAR);
  280. glMaterialf (GL_FRONT, GL_SHININESS, 128);
  281. glMaterialfv(GL_BACK, GL_AMBIENT,SILVER_AMBIENT);
  282. glMaterialfv(GL_BACK, GL_DIFFUSE,FAST_GREEN_DIFFUSE);
  283. glMaterialfv(GL_BACK, GL_SPECULAR,SILVER_SPECULAR);
  284. glMaterialf (GL_BACK, GL_SHININESS, 128);
  285. if(wireframe)
  286. {
  287. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  288. }
  289. glLineWidth(1.0);
  290. MatrixXd T;
  291. RotationList dQ;
  292. if(panim.is_animating)
  293. {
  294. double t = (get_seconds() - panim.start_time)/panim.DURATION;
  295. if(t > 1)
  296. {
  297. t = 1;
  298. panim.is_animating = false;
  299. }
  300. const auto & ease = [](const double t)
  301. {
  302. return 3.*t*t-2.*t*t*t;
  303. };
  304. double f = (t<0.5?ease(2.*t):ease(2.-2.*t));
  305. dQ.resize(panim.pose.size());
  306. for(int e = 0;e<(int)panim.pose.size();e++)
  307. {
  308. dQ[e] = panim.pose[e].slerp(f,Quaterniond::Identity()).normalized();
  309. }
  310. }else
  311. {
  312. dQ = s.mouse.rotations();
  313. }
  314. forward_kinematics(C,BE,P,dQ,T);
  315. MatrixXd U = M*T;
  316. MatrixXd UN;
  317. per_face_normals(U,F,UN);
  318. draw_mesh(U,F,UN);
  319. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  320. glDisable(GL_DEPTH_TEST);
  321. draw_skeleton(T);
  322. if(centroid_is_visible)
  323. {
  324. Vector3d cen;
  325. centroid(U,F,cen);
  326. glEnable(GL_DEPTH_TEST);
  327. glPushMatrix();
  328. glTranslated(cen(0),cen(1),cen(2));
  329. glScaled(bbd/2.,bbd/2.,bbd/2.);
  330. glScaled(0.1,0.1,0.1);
  331. glEnable(GL_POLYGON_OFFSET_FILL);
  332. glPolygonOffset(0,-100000);
  333. draw_beach_ball();
  334. glDisable(GL_POLYGON_OFFSET_FILL);
  335. glPopMatrix();
  336. }
  337. // Mouse is always on top
  338. glDisable(GL_DEPTH_TEST);
  339. if(!panim.is_animating)
  340. {
  341. s.mouse.draw();
  342. }
  343. pop_object();
  344. pop_scene();
  345. report_gl_error();
  346. TwDraw();
  347. glutSwapBuffers();
  348. if(canim.is_animating || panim.is_animating)
  349. {
  350. glutPostRedisplay();
  351. }
  352. }
  353. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  354. {
  355. using namespace std;
  356. using namespace igl;
  357. using namespace Eigen;
  358. GLint viewport[4];
  359. glGetIntegerv(GL_VIEWPORT,viewport);
  360. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  361. {
  362. static double mouse_scroll_y = 0;
  363. const double delta_y = 0.125*direction;
  364. mouse_scroll_y += delta_y;
  365. TwMouseWheel(mouse_scroll_y);
  366. return;
  367. }
  368. push_undo();
  369. if(wheel==0)
  370. {
  371. // factor of zoom change
  372. double s = (1.-0.01*direction);
  373. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  374. //camera.m_angle *= s;
  375. //camera.m_angle = min(max(camera.m_angle,1),89);
  376. camera.push_away(s);
  377. }else
  378. {
  379. // Dolly zoom:
  380. camera.dolly_zoom((double)direction*1.0);
  381. }
  382. glutPostRedisplay();
  383. }
  384. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  385. {
  386. using namespace std;
  387. using namespace Eigen;
  388. using namespace igl;
  389. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  390. const int mod = (glutButton <=2 ? glutGetModifiers() : 0);
  391. const bool option_down = mod & GLUT_ACTIVE_ALT;
  392. switch(glutButton)
  393. {
  394. case GLUT_RIGHT_BUTTON:
  395. case GLUT_LEFT_BUTTON:
  396. {
  397. push_scene();
  398. push_object();
  399. switch(glutState)
  400. {
  401. case 1:
  402. {
  403. // up
  404. const bool mouse_was_selecting = s.mouse.is_selecting();
  405. is_rotating = false;
  406. s.mouse.up(mouse_x,mouse_y);
  407. glutSetCursor(GLUT_CURSOR_INHERIT);
  408. if(mouse_was_selecting)
  409. {
  410. s.mouse.set_selection_from_last_drag(C,BE,P,RP);
  411. MouseController::VectorXb S;
  412. MouseController::propogate_to_descendants_if(
  413. s.mouse.selection(),P,S);
  414. MouseController::color_if(S,MAYA_SEA_GREEN,MAYA_VIOLET,s.colors);
  415. }
  416. break;
  417. }
  418. case 0:
  419. if(!tw_using)
  420. {
  421. down_x = mouse_x;
  422. down_y = mouse_y;
  423. if(option_down || glutButton==GLUT_RIGHT_BUTTON)
  424. {
  425. glutSetCursor(GLUT_CURSOR_CYCLE);
  426. // collect information for trackball
  427. is_rotating = true;
  428. down_camera = camera;
  429. }else
  430. {
  431. push_undo();
  432. s.mouse.down(mouse_x,mouse_y);
  433. }
  434. }
  435. break;
  436. }
  437. pop_object();
  438. pop_scene();
  439. break;
  440. }
  441. // Scroll down
  442. case 3:
  443. {
  444. mouse_wheel(0,-1,mouse_x,mouse_y);
  445. break;
  446. }
  447. // Scroll up
  448. case 4:
  449. {
  450. mouse_wheel(0,1,mouse_x,mouse_y);
  451. break;
  452. }
  453. // Scroll left
  454. case 5:
  455. {
  456. mouse_wheel(1,-1,mouse_x,mouse_y);
  457. break;
  458. }
  459. // Scroll right
  460. case 6:
  461. {
  462. mouse_wheel(1,1,mouse_x,mouse_y);
  463. break;
  464. }
  465. }
  466. glutPostRedisplay();
  467. }
  468. void mouse_drag(int mouse_x, int mouse_y)
  469. {
  470. using namespace igl;
  471. using namespace std;
  472. using namespace Eigen;
  473. push_scene();
  474. push_object();
  475. if(is_rotating)
  476. {
  477. glutSetCursor(GLUT_CURSOR_CYCLE);
  478. Quaterniond q;
  479. switch(rotation_type)
  480. {
  481. case ROTATION_TYPE_IGL_TRACKBALL:
  482. {
  483. // Rotate according to trackball
  484. igl::trackball(
  485. width,
  486. height,
  487. 2.0,
  488. down_camera.m_rotation_conj,
  489. down_x,
  490. down_y,
  491. mouse_x,
  492. mouse_y,
  493. q);
  494. break;
  495. }
  496. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  497. {
  498. // Rotate according to two axis valuator with fixed up vector
  499. two_axis_valuator_fixed_up(
  500. width, height,
  501. 2.0,
  502. down_camera.m_rotation_conj,
  503. down_x, down_y, mouse_x, mouse_y,
  504. q);
  505. break;
  506. }
  507. default:
  508. break;
  509. }
  510. camera.orbit(q.conjugate());
  511. }else if(s.mouse.drag(mouse_x,mouse_y))
  512. {
  513. }
  514. pop_object();
  515. pop_scene();
  516. glutPostRedisplay();
  517. }
  518. void init_relative()
  519. {
  520. using namespace Eigen;
  521. using namespace igl;
  522. using namespace std;
  523. per_face_normals(V,F,N);
  524. const auto Vmax = V.colwise().maxCoeff();
  525. const auto Vmin = V.colwise().minCoeff();
  526. Vmid = 0.5*(Vmax + Vmin);
  527. bbd = (Vmax-Vmin).norm();
  528. camera.push_away(2);
  529. }
  530. void undo()
  531. {
  532. using namespace std;
  533. if(!undo_stack.empty())
  534. {
  535. redo_stack.push(s);
  536. s = undo_stack.top();
  537. undo_stack.pop();
  538. s.mouse.reshape(width,height);
  539. }
  540. }
  541. void redo()
  542. {
  543. using namespace std;
  544. if(!redo_stack.empty())
  545. {
  546. undo_stack.push(s);
  547. s = redo_stack.top();
  548. redo_stack.pop();
  549. s.mouse.reshape(width,height);
  550. }
  551. }
  552. bool save_pose()
  553. {
  554. using namespace std;
  555. using namespace igl;
  556. using namespace Eigen;
  557. string output_filename;
  558. next_filename(output_pose_prefix,4,".dmat",output_filename);
  559. MatrixXd T;
  560. forward_kinematics(C,BE,P,s.mouse.rotations(),T);
  561. if(writeDMAT(output_filename,T))
  562. {
  563. cout<<GREENGIN("Current pose written to "+output_filename+".")<<endl;
  564. return true;
  565. }else
  566. {
  567. cout<<REDRUM("Writing to "+output_filename+" failed.")<<endl;
  568. return false;
  569. }
  570. }
  571. bool save_weights()
  572. {
  573. using namespace std;
  574. using namespace igl;
  575. using namespace Eigen;
  576. if(writeDMAT(output_weights_filename,W))
  577. {
  578. cout<<GREENGIN("Current weights written to "+
  579. output_weights_filename+".")<<endl;
  580. return true;
  581. }else
  582. {
  583. cout<<REDRUM("Writing to "+output_weights_filename+" failed.")<<endl;
  584. return false;
  585. }
  586. }
  587. void key(unsigned char key, int mouse_x, int mouse_y)
  588. {
  589. using namespace std;
  590. using namespace igl;
  591. using namespace Eigen;
  592. int mod = glutGetModifiers();
  593. const bool command_down = GLUT_ACTIVE_COMMAND & mod;
  594. const bool shift_down = GLUT_ACTIVE_SHIFT & mod;
  595. switch(key)
  596. {
  597. // ESC
  598. case char(27):
  599. rebar.save(REBAR_NAME);
  600. // ^C
  601. case char(3):
  602. exit(0);
  603. case 'A':
  604. case 'a':
  605. {
  606. panim.is_animating = !panim.is_animating;
  607. panim.pose = s.mouse.rotations();
  608. panim.start_time = get_seconds();
  609. break;
  610. }
  611. case 'D':
  612. case 'd':
  613. {
  614. push_undo();
  615. s.mouse.clear_selection();
  616. break;
  617. }
  618. case 'R':
  619. {
  620. push_undo();
  621. s.mouse.reset_selected_rotations();
  622. break;
  623. }
  624. case 'r':
  625. {
  626. push_undo();
  627. s.mouse.reset_rotations();
  628. break;
  629. }
  630. case 'S':
  631. case 's':
  632. {
  633. save_pose();
  634. break;
  635. }
  636. case 'W':
  637. case 'w':
  638. {
  639. save_weights();
  640. break;
  641. }
  642. case 'z':
  643. case 'Z':
  644. is_rotating = false;
  645. if(command_down)
  646. {
  647. if(shift_down)
  648. {
  649. redo();
  650. }else
  651. {
  652. undo();
  653. }
  654. break;
  655. }else
  656. {
  657. push_undo();
  658. Quaterniond q;
  659. snap_to_canonical_view_quat(camera.m_rotation_conj,1.0,q);
  660. camera.orbit(q.conjugate());
  661. }
  662. break;
  663. default:
  664. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  665. {
  666. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  667. }
  668. }
  669. glutPostRedisplay();
  670. }
  671. bool clean(
  672. const Eigen::MatrixXd & V,
  673. const Eigen::MatrixXi & F,
  674. Eigen::MatrixXd & CV,
  675. Eigen::MatrixXi & CF)
  676. {
  677. using namespace igl;
  678. using namespace Eigen;
  679. using namespace std;
  680. {
  681. MatrixXi _1;
  682. VectorXi _2,IM;
  683. #ifdef VERBOSE
  684. cout<<"remesh_self_intersections"<<endl;
  685. #endif
  686. remesh_self_intersections(V,F,{},CV,CF,_1,_2,IM);
  687. for_each(CF.data(),CF.data()+CF.size(),[&IM](int & a){a=IM(a);});
  688. MatrixXd oldCV = CV;
  689. MatrixXi oldCF = CF;
  690. remove_unreferenced(oldCV,oldCF,CV,CF,IM);
  691. }
  692. MatrixXd TV;
  693. MatrixXi TT;
  694. {
  695. MatrixXi _1;
  696. // c convex hull
  697. // Y no boundary steiners
  698. // p polygon input
  699. #ifdef VERBOSE
  700. cout<<"tetrahedralize"<<endl;
  701. #endif
  702. if(tetrahedralize(CV,CF,"cYpC",TV,TT,_1) != 0)
  703. {
  704. cout<<REDRUM("CDT failed.")<<endl;
  705. return false;
  706. }
  707. }
  708. {
  709. MatrixXd BC;
  710. barycenter(TV,TT,BC);
  711. VectorXd W;
  712. #ifdef VERBOSE
  713. cout<<"winding_number"<<endl;
  714. #endif
  715. winding_number(V,F,BC,W);
  716. W = W.array().abs();
  717. const double thresh = 0.5;
  718. const int count = (W.array()>thresh).cast<int>().sum();
  719. MatrixXi CT(count,TT.cols());
  720. int c = 0;
  721. for(int t = 0;t<TT.rows();t++)
  722. {
  723. if(W(t)>thresh)
  724. {
  725. CT.row(c++) = TT.row(t);
  726. }
  727. }
  728. assert(c==count);
  729. boundary_facets(CT,CF);
  730. }
  731. return true;
  732. }
  733. bool robust_weights(
  734. const Eigen::MatrixXd & V,
  735. const Eigen::MatrixXi & F,
  736. const Eigen::MatrixXd & C,
  737. const Eigen::MatrixXi & BE,
  738. Eigen::MatrixXd & W)
  739. {
  740. using namespace igl;
  741. using namespace Eigen;
  742. using namespace std;
  743. // clean mesh
  744. MatrixXd CV;
  745. MatrixXi CF;
  746. if(!clean(V,F,CV,CF))
  747. {
  748. return false;
  749. }
  750. MatrixXd TV;
  751. MatrixXi TT;
  752. // compute tet-mesh
  753. {
  754. MatrixXi _1;
  755. #ifdef VERBOSE
  756. cout<<"mesh_with_skeleton"<<endl;
  757. #endif
  758. if(!mesh_with_skeleton(CV,CF,C,{},BE,{},10,"pq1.5Y",TV,TT,_1))
  759. {
  760. cout<<REDRUM("tetgen failed.")<<endl;
  761. return false;
  762. }
  763. }
  764. // Finally, tetgen may have still included some insanely small tets.
  765. // Just ignore these during weight computation (and hope they don't isolate
  766. // any vertices).
  767. {
  768. const MatrixXi oldTT = TT;
  769. VectorXd vol;
  770. volume(TV,TT,vol);
  771. const double thresh = 1e-17;
  772. const int count = (vol.array()>thresh).cast<int>().sum();
  773. TT.resize(count,oldTT.cols());
  774. int c = 0;
  775. for(int t = 0;t<oldTT.rows();t++)
  776. {
  777. if(vol(t)>thresh)
  778. {
  779. TT.row(c++) = oldTT.row(t);
  780. }
  781. }
  782. }
  783. // compute weights
  784. VectorXi b;
  785. MatrixXd bc;
  786. if(!boundary_conditions(TV,TT,C,{},BE,{},b,bc))
  787. {
  788. cout<<REDRUM("boundary_conditions failed.")<<endl;
  789. return false;
  790. }
  791. // compute BBW
  792. // Default bbw data and flags
  793. BBWData bbw_data;
  794. bbw_data.verbosity = 1;
  795. #ifdef IGL_NO_MOSEK
  796. bbw_data.qp_solver = QP_SOLVER_IGL_ACTIVE_SET;
  797. bbw_data.active_set_params.max_iter = 4;
  798. #else
  799. bbw_data.mosek_data.douparam[MSK_DPAR_INTPNT_TOL_REL_GAP]=1e-14;
  800. bbw_data.qp_solver = QP_SOLVER_MOSEK;
  801. #endif
  802. // Weights matrix
  803. if(!bbw(TV,TT,b,bc,bbw_data,W))
  804. {
  805. return false;
  806. }
  807. // Normalize weights to sum to one
  808. normalize_row_sums(W,W);
  809. // keep first #V weights
  810. W.conservativeResize(V.rows(),W.cols());
  811. return true;
  812. }
  813. int main(int argc, char * argv[])
  814. {
  815. using namespace std;
  816. using namespace Eigen;
  817. using namespace igl;
  818. string filename = "../shared/cheburashka.off";
  819. string skel_filename = "../shared/cheburashka.tgf";
  820. string weights_filename = "";
  821. output_pose_prefix = "";
  822. switch(argc)
  823. {
  824. case 5:
  825. output_pose_prefix = argv[4];
  826. //fall through
  827. case 4:
  828. weights_filename = argv[3];
  829. //fall through
  830. case 3:
  831. skel_filename = argv[2];
  832. // Read and prepare mesh
  833. filename = argv[1];
  834. break;
  835. default:
  836. cerr<<"Usage:"<<endl<<" ./example model.obj skeleton.tgf [weights.dmat] [pose-prefix]"<<endl;
  837. cout<<endl<<"Opening default rig..."<<endl;
  838. }
  839. // print key commands
  840. cout<<"[Click] and [drag] Select a bone/Use onscreen widget to rotate bone."<<endl;
  841. cout<<"⌥ +[Click] and [drag] Rotate secene."<<endl;
  842. cout<<"⌫ Delete selected node(s) and incident bones."<<endl;
  843. cout<<"D,d Deselect all."<<endl;
  844. cout<<"R Reset selected rotation."<<endl;
  845. cout<<"r Reset all rotations."<<endl;
  846. cout<<"S,s Save current pose."<<endl;
  847. cout<<"W,w Save current weights."<<endl;
  848. cout<<"Z,z Snap to canonical view."<<endl;
  849. cout<<"⌘ Z Undo."<<endl;
  850. cout<<"⇧ ⌘ Z Redo."<<endl;
  851. cout<<"^C,ESC Exit (without saving)."<<endl;
  852. string dir,_1,_2,name;
  853. read_triangle_mesh(filename,V,F,dir,_1,_2,name);
  854. if(output_weights_filename.size() == 0)
  855. {
  856. output_weights_filename = dir+"/"+name+"-weights.dmat";
  857. }
  858. if(output_pose_prefix.size() == 0)
  859. {
  860. output_pose_prefix = dir+"/"+name+"-pose-";
  861. }
  862. {
  863. string output_filename;
  864. next_filename(output_pose_prefix,4,".dmat",output_filename);
  865. cout<<BLUEGIN("Output weights set to start with "<<
  866. output_weights_filename)<<endl;
  867. cout<<BLUEGIN("Output poses set to start with "<<output_filename)<<endl;
  868. }
  869. // Read in skeleton and precompute hierarchy
  870. readTGF(skel_filename,C,BE);
  871. // initialize mouse interface
  872. s.mouse.set_size(BE.rows());
  873. // Rigid parts (not used)
  874. colon<int>(0,BE.rows()-1,RP);
  875. assert(RP.size() == BE.rows());
  876. // Bone parents
  877. bone_parents(BE,P);
  878. if(weights_filename.size() == 0)
  879. {
  880. robust_weights(V,F,C,BE,W);
  881. }else
  882. {
  883. // Read in weights and precompute LBS matrix
  884. readDMAT(weights_filename,W);
  885. }
  886. lbs_matrix(V,W,M);
  887. init_relative();
  888. // Init glut
  889. glutInit(&argc,argv);
  890. if( !TwInit(TW_OPENGL, NULL) )
  891. {
  892. // A fatal error occured
  893. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  894. return 1;
  895. }
  896. // Create a tweak bar
  897. rebar.TwNewBar("TweakBar");
  898. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,
  899. camera.m_rotation_conj.coeffs().data(), "open readonly=true");
  900. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType",
  901. "igl_trackball,two-a...-fixed-up");
  902. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  903. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  904. rebar.TwAddVarRW("wireframe", TW_TYPE_BOOLCPP,&wireframe,"key=l");
  905. rebar.TwAddVarRW("centroid_is_visible", TW_TYPE_BOOLCPP,&centroid_is_visible,
  906. "keyIncr=C keyDecr=c label='centroid visible?'");
  907. TwType SkelStyleTypeTW = ReTwDefineEnumFromString("SkelStyleType",
  908. "3d,vector-graphics");
  909. rebar.TwAddVarRW("style",SkelStyleTypeTW,&skel_style,"");
  910. rebar.load(REBAR_NAME);
  911. // Init antweakbar
  912. glutInitDisplayString( "rgba depth double samples>=8 ");
  913. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  914. glutCreateWindow("skeleton-poser");
  915. glutDisplayFunc(display);
  916. glutReshapeFunc(reshape);
  917. glutKeyboardFunc(key);
  918. glutMouseFunc(mouse);
  919. glutMotionFunc(mouse_drag);
  920. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  921. glutMainLoop();
  922. return 0;
  923. }