example.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. #include <igl/C_STR.h>
  2. #include <igl/Camera.h>
  3. #include <igl/REDRUM.h>
  4. #include <igl/bfs_orient.h>
  5. #include <igl/components.h>
  6. #include <igl/opengl2/draw_floor.h>
  7. #include <igl/opengl2/draw_mesh.h>
  8. #include <igl/get_seconds.h>
  9. #include <igl/jet.h>
  10. #include <igl/list_to_matrix.h>
  11. #include <igl/material_colors.h>
  12. #include <igl/normalize_row_lengths.h>
  13. #include <igl/orient_outward.h>
  14. #include <igl/pathinfo.h>
  15. #include <igl/per_face_normals.h>
  16. #include <igl/polygon_mesh_to_triangle_mesh.h>
  17. #include <igl/quat_to_mat.h>
  18. #include <igl/randperm.h>
  19. #include <igl/readMESH.h>
  20. #include <igl/readOBJ.h>
  21. #include <igl/readOFF.h>
  22. #include <igl/readPLY.h>
  23. #include <igl/readWRL.h>
  24. #include <igl/opengl/report_gl_error.h>
  25. #include <igl/snap_to_canonical_view_quat.h>
  26. #include <igl/snap_to_fixed_up.h>
  27. #include <igl/trackball.h>
  28. #include <igl/two_axis_valuator_fixed_up.h>
  29. #include <igl/unique_simplices.h>
  30. #include <igl/writeOBJ.h>
  31. #include <igl/writeOFF.h>
  32. #include <igl/write_triangle_mesh.h>
  33. #include <igl/anttweakbar/ReAntTweakBar.h>
  34. #include <igl/embree/reorient_facets_raycast.h>
  35. #include <Eigen/Core>
  36. #include <Eigen/Geometry>
  37. #ifdef __APPLE__
  38. #include <GLUT/glut.h>
  39. #else
  40. #include <GL/glut.h>
  41. #endif
  42. #ifndef GLUT_WHEEL_UP
  43. #define GLUT_WHEEL_UP 3
  44. #endif
  45. #ifndef GLUT_WHEEL_DOWN
  46. #define GLUT_WHEEL_DOWN 4
  47. #endif
  48. #ifndef GLUT_WHEEL_RIGHT
  49. #define GLUT_WHEEL_RIGHT 5
  50. #endif
  51. #ifndef GLUT_WHEEL_LEFT
  52. #define GLUT_WHEEL_LEFT 6
  53. #endif
  54. #ifndef GLUT_ACTIVE_COMMAND
  55. #define GLUT_ACTIVE_COMMAND 8
  56. #endif
  57. #include <ctime>
  58. #include <string>
  59. #include <vector>
  60. #include <stack>
  61. #include <iostream>
  62. int cc_selected = -1;
  63. Eigen::MatrixXd V;
  64. Eigen::VectorXd Vmid,Vmin,Vmax;
  65. double bbd = 1.0;
  66. Eigen::MatrixXi F;
  67. Eigen::VectorXi CC;
  68. struct State
  69. {
  70. igl::Camera camera;
  71. Eigen::MatrixXd N;
  72. Eigen::MatrixXd C;
  73. } s;
  74. std::string out_filename;
  75. // See README for descriptions
  76. enum RotationType
  77. {
  78. ROTATION_TYPE_IGL_TRACKBALL = 0,
  79. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  80. NUM_ROTATION_TYPES = 2,
  81. } rotation_type;
  82. enum CenterType
  83. {
  84. CENTER_TYPE_ORBIT = 0,
  85. CENTER_TYPE_FPS = 1,
  86. NUM_CENTER_TYPES = 2,
  87. } center_type = CENTER_TYPE_ORBIT;
  88. enum OrientMethod
  89. {
  90. ORIENT_METHOD_OUTWARD = 0,
  91. ORIENT_METHOD_AO = 1,
  92. NUM_ORIENT_METHODS = 2,
  93. } orient_method = ORIENT_METHOD_AO;
  94. std::stack<State> undo_stack;
  95. std::stack<State> redo_stack;
  96. bool wireframe_visible = false;
  97. bool fill_visible = true;
  98. bool is_rotating = false;
  99. int down_x,down_y;
  100. igl::Camera down_camera;
  101. bool is_animating = false;
  102. double animation_start_time = 0;
  103. double ANIMATION_DURATION = 0.5;
  104. Eigen::Quaterniond animation_from_quat;
  105. Eigen::Quaterniond animation_to_quat;
  106. int width,height;
  107. Eigen::Vector4f light_pos(-0.1,-0.1,0.9,0);
  108. #define REBAR_NAME "temp.rbr"
  109. igl::anttweakbar::ReTwBar rebar;
  110. // Forward
  111. void init_patches();
  112. void init_relative();
  113. void push_undo()
  114. {
  115. undo_stack.push(s);
  116. // Clear
  117. redo_stack = std::stack<State>();
  118. }
  119. void TW_CALL set_orient_method(const void * value, void * clientData)
  120. {
  121. const OrientMethod old_orient_method = orient_method;
  122. orient_method = *(const OrientMethod *)value;
  123. if(orient_method != old_orient_method)
  124. {
  125. init_patches();
  126. init_relative();
  127. }
  128. }
  129. void TW_CALL get_orient_method(void * value, void *clientData)
  130. {
  131. OrientMethod * om = (OrientMethod *)(value);
  132. *om = orient_method;
  133. }
  134. void TW_CALL set_rotation_type(const void * value, void * clientData)
  135. {
  136. using namespace Eigen;
  137. using namespace std;
  138. using namespace igl;
  139. const RotationType old_rotation_type = rotation_type;
  140. rotation_type = *(const RotationType *)(value);
  141. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  142. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  143. {
  144. push_undo();
  145. animation_from_quat = s.camera.m_rotation_conj;
  146. snap_to_fixed_up(animation_from_quat,animation_to_quat);
  147. // start animation
  148. animation_start_time = get_seconds();
  149. is_animating = true;
  150. }
  151. }
  152. void TW_CALL get_rotation_type(void * value, void *clientData)
  153. {
  154. RotationType * rt = (RotationType *)(value);
  155. *rt = rotation_type;
  156. }
  157. void reshape(int width, int height)
  158. {
  159. ::width = width;
  160. ::height = height;
  161. glViewport(0,0,width,height);
  162. // Send the new window size to AntTweakBar
  163. TwWindowSize(width, height);
  164. s.camera.m_aspect = (double)width/(double)height;
  165. }
  166. void push_scene()
  167. {
  168. using namespace igl;
  169. using namespace std;
  170. glMatrixMode(GL_PROJECTION);
  171. glPushMatrix();
  172. glLoadIdentity();
  173. auto & camera = s.camera;
  174. glMultMatrixd(camera.projection().data());
  175. glMatrixMode(GL_MODELVIEW);
  176. glPushMatrix();
  177. glLoadIdentity();
  178. gluLookAt(
  179. camera.eye()(0), camera.eye()(1), camera.eye()(2),
  180. camera.at()(0), camera.at()(1), camera.at()(2),
  181. camera.up()(0), camera.up()(1), camera.up()(2));
  182. }
  183. void push_object()
  184. {
  185. using namespace igl;
  186. glPushMatrix();
  187. glScaled(2./bbd,2./bbd,2./bbd);
  188. glTranslated(-Vmid(0),-Vmid(1),-Vmid(2));
  189. }
  190. void pop_object()
  191. {
  192. glPopMatrix();
  193. }
  194. void pop_scene()
  195. {
  196. glMatrixMode(GL_PROJECTION);
  197. glPopMatrix();
  198. glMatrixMode(GL_MODELVIEW);
  199. glPopMatrix();
  200. }
  201. // Set up double-sided lights
  202. void lights()
  203. {
  204. using namespace std;
  205. using namespace Eigen;
  206. glEnable(GL_LIGHTING);
  207. //glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  208. glEnable(GL_LIGHT0);
  209. float WHITE[4] = {1,1,1,1.};
  210. float GREY[4] = {0.4,0.4,0.4,1.};
  211. float BLACK[4] = {0.,0.,0.,1.};
  212. float NEAR_BLACK[4] = {0.1,0.1,0.1,1.};
  213. Vector4f pos = light_pos;
  214. glLightfv(GL_LIGHT0,GL_AMBIENT,BLACK);
  215. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  216. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  217. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  218. //glEnable(GL_LIGHT1);
  219. //pos(0) *= -1;
  220. //pos(1) *= -1;
  221. //pos(2) *= -1;
  222. //glLightfv(GL_LIGHT1,GL_AMBIENT,BLACK);
  223. //glLightfv(GL_LIGHT1,GL_DIFFUSE,NEAR_BLACK);
  224. //glLightfv(GL_LIGHT1,GL_SPECULAR,BLACK);
  225. //glLightfv(GL_LIGHT1,GL_POSITION,pos.data());
  226. }
  227. void display()
  228. {
  229. using namespace igl;
  230. using namespace std;
  231. using namespace Eigen;
  232. glClearColor(1,1,1,0);
  233. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  234. if(is_animating)
  235. {
  236. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  237. if(t > 1)
  238. {
  239. t = 1;
  240. is_animating = false;
  241. }
  242. Quaterniond q = animation_from_quat.slerp(t,animation_to_quat).normalized();
  243. auto & camera = s.camera;
  244. switch(center_type)
  245. {
  246. default:
  247. case CENTER_TYPE_ORBIT:
  248. camera.orbit(q.conjugate());
  249. break;
  250. case CENTER_TYPE_FPS:
  251. camera.turn_eye(q.conjugate());
  252. break;
  253. }
  254. }
  255. glEnable(GL_DEPTH_TEST);
  256. glEnable(GL_NORMALIZE);
  257. lights();
  258. push_scene();
  259. push_object();
  260. // Set material properties
  261. glEnable(GL_COLOR_MATERIAL);
  262. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  263. if(wireframe_visible)
  264. {
  265. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  266. if(fill_visible)
  267. {
  268. glColor3f(0,0,0);
  269. igl::opengl2::draw_mesh(V,F,s.N);
  270. }else
  271. {
  272. igl::opengl2::draw_mesh(V,F,s.N,s.C);
  273. }
  274. // visualize selected patch
  275. glLineWidth(10);
  276. glBegin(GL_TRIANGLES);
  277. glColor3d(0, 0, 0);
  278. // loop over faces
  279. for(int i = 0; i<F.rows();i++)
  280. {
  281. if (CC(i) != cc_selected) continue;
  282. // loop over corners of triangle
  283. for(int j = 0;j<3;j++)
  284. {
  285. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  286. }
  287. }
  288. glEnd();
  289. glLineWidth(1);
  290. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  291. glBegin(GL_TRIANGLES);
  292. glColor3d(1, 0, 0);
  293. // loop over faces
  294. for(int i = 0; i<F.rows();i++)
  295. {
  296. if (CC(i) != cc_selected) continue;
  297. // loop over corners of triangle
  298. glNormal3d(s.N(i,0),s.N(i,1),s.N(i,2));
  299. for(int j = 0;j<3;j++)
  300. {
  301. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  302. }
  303. }
  304. glEnd();
  305. }
  306. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  307. if(fill_visible)
  308. {
  309. glEnable(GL_POLYGON_OFFSET_FILL); // Avoid Stitching!
  310. glPolygonOffset(1.0, 0);
  311. igl::opengl2::draw_mesh(V,F,s.N,s.C);
  312. }
  313. pop_object();
  314. // Draw a nice floor
  315. glPushMatrix();
  316. const double floor_offset =
  317. -2./bbd*(V.col(1).maxCoeff()-Vmid(1));
  318. glTranslated(0,floor_offset,0);
  319. const float GREY[4] = {0.5,0.5,0.6,1.0};
  320. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  321. igl::opengl2::draw_floor(GREY,DARK_GREY);
  322. glPopMatrix();
  323. pop_scene();
  324. igl::opengl::report_gl_error();
  325. TwDraw();
  326. glutSwapBuffers();
  327. glutPostRedisplay();
  328. }
  329. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  330. {
  331. using namespace std;
  332. using namespace igl;
  333. using namespace Eigen;
  334. GLint viewport[4];
  335. glGetIntegerv(GL_VIEWPORT,viewport);
  336. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  337. {
  338. static double mouse_scroll_y = 0;
  339. const double delta_y = 0.125*direction;
  340. mouse_scroll_y += delta_y;
  341. TwMouseWheel(mouse_scroll_y);
  342. return;
  343. }
  344. push_undo();
  345. auto & camera = s.camera;
  346. switch(center_type)
  347. {
  348. case CENTER_TYPE_ORBIT:
  349. if(wheel==0)
  350. {
  351. // factor of zoom change
  352. double s = (1.-0.01*direction);
  353. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  354. //camera.m_angle *= s;
  355. //camera.m_angle = min(max(camera.m_angle,1),89);
  356. camera.push_away(s);
  357. }else
  358. {
  359. // Dolly zoom:
  360. camera.dolly_zoom((double)direction*1.0);
  361. }
  362. break;
  363. default:
  364. case CENTER_TYPE_FPS:
  365. // Move `eye` and `at`
  366. camera.dolly((wheel==0?Vector3d(0,0,1):Vector3d(-1,0,0))*0.1*direction);
  367. break;
  368. }
  369. }
  370. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  371. {
  372. using namespace std;
  373. using namespace Eigen;
  374. using namespace igl;
  375. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  376. switch(glutButton)
  377. {
  378. case GLUT_RIGHT_BUTTON:
  379. case GLUT_LEFT_BUTTON:
  380. {
  381. switch(glutState)
  382. {
  383. case 1:
  384. // up
  385. glutSetCursor(GLUT_CURSOR_INHERIT);
  386. is_rotating = false;
  387. break;
  388. case 0:
  389. if(!tw_using)
  390. {
  391. push_undo();
  392. glutSetCursor(GLUT_CURSOR_CYCLE);
  393. // collect information for trackball
  394. is_rotating = true;
  395. down_camera = s.camera;
  396. down_x = mouse_x;
  397. down_y = mouse_y;
  398. }
  399. break;
  400. }
  401. break;
  402. // Scroll down
  403. case GLUT_WHEEL_DOWN:
  404. {
  405. mouse_wheel(0,-1,mouse_x,mouse_y);
  406. break;
  407. }
  408. // Scroll up
  409. case GLUT_WHEEL_UP:
  410. {
  411. mouse_wheel(0,1,mouse_x,mouse_y);
  412. break;
  413. }
  414. // Scroll left
  415. case GLUT_WHEEL_LEFT:
  416. {
  417. mouse_wheel(1,-1,mouse_x,mouse_y);
  418. break;
  419. }
  420. // Scroll right
  421. case GLUT_WHEEL_RIGHT:
  422. {
  423. mouse_wheel(1,1,mouse_x,mouse_y);
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. void mouse_drag(int mouse_x, int mouse_y)
  430. {
  431. using namespace igl;
  432. using namespace std;
  433. using namespace Eigen;
  434. bool tw_using = TwMouseMotion(mouse_x,mouse_y);
  435. if(is_rotating)
  436. {
  437. glutSetCursor(GLUT_CURSOR_CYCLE);
  438. Quaterniond q;
  439. auto & camera = s.camera;
  440. switch(rotation_type)
  441. {
  442. case ROTATION_TYPE_IGL_TRACKBALL:
  443. {
  444. // Rotate according to trackball
  445. igl::trackball<double>(
  446. width,
  447. height,
  448. 2.0,
  449. down_camera.m_rotation_conj.coeffs().data(),
  450. down_x,
  451. down_y,
  452. mouse_x,
  453. mouse_y,
  454. q.coeffs().data());
  455. break;
  456. }
  457. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  458. {
  459. // Rotate according to two axis valuator with fixed up vector
  460. two_axis_valuator_fixed_up(
  461. width, height,
  462. 2.0,
  463. down_camera.m_rotation_conj,
  464. down_x, down_y, mouse_x, mouse_y,
  465. q);
  466. break;
  467. }
  468. default:
  469. break;
  470. }
  471. switch(center_type)
  472. {
  473. default:
  474. case CENTER_TYPE_ORBIT:
  475. camera.orbit(q.conjugate());
  476. break;
  477. case CENTER_TYPE_FPS:
  478. camera.turn_eye(q.conjugate());
  479. break;
  480. }
  481. }
  482. }
  483. void init_relative()
  484. {
  485. using namespace Eigen;
  486. using namespace igl;
  487. per_face_normals(V,F,s.N);
  488. normalize_row_lengths(s.N,s.N);
  489. Vmax = V.colwise().maxCoeff();
  490. Vmin = V.colwise().minCoeff();
  491. Vmid = 0.5*(Vmax + Vmin);
  492. bbd = (Vmax-Vmin).norm();
  493. }
  494. void randomly_color(
  495. const Eigen::VectorXi & CC,
  496. Eigen::MatrixXd & C)
  497. {
  498. using namespace Eigen;
  499. using namespace igl;
  500. using namespace std;
  501. VectorXi I;
  502. srand ( unsigned ( time(0) ) );
  503. double num_cc = (double)CC.maxCoeff()+1.0;
  504. randperm(num_cc,I);
  505. C.resize(CC.rows(),3);
  506. for(int f = 0;f<CC.rows();f++)
  507. {
  508. jet(
  509. (double)I(CC(f))/num_cc,
  510. C(f,0),
  511. C(f,1),
  512. C(f,2));
  513. }
  514. }
  515. void TW_CALL randomize_colors(void * /*clientData*/)
  516. {
  517. push_undo();
  518. randomly_color(CC,s.C);
  519. }
  520. void init_patches()
  521. {
  522. using namespace Eigen;
  523. using namespace igl;
  524. using namespace std;
  525. {
  526. VectorXi VCC;
  527. components(F,VCC);
  528. cout<<"There are "<<VCC.maxCoeff()+1<<" connected components of vertices."<<endl;
  529. }
  530. bfs_orient(F,F,CC);
  531. VectorXi I;
  532. switch(orient_method)
  533. {
  534. case ORIENT_METHOD_AO:
  535. {
  536. cout<<"orient_outward_ao()"<<endl;
  537. igl::embree::reorient_facets_raycast(V,F,F,I);
  538. break;
  539. }
  540. case ORIENT_METHOD_OUTWARD:
  541. default:
  542. cout<<"orient_outward()"<<endl;
  543. orient_outward(V,F,CC,F,I);
  544. break;
  545. }
  546. double num_cc = (double)CC.maxCoeff()+1.0;
  547. cout<<"There are "<<num_cc<<" 'manifold/orientable' patches of faces."<<endl;
  548. }
  549. void undo()
  550. {
  551. using namespace std;
  552. if(!undo_stack.empty())
  553. {
  554. redo_stack.push(s);
  555. s = undo_stack.top();
  556. undo_stack.pop();
  557. }
  558. }
  559. void redo()
  560. {
  561. using namespace std;
  562. if(!redo_stack.empty())
  563. {
  564. undo_stack.push(s);
  565. s = redo_stack.top();
  566. redo_stack.pop();
  567. }
  568. }
  569. bool save(const std::string & out_filename)
  570. {
  571. using namespace std;
  572. using namespace igl;
  573. if(write_triangle_mesh(out_filename,V,F))
  574. {
  575. cout<<GREENGIN("Saved mesh to `"<<out_filename<<"` successfully.")<<endl;
  576. return true;
  577. }else
  578. {
  579. cout<<REDRUM("Failed to save mesh to `"<<out_filename<<"`.")<<endl;
  580. return false;
  581. }
  582. }
  583. void TW_CALL saveCB(void * /*clientData*/)
  584. {
  585. save(out_filename);
  586. }
  587. void key(unsigned char key, int mouse_x, int mouse_y)
  588. {
  589. using namespace std;
  590. using namespace Eigen;
  591. using namespace igl;
  592. int mod = glutGetModifiers();
  593. switch(key)
  594. {
  595. // ESC
  596. case char(27):
  597. rebar.save(REBAR_NAME);
  598. // ^C
  599. case char(3):
  600. exit(0);
  601. case 'I':
  602. case 'i':
  603. {
  604. push_undo();
  605. s.N *= -1.0;
  606. F = F.rowwise().reverse().eval();
  607. break;
  608. }
  609. case 'z':
  610. case 'Z':
  611. if(mod & GLUT_ACTIVE_COMMAND)
  612. {
  613. if(mod & GLUT_ACTIVE_SHIFT)
  614. {
  615. redo();
  616. }else
  617. {
  618. undo();
  619. }
  620. }else
  621. {
  622. push_undo();
  623. Quaterniond q;
  624. snap_to_canonical_view_quat(s.camera.m_rotation_conj,1.0,q);
  625. switch(center_type)
  626. {
  627. default:
  628. case CENTER_TYPE_ORBIT:
  629. s.camera.orbit(q.conjugate());
  630. break;
  631. case CENTER_TYPE_FPS:
  632. s.camera.turn_eye(q.conjugate());
  633. break;
  634. }
  635. }
  636. break;
  637. case 'u':
  638. mouse_wheel(0, 1,mouse_x,mouse_y);
  639. break;
  640. case 'j':
  641. mouse_wheel(0,-1,mouse_x,mouse_y);
  642. break;
  643. case 'n':
  644. cc_selected = (cc_selected + 1) % (CC.maxCoeff() + 2);
  645. cout << "selected cc: " << cc_selected << endl;
  646. glutPostRedisplay();
  647. break;
  648. default:
  649. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  650. {
  651. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  652. }
  653. }
  654. }
  655. int main(int argc, char * argv[])
  656. {
  657. using namespace std;
  658. using namespace Eigen;
  659. using namespace igl;
  660. string filename = "../shared/truck.obj";
  661. switch(argc)
  662. {
  663. case 3:
  664. out_filename = argv[2];
  665. case 2:
  666. // Read and prepare mesh
  667. filename = argv[1];
  668. break;
  669. default:
  670. cerr<<"Usage:"<<endl<<" ./example input.obj (output.obj)"<<endl;
  671. cout<<endl<<"Opening default mesh..."<<endl;
  672. break;
  673. }
  674. // print key commands
  675. cout<<"[Click] and [drag] Rotate model using trackball."<<endl;
  676. cout<<"[Z,z] Snap rotation to canonical view."<<endl;
  677. cout<<"[Command+Z] Undo."<<endl;
  678. cout<<"[Shift+Command+Z] Redo."<<endl;
  679. cout<<"[^C,ESC] Exit."<<endl;
  680. // dirname, basename, extension and filename
  681. string d,b,ext,f;
  682. pathinfo(filename,d,b,ext,f);
  683. // Convert extension to lower case
  684. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  685. vector<vector<double > > vV,vN,vTC;
  686. vector<vector<int > > vF,vFTC,vFN;
  687. if(ext == "obj")
  688. {
  689. // Convert extension to lower case
  690. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  691. {
  692. return 1;
  693. }
  694. }else if(ext == "off")
  695. {
  696. // Convert extension to lower case
  697. if(!igl::readOFF(filename,vV,vF,vN))
  698. {
  699. return 1;
  700. }
  701. }else if(ext == "ply")
  702. {
  703. // Convert extension to lower case
  704. if(!igl::readPLY(filename,vV,vF,vN,vTC))
  705. {
  706. return 1;
  707. }
  708. }else if(ext == "wrl")
  709. {
  710. // Convert extension to lower case
  711. if(!igl::readWRL(filename,vV,vF))
  712. {
  713. return 1;
  714. }
  715. //}else
  716. //{
  717. // // Convert extension to lower case
  718. // MatrixXi T;
  719. // if(!igl::readMESH(filename,V,T,F))
  720. // {
  721. // return 1;
  722. // }
  723. // //if(F.size() > T.size() || F.size() == 0)
  724. // {
  725. // boundary_facets(T,F);
  726. // }
  727. }
  728. if(vV.size() > 0)
  729. {
  730. if(!list_to_matrix(vV,V))
  731. {
  732. return 1;
  733. }
  734. polygon_mesh_to_triangle_mesh(vF,F);
  735. }
  736. MatrixXi F_unique;
  737. unique_simplices(F, F_unique);
  738. F = F_unique;
  739. init_patches();
  740. init_relative();
  741. randomly_color(CC,s.C);
  742. // Init glut
  743. glutInit(&argc,argv);
  744. if( !TwInit(TW_OPENGL, NULL) )
  745. {
  746. // A fatal error occured
  747. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  748. return 1;
  749. }
  750. // Create a tweak bar
  751. rebar.TwNewBar("bar");
  752. TwDefine("bar label='Patches' size='200 550' text=light alpha='200' color='68 68 68'");
  753. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,
  754. s.camera.m_rotation_conj.coeffs().data(), "open readonly=true");
  755. TwType RotationTypeTW = igl::anttweakbar::ReTwDefineEnumFromString("RotationType",
  756. "igl_trackball,two-axis-valuator-fixed-up");
  757. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  758. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  759. TwType CenterTypeTW = igl::anttweakbar::ReTwDefineEnumFromString("CenterType","orbit,fps");
  760. rebar.TwAddVarRW("center_type", CenterTypeTW,&center_type,
  761. "keyIncr={ keyDecr=}");
  762. TwType OrientMethodTW = igl::anttweakbar::ReTwDefineEnumFromString("OrientMethod",
  763. "outward,ambient-occlusion");
  764. rebar.TwAddVarCB( "orient_method", OrientMethodTW,
  765. set_orient_method,get_orient_method,NULL,"keyIncr=< keyDecr=>");
  766. rebar.TwAddVarRW("wireframe_visible",TW_TYPE_BOOLCPP,&wireframe_visible,"key=l");
  767. rebar.TwAddVarRW("fill_visible",TW_TYPE_BOOLCPP,&fill_visible,"key=f");
  768. rebar.TwAddButton("randomize_colors",randomize_colors,NULL,"key=c");
  769. if(out_filename != "")
  770. {
  771. rebar.TwAddButton("save",
  772. saveCB,NULL,
  773. C_STR("label='Save to `"<<out_filename<<"`' "<<
  774. "key=s"));
  775. }
  776. rebar.load(REBAR_NAME);
  777. animation_from_quat = Quaterniond(1,0,0,0);
  778. s.camera.m_rotation_conj = animation_from_quat;
  779. animation_start_time = get_seconds();
  780. // Init antweakbar
  781. #ifdef __APPLE__
  782. glutInitDisplayString( "rgba depth double samples>=8");
  783. #else
  784. glutInitDisplayString( "rgba depth double "); // samples>=8 somehow not supported on Kenshi's machines...?
  785. #endif
  786. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  787. glutCreateWindow("patches");
  788. glutDisplayFunc(display);
  789. glutReshapeFunc(reshape);
  790. glutKeyboardFunc(key);
  791. glutMouseFunc(mouse);
  792. glutMotionFunc(mouse_drag);
  793. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  794. glutMainLoop();
  795. return 0;
  796. }