example.cpp 19 KB

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