ViewerData.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "ViewerData.h"
  9. #include "ViewerCore.h"
  10. #include "../per_face_normals.h"
  11. #include "../material_colors.h"
  12. #include "../per_vertex_normals.h"
  13. // Really? Just for GL_NEAREST?
  14. #include "gl.h"
  15. #include <iostream>
  16. IGL_INLINE igl::opengl::ViewerData::ViewerData()
  17. : dirty(MeshGL::DIRTY_ALL),
  18. show_faces (~unsigned(0)),
  19. show_lines (~unsigned(0)),
  20. face_based (false),
  21. double_sided (false),
  22. invert_normals (false),
  23. show_overlay (~unsigned(0)),
  24. show_overlay_depth(~unsigned(0)),
  25. show_vertex_labels(0),
  26. show_face_labels (0),
  27. show_custom_labels(0),
  28. show_texture (false),
  29. use_matcap (false),
  30. point_size(30),
  31. line_width(0.5f),
  32. line_color(0,0,0,1),
  33. label_color(0,0,0.04,1),
  34. shininess(35.0f),
  35. id(-1),
  36. is_visible (~unsigned(0))
  37. {
  38. clear();
  39. };
  40. IGL_INLINE void igl::opengl::ViewerData::set_face_based(bool newvalue)
  41. {
  42. if (face_based != newvalue)
  43. {
  44. face_based = newvalue;
  45. dirty = MeshGL::DIRTY_ALL;
  46. }
  47. }
  48. // Helpers that draws the most common meshes
  49. IGL_INLINE void igl::opengl::ViewerData::set_mesh(
  50. const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F)
  51. {
  52. using namespace std;
  53. Eigen::MatrixXd V_temp;
  54. // If V only has two columns, pad with a column of zeros
  55. if (_V.cols() == 2)
  56. {
  57. V_temp = Eigen::MatrixXd::Zero(_V.rows(),3);
  58. V_temp.block(0,0,_V.rows(),2) = _V;
  59. }
  60. else
  61. V_temp = _V;
  62. if (V.rows() == 0 && F.rows() == 0)
  63. {
  64. V = V_temp;
  65. F = _F;
  66. compute_normals();
  67. uniform_colors(
  68. Eigen::Vector3d(GOLD_AMBIENT[0], GOLD_AMBIENT[1], GOLD_AMBIENT[2]),
  69. Eigen::Vector3d(GOLD_DIFFUSE[0], GOLD_DIFFUSE[1], GOLD_DIFFUSE[2]),
  70. Eigen::Vector3d(GOLD_SPECULAR[0], GOLD_SPECULAR[1], GOLD_SPECULAR[2]));
  71. // Generates a checkerboard texture
  72. grid_texture();
  73. }
  74. else
  75. {
  76. if (_V.rows() == V.rows() && _F.rows() == F.rows())
  77. {
  78. V = V_temp;
  79. F = _F;
  80. }
  81. else
  82. cerr << "ERROR (set_mesh): The new mesh has a different number of vertices/faces. Please clear the mesh before plotting."<<endl;
  83. }
  84. dirty |= MeshGL::DIRTY_FACE | MeshGL::DIRTY_POSITION;
  85. }
  86. IGL_INLINE void igl::opengl::ViewerData::set_vertices(const Eigen::MatrixXd& _V)
  87. {
  88. V = _V;
  89. assert(F.size() == 0 || F.maxCoeff() < V.rows());
  90. dirty |= MeshGL::DIRTY_POSITION;
  91. }
  92. IGL_INLINE void igl::opengl::ViewerData::set_normals(const Eigen::MatrixXd& N)
  93. {
  94. using namespace std;
  95. if (N.rows() == V.rows())
  96. {
  97. set_face_based(false);
  98. V_normals = N;
  99. }
  100. else if (N.rows() == F.rows() || N.rows() == F.rows()*3)
  101. {
  102. set_face_based(true);
  103. F_normals = N;
  104. }
  105. else
  106. cerr << "ERROR (set_normals): Please provide a normal per face, per corner or per vertex."<<endl;
  107. dirty |= MeshGL::DIRTY_NORMAL;
  108. }
  109. IGL_INLINE void igl::opengl::ViewerData::set_visible(bool value, unsigned int core_id /*= 1*/)
  110. {
  111. if (value)
  112. is_visible |= core_id;
  113. else
  114. is_visible &= ~core_id;
  115. }
  116. IGL_INLINE void igl::opengl::ViewerData::copy_options(const ViewerCore &from, const ViewerCore &to)
  117. {
  118. to.set(show_overlay , from.is_set(show_overlay) );
  119. to.set(show_overlay_depth, from.is_set(show_overlay_depth));
  120. to.set(show_texture , from.is_set(show_texture) );
  121. to.set(use_matcap , from.is_set(use_matcap) );
  122. to.set(show_faces , from.is_set(show_faces) );
  123. to.set(show_lines , from.is_set(show_lines) );
  124. }
  125. IGL_INLINE void igl::opengl::ViewerData::set_colors(const Eigen::MatrixXd &C)
  126. {
  127. using namespace std;
  128. using namespace Eigen;
  129. // This Gouraud coloring should be deprecated in favor of Phong coloring in
  130. // set-data
  131. if(C.rows()>0 && C.cols() == 1)
  132. {
  133. assert(false && "deprecated: call set_data directly instead");
  134. return set_data(C);
  135. }
  136. // Ambient color should be darker color
  137. const auto ambient = [](const MatrixXd & C)->MatrixXd
  138. {
  139. MatrixXd T = 0.1*C;
  140. T.col(3) = C.col(3);
  141. return T;
  142. };
  143. // Specular color should be a less saturated and darker color: dampened
  144. // highlights
  145. const auto specular = [](const MatrixXd & C)->MatrixXd
  146. {
  147. const double grey = 0.3;
  148. MatrixXd T = grey+0.1*(C.array()-grey);
  149. T.col(3) = C.col(3);
  150. return T;
  151. };
  152. if (C.rows() == 1)
  153. {
  154. for (unsigned i=0;i<V_material_diffuse.rows();++i)
  155. {
  156. if (C.cols() == 3)
  157. V_material_diffuse.row(i) << C.row(0),1;
  158. else if (C.cols() == 4)
  159. V_material_diffuse.row(i) << C.row(0);
  160. }
  161. V_material_ambient = ambient(V_material_diffuse);
  162. V_material_specular = specular(V_material_diffuse);
  163. for (unsigned i=0;i<F_material_diffuse.rows();++i)
  164. {
  165. if (C.cols() == 3)
  166. F_material_diffuse.row(i) << C.row(0),1;
  167. else if (C.cols() == 4)
  168. F_material_diffuse.row(i) << C.row(0);
  169. }
  170. F_material_ambient = ambient(F_material_diffuse);
  171. F_material_specular = specular(F_material_diffuse);
  172. }
  173. else if(C.rows() == V.rows() || C.rows() == F.rows())
  174. {
  175. // face based colors?
  176. if((C.rows()==F.rows()) && (C.rows() != V.rows() || face_based))
  177. {
  178. set_face_based(true);
  179. for (unsigned i=0;i<F_material_diffuse.rows();++i)
  180. {
  181. if (C.cols() == 3)
  182. F_material_diffuse.row(i) << C.row(i), 1;
  183. else if (C.cols() == 4)
  184. F_material_diffuse.row(i) << C.row(i);
  185. }
  186. F_material_ambient = ambient(F_material_diffuse);
  187. F_material_specular = specular(F_material_diffuse);
  188. }
  189. else/*(C.rows() == V.rows())*/
  190. {
  191. set_face_based(false);
  192. for (unsigned i=0;i<V_material_diffuse.rows();++i)
  193. {
  194. if (C.cols() == 3)
  195. V_material_diffuse.row(i) << C.row(i), 1;
  196. else if (C.cols() == 4)
  197. V_material_diffuse.row(i) << C.row(i);
  198. }
  199. V_material_ambient = ambient(V_material_diffuse);
  200. V_material_specular = specular(V_material_diffuse);
  201. }
  202. }
  203. else
  204. cerr << "ERROR (set_colors): Please provide a single color, or a color per face or per vertex."<<endl;
  205. dirty |= MeshGL::DIRTY_DIFFUSE | MeshGL::DIRTY_SPECULAR | MeshGL::DIRTY_AMBIENT;
  206. }
  207. IGL_INLINE void igl::opengl::ViewerData::set_uv(const Eigen::MatrixXd& UV)
  208. {
  209. using namespace std;
  210. if (UV.rows() == V.rows())
  211. {
  212. set_face_based(false);
  213. V_uv = UV;
  214. }
  215. else
  216. cerr << "ERROR (set_UV): Please provide uv per vertex."<<endl;;
  217. dirty |= MeshGL::DIRTY_UV;
  218. }
  219. IGL_INLINE void igl::opengl::ViewerData::set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F)
  220. {
  221. set_face_based(true);
  222. V_uv = UV_V.block(0,0,UV_V.rows(),2);
  223. F_uv = UV_F;
  224. dirty |= MeshGL::DIRTY_UV;
  225. }
  226. IGL_INLINE void igl::opengl::ViewerData::set_texture(
  227. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  228. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  229. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B)
  230. {
  231. texture_R = R;
  232. texture_G = G;
  233. texture_B = B;
  234. texture_A = Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>::Constant(R.rows(),R.cols(),255);
  235. dirty |= MeshGL::DIRTY_TEXTURE;
  236. }
  237. IGL_INLINE void igl::opengl::ViewerData::set_texture(
  238. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  239. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  240. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  241. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A)
  242. {
  243. texture_R = R;
  244. texture_G = G;
  245. texture_B = B;
  246. texture_A = A;
  247. dirty |= MeshGL::DIRTY_TEXTURE;
  248. }
  249. IGL_INLINE void igl::opengl::ViewerData::set_data(
  250. const Eigen::VectorXd & D,
  251. double caxis_min,
  252. double caxis_max,
  253. igl::ColorMapType cmap,
  254. int num_steps)
  255. {
  256. if(!show_texture)
  257. {
  258. Eigen::MatrixXd CM;
  259. igl::colormap(cmap,Eigen::VectorXd::LinSpaced(num_steps,0,1).eval(),0,1,CM);
  260. set_colormap(CM);
  261. }
  262. set_uv(((D.array()-caxis_min)/(caxis_max-caxis_min)).replicate(1,2));
  263. }
  264. IGL_INLINE void igl::opengl::ViewerData::set_data(const Eigen::VectorXd & D, igl::ColorMapType cmap, int num_steps)
  265. {
  266. const double caxis_min = D.minCoeff();
  267. const double caxis_max = D.maxCoeff();
  268. return set_data(D,caxis_min,caxis_max,cmap,num_steps);
  269. }
  270. IGL_INLINE void igl::opengl::ViewerData::set_colormap(const Eigen::MatrixXd & CM)
  271. {
  272. assert(CM.cols() == 3 && "colormap CM should have 3 columns");
  273. // Convert to R,G,B textures
  274. const Eigen::Matrix<unsigned char,Eigen::Dynamic, Eigen::Dynamic> R =
  275. (CM.col(0)*255.0).cast<unsigned char>();
  276. const Eigen::Matrix<unsigned char,Eigen::Dynamic, Eigen::Dynamic> G =
  277. (CM.col(1)*255.0).cast<unsigned char>();
  278. const Eigen::Matrix<unsigned char,Eigen::Dynamic, Eigen::Dynamic> B =
  279. (CM.col(2)*255.0).cast<unsigned char>();
  280. set_colors(Eigen::RowVector3d(1,1,1));
  281. set_texture(R,G,B);
  282. show_texture = ~unsigned(0);
  283. meshgl.tex_filter = GL_NEAREST;
  284. meshgl.tex_wrap = GL_CLAMP_TO_EDGE;
  285. }
  286. IGL_INLINE void igl::opengl::ViewerData::set_points(
  287. const Eigen::MatrixXd& P,
  288. const Eigen::MatrixXd& C)
  289. {
  290. // clear existing points
  291. points.resize(0,0);
  292. add_points(P,C);
  293. }
  294. IGL_INLINE void igl::opengl::ViewerData::add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C)
  295. {
  296. Eigen::MatrixXd P_temp;
  297. // If P only has two columns, pad with a column of zeros
  298. if (P.cols() == 2)
  299. {
  300. P_temp = Eigen::MatrixXd::Zero(P.rows(),3);
  301. P_temp.block(0,0,P.rows(),2) = P;
  302. }
  303. else
  304. P_temp = P;
  305. int lastid = points.rows();
  306. points.conservativeResize(points.rows() + P_temp.rows(),6);
  307. for (unsigned i=0; i<P_temp.rows(); ++i)
  308. points.row(lastid+i) << P_temp.row(i), i<C.rows() ? C.row(i) : C.row(C.rows()-1);
  309. dirty |= MeshGL::DIRTY_OVERLAY_POINTS;
  310. }
  311. IGL_INLINE void igl::opengl::ViewerData::clear_points()
  312. {
  313. points.resize(0, 6);
  314. }
  315. IGL_INLINE void igl::opengl::ViewerData::set_edges(
  316. const Eigen::MatrixXd& P,
  317. const Eigen::MatrixXi& E,
  318. const Eigen::MatrixXd& C)
  319. {
  320. using namespace Eigen;
  321. lines.resize(E.rows(),9);
  322. assert(C.cols() == 3);
  323. for(int e = 0;e<E.rows();e++)
  324. {
  325. RowVector3d color;
  326. if(C.size() == 3)
  327. {
  328. color<<C;
  329. }else if(C.rows() == E.rows())
  330. {
  331. color<<C.row(e);
  332. }
  333. lines.row(e)<< P.row(E(e,0)), P.row(E(e,1)), color;
  334. }
  335. dirty |= MeshGL::DIRTY_OVERLAY_LINES;
  336. }
  337. IGL_INLINE void igl::opengl::ViewerData::set_edges_from_vector_field(
  338. const Eigen::MatrixXd& P,
  339. const Eigen::MatrixXd& V,
  340. const Eigen::MatrixXd& C)
  341. {
  342. assert(P.rows() == V.rows());
  343. Eigen::MatrixXi E(P.rows(),2);
  344. const Eigen::MatrixXd PV =
  345. (Eigen::MatrixXd(P.rows()+V.rows(),3)<<P,P+V).finished();
  346. for(int i = 0;i<P.rows();i++)
  347. {
  348. E(i,0) = i;
  349. E(i,1) = i+P.rows();
  350. }
  351. const Eigen::MatrixXd CC = C.replicate<2,1>();
  352. set_edges(PV,E, C.rows() == 1?C:C.replicate<2,1>());
  353. }
  354. IGL_INLINE void igl::opengl::ViewerData::add_edges(const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C)
  355. {
  356. Eigen::MatrixXd P1_temp,P2_temp;
  357. // If P1 only has two columns, pad with a column of zeros
  358. if (P1.cols() == 2)
  359. {
  360. P1_temp = Eigen::MatrixXd::Zero(P1.rows(),3);
  361. P1_temp.block(0,0,P1.rows(),2) = P1;
  362. P2_temp = Eigen::MatrixXd::Zero(P2.rows(),3);
  363. P2_temp.block(0,0,P2.rows(),2) = P2;
  364. }
  365. else
  366. {
  367. P1_temp = P1;
  368. P2_temp = P2;
  369. }
  370. int lastid = lines.rows();
  371. lines.conservativeResize(lines.rows() + P1_temp.rows(),9);
  372. for (unsigned i=0; i<P1_temp.rows(); ++i)
  373. lines.row(lastid+i) << P1_temp.row(i), P2_temp.row(i), i<C.rows() ? C.row(i) : C.row(C.rows()-1);
  374. dirty |= MeshGL::DIRTY_OVERLAY_LINES;
  375. }
  376. IGL_INLINE void igl::opengl::ViewerData::clear_edges()
  377. {
  378. lines.resize(0, 9);
  379. }
  380. IGL_INLINE void igl::opengl::ViewerData::add_label(const Eigen::VectorXd& P, const std::string& str)
  381. {
  382. Eigen::RowVectorXd P_temp;
  383. // If P only has two columns, pad with a column of zeros
  384. if (P.size() == 2)
  385. {
  386. P_temp = Eigen::RowVectorXd::Zero(3);
  387. P_temp << P.transpose(), 0;
  388. }
  389. else
  390. P_temp = P;
  391. int lastid = labels_positions.rows();
  392. labels_positions.conservativeResize(lastid+1, 3);
  393. labels_positions.row(lastid) = P_temp;
  394. labels_strings.push_back(str);
  395. dirty |= MeshGL::DIRTY_CUSTOM_LABELS;
  396. }
  397. IGL_INLINE void igl::opengl::ViewerData::set_labels(const Eigen::MatrixXd& P, const std::vector<std::string>& str)
  398. {
  399. assert(P.rows() == str.size() && "position # and label # do not match!");
  400. assert(P.cols() == 3 && "dimension of label positions incorrect!");
  401. labels_positions = P;
  402. labels_strings = str;
  403. }
  404. IGL_INLINE void igl::opengl::ViewerData::clear_labels()
  405. {
  406. labels_positions.resize(0,3);
  407. labels_strings.clear();
  408. }
  409. IGL_INLINE void igl::opengl::ViewerData::clear()
  410. {
  411. V = Eigen::MatrixXd (0,3);
  412. F = Eigen::MatrixXi (0,3);
  413. F_material_ambient = Eigen::MatrixXd (0,4);
  414. F_material_diffuse = Eigen::MatrixXd (0,4);
  415. F_material_specular = Eigen::MatrixXd (0,4);
  416. V_material_ambient = Eigen::MatrixXd (0,4);
  417. V_material_diffuse = Eigen::MatrixXd (0,4);
  418. V_material_specular = Eigen::MatrixXd (0,4);
  419. F_normals = Eigen::MatrixXd (0,3);
  420. V_normals = Eigen::MatrixXd (0,3);
  421. V_uv = Eigen::MatrixXd (0,2);
  422. F_uv = Eigen::MatrixXi (0,3);
  423. lines = Eigen::MatrixXd (0,9);
  424. points = Eigen::MatrixXd (0,6);
  425. vertex_labels_positions = Eigen::MatrixXd (0,3);
  426. face_labels_positions = Eigen::MatrixXd (0,3);
  427. labels_positions = Eigen::MatrixXd (0,3);
  428. vertex_labels_strings.clear();
  429. face_labels_strings.clear();
  430. labels_strings.clear();
  431. face_based = false;
  432. double_sided = false;
  433. invert_normals = false;
  434. show_texture = false;
  435. use_matcap = false;
  436. }
  437. IGL_INLINE void igl::opengl::ViewerData::compute_normals()
  438. {
  439. if(V.cols() == 2)
  440. {
  441. F_normals = Eigen::RowVector3d(0,0,1).replicate(F.rows(),1);
  442. V_normals = Eigen::RowVector3d(0,0,1).replicate(V.rows(),1);
  443. }else
  444. {
  445. assert(V.cols() == 3);
  446. igl::per_face_normals(V, F, F_normals);
  447. igl::per_vertex_normals(V, F, F_normals, V_normals);
  448. }
  449. dirty |= MeshGL::DIRTY_NORMAL;
  450. }
  451. IGL_INLINE void igl::opengl::ViewerData::uniform_colors(
  452. const Eigen::Vector3d& ambient,
  453. const Eigen::Vector3d& diffuse,
  454. const Eigen::Vector3d& specular)
  455. {
  456. Eigen::Vector4d ambient4;
  457. Eigen::Vector4d diffuse4;
  458. Eigen::Vector4d specular4;
  459. ambient4 << ambient, 1;
  460. diffuse4 << diffuse, 1;
  461. specular4 << specular, 1;
  462. uniform_colors(ambient4,diffuse4,specular4);
  463. }
  464. IGL_INLINE void igl::opengl::ViewerData::uniform_colors(
  465. const Eigen::Vector4d& ambient,
  466. const Eigen::Vector4d& diffuse,
  467. const Eigen::Vector4d& specular)
  468. {
  469. V_material_ambient.resize(V.rows(),4);
  470. V_material_diffuse.resize(V.rows(),4);
  471. V_material_specular.resize(V.rows(),4);
  472. for (unsigned i=0; i<V.rows();++i)
  473. {
  474. V_material_ambient.row(i) = ambient;
  475. V_material_diffuse.row(i) = diffuse;
  476. V_material_specular.row(i) = specular;
  477. }
  478. F_material_ambient.resize(F.rows(),4);
  479. F_material_diffuse.resize(F.rows(),4);
  480. F_material_specular.resize(F.rows(),4);
  481. for (unsigned i=0; i<F.rows();++i)
  482. {
  483. F_material_ambient.row(i) = ambient;
  484. F_material_diffuse.row(i) = diffuse;
  485. F_material_specular.row(i) = specular;
  486. }
  487. dirty |= MeshGL::DIRTY_SPECULAR | MeshGL::DIRTY_DIFFUSE | MeshGL::DIRTY_AMBIENT;
  488. }
  489. IGL_INLINE void igl::opengl::ViewerData::normal_matcap()
  490. {
  491. const int size = 512;
  492. texture_R.resize(size, size);
  493. texture_G.resize(size, size);
  494. texture_B.resize(size, size);
  495. const Eigen::Vector3d navy(0.3,0.3,0.5);
  496. static const auto clamp = [](double t){ return std::max(std::min(t,1.0),0.0);};
  497. for(int i = 0;i<size;i++)
  498. {
  499. const double x = (double(i)/double(size-1)*2.-1.);
  500. for(int j = 0;j<size;j++)
  501. {
  502. const double y = (double(j)/double(size-1)*2.-1.);
  503. const double z = sqrt(1.0-std::min(x*x+y*y,1.0));
  504. Eigen::Vector3d C = Eigen::Vector3d(x*0.5+0.5,y*0.5+0.5,z);
  505. texture_R(i,j) = clamp(C(0))*255;
  506. texture_G(i,j) = clamp(C(1))*255;
  507. texture_B(i,j) = clamp(C(2))*255;
  508. }
  509. }
  510. texture_A.setConstant(texture_R.rows(),texture_R.cols(),255);
  511. dirty |= MeshGL::DIRTY_TEXTURE;
  512. }
  513. IGL_INLINE void igl::opengl::ViewerData::grid_texture()
  514. {
  515. unsigned size = 128;
  516. unsigned size2 = size/2;
  517. texture_R.resize(size, size);
  518. for (unsigned i=0; i<size; ++i)
  519. {
  520. for (unsigned j=0; j<size; ++j)
  521. {
  522. texture_R(i,j) = 0;
  523. if ((i<size2 && j<size2) || (i>=size2 && j>=size2))
  524. texture_R(i,j) = 255;
  525. }
  526. }
  527. texture_G = texture_R;
  528. texture_B = texture_R;
  529. texture_A = Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>::Constant(texture_R.rows(),texture_R.cols(),255);
  530. dirty |= MeshGL::DIRTY_TEXTURE;
  531. }
  532. // Populate VBOs of a particular label stype (Vert, Face, Custom)
  533. IGL_INLINE void igl::opengl::ViewerData::update_labels(
  534. igl::opengl::MeshGL& meshgl,
  535. igl::opengl::MeshGL::TextGL& GL_labels,
  536. const Eigen::MatrixXd& positions,
  537. const std::vector<std::string>& strings
  538. ){
  539. if (positions.rows()>0)
  540. {
  541. int numCharsToRender = 0;
  542. for(size_t p=0; p<positions.rows(); p++)
  543. {
  544. numCharsToRender += strings.at(p).length();
  545. }
  546. GL_labels.label_pos_vbo.resize(numCharsToRender, 3);
  547. GL_labels.label_char_vbo.resize(numCharsToRender, 1);
  548. GL_labels.label_offset_vbo.resize(numCharsToRender, 1);
  549. GL_labels.label_indices_vbo.resize(numCharsToRender, 1);
  550. int idx=0;
  551. assert(strings.size() == positions.rows());
  552. for(size_t s=0; s<strings.size(); s++)
  553. {
  554. const auto & label = strings.at(s);
  555. for(size_t c=0; c<label.length(); c++)
  556. {
  557. GL_labels.label_pos_vbo.row(idx) = positions.row(s).cast<float>();
  558. GL_labels.label_char_vbo(idx) = (float)(label.at(c));
  559. GL_labels.label_offset_vbo(idx) = c;
  560. GL_labels.label_indices_vbo(idx) = idx;
  561. idx++;
  562. }
  563. }
  564. }
  565. }
  566. IGL_INLINE void igl::opengl::ViewerData::updateGL(
  567. const igl::opengl::ViewerData& data,
  568. const bool invert_normals,
  569. igl::opengl::MeshGL& meshgl
  570. )
  571. {
  572. if (!meshgl.is_initialized)
  573. {
  574. meshgl.init();
  575. }
  576. bool per_corner_uv = (data.F_uv.rows() == data.F.rows());
  577. bool per_corner_normals = (data.F_normals.rows() == 3 * data.F.rows());
  578. meshgl.dirty |= data.dirty;
  579. // Input:
  580. // X #F by dim quantity
  581. // Output:
  582. // X_vbo #F*3 by dim scattering per corner
  583. const auto per_face = [&data](
  584. const Eigen::MatrixXd & X,
  585. MeshGL::RowMatrixXf & X_vbo)
  586. {
  587. assert(X.cols() == 4);
  588. X_vbo.resize(data.F.rows()*3,4);
  589. for (unsigned i=0; i<data.F.rows();++i)
  590. for (unsigned j=0;j<3;++j)
  591. X_vbo.row(i*3+j) = X.row(i).cast<float>();
  592. };
  593. // Input:
  594. // X #V by dim quantity
  595. // Output:
  596. // X_vbo #F*3 by dim scattering per corner
  597. const auto per_corner = [&data](
  598. const Eigen::MatrixXd & X,
  599. MeshGL::RowMatrixXf & X_vbo)
  600. {
  601. X_vbo.resize(data.F.rows()*3,X.cols());
  602. for (unsigned i=0; i<data.F.rows();++i)
  603. for (unsigned j=0;j<3;++j)
  604. X_vbo.row(i*3+j) = X.row(data.F(i,j)).cast<float>();
  605. };
  606. if (!data.face_based)
  607. {
  608. if (!(per_corner_uv || per_corner_normals))
  609. {
  610. // Vertex positions
  611. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  612. meshgl.V_vbo = data.V.cast<float>();
  613. // Vertex normals
  614. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  615. {
  616. meshgl.V_normals_vbo = data.V_normals.cast<float>();
  617. if (invert_normals)
  618. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  619. }
  620. // Per-vertex material settings
  621. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  622. meshgl.V_ambient_vbo = data.V_material_ambient.cast<float>();
  623. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  624. meshgl.V_diffuse_vbo = data.V_material_diffuse.cast<float>();
  625. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  626. meshgl.V_specular_vbo = data.V_material_specular.cast<float>();
  627. // Face indices
  628. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  629. meshgl.F_vbo = data.F.cast<unsigned>();
  630. // Texture coordinates
  631. if (meshgl.dirty & MeshGL::DIRTY_UV)
  632. {
  633. meshgl.V_uv_vbo = data.V_uv.cast<float>();
  634. }
  635. }
  636. else
  637. {
  638. // Per vertex properties with per corner UVs
  639. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  640. {
  641. per_corner(data.V,meshgl.V_vbo);
  642. }
  643. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  644. {
  645. meshgl.V_ambient_vbo.resize(data.F.rows()*3,4);
  646. per_corner(data.V_material_ambient,meshgl.V_ambient_vbo);
  647. }
  648. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  649. {
  650. meshgl.V_diffuse_vbo.resize(data.F.rows()*3,4);
  651. per_corner(data.V_material_diffuse,meshgl.V_diffuse_vbo);
  652. }
  653. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  654. {
  655. meshgl.V_specular_vbo.resize(data.F.rows()*3,4);
  656. per_corner(data.V_material_specular,meshgl.V_specular_vbo);
  657. }
  658. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  659. {
  660. meshgl.V_normals_vbo.resize(data.F.rows()*3,3);
  661. per_corner(data.V_normals,meshgl.V_normals_vbo);
  662. if (invert_normals)
  663. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  664. }
  665. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  666. {
  667. meshgl.F_vbo.resize(data.F.rows(),3);
  668. for (unsigned i=0; i<data.F.rows();++i)
  669. meshgl.F_vbo.row(i) << i*3+0, i*3+1, i*3+2;
  670. }
  671. if ( (meshgl.dirty & MeshGL::DIRTY_UV) && data.V_uv.rows()>0)
  672. {
  673. meshgl.V_uv_vbo.resize(data.F.rows()*3,2);
  674. for (unsigned i=0; i<data.F.rows();++i)
  675. for (unsigned j=0;j<3;++j)
  676. meshgl.V_uv_vbo.row(i*3+j) =
  677. data.V_uv.row(per_corner_uv ?
  678. data.F_uv(i,j) : data.F(i,j)).cast<float>();
  679. }
  680. }
  681. } else
  682. {
  683. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  684. {
  685. per_corner(data.V,meshgl.V_vbo);
  686. }
  687. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  688. {
  689. per_face(data.F_material_ambient,meshgl.V_ambient_vbo);
  690. }
  691. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  692. {
  693. per_face(data.F_material_diffuse,meshgl.V_diffuse_vbo);
  694. }
  695. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  696. {
  697. per_face(data.F_material_specular,meshgl.V_specular_vbo);
  698. }
  699. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  700. {
  701. meshgl.V_normals_vbo.resize(data.F.rows()*3,3);
  702. for (unsigned i=0; i<data.F.rows();++i)
  703. for (unsigned j=0;j<3;++j)
  704. meshgl.V_normals_vbo.row(i*3+j) =
  705. per_corner_normals ?
  706. data.F_normals.row(i*3+j).cast<float>() :
  707. data.F_normals.row(i).cast<float>();
  708. if (invert_normals)
  709. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  710. }
  711. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  712. {
  713. meshgl.F_vbo.resize(data.F.rows(),3);
  714. for (unsigned i=0; i<data.F.rows();++i)
  715. meshgl.F_vbo.row(i) << i*3+0, i*3+1, i*3+2;
  716. }
  717. if( (meshgl.dirty & MeshGL::DIRTY_UV) && data.V_uv.rows()>0)
  718. {
  719. meshgl.V_uv_vbo.resize(data.F.rows()*3,2);
  720. for (unsigned i=0; i<data.F.rows();++i)
  721. for (unsigned j=0;j<3;++j)
  722. meshgl.V_uv_vbo.row(i*3+j) = data.V_uv.row(per_corner_uv ? data.F_uv(i,j) : data.F(i,j)).cast<float>();
  723. }
  724. }
  725. if (meshgl.dirty & MeshGL::DIRTY_TEXTURE)
  726. {
  727. meshgl.tex_u = data.texture_R.rows();
  728. meshgl.tex_v = data.texture_R.cols();
  729. meshgl.tex.resize(data.texture_R.size()*4);
  730. for (unsigned i=0;i<data.texture_R.size();++i)
  731. {
  732. meshgl.tex(i*4+0) = data.texture_R(i);
  733. meshgl.tex(i*4+1) = data.texture_G(i);
  734. meshgl.tex(i*4+2) = data.texture_B(i);
  735. meshgl.tex(i*4+3) = data.texture_A(i);
  736. }
  737. }
  738. if (meshgl.dirty & MeshGL::DIRTY_OVERLAY_LINES)
  739. {
  740. meshgl.lines_V_vbo.resize(data.lines.rows()*2,3);
  741. meshgl.lines_V_colors_vbo.resize(data.lines.rows()*2,3);
  742. meshgl.lines_F_vbo.resize(data.lines.rows()*2,1);
  743. for (unsigned i=0; i<data.lines.rows();++i)
  744. {
  745. meshgl.lines_V_vbo.row(2*i+0) = data.lines.block<1, 3>(i, 0).cast<float>();
  746. meshgl.lines_V_vbo.row(2*i+1) = data.lines.block<1, 3>(i, 3).cast<float>();
  747. meshgl.lines_V_colors_vbo.row(2*i+0) = data.lines.block<1, 3>(i, 6).cast<float>();
  748. meshgl.lines_V_colors_vbo.row(2*i+1) = data.lines.block<1, 3>(i, 6).cast<float>();
  749. meshgl.lines_F_vbo(2*i+0) = 2*i+0;
  750. meshgl.lines_F_vbo(2*i+1) = 2*i+1;
  751. }
  752. }
  753. if (meshgl.dirty & MeshGL::DIRTY_OVERLAY_POINTS)
  754. {
  755. meshgl.points_V_vbo.resize(data.points.rows(),3);
  756. meshgl.points_V_colors_vbo.resize(data.points.rows(),3);
  757. meshgl.points_F_vbo.resize(data.points.rows(),1);
  758. for (unsigned i=0; i<data.points.rows();++i)
  759. {
  760. meshgl.points_V_vbo.row(i) = data.points.block<1, 3>(i, 0).cast<float>();
  761. meshgl.points_V_colors_vbo.row(i) = data.points.block<1, 3>(i, 3).cast<float>();
  762. meshgl.points_F_vbo(i) = i;
  763. }
  764. }
  765. if (meshgl.dirty & MeshGL::DIRTY_FACE_LABELS)
  766. {
  767. if(face_labels_positions.rows()==0)
  768. {
  769. face_labels_positions.conservativeResize(F.rows(), 3);
  770. Eigen::MatrixXd faceNormals = F_normals.normalized();
  771. for (int f=0; f<F.rows();++f)
  772. {
  773. std::string faceName = std::to_string(f);
  774. face_labels_positions.row(f) = V.row(F.row(f)(0));
  775. face_labels_positions.row(f) += V.row(F.row(f)(1));
  776. face_labels_positions.row(f) += V.row(F.row(f)(2));
  777. face_labels_positions.row(f) /= 3.;
  778. face_labels_positions.row(f) = (faceNormals*0.05).row(f) + face_labels_positions.row(f);
  779. face_labels_strings.push_back(faceName);
  780. }
  781. }
  782. update_labels(
  783. meshgl,
  784. meshgl.face_labels,
  785. face_labels_positions,
  786. face_labels_strings
  787. );
  788. }
  789. if (meshgl.dirty & MeshGL::DIRTY_VERTEX_LABELS)
  790. {
  791. if(vertex_labels_positions.rows()==0)
  792. {
  793. vertex_labels_positions.conservativeResize(V.rows(), 3);
  794. Eigen::MatrixXd normalized = V_normals.normalized();
  795. for (int v=0; v<V.rows();++v)
  796. {
  797. std::string vertName = std::to_string(v);
  798. vertex_labels_positions.row(v) = (normalized*0.1).row(v) + V.row(v);
  799. vertex_labels_strings.push_back(vertName);
  800. }
  801. }
  802. update_labels(
  803. meshgl,
  804. meshgl.vertex_labels,
  805. vertex_labels_positions,
  806. vertex_labels_strings
  807. );
  808. }
  809. if (meshgl.dirty & MeshGL::DIRTY_CUSTOM_LABELS)
  810. {
  811. update_labels(
  812. meshgl,
  813. meshgl.custom_labels,
  814. labels_positions,
  815. labels_strings
  816. );
  817. }
  818. }