ViewerData.h 15 KB

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