ViewerData.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 by 1 list of scalar values
  95. // cmap colormap type
  96. // num_steps number of intervals to discretize the colormap
  97. //
  98. // To-do: support #F by 1 per-face data
  99. IGL_INLINE void set_data(
  100. const Eigen::VectorXd & D,
  101. double caxis_min,
  102. double caxis_max,
  103. igl::ColorMapType cmap = igl::COLOR_MAP_TYPE_VIRIDIS,
  104. int num_steps = 21);
  105. // Use min(D) and max(D) to set caxis.
  106. IGL_INLINE void set_data(const Eigen::VectorXd & D,
  107. igl::ColorMapType cmap = igl::COLOR_MAP_TYPE_VIRIDIS,
  108. int num_steps = 21);
  109. // Not to be confused with set_colors, this creates a _texture_ that will be
  110. // referenced to pseudocolor according to the scalar field passed to set_data.
  111. //
  112. // Inputs:
  113. // CM #CM by 3 list of colors
  114. IGL_INLINE void set_colormap(const Eigen::MatrixXd & CM);
  115. // Sets points given a list of point vertices. In constrast to `add_points`
  116. // this will (purposefully) clober existing points.
  117. //
  118. // Inputs:
  119. // P #P by 3 list of vertex positions
  120. // C #P|1 by 3 color(s)
  121. IGL_INLINE void set_points(
  122. const Eigen::MatrixXd& P,
  123. const Eigen::MatrixXd& C);
  124. IGL_INLINE void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  125. // Clear the point data
  126. IGL_INLINE void clear_points();
  127. // Sets edges given a list of edge vertices and edge indices. In constrast
  128. // to `add_edges` this will (purposefully) clober existing edges.
  129. //
  130. // Inputs:
  131. // P #P by 3 list of vertex positions
  132. // E #E by 2 list of edge indices into P
  133. // C #E|1 by 3 color(s)
  134. IGL_INLINE void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  135. // Alec: This is very confusing. Why does add_edges have a different API from
  136. // set_edges?
  137. IGL_INLINE void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  138. // Sets edges given a list of points and eminating vectors
  139. IGL_INLINE void set_edges_from_vector_field(
  140. const Eigen::MatrixXd& P,
  141. const Eigen::MatrixXd& V,
  142. const Eigen::MatrixXd& C);
  143. // Clear the edge data
  144. IGL_INLINE void clear_edges();
  145. // Sets / Adds text labels at the given positions in 3D.
  146. // Note: This requires the ImGui viewer plugin to display text labels.
  147. IGL_INLINE void add_label (const Eigen::VectorXd& P, const std::string& str);
  148. IGL_INLINE void set_labels (const Eigen::MatrixXd& P, const std::vector<std::string>& str);
  149. // Clear the label data
  150. IGL_INLINE void clear_labels ();
  151. // Computes the normals of the mesh
  152. IGL_INLINE void compute_normals();
  153. // Assigns uniform colors to all faces/vertices
  154. IGL_INLINE void uniform_colors(
  155. const Eigen::Vector3d& diffuse,
  156. const Eigen::Vector3d& ambient,
  157. const Eigen::Vector3d& specular);
  158. // Assigns uniform colors to all faces/vertices
  159. IGL_INLINE void uniform_colors(
  160. const Eigen::Vector4d& ambient,
  161. const Eigen::Vector4d& diffuse,
  162. const Eigen::Vector4d& specular);
  163. // Generate a normal image matcap
  164. IGL_INLINE void normal_matcap();
  165. // Generates a default grid texture (without uvs)
  166. IGL_INLINE void grid_texture();
  167. // Copy visualization options from one viewport to another
  168. IGL_INLINE void copy_options(const ViewerCore &from, const ViewerCore &to);
  169. Eigen::MatrixXd V; // Vertices of the current mesh (#V x 3)
  170. Eigen::MatrixXi F; // Faces of the mesh (#F x 3)
  171. // Per face attributes
  172. Eigen::MatrixXd F_normals; // One normal per face
  173. Eigen::MatrixXd F_material_ambient; // Per face ambient color
  174. Eigen::MatrixXd F_material_diffuse; // Per face diffuse color
  175. Eigen::MatrixXd F_material_specular; // Per face specular color
  176. // Per vertex attributes
  177. Eigen::MatrixXd V_normals; // One normal per vertex
  178. Eigen::MatrixXd V_material_ambient; // Per vertex ambient color
  179. Eigen::MatrixXd V_material_diffuse; // Per vertex diffuse color
  180. Eigen::MatrixXd V_material_specular; // Per vertex specular color
  181. // UV parametrization
  182. Eigen::MatrixXd V_uv; // UV vertices
  183. Eigen::MatrixXi F_uv; // optional faces for UVs
  184. // Texture
  185. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_R;
  186. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_G;
  187. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_B;
  188. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> texture_A;
  189. // Overlays
  190. // Lines plotted over the scene
  191. // (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),
  192. // 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
  193. Eigen::MatrixXd lines;
  194. // Points plotted over the scene
  195. // (Every row contains 6 doubles in the following format P_x, P_y, P_z, C_r, C_g, C_b),
  196. // with P the position in global coordinates of the center of the point, and C the color in floating point rgb format
  197. Eigen::MatrixXd points;
  198. // Text labels plotted over the scene
  199. // Textp contains, in the i-th row, the position in global coordinates where the i-th label should be anchored
  200. // Texts contains in the i-th position the text of the i-th label
  201. Eigen::MatrixXd vertex_labels_positions;
  202. Eigen::MatrixXd face_labels_positions;
  203. Eigen::MatrixXd labels_positions;
  204. std::vector<std::string> vertex_labels_strings;
  205. std::vector<std::string> face_labels_strings;
  206. std::vector<std::string> labels_strings;
  207. // Marks dirty buffers that need to be uploaded to OpenGL
  208. uint32_t dirty;
  209. // Enable per-face or per-vertex properties
  210. bool face_based;
  211. // Enable double-sided lighting on faces
  212. bool double_sided;
  213. // Invert mesh normals
  214. bool invert_normals;
  215. // Visualization options
  216. // Each option is a binary mask specifying on which viewport each option is set.
  217. // When using a single viewport, standard boolean can still be used for simplicity.
  218. unsigned int is_visible;
  219. unsigned int show_overlay;
  220. unsigned int show_overlay_depth;
  221. unsigned int show_texture;
  222. unsigned int use_matcap;
  223. unsigned int show_faces;
  224. unsigned int show_lines;
  225. unsigned int show_vertex_labels;
  226. unsigned int show_face_labels;
  227. unsigned int show_custom_labels;
  228. // Point size / line width
  229. float point_size;
  230. // line_width is NOT SUPPORTED on Mac OS and Windows
  231. float line_width;
  232. float label_size;
  233. Eigen::Matrix<float, 4, 1, Eigen::DontAlign> line_color;
  234. Eigen::Matrix<float, 4, 1, Eigen::DontAlign> label_color;
  235. // Shape material
  236. float shininess;
  237. // Unique identifier
  238. int id;
  239. // OpenGL representation of the mesh
  240. igl::opengl::MeshGL meshgl;
  241. // Update contents from a 'Data' instance
  242. IGL_INLINE void update_labels(
  243. igl::opengl::MeshGL& meshgl,
  244. igl::opengl::MeshGL::TextGL& GL_labels,
  245. const Eigen::MatrixXd& positions,
  246. const std::vector<std::string>& strings
  247. );
  248. IGL_INLINE void updateGL(
  249. const igl::opengl::ViewerData& data,
  250. const bool invert_normals,
  251. igl::opengl::MeshGL& meshgl);
  252. };
  253. } // namespace opengl
  254. } // namespace igl
  255. ////////////////////////////////////////////////////////////////////////////////
  256. #include <igl/serialize.h>
  257. namespace igl
  258. {
  259. namespace serialization
  260. {
  261. inline void serialization(bool s, igl::opengl::ViewerData& obj, std::vector<char>& buffer)
  262. {
  263. SERIALIZE_MEMBER(V);
  264. SERIALIZE_MEMBER(F);
  265. SERIALIZE_MEMBER(F_normals);
  266. SERIALIZE_MEMBER(F_material_ambient);
  267. SERIALIZE_MEMBER(F_material_diffuse);
  268. SERIALIZE_MEMBER(F_material_specular);
  269. SERIALIZE_MEMBER(V_normals);
  270. SERIALIZE_MEMBER(V_material_ambient);
  271. SERIALIZE_MEMBER(V_material_diffuse);
  272. SERIALIZE_MEMBER(V_material_specular);
  273. SERIALIZE_MEMBER(V_uv);
  274. SERIALIZE_MEMBER(F_uv);
  275. SERIALIZE_MEMBER(texture_R);
  276. SERIALIZE_MEMBER(texture_G);
  277. SERIALIZE_MEMBER(texture_B);
  278. SERIALIZE_MEMBER(texture_A);
  279. SERIALIZE_MEMBER(lines);
  280. SERIALIZE_MEMBER(points);
  281. SERIALIZE_MEMBER(labels_positions);
  282. SERIALIZE_MEMBER(labels_strings);
  283. SERIALIZE_MEMBER(dirty);
  284. SERIALIZE_MEMBER(face_based);
  285. SERIALIZE_MEMBER(show_faces);
  286. SERIALIZE_MEMBER(show_lines);
  287. SERIALIZE_MEMBER(invert_normals);
  288. SERIALIZE_MEMBER(show_overlay);
  289. SERIALIZE_MEMBER(show_overlay_depth);
  290. SERIALIZE_MEMBER(show_vertex_labels);
  291. SERIALIZE_MEMBER(show_face_labels);
  292. SERIALIZE_MEMBER(show_custom_labels);
  293. SERIALIZE_MEMBER(show_texture);
  294. SERIALIZE_MEMBER(double_sided);
  295. SERIALIZE_MEMBER(point_size);
  296. SERIALIZE_MEMBER(line_width);
  297. SERIALIZE_MEMBER(line_color);
  298. SERIALIZE_MEMBER(shininess);
  299. SERIALIZE_MEMBER(id);
  300. }
  301. template<>
  302. inline void serialize(const igl::opengl::ViewerData& obj, std::vector<char>& buffer)
  303. {
  304. serialization(true, const_cast<igl::opengl::ViewerData&>(obj), buffer);
  305. }
  306. template<>
  307. inline void deserialize(igl::opengl::ViewerData& obj, const std::vector<char>& buffer)
  308. {
  309. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  310. obj.dirty = igl::opengl::MeshGL::DIRTY_ALL;
  311. }
  312. }
  313. }
  314. #ifndef IGL_STATIC_LIBRARY
  315. # include "ViewerData.cpp"
  316. #endif
  317. #endif