example.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. #include <igl/Camera.h>
  2. #include <igl/REDRUM.h>
  3. #include <igl/REDRUM.h>
  4. #include <igl/adjacency_list.h>
  5. #include <igl/adjacency_matrix.h>
  6. #include <igl/boundary_conditions.h>
  7. #include <igl/centroid.h>
  8. #include <igl/colon.h>
  9. #include <igl/opengl2/draw_floor.h>
  10. #include <igl/opengl2/draw_mesh.h>
  11. #include <igl/opengl2/draw_skeleton_3d.h>
  12. #include <igl/opengl2/draw_skeleton_vector_graphics.h>
  13. #include <igl/file_exists.h>
  14. #include <igl/forward_kinematics.h>
  15. #include <igl/get_seconds.h>
  16. #include <igl/lbs_matrix.h>
  17. #include <igl/list_to_matrix.h>
  18. #include <igl/material_colors.h>
  19. #include <igl/matlab_format.h>
  20. #include <igl/normalize_row_sums.h>
  21. #include <igl/pathinfo.h>
  22. #include <igl/per_face_normals.h>
  23. #include <igl/opengl2/project.h>
  24. #include <igl/quat_to_mat.h>
  25. #include <igl/readTGF.h>
  26. #include <igl/read_triangle_mesh.h>
  27. #include <igl/remove_unreferenced.h>
  28. #include <igl/opengl/report_gl_error.h>
  29. #include <igl/opengl2/right_axis.h>
  30. #include <igl/slice.h>
  31. #include <igl/snap_to_canonical_view_quat.h>
  32. #include <igl/snap_to_fixed_up.h>
  33. #include <igl/opengl2/sort_triangles.h>
  34. #include <igl/trackball.h>
  35. #include <igl/two_axis_valuator_fixed_up.h>
  36. #include <igl/unique.h>
  37. #include <igl/opengl2/unproject.h>
  38. #include <igl/opengl2/model_proj_viewport.h>
  39. #include <igl/writeOBJ.h>
  40. #include <igl/writeOFF.h>
  41. #include <igl/writeTGF.h>
  42. #include <igl/anttweakbar/ReAntTweakBar.h>
  43. #include <igl/embree/EmbreeIntersector.h>
  44. #include <igl/embree/unproject_in_mesh.h>
  45. #include <Eigen/Core>
  46. #include <Eigen/Geometry>
  47. #ifdef __APPLE__
  48. #include <GLUT/glut.h>
  49. #else
  50. #include <GL/glut.h>
  51. #endif
  52. #ifndef GLUT_WHEEL_UP
  53. #define GLUT_WHEEL_UP 3
  54. #endif
  55. #ifndef GLUT_WHEEL_DOWN
  56. #define GLUT_WHEEL_DOWN 4
  57. #endif
  58. #ifndef GLUT_WHEEL_RIGHT
  59. #define GLUT_WHEEL_RIGHT 5
  60. #endif
  61. #ifndef GLUT_WHEEL_LEFT
  62. #define GLUT_WHEEL_LEFT 6
  63. #endif
  64. #ifndef GLUT_ACTIVE_COMMAND
  65. #define GLUT_ACTIVE_COMMAND 8
  66. #endif
  67. #include <string>
  68. #include <vector>
  69. #include <queue>
  70. #include <stack>
  71. #include <iostream>
  72. enum SkelStyleType
  73. {
  74. SKEL_STYLE_TYPE_3D = 0,
  75. SKEL_STYLE_TYPE_VECTOR_GRAPHICS = 1,
  76. NUM_SKEL_STYLE_TYPE = 2
  77. }skel_style;
  78. Eigen::MatrixXd V,N,sorted_N;
  79. Eigen::Vector3d Vmid,Vcen;
  80. double bbd = 1.0;
  81. Eigen::MatrixXi F,sorted_F;
  82. Eigen::VectorXi P;
  83. igl::Camera camera;
  84. struct State
  85. {
  86. Eigen::MatrixXd C;
  87. Eigen::MatrixXi BE;
  88. Eigen::VectorXi sel;
  89. } s;
  90. bool wireframe = false;
  91. bool skeleton_on_top = false;
  92. double alpha = 0.5;
  93. // See README for descriptions
  94. enum RotationType
  95. {
  96. ROTATION_TYPE_IGL_TRACKBALL = 0,
  97. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  98. NUM_ROTATION_TYPES = 2,
  99. } rotation_type = ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP;
  100. std::stack<State> undo_stack;
  101. std::stack<State> redo_stack;
  102. bool is_rotating = false;
  103. bool is_dragging = false;
  104. bool new_leaf_on_drag = false;
  105. bool new_root_on_drag = false;
  106. int down_x,down_y;
  107. Eigen::MatrixXd down_C;
  108. igl::Camera down_camera;
  109. std::string output_filename;
  110. bool is_animating = false;
  111. double animation_start_time = 0;
  112. double ANIMATION_DURATION = 0.5;
  113. Eigen::Quaterniond animation_from_quat;
  114. Eigen::Quaterniond animation_to_quat;
  115. int width,height;
  116. Eigen::Vector4f light_pos(-0.1,-0.1,0.9,0);
  117. #define REBAR_NAME "temp.rbr"
  118. igl::anttweakbar::ReTwBar rebar;
  119. igl::embree::EmbreeIntersector ei;
  120. void push_undo()
  121. {
  122. undo_stack.push(s);
  123. // Clear
  124. redo_stack = std::stack<State>();
  125. }
  126. // No-op setter, does nothing
  127. void TW_CALL no_op(const void * /*value*/, void * /*clientData*/)
  128. {
  129. }
  130. void TW_CALL set_rotation_type(const void * value, void * clientData)
  131. {
  132. using namespace Eigen;
  133. using namespace std;
  134. using namespace igl;
  135. const RotationType old_rotation_type = rotation_type;
  136. rotation_type = *(const RotationType *)(value);
  137. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  138. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  139. {
  140. push_undo();
  141. animation_from_quat = camera.m_rotation_conj;
  142. snap_to_fixed_up(animation_from_quat,animation_to_quat);
  143. // start animation
  144. animation_start_time = get_seconds();
  145. is_animating = true;
  146. }
  147. }
  148. void TW_CALL get_rotation_type(void * value, void *clientData)
  149. {
  150. RotationType * rt = (RotationType *)(value);
  151. *rt = rotation_type;
  152. }
  153. void reshape(int width, int height)
  154. {
  155. ::width = width;
  156. ::height = height;
  157. glViewport(0,0,width,height);
  158. // Send the new window size to AntTweakBar
  159. TwWindowSize(width, height);
  160. camera.m_aspect = (double)width/(double)height;
  161. }
  162. void push_scene()
  163. {
  164. using namespace igl;
  165. using namespace std;
  166. glMatrixMode(GL_PROJECTION);
  167. glPushMatrix();
  168. glLoadIdentity();
  169. gluPerspective(camera.m_angle,camera.m_aspect,camera.m_near,camera.m_far);
  170. glMatrixMode(GL_MODELVIEW);
  171. glPushMatrix();
  172. glLoadIdentity();
  173. gluLookAt(
  174. camera.eye()(0), camera.eye()(1), camera.eye()(2),
  175. camera.at()(0), camera.at()(1), camera.at()(2),
  176. camera.up()(0), camera.up()(1), camera.up()(2));
  177. }
  178. void push_object()
  179. {
  180. using namespace igl;
  181. glPushMatrix();
  182. glScaled(2./bbd,2./bbd,2./bbd);
  183. glTranslated(-Vmid(0),-Vmid(1),-Vmid(2));
  184. }
  185. void pop_object()
  186. {
  187. glPopMatrix();
  188. }
  189. void pop_scene()
  190. {
  191. glMatrixMode(GL_PROJECTION);
  192. glPopMatrix();
  193. glMatrixMode(GL_MODELVIEW);
  194. glPopMatrix();
  195. }
  196. // Set up double-sided lights
  197. void lights()
  198. {
  199. using namespace std;
  200. using namespace Eigen;
  201. glEnable(GL_LIGHTING);
  202. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  203. glEnable(GL_LIGHT0);
  204. glEnable(GL_LIGHT1);
  205. float WHITE[4] = {0.8,0.8,0.8,1.};
  206. float GREY[4] = {0.4,0.4,0.4,1.};
  207. float BLACK[4] = {0.,0.,0.,1.};
  208. Vector4f pos = light_pos;
  209. glLightfv(GL_LIGHT0,GL_AMBIENT,GREY);
  210. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  211. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  212. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  213. pos(0) *= -1;
  214. pos(1) *= -1;
  215. pos(2) *= -1;
  216. glLightfv(GL_LIGHT1,GL_AMBIENT,GREY);
  217. glLightfv(GL_LIGHT1,GL_DIFFUSE,WHITE);
  218. glLightfv(GL_LIGHT1,GL_SPECULAR,BLACK);
  219. glLightfv(GL_LIGHT1,GL_POSITION,pos.data());
  220. }
  221. void sort()
  222. {
  223. using namespace std;
  224. using namespace Eigen;
  225. using namespace igl;
  226. push_scene();
  227. push_object();
  228. VectorXi I;
  229. igl::opengl2::sort_triangles(V,F,sorted_F,I);
  230. slice(N,I,1,sorted_N);
  231. pop_object();
  232. pop_scene();
  233. }
  234. void display()
  235. {
  236. using namespace igl;
  237. using namespace std;
  238. using namespace Eigen;
  239. const float back[4] = {0.75, 0.75, 0.75,0};
  240. glClearColor(back[0],back[1],back[2],0);
  241. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  242. static bool first = true;
  243. if(first)
  244. {
  245. sort();
  246. first = false;
  247. }
  248. if(is_animating)
  249. {
  250. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  251. if(t > 1)
  252. {
  253. t = 1;
  254. is_animating = false;
  255. }
  256. Quaterniond q = animation_from_quat.slerp(t,animation_to_quat).normalized();
  257. camera.orbit(q.conjugate());
  258. }
  259. glEnable(GL_DEPTH_TEST);
  260. glDepthFunc(GL_LEQUAL);
  261. glEnable(GL_NORMALIZE);
  262. glEnable(GL_BLEND);
  263. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  264. lights();
  265. push_scene();
  266. // Draw a nice floor
  267. glEnable(GL_DEPTH_TEST);
  268. glPushMatrix();
  269. const double floor_offset =
  270. -2./bbd*(V.col(1).maxCoeff()-Vmid(1));
  271. glTranslated(0,floor_offset,0);
  272. const float GREY[4] = {0.5,0.5,0.6,1.0};
  273. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  274. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  275. glEnable(GL_CULL_FACE);
  276. glCullFace(GL_BACK);
  277. igl::opengl2::draw_floor(GREY,DARK_GREY);
  278. glDisable(GL_CULL_FACE);
  279. glPopMatrix();
  280. push_object();
  281. const auto & draw_skeleton = []()
  282. {
  283. switch(skel_style)
  284. {
  285. default:
  286. case SKEL_STYLE_TYPE_3D:
  287. {
  288. MatrixXf colors = MAYA_VIOLET.transpose().replicate(s.BE.rows(),1);
  289. for(int si=0;si<s.sel.size();si++)
  290. {
  291. for(int b=0;b<s.BE.rows();b++)
  292. {
  293. if(s.BE(b,0) == s.sel(si) || s.BE(b,1) == s.sel(si))
  294. {
  295. colors.row(b) = MAYA_SEA_GREEN;
  296. }
  297. }
  298. }
  299. igl::opengl2::draw_skeleton_3d(s.C,s.BE,MatrixXd(),colors,bbd*0.5);
  300. break;
  301. }
  302. case SKEL_STYLE_TYPE_VECTOR_GRAPHICS:
  303. igl::opengl2::draw_skeleton_vector_graphics(s.C,s.BE);
  304. break;
  305. }
  306. };
  307. if(!skeleton_on_top)
  308. {
  309. draw_skeleton();
  310. }
  311. // Set material properties
  312. glDisable(GL_COLOR_MATERIAL);
  313. glMaterialfv(GL_FRONT, GL_AMBIENT,
  314. Vector4f(GOLD_AMBIENT[0],GOLD_AMBIENT[1],GOLD_AMBIENT[2],alpha).data());
  315. glMaterialfv(GL_FRONT, GL_DIFFUSE,
  316. Vector4f(GOLD_DIFFUSE[0],GOLD_DIFFUSE[1],GOLD_DIFFUSE[2],alpha).data());
  317. glMaterialfv(GL_FRONT, GL_SPECULAR,
  318. Vector4f(GOLD_SPECULAR[0],GOLD_SPECULAR[1],GOLD_SPECULAR[2],alpha).data());
  319. glMaterialf (GL_FRONT, GL_SHININESS, 128);
  320. glMaterialfv(GL_BACK, GL_AMBIENT,
  321. Vector4f(SILVER_AMBIENT[0],SILVER_AMBIENT[1],SILVER_AMBIENT[2],alpha).data());
  322. glMaterialfv(GL_BACK, GL_DIFFUSE,
  323. Vector4f(FAST_GREEN_DIFFUSE[0],FAST_GREEN_DIFFUSE[1],FAST_GREEN_DIFFUSE[2],alpha).data());
  324. glMaterialfv(GL_BACK, GL_SPECULAR,
  325. Vector4f(SILVER_SPECULAR[0],SILVER_SPECULAR[1],SILVER_SPECULAR[2],alpha).data());
  326. glMaterialf (GL_BACK, GL_SHININESS, 128);
  327. if(wireframe)
  328. {
  329. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  330. }
  331. glLineWidth(1.0);
  332. igl::opengl2::draw_mesh(V,sorted_F,sorted_N);
  333. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  334. if(skeleton_on_top)
  335. {
  336. glDisable(GL_DEPTH_TEST);
  337. draw_skeleton();
  338. }
  339. pop_object();
  340. pop_scene();
  341. igl::opengl::report_gl_error();
  342. TwDraw();
  343. glutSwapBuffers();
  344. if(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. Eigen::VectorXi selection(const std::vector<bool> & mask)
  381. {
  382. const int count = std::count(mask.begin(),mask.end(),true);
  383. Eigen::VectorXi sel(count);
  384. int s = 0;
  385. for(int c = 0;c<(int)mask.size();c++)
  386. {
  387. if(mask[c])
  388. {
  389. sel(s) = c;
  390. s++;
  391. }
  392. }
  393. return sel;
  394. }
  395. std::vector<bool> selection_mask(const Eigen::VectorXi & sel, const int n)
  396. {
  397. std::vector<bool> mask(n,false);
  398. for(int si = 0;si<sel.size();si++)
  399. {
  400. const int i = sel(si);
  401. mask[i] = true;
  402. }
  403. return mask;
  404. }
  405. bool ss_select(
  406. const double mouse_x,
  407. const double mouse_y,
  408. const Eigen::MatrixXd & C,
  409. const bool accum,
  410. Eigen::VectorXi & sel)
  411. {
  412. using namespace igl;
  413. using namespace Eigen;
  414. using namespace std;
  415. //// zap old list
  416. //if(!accum)
  417. //{
  418. // sel.resize(0,1);
  419. //}
  420. vector<bool> old_mask = selection_mask(s.sel,s.C.rows());
  421. vector<bool> mask(old_mask.size(),false);
  422. double min_dist = 1e25;
  423. bool sel_changed = false;
  424. bool found = false;
  425. for(int c = 0;c<C.rows();c++)
  426. {
  427. const RowVector3d & Cc = C.row(c);
  428. const auto Pc = igl::opengl2::project(Cc);
  429. const double SELECTION_DIST = 18;//pixels
  430. const double dist = (Pc.head(2)-RowVector2d(mouse_x,height-mouse_y)).norm();
  431. if(dist < SELECTION_DIST && (accum || dist < min_dist))
  432. {
  433. mask[c] = true;
  434. min_dist = dist;
  435. found = true;
  436. sel_changed |= mask[c] != old_mask[c];
  437. }
  438. }
  439. for(int c = 0;c<C.rows();c++)
  440. {
  441. if(accum)
  442. {
  443. mask[c] = mask[c] ^ old_mask[c];
  444. }else
  445. {
  446. if(!sel_changed)
  447. {
  448. mask[c] = mask[c] || old_mask[c];
  449. }
  450. }
  451. }
  452. sel = selection(mask);
  453. return found;
  454. }
  455. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  456. {
  457. using namespace std;
  458. using namespace Eigen;
  459. using namespace igl;
  460. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  461. const int mod = (glutButton <=2 ? glutGetModifiers() : 0);
  462. const bool option_down = mod & GLUT_ACTIVE_ALT;
  463. const bool shift_down = mod & GLUT_ACTIVE_SHIFT;
  464. const bool command_down = GLUT_ACTIVE_COMMAND & mod;
  465. switch(glutButton)
  466. {
  467. case GLUT_RIGHT_BUTTON:
  468. case GLUT_LEFT_BUTTON:
  469. {
  470. switch(glutState)
  471. {
  472. case 1:
  473. // up
  474. glutSetCursor(GLUT_CURSOR_INHERIT);
  475. if(is_rotating)
  476. {
  477. sort();
  478. }
  479. is_rotating = false;
  480. is_dragging = false;
  481. break;
  482. case 0:
  483. new_leaf_on_drag = false;
  484. new_root_on_drag = false;
  485. if(!tw_using)
  486. {
  487. down_x = mouse_x;
  488. down_y = mouse_y;
  489. if(option_down)
  490. {
  491. glutSetCursor(GLUT_CURSOR_CYCLE);
  492. // collect information for trackball
  493. is_rotating = true;
  494. down_camera = camera;
  495. }else
  496. {
  497. push_undo();
  498. push_scene();
  499. push_object();
  500. // Zap selection
  501. if(shift_down)
  502. {
  503. s.sel.resize(0,1);
  504. }
  505. if(ss_select(mouse_x,mouse_y,s.C,
  506. command_down && !shift_down,
  507. s.sel))
  508. {
  509. if(shift_down)
  510. {
  511. new_leaf_on_drag = true;
  512. }
  513. }else
  514. {
  515. new_root_on_drag = true;
  516. }
  517. is_dragging = !command_down;
  518. down_C = s.C;
  519. pop_object();
  520. pop_scene();
  521. }
  522. }
  523. break;
  524. }
  525. break;
  526. }
  527. // Scroll down
  528. case 3:
  529. {
  530. mouse_wheel(0,-1,mouse_x,mouse_y);
  531. break;
  532. }
  533. // Scroll up
  534. case 4:
  535. {
  536. mouse_wheel(0,1,mouse_x,mouse_y);
  537. break;
  538. }
  539. // Scroll left
  540. case 5:
  541. {
  542. mouse_wheel(1,-1,mouse_x,mouse_y);
  543. break;
  544. }
  545. // Scroll right
  546. case 6:
  547. {
  548. mouse_wheel(1,1,mouse_x,mouse_y);
  549. break;
  550. }
  551. }
  552. glutPostRedisplay();
  553. }
  554. void mouse_drag(int mouse_x, int mouse_y)
  555. {
  556. using namespace igl;
  557. using namespace std;
  558. using namespace Eigen;
  559. if(is_rotating)
  560. {
  561. glutSetCursor(GLUT_CURSOR_CYCLE);
  562. Quaterniond q;
  563. switch(rotation_type)
  564. {
  565. case ROTATION_TYPE_IGL_TRACKBALL:
  566. {
  567. // Rotate according to trackball
  568. igl::trackball<double>(
  569. width,
  570. height,
  571. 2.0,
  572. down_camera.m_rotation_conj.coeffs().data(),
  573. down_x,
  574. down_y,
  575. mouse_x,
  576. mouse_y,
  577. q.coeffs().data());
  578. break;
  579. }
  580. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  581. {
  582. // Rotate according to two axis valuator with fixed up vector
  583. two_axis_valuator_fixed_up(
  584. width, height,
  585. 2.0,
  586. down_camera.m_rotation_conj,
  587. down_x, down_y, mouse_x, mouse_y,
  588. q);
  589. break;
  590. }
  591. default:
  592. break;
  593. }
  594. camera.orbit(q.conjugate());
  595. }
  596. if(is_dragging)
  597. {
  598. push_scene();
  599. push_object();
  600. if(new_leaf_on_drag)
  601. {
  602. assert(s.C.size() >= 1);
  603. // one new node
  604. s.C.conservativeResize(s.C.rows()+1,3);
  605. const int nc = s.C.rows();
  606. assert(s.sel.size() >= 1);
  607. s.C.row(nc-1) = s.C.row(s.sel(0));
  608. // one new bone
  609. s.BE.conservativeResize(s.BE.rows()+1,2);
  610. s.BE.row(s.BE.rows()-1) = RowVector2i(s.sel(0),nc-1);
  611. // select just last node
  612. s.sel.resize(1,1);
  613. s.sel(0) = nc-1;
  614. // reset down_C
  615. down_C = s.C;
  616. new_leaf_on_drag = false;
  617. }
  618. Eigen::Matrix4f model,proj;
  619. Eigen::Vector4f viewport;
  620. igl::opengl2::model_proj_viewport(model,proj,viewport);
  621. Eigen::Vector2f pos(mouse_x,height-mouse_y);
  622. if(new_root_on_drag)
  623. {
  624. // two new nodes
  625. s.C.conservativeResize(s.C.rows()+2,3);
  626. const int nc = s.C.rows();
  627. Vector3d obj;
  628. int nhits = igl::embree::unproject_in_mesh(
  629. pos,model,proj,viewport,ei,obj);
  630. if(nhits == 0)
  631. {
  632. Vector3d pV_mid = igl::opengl2::project(Vcen);
  633. obj = igl::opengl2::unproject(Vector3d(mouse_x,height-mouse_y,pV_mid(2)));
  634. }
  635. s.C.row(nc-2) = obj;
  636. s.C.row(nc-1) = obj;
  637. // select last node
  638. s.sel.resize(1,1);
  639. s.sel(0) = nc-1;
  640. // one new bone
  641. s.BE.conservativeResize(s.BE.rows()+1,2);
  642. s.BE.row(s.BE.rows()-1) = RowVector2i(nc-2,nc-1);
  643. // reset down_C
  644. down_C = s.C;
  645. new_root_on_drag = false;
  646. }
  647. double z = 0;
  648. Vector3d obj,win;
  649. int nhits = igl::embree::unproject_in_mesh(pos,model,proj,viewport,ei,obj);
  650. igl::opengl2::project(obj,win);
  651. z = win(2);
  652. for(int si = 0;si<s.sel.size();si++)
  653. {
  654. const int c = s.sel(si);
  655. Vector3d pc = igl::opengl2::project((RowVector3d) down_C.row(c));
  656. pc(0) += mouse_x-down_x;
  657. pc(1) += (height-mouse_y)-(height-down_y);
  658. if(nhits > 0)
  659. {
  660. pc(2) = z;
  661. }
  662. s.C.row(c) = igl::opengl2::unproject(pc);
  663. }
  664. pop_object();
  665. pop_scene();
  666. }
  667. glutPostRedisplay();
  668. }
  669. void init_relative()
  670. {
  671. using namespace Eigen;
  672. using namespace igl;
  673. using namespace std;
  674. per_face_normals(V,F,N);
  675. const auto Vmax = V.colwise().maxCoeff();
  676. const auto Vmin = V.colwise().minCoeff();
  677. Vmid = 0.5*(Vmax + Vmin);
  678. centroid(V,F,Vcen);
  679. bbd = (Vmax-Vmin).norm();
  680. cout<<"bbd: "<<bbd<<endl;
  681. camera.push_away(2);
  682. }
  683. void undo()
  684. {
  685. using namespace std;
  686. if(!undo_stack.empty())
  687. {
  688. redo_stack.push(s);
  689. s = undo_stack.top();
  690. undo_stack.pop();
  691. }
  692. }
  693. void redo()
  694. {
  695. using namespace std;
  696. if(!redo_stack.empty())
  697. {
  698. undo_stack.push(s);
  699. s = redo_stack.top();
  700. redo_stack.pop();
  701. }
  702. }
  703. void symmetrize()
  704. {
  705. using namespace std;
  706. using namespace igl;
  707. using namespace Eigen;
  708. if(s.sel.size() == 0)
  709. {
  710. cout<<YELLOWGIN("Make a selection first.")<<endl;
  711. return;
  712. }
  713. push_undo();
  714. push_scene();
  715. push_object();
  716. Vector3d right;
  717. igl::opengl2::right_axis(right.data(),right.data()+1,right.data()+2);
  718. right.normalize();
  719. MatrixXd RC(s.C.rows(),s.C.cols());
  720. MatrixXd old_C = s.C;
  721. for(int c = 0;c<s.C.rows();c++)
  722. {
  723. const Vector3d Cc = s.C.row(c);
  724. const auto A = Cc-Vcen;
  725. const auto A1 = A.dot(right) * right;
  726. const auto A2 = A-A1;
  727. RC.row(c) = Vcen + A2 - A1;
  728. }
  729. vector<bool> mask = selection_mask(s.sel,s.C.rows());
  730. // stupid O(n²) matching
  731. for(int c = 0;c<s.C.rows();c++)
  732. {
  733. // not selected
  734. if(!mask[c])
  735. {
  736. continue;
  737. }
  738. const Vector3d Cc = s.C.row(c);
  739. int min_r = -1;
  740. double min_dist = 1e25;
  741. double max_dist = 0.1*bbd;
  742. for(int r= 0;r<RC.rows();r++)
  743. {
  744. const Vector3d RCr = RC.row(r);
  745. const double dist = (Cc-RCr).norm();
  746. if(
  747. dist<min_dist && // closest
  748. dist<max_dist && // not too far away
  749. (c==r || (Cc-Vcen).dot(right)*(RCr-Vcen).dot(right) > 0) // on same side
  750. )
  751. {
  752. min_dist = dist;
  753. min_r = r;
  754. }
  755. }
  756. if(min_r>=0)
  757. {
  758. if(mask[min_r])
  759. {
  760. s.C.row(c) = 0.5*(Cc.transpose()+RC.row(min_r));
  761. }else
  762. {
  763. s.C.row(c) = RC.row(min_r);
  764. }
  765. }
  766. }
  767. pop_object();
  768. pop_scene();
  769. }
  770. bool save()
  771. {
  772. using namespace std;
  773. using namespace igl;
  774. if(writeTGF(output_filename,s.C,s.BE))
  775. {
  776. cout<<GREENGIN("Current skeleton written to "+output_filename+".")<<endl;
  777. return true;
  778. }else
  779. {
  780. cout<<REDRUM("Writing to "+output_filename+" failed.")<<endl;
  781. return false;
  782. }
  783. }
  784. void key(unsigned char key, int mouse_x, int mouse_y)
  785. {
  786. using namespace std;
  787. using namespace igl;
  788. using namespace Eigen;
  789. int mod = glutGetModifiers();
  790. const bool command_down = GLUT_ACTIVE_COMMAND & mod;
  791. const bool shift_down = GLUT_ACTIVE_SHIFT & mod;
  792. switch(key)
  793. {
  794. // ESC
  795. case char(27):
  796. rebar.save(REBAR_NAME);
  797. // ^C
  798. case char(3):
  799. exit(0);
  800. case char(127):
  801. {
  802. push_undo();
  803. // delete
  804. MatrixXi new_BE(s.BE.rows(),s.BE.cols());
  805. int count = 0;
  806. for(int b=0;b<s.BE.rows();b++)
  807. {
  808. bool selected = false;
  809. for(int si=0;si<s.sel.size();si++)
  810. {
  811. if(s.BE(b,0) == s.sel(si) || s.BE(b,1) == s.sel(si))
  812. {
  813. selected = true;
  814. break;
  815. }
  816. }
  817. if(!selected)
  818. {
  819. new_BE.row(count) = s.BE.row(b);
  820. count++;
  821. }
  822. }
  823. new_BE.conservativeResize(count,new_BE.cols());
  824. const auto old_C = s.C;
  825. VectorXi I;
  826. remove_unreferenced(old_C,new_BE,s.C,s.BE,I);
  827. s.sel.resize(0,1);
  828. break;
  829. }
  830. case 'A':
  831. case 'a':
  832. {
  833. push_undo();
  834. s.sel = colon<int>(0,s.C.rows()-1);
  835. break;
  836. }
  837. case 'C':
  838. case 'c':
  839. {
  840. push_undo();
  841. // snap close vertices
  842. SparseMatrix<double> A;
  843. adjacency_matrix(s.BE,A);
  844. VectorXi J = colon<int>(0,s.C.rows()-1);
  845. // stupid O(n²) version
  846. for(int c = 0;c<s.C.rows();c++)
  847. {
  848. for(int d = c+1;d<s.C.rows();d++)
  849. {
  850. if(
  851. A.coeff(c,d) == 0 && // no edge
  852. (s.C.row(c)-s.C.row(d)).norm() < 0.02*bbd //close
  853. )
  854. {
  855. // c < d
  856. J(d) = c;
  857. }
  858. }
  859. }
  860. for(int e = 0;e<s.BE.rows();e++)
  861. {
  862. s.BE(e,0) = J(s.BE(e,0));
  863. s.BE(e,1) = J(s.BE(e,1));
  864. }
  865. const auto old_BE = s.BE;
  866. const auto old_C = s.C;
  867. VectorXi I;
  868. remove_unreferenced(old_C,old_BE,s.C,s.BE,I);
  869. for(int i = 0;i<s.sel.size();i++)
  870. {
  871. s.sel(i) = J(s.sel(i));
  872. }
  873. VectorXi _;
  874. igl::unique(s.sel,s.sel,_,_);
  875. break;
  876. }
  877. case 'D':
  878. case 'd':
  879. {
  880. push_undo();
  881. s.sel.resize(0,1);
  882. break;
  883. }
  884. case 'P':
  885. case 'p':
  886. {
  887. // add bone to parents (should really only be one)
  888. push_undo();
  889. vector<int> new_sel;
  890. const int old_nbe = s.BE.rows();
  891. for(int si=0;si<s.sel.size();si++)
  892. {
  893. for(int b=0;b<old_nbe;b++)
  894. {
  895. if(s.BE(b,1) == s.sel(si))
  896. {
  897. // one new node
  898. s.C.conservativeResize(s.C.rows()+1,3);
  899. const int nc = s.C.rows();
  900. s.C.row(nc-1) = 0.5*(s.C.row(s.BE(b,1)) + s.C.row(s.BE(b,0)));
  901. // one new bone
  902. s.BE.conservativeResize(s.BE.rows()+1,2);
  903. s.BE.row(s.BE.rows()-1) = RowVector2i(nc-1,s.BE(b,1));
  904. s.BE(b,1) = nc-1;
  905. // select last node
  906. new_sel.push_back(nc-1);
  907. }
  908. }
  909. }
  910. list_to_matrix(new_sel,s.sel);
  911. break;
  912. }
  913. case 'R':
  914. case 'r':
  915. {
  916. // re-root try at first selected
  917. if(s.sel.size() > 0)
  918. {
  919. push_undo();
  920. // only use first
  921. s.sel.conservativeResize(1,1);
  922. // Ideally this should only effect the connected component of s.sel(0)
  923. const auto & C = s.C;
  924. auto & BE = s.BE;
  925. vector<bool> seen(C.rows(),false);
  926. // adjacency list
  927. vector<vector< int> > A;
  928. adjacency_list(BE,A,false);
  929. int e = 0;
  930. queue<int> Q;
  931. Q.push(s.sel(0));
  932. seen[s.sel(0)] = true;
  933. while(!Q.empty())
  934. {
  935. const int c = Q.front();
  936. Q.pop();
  937. for(const auto & d : A[c])
  938. {
  939. if(!seen[d])
  940. {
  941. BE(e,0) = c;
  942. BE(e,1) = d;
  943. e++;
  944. Q.push(d);
  945. seen[d] = true;
  946. }
  947. }
  948. }
  949. // only keep tree
  950. BE.conservativeResize(e,BE.cols());
  951. }
  952. break;
  953. }
  954. case 'S':
  955. case 's':
  956. {
  957. save();
  958. break;
  959. }
  960. case 'U':
  961. case 'u':
  962. {
  963. push_scene();
  964. push_object();
  965. for(int c = 0;c<s.C.rows();c++)
  966. {
  967. Vector3d P = igl::opengl2::project((Vector3d)s.C.row(c));
  968. Vector3d obj;
  969. Eigen::Matrix4f model,proj;
  970. Eigen::Vector4f viewport;
  971. igl::opengl2::model_proj_viewport(model,proj,viewport);
  972. Eigen::Vector2f pos(P(0),P(1));
  973. int nhits = igl::embree::unproject_in_mesh(pos,model,proj,viewport,ei,obj);
  974. if(nhits > 0)
  975. {
  976. s.C.row(c) = obj;
  977. }
  978. }
  979. pop_object();
  980. pop_scene();
  981. break;
  982. }
  983. case 'Y':
  984. case 'y':
  985. {
  986. symmetrize();
  987. break;
  988. }
  989. case 'z':
  990. case 'Z':
  991. is_rotating = false;
  992. is_dragging = false;
  993. if(command_down)
  994. {
  995. if(shift_down)
  996. {
  997. redo();
  998. }else
  999. {
  1000. undo();
  1001. }
  1002. break;
  1003. }else
  1004. {
  1005. push_undo();
  1006. Quaterniond q;
  1007. snap_to_canonical_view_quat(camera.m_rotation_conj,1.0,q);
  1008. camera.orbit(q.conjugate());
  1009. }
  1010. break;
  1011. default:
  1012. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  1013. {
  1014. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  1015. }
  1016. }
  1017. glutPostRedisplay();
  1018. }
  1019. int main(int argc, char * argv[])
  1020. {
  1021. using namespace std;
  1022. using namespace Eigen;
  1023. using namespace igl;
  1024. string filename = "../shared/decimated-knight.obj";
  1025. string skel_filename = "";
  1026. output_filename = "";
  1027. switch(argc)
  1028. {
  1029. case 4:
  1030. output_filename = argv[3];
  1031. //fall through
  1032. case 3:
  1033. skel_filename = argv[2];
  1034. if(output_filename.size() == 0)
  1035. {
  1036. output_filename = skel_filename;
  1037. }
  1038. //fall through
  1039. case 2:
  1040. // Read and prepare mesh
  1041. filename = argv[1];
  1042. break;
  1043. default:
  1044. cerr<<"Usage:"<<endl<<" ./example input.obj [input/output.tgf]"<<endl;
  1045. cout<<endl<<"Opening default mesh..."<<endl;
  1046. }
  1047. // print key commands
  1048. cout<<"[Click] and [drag] Create bone (or select node) and reposition."<<endl;
  1049. cout<<"⇧ +[Click] and [drag] Select node (or create one) and _pull out_ new bone."<<endl;
  1050. cout<<"⌥ +[Click] and [drag] Rotate secene."<<endl;
  1051. cout<<"⌫ Delete selected node(s) and incident bones."<<endl;
  1052. cout<<"A,a Select all."<<endl;
  1053. cout<<"D,d Deselect all."<<endl;
  1054. cout<<"C,c Snap close nodes."<<endl;
  1055. cout<<"P,p Split \"parent\" bone(s) of selection by creating new node(s)."<<endl;
  1056. cout<<"R,r Breadth first search at selection to redirect skeleton into tree."<<endl;
  1057. cout<<"S,s Save current skeleton to output .tgf file."<<endl;
  1058. cout<<"U,u Project then igl::opengl2::unproject inside mesh (as if dragging each by ε)."<<endl;
  1059. cout<<"Y,Y Symmetrize selection over plane through object centroid and right vector."<<endl;
  1060. cout<<"Z,z Snap to canonical view."<<endl;
  1061. cout<<"⌘ Z Undo."<<endl;
  1062. cout<<"⇧ ⌘ Z Redo."<<endl;
  1063. cout<<"^C,ESC Exit (without saving)."<<endl;
  1064. string dir,_1,_2,name;
  1065. read_triangle_mesh(filename,V,F,dir,_1,_2,name);
  1066. if(output_filename.size() == 0)
  1067. {
  1068. output_filename = dir+"/"+name+".tgf";
  1069. }
  1070. if(file_exists(output_filename.c_str()))
  1071. {
  1072. cout<<YELLOWGIN("Output set to overwrite "<<output_filename)<<endl;
  1073. }else
  1074. {
  1075. cout<<BLUEGIN("Output set to "<<output_filename)<<endl;
  1076. }
  1077. if(skel_filename.length() > 0)
  1078. {
  1079. readTGF(skel_filename,s.C,s.BE);
  1080. }
  1081. init_relative();
  1082. ei.init(V.cast<float>(),F.cast<int>());
  1083. // Init glut
  1084. glutInit(&argc,argv);
  1085. if( !TwInit(TW_OPENGL, NULL) )
  1086. {
  1087. // A fatal error occured
  1088. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  1089. return 1;
  1090. }
  1091. // Create a tweak bar
  1092. rebar.TwNewBar("TweakBar");
  1093. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,
  1094. camera.m_rotation_conj.coeffs().data(), "open readonly=true");
  1095. TwType RotationTypeTW = igl::anttweakbar::ReTwDefineEnumFromString("RotationType",
  1096. "igl_trackball,two-a...-fixed-up");
  1097. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  1098. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  1099. rebar.TwAddVarRW("skeleton_on_top", TW_TYPE_BOOLCPP,&skeleton_on_top,"key=O");
  1100. rebar.TwAddVarRW("wireframe", TW_TYPE_BOOLCPP,&wireframe,"key=l");
  1101. TwType SkelStyleTypeTW = igl::anttweakbar::ReTwDefineEnumFromString("SkelStyleType",
  1102. "3d,vector-graphics");
  1103. rebar.TwAddVarRW("style",SkelStyleTypeTW,&skel_style,"");
  1104. rebar.TwAddVarRW("alpha",TW_TYPE_DOUBLE,&alpha,
  1105. "keyIncr=} keyDecr={ min=0 max=1 step=0.1");
  1106. rebar.load(REBAR_NAME);
  1107. // Init antweakbar
  1108. glutInitDisplayString( "rgba depth double samples>=8 ");
  1109. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  1110. glutCreateWindow("skeleton-builder");
  1111. glutDisplayFunc(display);
  1112. glutReshapeFunc(reshape);
  1113. glutKeyboardFunc(key);
  1114. glutMouseFunc(mouse);
  1115. glutMotionFunc(mouse_drag);
  1116. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  1117. glutMainLoop();
  1118. return 0;
  1119. }