ViewerData.cpp 11 KB

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