example.cpp 22 KB

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