ViewerData.cpp 9.9 KB

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