ViewerData.h 15 KB

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