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