ViewerData.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #ifndef IGL_VIEWERDATA_H
  9. #define IGL_VIEWERDATA_H
  10. #include <cstdint>
  11. #include <vector>
  12. #include <Eigen/Core>
  13. #include "../igl_inline.h"
  14. #include "../Attributes.h"
  15. #include "MeshGL.h"
  16. // Alec: This is a mesh class containing a variety of data types (normals,
  17. // overlays, material colors, etc.)
  18. //
  19. namespace igl
  20. {
  21. // TODO: write documentation
  22. namespace opengl
  23. {
  24. class ViewerData
  25. {
  26. public:
  27. ViewerData();
  28. // Empty all fields
  29. IGL_INLINE void clear();
  30. // Change the visualization mode, invalidating the cache if necessary
  31. IGL_INLINE void set_face_based(bool newvalue);
  32. // Helpers that can draw the most common meshes
  33. IGL_INLINE void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  34. IGL_INLINE void set_vertices(const Eigen::MatrixXd& V);
  35. IGL_INLINE void set_normals(const Eigen::MatrixXd& N);
  36. // Set the color of the mesh
  37. //
  38. // Inputs:
  39. // C #V|#F|1 by 3 list of colors
  40. IGL_INLINE void set_colors(const Eigen::MatrixXd &C);
  41. // Set per-vertex UV coordinates
  42. //
  43. // Inputs:
  44. // UV #V by 2 list of UV coordinates (indexed by F)
  45. IGL_INLINE void set_uv(const Eigen::MatrixXd& UV);
  46. // Set per-corner UV coordinates
  47. //
  48. // Inputs:
  49. // UV_V #UV by 2 list of UV coordinates
  50. // UV_F #F by 3 list of UV indices into UV_V
  51. IGL_INLINE void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  52. // Set the texture associated with the mesh.
  53. //
  54. // Inputs:
  55. // R width by height image matrix of red channel
  56. // G width by height image matrix of green channel
  57. // B width by height image matrix of blue channel
  58. //
  59. IGL_INLINE void set_texture(
  60. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  61. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  62. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B);
  63. // Set the texture associated with the mesh.
  64. //
  65. // Inputs:
  66. // R width by height image matrix of red channel
  67. // G width by height image matrix of green channel
  68. // B width by height image matrix of blue channel
  69. // A width by height image matrix of alpha channel
  70. //
  71. IGL_INLINE void set_texture(
  72. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  73. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  74. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  75. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
  76. // Sets points given a list of point vertices. In constrast to `set_points`
  77. // this will (purposefully) clober existing points.
  78. //
  79. // Inputs:
  80. // P #P by 3 list of vertex positions
  81. // C #P|1 by 3 color(s)
  82. IGL_INLINE void set_points(
  83. const Eigen::MatrixXd& P,
  84. const Eigen::MatrixXd& C);
  85. IGL_INLINE void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  86. // Sets edges given a list of edge vertices and edge indices. In constrast
  87. // to `add_edges` this will (purposefully) clober existing edges.
  88. //
  89. // Inputs:
  90. // P #P by 3 list of vertex positions
  91. // E #E by 2 list of edge indices into P
  92. // C #E|1 by 3 color(s)
  93. IGL_INLINE void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  94. IGL_INLINE void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  95. IGL_INLINE void add_label (const Eigen::VectorXd& P, const std::string& str);
  96. // Computes the normals of the mesh
  97. IGL_INLINE void compute_normals();
  98. // Assigns uniform colors to all faces/vertices
  99. IGL_INLINE void uniform_colors(
  100. const Eigen::Vector3d& diffuse,
  101. const Eigen::Vector3d& ambient,
  102. const Eigen::Vector3d& specular);
  103. // Assigns uniform colors to all faces/vertices
  104. IGL_INLINE void uniform_colors(
  105. const Eigen::Vector4d& ambient,
  106. const Eigen::Vector4d& diffuse,
  107. const Eigen::Vector4d& specular);
  108. // Generates a default grid texture
  109. IGL_INLINE void grid_texture();
  110. Eigen::MatrixXd V; // Vertices of the current mesh (#V x 3)
  111. Eigen::MatrixXi F; // Faces of the mesh (#F x 3)
  112. // Per face attributes
  113. Eigen::MatrixXd F_normals; // One normal per face
  114. Eigen::MatrixXd F_material_ambient; // Per face ambient color
  115. Eigen::MatrixXd F_material_diffuse; // Per face diffuse color
  116. Eigen::MatrixXd F_material_specular; // Per face specular color
  117. // Per vertex attributes
  118. Eigen::MatrixXd V_normals; // One normal per vertex
  119. Eigen::MatrixXd V_material_ambient; // Per vertex ambient color
  120. Eigen::MatrixXd V_material_diffuse; // Per vertex diffuse color
  121. Eigen::MatrixXd V_material_specular; // Per vertex specular color
  122. // UV parametrization
  123. Eigen::MatrixXd V_uv; // UV vertices
  124. Eigen::MatrixXi F_uv; // optional faces for UVs
  125. // Texture
  126. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_R;
  127. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_G;
  128. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_B;
  129. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_A;
  130. // Overlays
  131. // Lines plotted over the scene
  132. // (Every row contains 9 doubles in the following format S_x, S_y, S_z, T_x, T_y, T_z, C_r, C_g, C_b),
  133. // with S and T the coordinates of the two vertices of the line in global coordinates, and C the color in floating point rgb format
  134. Eigen::MatrixXd lines;
  135. // Points plotted over the scene
  136. // (Every row contains 6 doubles in the following format P_x, P_y, P_z, C_r, C_g, C_b),
  137. // with P the position in global coordinates of the center of the point, and C the color in floating point rgb format
  138. Eigen::MatrixXd points;
  139. // Text labels plotted over the scene
  140. // Textp contains, in the i-th row, the position in global coordinates where the i-th label should be anchored
  141. // Texts contains in the i-th position the text of the i-th label
  142. Eigen::MatrixXd labels_positions;
  143. std::vector<std::string> labels_strings;
  144. // Marks dirty buffers that need to be uploaded to OpenGL
  145. uint32_t dirty;
  146. // Enable per-face or per-vertex properties
  147. bool face_based;
  148. // Visualization options
  149. bool show_overlay;
  150. bool show_overlay_depth;
  151. bool show_texture;
  152. bool show_faces;
  153. bool show_lines;
  154. bool show_vertid;
  155. bool show_faceid;
  156. bool invert_normals;
  157. // Point size / line width
  158. float point_size;
  159. float line_width;
  160. Eigen::Vector4f line_color;
  161. // Shape material
  162. float shininess;
  163. // OpenGL representation of the mesh
  164. igl::opengl::MeshGL meshgl;
  165. // User-defined attribute
  166. AttributeManager attr;
  167. // Update contents from a 'Data' instance
  168. IGL_INLINE void updateGL(
  169. const igl::opengl::ViewerData& data,
  170. const bool invert_normals,
  171. igl::opengl::MeshGL& meshgl);
  172. };
  173. }
  174. }
  175. #ifdef ENABLE_SERIALIZATION
  176. #include <igl/serialize.h>
  177. namespace igl
  178. {
  179. namespace serialization
  180. {
  181. inline void serialization(bool s, igl::opengl::ViewerData& obj, std::vector<char>& buffer)
  182. {
  183. SERIALIZE_MEMBER(V);
  184. SERIALIZE_MEMBER(F);
  185. SERIALIZE_MEMBER(F_normals);
  186. SERIALIZE_MEMBER(F_material_ambient);
  187. SERIALIZE_MEMBER(F_material_diffuse);
  188. SERIALIZE_MEMBER(F_material_specular);
  189. SERIALIZE_MEMBER(V_normals);
  190. SERIALIZE_MEMBER(V_material_ambient);
  191. SERIALIZE_MEMBER(V_material_diffuse);
  192. SERIALIZE_MEMBER(V_material_specular);
  193. SERIALIZE_MEMBER(V_uv);
  194. SERIALIZE_MEMBER(F_uv);
  195. SERIALIZE_MEMBER(texture_R);
  196. SERIALIZE_MEMBER(texture_G);
  197. SERIALIZE_MEMBER(texture_B);
  198. SERIALIZE_MEMBER(texture_A);
  199. SERIALIZE_MEMBER(lines);
  200. SERIALIZE_MEMBER(points);
  201. SERIALIZE_MEMBER(labels_positions);
  202. SERIALIZE_MEMBER(labels_strings);
  203. SERIALIZE_MEMBER(dirty);
  204. SERIALIZE_MEMBER(face_based);
  205. SERIALIZE_MEMBER(show_faces);
  206. SERIALIZE_MEMBER(show_lines);
  207. SERIALIZE_MEMBER(invert_normals);
  208. SERIALIZE_MEMBER(show_overlay);
  209. SERIALIZE_MEMBER(show_overlay_depth);
  210. SERIALIZE_MEMBER(show_vertid);
  211. SERIALIZE_MEMBER(show_faceid);
  212. SERIALIZE_MEMBER(show_texture);
  213. SERIALIZE_MEMBER(point_size);
  214. SERIALIZE_MEMBER(line_width);
  215. SERIALIZE_MEMBER(line_color);
  216. SERIALIZE_MEMBER(shininess);
  217. }
  218. template<>
  219. inline void serialize(const igl::opengl::ViewerData& obj, std::vector<char>& buffer)
  220. {
  221. serialization(true, const_cast<igl::opengl::ViewerData&>(obj), buffer);
  222. }
  223. template<>
  224. inline void deserialize(igl::opengl::ViewerData& obj, const std::vector<char>& buffer)
  225. {
  226. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  227. obj.dirty = igl::opengl::MeshGL::DIRTY_ALL;
  228. }
  229. }
  230. }
  231. #endif
  232. #ifndef IGL_STATIC_LIBRARY
  233. # include "ViewerData.cpp"
  234. #endif
  235. #endif