ViewerData.h 10 KB

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