ViewerData.cpp 11 KB

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