ViewerData.h 9.6 KB

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