ViewerData.h 9.0 KB

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