ViewerData.cpp 11 KB

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