example.cpp 20 KB

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