ViewerData.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  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 "MeshGL.h"
  11. #include <igl/igl_inline.h>
  12. #include <igl/colormap.h>
  13. #include <cassert>
  14. #include <cstdint>
  15. #include <Eigen/Core>
  16. #include <memory>
  17. #include <vector>
  18. // Alec: This is a mesh class containing a variety of data types (normals,
  19. // overlays, material colors, etc.)
  20. //
  21. // WARNING: Eigen data members (such as Eigen::Vector4f) should explicitly
  22. // disable alignment (e.g. use `Eigen::Matrix<float, 4, 1, Eigen::DontAlign>`),
  23. // in order to avoid alignment issues further down the line (esp. if the
  24. // structure are stored in a std::vector).
  25. //
  26. // See this thread for a more detailed discussion:
  27. // https://github.com/libigl/libigl/pull/1029
  28. //
  29. namespace igl
  30. {
  31. // TODO: write documentation
  32. namespace opengl
  33. {
  34. // Forward declaration
  35. class ViewerCore;
  36. class ViewerData
  37. {
  38. public:
  39. ViewerData();
  40. // Empty all fields
  41. IGL_INLINE void clear();
  42. // Change the visualization mode, invalidating the cache if necessary
  43. IGL_INLINE void set_face_based(bool newvalue);
  44. // Helpers that can draw the most common meshes
  45. IGL_INLINE void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  46. IGL_INLINE void set_vertices(const Eigen::MatrixXd& V);
  47. IGL_INLINE void set_normals(const Eigen::MatrixXd& N);
  48. IGL_INLINE void set_visible(bool value, unsigned int core_id = 1);
  49. // Set the color of the mesh
  50. //
  51. // Inputs:
  52. // C #V|#F|1 by 3 list of colors
  53. IGL_INLINE void set_colors(const Eigen::MatrixXd &C);
  54. // Set per-vertex UV coordinates
  55. //
  56. // Inputs:
  57. // UV #V by 2 list of UV coordinates (indexed by F)
  58. IGL_INLINE void set_uv(const Eigen::MatrixXd& UV);
  59. // Set per-corner UV coordinates
  60. //
  61. // Inputs:
  62. // UV_V #UV by 2 list of UV coordinates
  63. // UV_F #F by 3 list of UV indices into UV_V
  64. IGL_INLINE void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  65. // Set the texture associated with the mesh.
  66. //
  67. // Inputs:
  68. // R width by height image matrix of red channel
  69. // G width by height image matrix of green channel
  70. // B width by height image matrix of blue channel
  71. //
  72. IGL_INLINE void set_texture(
  73. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  74. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  75. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B);
  76. // Set the texture associated with the mesh.
  77. //
  78. // Inputs:
  79. // R width by height image matrix of red channel
  80. // G width by height image matrix of green channel
  81. // B width by height image matrix of blue channel
  82. // A width by height image matrix of alpha channel
  83. //
  84. IGL_INLINE void set_texture(
  85. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  86. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  87. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  88. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
  89. // Set pseudo-colorable scalar data associated with the mesh.
  90. //
  91. // Inputs:
  92. // caxis_min caxis minimum bound
  93. // caxis_max caxis maximum bound
  94. // D #V|#F by 1 list of scalar values
  95. // cmap colormap type
  96. // num_steps number of intervals to discretize the colormap
  97. IGL_INLINE void set_data(
  98. const Eigen::VectorXd & D,
  99. double caxis_min,
  100. double caxis_max,
  101. igl::ColorMapType cmap = igl::COLOR_MAP_TYPE_VIRIDIS,
  102. int num_steps = 21);
  103. // Use min(D) and max(D) to set caxis.
  104. IGL_INLINE void set_data(const Eigen::VectorXd & D,
  105. igl::ColorMapType cmap = igl::COLOR_MAP_TYPE_VIRIDIS,
  106. int num_steps = 21);
  107. // Not to be confused with set_colors, this creates a _texture_ that will be
  108. // referenced to pseudocolor according to the scalar field passed to set_data.
  109. //
  110. // Inputs:
  111. // CM #CM by 3 list of colors
  112. IGL_INLINE void set_colormap(const Eigen::MatrixXd & CM);
  113. // Sets points given a list of point vertices. In constrast to `add_points`
  114. // this will (purposefully) clober existing points.
  115. //
  116. // Inputs:
  117. // P #P by 3 list of vertex positions
  118. // C #P|1 by 3 color(s)
  119. IGL_INLINE void set_points(
  120. const Eigen::MatrixXd& P,
  121. const Eigen::MatrixXd& C);
  122. IGL_INLINE void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  123. // Clear the point data
  124. IGL_INLINE void clear_points();
  125. // Sets edges given a list of edge vertices and edge indices. In constrast
  126. // to `add_edges` this will (purposefully) clober existing edges.
  127. //
  128. // Inputs:
  129. // P #P by 3 list of vertex positions
  130. // E #E by 2 list of edge indices into P
  131. // C #E|1 by 3 color(s)
  132. IGL_INLINE void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  133. // Alec: This is very confusing. Why does add_edges have a different API from
  134. // set_edges?
  135. IGL_INLINE void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  136. // Sets edges given a list of points and eminating vectors
  137. IGL_INLINE void set_edges_from_vector_field(
  138. const Eigen::MatrixXd& P,
  139. const Eigen::MatrixXd& V,
  140. const Eigen::MatrixXd& C);
  141. // Clear the edge data
  142. IGL_INLINE void clear_edges();
  143. // Sets / Adds text labels at the given positions in 3D.
  144. // Note: This requires the ImGui viewer plugin to display text labels.
  145. IGL_INLINE void add_label (const Eigen::VectorXd& P, const std::string& str);
  146. IGL_INLINE void set_labels (const Eigen::MatrixXd& P, const std::vector<std::string>& str);
  147. // Clear the label data
  148. IGL_INLINE void clear_labels ();
  149. // Computes the normals of the mesh
  150. IGL_INLINE void compute_normals();
  151. // Assigns uniform colors to all faces/vertices
  152. IGL_INLINE void uniform_colors(
  153. const Eigen::Vector3d& diffuse,
  154. const Eigen::Vector3d& ambient,
  155. const Eigen::Vector3d& specular);
  156. // Assigns uniform colors to all faces/vertices
  157. IGL_INLINE void uniform_colors(
  158. const Eigen::Vector4d& ambient,
  159. const Eigen::Vector4d& diffuse,
  160. const Eigen::Vector4d& specular);
  161. // Generate a normal image matcap
  162. IGL_INLINE void normal_matcap();
  163. // Generates a default grid texture (without uvs)
  164. IGL_INLINE void grid_texture();
  165. // Copy visualization options from one viewport to another
  166. IGL_INLINE void copy_options(const ViewerCore &from, const ViewerCore &to);
  167. Eigen::MatrixXd V; // Vertices of the current mesh (#V x 3)
  168. Eigen::MatrixXi F; // Faces of the mesh (#F x 3)
  169. // Per face attributes
  170. Eigen::MatrixXd F_normals; // One normal per face
  171. Eigen::MatrixXd F_material_ambient; // Per face ambient color
  172. Eigen::MatrixXd F_material_diffuse; // Per face diffuse color
  173. Eigen::MatrixXd F_material_specular; // Per face specular color
  174. // Per vertex attributes
  175. Eigen::MatrixXd V_normals; // One normal per vertex
  176. Eigen::MatrixXd V_material_ambient; // Per vertex ambient color
  177. Eigen::MatrixXd V_material_diffuse; // Per vertex diffuse color
  178. Eigen::MatrixXd V_material_specular; // Per vertex specular color
  179. // UV parametrization
  180. Eigen::MatrixXd V_uv; // UV vertices
  181. Eigen::MatrixXi F_uv; // optional faces for UVs
  182. // Texture
  183. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_R;
  184. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_G;
  185. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_B;
  186. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_A;
  187. // Overlays
  188. // Lines plotted over the scene
  189. // (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),
  190. // 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
  191. Eigen::MatrixXd lines;
  192. // Points plotted over the scene
  193. // (Every row contains 6 doubles in the following format P_x, P_y, P_z, C_r, C_g, C_b),
  194. // with P the position in global coordinates of the center of the point, and C the color in floating point rgb format
  195. Eigen::MatrixXd points;
  196. // Text labels plotted over the scene
  197. // Textp contains, in the i-th row, the position in global coordinates where the i-th label should be anchored
  198. // Texts contains in the i-th position the text of the i-th label
  199. Eigen::MatrixXd vertex_labels_positions;
  200. Eigen::MatrixXd face_labels_positions;
  201. Eigen::MatrixXd labels_positions;
  202. std::vector<std::string> vertex_labels_strings;
  203. std::vector<std::string> face_labels_strings;
  204. std::vector<std::string> labels_strings;
  205. // Marks dirty buffers that need to be uploaded to OpenGL
  206. uint32_t dirty;
  207. // Enable per-face or per-vertex properties
  208. bool face_based;
  209. // Enable double-sided lighting on faces
  210. bool double_sided;
  211. // Invert mesh normals
  212. bool invert_normals;
  213. // Visualization options
  214. // Each option is a binary mask specifying on which viewport each option is set.
  215. // When using a single viewport, standard boolean can still be used for simplicity.
  216. unsigned int is_visible;
  217. unsigned int show_overlay;
  218. unsigned int show_overlay_depth;
  219. unsigned int show_texture;
  220. unsigned int use_matcap;
  221. unsigned int show_faces;
  222. unsigned int show_lines;
  223. unsigned int show_vertex_labels;
  224. unsigned int show_face_labels;
  225. unsigned int show_custom_labels;
  226. // Point size / line width
  227. float point_size;
  228. // line_width is NOT SUPPORTED on Mac OS and Windows
  229. float line_width;
  230. float label_size;
  231. Eigen::Matrix<float, 4, 1, Eigen::DontAlign> line_color;
  232. Eigen::Matrix<float, 4, 1, Eigen::DontAlign> label_color;
  233. // Shape material
  234. float shininess;
  235. // Unique identifier
  236. int id;
  237. // OpenGL representation of the mesh
  238. igl::opengl::MeshGL meshgl;
  239. // Update contents from a 'Data' instance
  240. IGL_INLINE void update_labels(
  241. igl::opengl::MeshGL& meshgl,
  242. igl::opengl::MeshGL::TextGL& GL_labels,
  243. const Eigen::MatrixXd& positions,
  244. const std::vector<std::string>& strings
  245. );
  246. IGL_INLINE void updateGL(
  247. const igl::opengl::ViewerData& data,
  248. const bool invert_normals,
  249. igl::opengl::MeshGL& meshgl);
  250. };
  251. } // namespace opengl
  252. } // namespace igl
  253. ////////////////////////////////////////////////////////////////////////////////
  254. #include <igl/serialize.h>
  255. namespace igl
  256. {
  257. namespace serialization
  258. {
  259. inline void serialization(bool s, igl::opengl::ViewerData& obj, std::vector<char>& buffer)
  260. {
  261. SERIALIZE_MEMBER(V);
  262. SERIALIZE_MEMBER(F);
  263. SERIALIZE_MEMBER(F_normals);
  264. SERIALIZE_MEMBER(F_material_ambient);
  265. SERIALIZE_MEMBER(F_material_diffuse);
  266. SERIALIZE_MEMBER(F_material_specular);
  267. SERIALIZE_MEMBER(V_normals);
  268. SERIALIZE_MEMBER(V_material_ambient);
  269. SERIALIZE_MEMBER(V_material_diffuse);
  270. SERIALIZE_MEMBER(V_material_specular);
  271. SERIALIZE_MEMBER(V_uv);
  272. SERIALIZE_MEMBER(F_uv);
  273. SERIALIZE_MEMBER(texture_R);
  274. SERIALIZE_MEMBER(texture_G);
  275. SERIALIZE_MEMBER(texture_B);
  276. SERIALIZE_MEMBER(texture_A);
  277. SERIALIZE_MEMBER(lines);
  278. SERIALIZE_MEMBER(points);
  279. SERIALIZE_MEMBER(labels_positions);
  280. SERIALIZE_MEMBER(labels_strings);
  281. SERIALIZE_MEMBER(dirty);
  282. SERIALIZE_MEMBER(face_based);
  283. SERIALIZE_MEMBER(show_faces);
  284. SERIALIZE_MEMBER(show_lines);
  285. SERIALIZE_MEMBER(invert_normals);
  286. SERIALIZE_MEMBER(show_overlay);
  287. SERIALIZE_MEMBER(show_overlay_depth);
  288. SERIALIZE_MEMBER(show_vertex_labels);
  289. SERIALIZE_MEMBER(show_face_labels);
  290. SERIALIZE_MEMBER(show_custom_labels);
  291. SERIALIZE_MEMBER(show_texture);
  292. SERIALIZE_MEMBER(double_sided);
  293. SERIALIZE_MEMBER(point_size);
  294. SERIALIZE_MEMBER(line_width);
  295. SERIALIZE_MEMBER(line_color);
  296. SERIALIZE_MEMBER(shininess);
  297. SERIALIZE_MEMBER(id);
  298. }
  299. template<>
  300. inline void serialize(const igl::opengl::ViewerData& obj, std::vector<char>& buffer)
  301. {
  302. serialization(true, const_cast<igl::opengl::ViewerData&>(obj), buffer);
  303. }
  304. template<>
  305. inline void deserialize(igl::opengl::ViewerData& obj, const std::vector<char>& buffer)
  306. {
  307. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  308. obj.dirty = igl::opengl::MeshGL::DIRTY_ALL;
  309. }
  310. }
  311. }
  312. #ifndef IGL_STATIC_LIBRARY
  313. # include "ViewerData.cpp"
  314. #endif
  315. #endif