example.cpp 23 KB

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