ViewerData.cpp 11 KB

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