example.cpp 21 KB

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