ViewerData.cpp 11 KB

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