example.cpp 22 KB

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