ViewerData.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  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 <iostream>
  10. #include <igl/per_face_normals.h>
  11. #include <igl/material_colors.h>
  12. #include <igl/parula.h>
  13. #include <igl/per_vertex_normals.h>
  14. #ifdef ENABLE_SERIALIZATION
  15. #include <igl/serialize.h>
  16. namespace igl {
  17. namespace serialization {
  18. IGL_INLINE void serialization(bool s,igl::viewer::ViewerData& obj,std::vector<char>& buffer)
  19. {
  20. SERIALIZE_MEMBER(V);
  21. SERIALIZE_MEMBER(F);
  22. SERIALIZE_MEMBER(F_normals);
  23. SERIALIZE_MEMBER(F_material_ambient);
  24. SERIALIZE_MEMBER(F_material_diffuse);
  25. SERIALIZE_MEMBER(F_material_specular);
  26. SERIALIZE_MEMBER(V_normals);
  27. SERIALIZE_MEMBER(V_material_ambient);
  28. SERIALIZE_MEMBER(V_material_diffuse);
  29. SERIALIZE_MEMBER(V_material_specular);
  30. SERIALIZE_MEMBER(V_uv);
  31. SERIALIZE_MEMBER(F_uv);
  32. SERIALIZE_MEMBER(texture_R);
  33. SERIALIZE_MEMBER(texture_G);
  34. SERIALIZE_MEMBER(texture_B);
  35. SERIALIZE_MEMBER(texture_A);
  36. SERIALIZE_MEMBER(lines);
  37. SERIALIZE_MEMBER(points);
  38. SERIALIZE_MEMBER(labels_positions);
  39. SERIALIZE_MEMBER(labels_strings);
  40. SERIALIZE_MEMBER(dirty);
  41. SERIALIZE_MEMBER(face_based);
  42. }
  43. template<>
  44. IGL_INLINE void serialize(const igl::viewer::ViewerData& obj,std::vector<char>& buffer)
  45. {
  46. serialization(true,const_cast<igl::viewer::ViewerData&>(obj),buffer);
  47. }
  48. template<>
  49. IGL_INLINE void deserialize(igl::viewer::ViewerData& obj,const std::vector<char>& buffer)
  50. {
  51. serialization(false,obj,const_cast<std::vector<char>&>(buffer));
  52. obj.dirty = igl::viewer::ViewerData::DIRTY_ALL;
  53. }
  54. }
  55. }
  56. #endif
  57. IGL_INLINE igl::viewer::ViewerData::ViewerData()
  58. : dirty(DIRTY_ALL)
  59. {
  60. clear();
  61. };
  62. IGL_INLINE void igl::viewer::ViewerData::set_face_based(bool newvalue)
  63. {
  64. if (face_based != newvalue)
  65. {
  66. face_based = newvalue;
  67. dirty = DIRTY_ALL;
  68. }
  69. }
  70. // Helpers that draws the most common meshes
  71. IGL_INLINE void igl::viewer::ViewerData::set_mesh(const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F)
  72. {
  73. using namespace std;
  74. Eigen::MatrixXd V_temp;
  75. // If V only has two columns, pad with a column of zeros
  76. if (_V.cols() == 2)
  77. {
  78. V_temp = Eigen::MatrixXd::Zero(_V.rows(),3);
  79. V_temp.block(0,0,_V.rows(),2) = _V;
  80. }
  81. else
  82. V_temp = _V;
  83. if (V.rows() == 0 && F.rows() == 0)
  84. {
  85. V = V_temp;
  86. F = _F;
  87. compute_normals();
  88. uniform_colors(
  89. Eigen::Vector3d(GOLD_AMBIENT[0], GOLD_AMBIENT[1], GOLD_AMBIENT[2]),
  90. Eigen::Vector3d(GOLD_DIFFUSE[0], GOLD_DIFFUSE[1], GOLD_DIFFUSE[2]),
  91. Eigen::Vector3d(GOLD_SPECULAR[0], GOLD_SPECULAR[1], GOLD_SPECULAR[2]));
  92. grid_texture();
  93. }
  94. else
  95. {
  96. if (_V.rows() == V.rows() && _F.rows() == F.rows())
  97. {
  98. V = V_temp;
  99. F = _F;
  100. }
  101. else
  102. cerr << "ERROR (set_mesh): The new mesh has a different number of vertices/faces. Please clear the mesh before plotting."<<endl;
  103. }
  104. dirty |= DIRTY_FACE | DIRTY_POSITION;
  105. }
  106. IGL_INLINE void igl::viewer::ViewerData::set_vertices(const Eigen::MatrixXd& _V)
  107. {
  108. V = _V;
  109. assert(F.size() == 0 || F.maxCoeff() < V.rows());
  110. dirty |= DIRTY_POSITION;
  111. }
  112. IGL_INLINE void igl::viewer::ViewerData::set_normals(const Eigen::MatrixXd& N)
  113. {
  114. using namespace std;
  115. if (N.rows() == V.rows())
  116. {
  117. set_face_based(false);
  118. V_normals = N;
  119. }
  120. else if (N.rows() == F.rows() || N.rows() == F.rows()*3)
  121. {
  122. set_face_based(true);
  123. F_normals = N;
  124. }
  125. else
  126. cerr << "ERROR (set_normals): Please provide a normal per face, per corner or per vertex."<<endl;
  127. dirty |= DIRTY_NORMAL;
  128. }
  129. IGL_INLINE void igl::viewer::ViewerData::set_colors(const Eigen::MatrixXd &C)
  130. {
  131. using namespace std;
  132. using namespace Eigen;
  133. if(C.rows()>0 && C.cols() == 1)
  134. {
  135. Eigen::MatrixXd C3;
  136. igl::parula(C,true,C3);
  137. return set_colors(C3);
  138. }
  139. // Ambient color should be darker color
  140. const auto ambient = [](const MatrixXd & C)->MatrixXd
  141. {
  142. MatrixXd T = 0.1*C;
  143. T.col(3) = C.col(3);
  144. return T;
  145. };
  146. // Specular color should be a less saturated and darker color: dampened
  147. // highlights
  148. const auto specular = [](const MatrixXd & C)->MatrixXd
  149. {
  150. const double grey = 0.3;
  151. MatrixXd T = grey+0.1*(C.array()-grey);
  152. T.col(3) = C.col(3);
  153. return T;
  154. };
  155. if (C.rows() == 1)
  156. {
  157. for (unsigned i=0;i<V_material_diffuse.rows();++i)
  158. {
  159. V_material_diffuse.row(i) << C.row(0),1;
  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. F_material_diffuse.row(i) << C.row(0),1;
  166. }
  167. F_material_ambient = ambient(F_material_diffuse);
  168. F_material_specular = specular(F_material_diffuse);
  169. }
  170. else if (C.rows() == V.rows())
  171. {
  172. set_face_based(false);
  173. for (unsigned i=0;i<V_material_diffuse.rows();++i)
  174. {
  175. V_material_diffuse.row(i) << C.row(i),1;
  176. }
  177. V_material_ambient = ambient(V_material_diffuse);
  178. V_material_specular = specular(V_material_diffuse);
  179. }
  180. else if (C.rows() == F.rows())
  181. {
  182. set_face_based(true);
  183. for (unsigned i=0;i<F_material_diffuse.rows();++i)
  184. {
  185. F_material_diffuse.row(i) << C.row(i),1;
  186. }
  187. F_material_ambient = ambient(F_material_diffuse);
  188. F_material_specular = specular(F_material_diffuse);
  189. }
  190. else
  191. cerr << "ERROR (set_colors): Please provide a single color, or a color per face or per vertex."<<endl;;
  192. dirty |= DIRTY_DIFFUSE;
  193. }
  194. IGL_INLINE void igl::viewer::ViewerData::set_uv(const Eigen::MatrixXd& UV)
  195. {
  196. using namespace std;
  197. if (UV.rows() == V.rows())
  198. {
  199. set_face_based(false);
  200. V_uv = UV;
  201. }
  202. else
  203. cerr << "ERROR (set_UV): Please provide uv per vertex."<<endl;;
  204. dirty |= DIRTY_UV;
  205. }
  206. IGL_INLINE void igl::viewer::ViewerData::set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F)
  207. {
  208. set_face_based(true);
  209. V_uv = UV_V.block(0,0,UV_V.rows(),2);
  210. F_uv = UV_F;
  211. dirty |= DIRTY_UV;
  212. }
  213. IGL_INLINE void igl::viewer::ViewerData::set_texture(
  214. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  215. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  216. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B)
  217. {
  218. texture_R = R;
  219. texture_G = G;
  220. texture_B = B;
  221. texture_A = Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>::Constant(R.rows(),R.cols(),255);
  222. dirty |= DIRTY_TEXTURE;
  223. }
  224. IGL_INLINE void igl::viewer::ViewerData::set_texture(
  225. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  226. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  227. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  228. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A)
  229. {
  230. texture_R = R;
  231. texture_G = G;
  232. texture_B = B;
  233. texture_A = A;
  234. dirty |= DIRTY_TEXTURE;
  235. }
  236. IGL_INLINE void igl::viewer::ViewerData::set_points(
  237. const Eigen::MatrixXd& P,
  238. const Eigen::MatrixXd& C)
  239. {
  240. // clear existing points
  241. points.resize(0,0);
  242. add_points(P,C);
  243. }
  244. IGL_INLINE void igl::viewer::ViewerData::add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C)
  245. {
  246. Eigen::MatrixXd P_temp;
  247. // If P only has two columns, pad with a column of zeros
  248. if (P.cols() == 2)
  249. {
  250. P_temp = Eigen::MatrixXd::Zero(P.rows(),3);
  251. P_temp.block(0,0,P.rows(),2) = P;
  252. }
  253. else
  254. P_temp = P;
  255. int lastid = points.rows();
  256. points.conservativeResize(points.rows() + P_temp.rows(),6);
  257. for (unsigned i=0; i<P_temp.rows(); ++i)
  258. points.row(lastid+i) << P_temp.row(i), i<C.rows() ? C.row(i) : C.row(C.rows()-1);
  259. dirty |= DIRTY_OVERLAY_POINTS;
  260. }
  261. IGL_INLINE void igl::viewer::ViewerData::set_edges(
  262. const Eigen::MatrixXd& P,
  263. const Eigen::MatrixXi& E,
  264. const Eigen::MatrixXd& C)
  265. {
  266. using namespace Eigen;
  267. lines.resize(E.rows(),9);
  268. assert(C.cols() == 3);
  269. for(int e = 0;e<E.rows();e++)
  270. {
  271. RowVector3d color;
  272. if(C.size() == 3)
  273. {
  274. color<<C;
  275. }else if(C.rows() == E.rows())
  276. {
  277. color<<C.row(e);
  278. }
  279. lines.row(e)<< P.row(E(e,0)), P.row(E(e,1)), color;
  280. }
  281. dirty |= DIRTY_OVERLAY_LINES;
  282. }
  283. IGL_INLINE void igl::viewer::ViewerData::add_edges(const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C)
  284. {
  285. Eigen::MatrixXd P1_temp,P2_temp;
  286. // If P1 only has two columns, pad with a column of zeros
  287. if (P1.cols() == 2)
  288. {
  289. P1_temp = Eigen::MatrixXd::Zero(P1.rows(),3);
  290. P1_temp.block(0,0,P1.rows(),2) = P1;
  291. P2_temp = Eigen::MatrixXd::Zero(P2.rows(),3);
  292. P2_temp.block(0,0,P2.rows(),2) = P2;
  293. }
  294. else
  295. {
  296. P1_temp = P1;
  297. P2_temp = P2;
  298. }
  299. int lastid = lines.rows();
  300. lines.conservativeResize(lines.rows() + P1_temp.rows(),9);
  301. for (unsigned i=0; i<P1_temp.rows(); ++i)
  302. lines.row(lastid+i) << P1_temp.row(i), P2_temp.row(i), i<C.rows() ? C.row(i) : C.row(C.rows()-1);
  303. dirty |= DIRTY_OVERLAY_LINES;
  304. }
  305. IGL_INLINE void igl::viewer::ViewerData::add_label(const Eigen::VectorXd& P, const std::string& str)
  306. {
  307. Eigen::RowVectorXd P_temp;
  308. // If P only has two columns, pad with a column of zeros
  309. if (P.size() == 2)
  310. {
  311. P_temp = Eigen::RowVectorXd::Zero(3);
  312. P_temp << P.transpose(), 0;
  313. }
  314. else
  315. P_temp = P;
  316. int lastid = labels_positions.rows();
  317. labels_positions.conservativeResize(lastid+1, 3);
  318. labels_positions.row(lastid) = P_temp;
  319. labels_strings.push_back(str);
  320. }
  321. IGL_INLINE void igl::viewer::ViewerData::clear()
  322. {
  323. V = Eigen::MatrixXd (0,3);
  324. F = Eigen::MatrixXi (0,3);
  325. F_material_ambient = Eigen::MatrixXd (0,4);
  326. F_material_diffuse = Eigen::MatrixXd (0,4);
  327. F_material_specular = Eigen::MatrixXd (0,4);
  328. V_material_ambient = Eigen::MatrixXd (0,4);
  329. V_material_diffuse = Eigen::MatrixXd (0,4);
  330. V_material_specular = Eigen::MatrixXd (0,4);
  331. F_normals = Eigen::MatrixXd (0,3);
  332. V_normals = Eigen::MatrixXd (0,3);
  333. V_uv = Eigen::MatrixXd (0,2);
  334. F_uv = Eigen::MatrixXi (0,3);
  335. lines = Eigen::MatrixXd (0,9);
  336. points = Eigen::MatrixXd (0,6);
  337. labels_positions = Eigen::MatrixXd (0,3);
  338. labels_strings.clear();
  339. face_based = false;
  340. }
  341. IGL_INLINE void igl::viewer::ViewerData::compute_normals()
  342. {
  343. igl::per_face_normals(V, F, F_normals);
  344. igl::per_vertex_normals(V, F, F_normals, V_normals);
  345. dirty |= DIRTY_NORMAL;
  346. }
  347. IGL_INLINE void igl::viewer::ViewerData::uniform_colors(Eigen::Vector3d ambient, Eigen::Vector3d diffuse, Eigen::Vector3d specular)
  348. {
  349. Eigen::Vector4d ambient4;
  350. Eigen::Vector4d diffuse4;
  351. Eigen::Vector4d specular4;
  352. ambient4 << ambient, 1;
  353. diffuse4 << diffuse, 1;
  354. specular4 << specular, 1;
  355. uniform_colors(ambient4,diffuse4,specular4);
  356. }
  357. IGL_INLINE void igl::viewer::ViewerData::uniform_colors(Eigen::Vector4d ambient, Eigen::Vector4d diffuse, Eigen::Vector4d specular)
  358. {
  359. V_material_ambient.resize(V.rows(),4);
  360. V_material_diffuse.resize(V.rows(),4);
  361. V_material_specular.resize(V.rows(),4);
  362. for (unsigned i=0; i<V.rows();++i)
  363. {
  364. V_material_ambient.row(i) = ambient;
  365. V_material_diffuse.row(i) = diffuse;
  366. V_material_specular.row(i) = specular;
  367. }
  368. F_material_ambient.resize(F.rows(),4);
  369. F_material_diffuse.resize(F.rows(),4);
  370. F_material_specular.resize(F.rows(),4);
  371. for (unsigned i=0; i<F.rows();++i)
  372. {
  373. F_material_ambient.row(i) = ambient;
  374. F_material_diffuse.row(i) = diffuse;
  375. F_material_specular.row(i) = specular;
  376. }
  377. dirty |= DIRTY_SPECULAR | DIRTY_DIFFUSE | DIRTY_AMBIENT;
  378. }
  379. IGL_INLINE void igl::viewer::ViewerData::grid_texture()
  380. {
  381. // Don't do anything for an empty mesh
  382. if(V.rows() == 0)
  383. {
  384. V_uv.resize(V.rows(),2);
  385. return;
  386. }
  387. if (V_uv.rows() == 0)
  388. {
  389. V_uv = V.block(0, 0, V.rows(), 2);
  390. V_uv.col(0) = V_uv.col(0).array() - V_uv.col(0).minCoeff();
  391. V_uv.col(0) = V_uv.col(0).array() / V_uv.col(0).maxCoeff();
  392. V_uv.col(1) = V_uv.col(1).array() - V_uv.col(1).minCoeff();
  393. V_uv.col(1) = V_uv.col(1).array() / V_uv.col(1).maxCoeff();
  394. V_uv = V_uv.array() * 10;
  395. dirty |= DIRTY_TEXTURE;
  396. }
  397. unsigned size = 128;
  398. unsigned size2 = size/2;
  399. texture_R.resize(size, size);
  400. for (unsigned i=0; i<size; ++i)
  401. {
  402. for (unsigned j=0; j<size; ++j)
  403. {
  404. texture_R(i,j) = 0;
  405. if ((i<size2 && j<size2) || (i>=size2 && j>=size2))
  406. texture_R(i,j) = 255;
  407. }
  408. }
  409. texture_G = texture_R;
  410. texture_B = texture_R;
  411. texture_A = Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>::Constant(texture_R.rows(),texture_R.cols(),255);
  412. dirty |= DIRTY_TEXTURE;
  413. }