ViewerData.cpp 10 KB

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