Loading...
Searching...
No Matches
X3DOM.h
// Copyright (C) 2016 Quang T. Ha, Chris Richardson and Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#ifndef __DOLFIN_X3DOM_H
#define __DOLFIN_X3DOM_H
#include <array>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <boost/multi_array.hpp>
#include <dolfin/geometry/Point.h>
// TODO:
// - Can we order vertices so that attribute 'solid' can be set to true?
// - Add ambient intensity parameter
// - Test and add more sanity checks for Functions
// - Add support for GenericFunction
// - Add support for MeshFunctions
// - Add support for DG1 Functions
// - Add vector support (arrows) - including correct placement for RT0
// etc. - advanced
// - Document all class methods below properly
namespace pugi
{
class xml_node;
class xml_document;
}
namespace dolfin
{
class X3DOMParameters
{
// Developer note: X3DOMParameters is declared outside the X3DOM
// class because SWIG cannot wrap nested classes.
public:
enum class Representation {surface, surface_with_edges, wireframe};
X3DOMParameters();
void set_representation(Representation representation);
Representation get_representation() const;
std::array<double, 2> get_viewport_size() const;
void set_diffuse_color(std::array<double, 3> rgb);
std::array<double, 3> get_diffuse_color() const;
void set_emissive_color(std::array<double, 3> rgb);
std::array<double, 3> get_emissive_color() const;
void set_specular_color(std::array<double, 3> rgb);
std::array<double, 3> get_specular_color() const;
void set_background_color(std::array<double, 3> rgb);
std::array<double, 3> get_background_color() const;
void set_ambient_intensity(double intensity);
double get_ambient_intensity() const;
void set_shininess(double shininess);
double get_shininess() const;
void set_transparency(double transparency);
double get_transparency() const;
void set_color_map(const std::vector<double>& color_data);
std::vector<double> get_color_map() const;
boost::multi_array<float, 2> get_color_map_array() const;
void set_x3d_stats(bool show);
bool get_x3d_stats() const;
void set_menu_display(bool show);
bool get_menu_display() const;
private:
// Check that RGB colors are valid. Throws error if value is not
// invalid.
static void check_rgb(std::array<double, 3>& rgb);
// Check that value is valid. Throws error if value is not
// invalid.
static void check_value_range(double value, double lower, double upper);
// Surface, surface with edges or wireframe
Representation _representation;
// Dimensions of viewing area
std::array<double, 2> _size;
// TODO: document
// RGB colours, see http://doc.x3dom.org/author/Shape/Material.html
std::array<double, 3> _diffuse_color, _emissive_color, _specular_color,
_background_color;
// TODO: document
double _ambient_intensity, _shininess, _transparency;
// RGB color map (256 values)
boost::multi_array<float, 2> _color_map;
// Turn X3D stats on/off
bool _show_x3d_stats;
// Turn menu on/off
bool _menu_display;
};
// Forward declarations
class Function;
class Mesh;
class Point;
//
// Developer note: pugixml is used to created X3DOM and HTML5. By
// using pugixml, we produce valid XML, but care must be taken that
// the XML is also valid HTML. This includes not letting pugixml
// create self-closing elements, in cases. E.g., <foo
// bar="foobar"></foo> is fine, but the self-closing syntax <foo
// bar="foobar" /> while being valid XML is is not valid HTML5. See
// https://github.com/x3dom/x3dom/issues/600.
//
class X3DOM
{
public:
static std::string str(const Mesh& mesh,
X3DOMParameters parameters=X3DOMParameters());
static std::string str(const Function& u,
X3DOMParameters parameters=X3DOMParameters());
static std::string html(const Mesh& mesh,
X3DOMParameters parameters=X3DOMParameters());
static std::string html(const Function& u,
X3DOMParameters parameters=X3DOMParameters());
static void
build_x3dom_tree(pugi::xml_document& xml_doc,
const Mesh& mesh,
const X3DOMParameters& parameters=X3DOMParameters());
static void
build_x3dom_tree(pugi::xml_document& xml_doc,
const Function& u,
const X3DOMParameters& parameters=X3DOMParameters());
private:
// FIXME: This should be a C++11 style enum (enum Viewpoint class
// {...};), but a Swig bug needs fixing
// (https://github.com/swig/swig/issues/594)
enum Viewpoint {top, bottom, left, right, back, front, default_view};
// Build X3DOM pugixml tree
static void x3dom(pugi::xml_document& xml_doc, const Mesh& mesh,
const std::vector<double>& vertex_values,
const std::vector<double>& facet_values,
const X3DOMParameters& parameters);
// Build HTML pugixml tree
static void html(pugi::xml_document& xml_doc, const Mesh& mesh,
const std::vector<double>& vertex_values,
const std::vector<double>& facet_values,
const X3DOMParameters& parameters);
// Add HTML preamble (HTML) to XML doc and return 'html' node
static pugi::xml_node add_html_preamble(pugi::xml_node& xml_node);
// Add X3D doctype (an XML document should have no more than one
// doc_type node)
static void add_x3dom_doctype(pugi::xml_node& xml_node);
// Add HTML doctype (an XML document should have no more than one
// doc_type node)
static void add_html_doctype(pugi::xml_node& xml_node);
// Add X3D node and attributes, and return handle to node (x3D)
static pugi::xml_node add_x3d_node(pugi::xml_node& xml_node,
std::array<double, 2> size,
bool show_stats);
// Add X3DOM Mesh data to XML node (X3D)
static void add_x3dom_data(pugi::xml_node& xml_node, const Mesh& mesh,
const std::vector<double>& vertex_values,
const std::vector<double>& facet_values,
const X3DOMParameters& parameters);
// Add mesh topology and geometry to XML. 'surface' flag controls
// surface vs wireframe representation (X3D)
static void add_mesh_data(pugi::xml_node& xml_node, const Mesh& mesh,
const std::vector<double>& vertex_values,
const std::vector<double>& facet_values,
const X3DOMParameters& parameters,
bool surface);
// Add a collection viewpoint nodes (X3D)
static void add_viewpoint_nodes(pugi::xml_node& xml_scene,
const Point p, double d,
bool show_viewpoint_buttons);
// Add a single viewpoint node (X3D)
static void add_viewpoint_node(pugi::xml_node& xml_scene,
Viewpoint viewpoint,
const Point p,
const double s);
// Add the menu display and the desired subsections
static void add_menu_display(pugi::xml_node& xml_node, const Mesh& mesh,
const X3DOMParameters& parameters);
// Add the button for a tab to be added to the menu
static void add_menu_tab_button(pugi::xml_node& xml_node, std::string name,
bool checked);
// Create a generic content node to hold specific menu content
static pugi::xml_node create_menu_content_node(pugi::xml_node& xml_node,
std::string name, bool show);
// Add the options tab in the menu display
static void add_menu_options_tab(pugi::xml_node& xml_node);
// Add an option to the options tab in menu display
static void add_menu_options_option(pugi::xml_node& xml_node,
std::string name);
// Add the summary tab in the menu display
static void add_menu_summary_tab(pugi::xml_node& xml_node,
const Mesh& mesh);
// Add the color tab in the menu display
static void add_menu_color_tab(pugi::xml_node& xml_node);
// Add the warp tab in the menu display
static void add_menu_warp_tab(pugi::xml_node& xml_node);
// Add the viewpoint tab in the menu display
static void add_menu_viewpoint_tab(pugi::xml_node& xml_node);
// Add a viewpoint button to the appropriate parent
static void add_menu_viewpoint_button(pugi::xml_node& xml_node,
std::string name);
// Get centre point of mesh bounds, and a reasonable viewpoint
// distance from it
static std::pair<Point, double> mesh_centre_and_distance(const Mesh& mesh);
// Get list of vertex indices which are on surface
static std::set<int> surface_vertex_indices(const Mesh& mesh);
// Get the values of a function at vertices, (or on facets for P0)
static void get_function_values(const Function& u,
std::vector<double>& vertex_values,
std::vector<double>& facet_values);
// Build topology and geometry data from a Mesh ready for X3DOM
// output
static void build_mesh_data(std::vector<int>& topology,
std::vector<double>& geometry,
std::vector<double>& value_data,
const Mesh& mesh,
const std::vector<double>& vertex_values,
const std::vector<double>& facet_values,
bool surface);
// Return "x[0] x[1] x[2]" string from array of color RGB
static std::string array_to_string3(std::array<double, 3> x);
// Utility to convert pugi::xml_document into a std::string
static std::string to_string(pugi::xml_document& xml_doc,
unsigned int flags);
};
}
#endif
dolfin::Function
Definition Function.h:66
dolfin::Mesh
Definition Mesh.h:84
dolfin::Point
Definition Point.h:41
dolfin::X3DOMParameters
Class data to store X3DOM view parameters.
Definition X3DOM.h:52
dolfin::X3DOMParameters::get_emissive_color
std::array< double, 3 > get_emissive_color() const
Get the RGB emissive color.
Definition X3DOM.cpp:111
dolfin::X3DOMParameters::get_viewport_size
std::array< double, 2 > get_viewport_size() const
Get the size of the viewport.
Definition X3DOM.cpp:89
dolfin::X3DOMParameters::Representation
Representation
X3DOM representation type.
Definition X3DOM.h:59
dolfin::X3DOMParameters::set_background_color
void set_background_color(std::array< double, 3 > rgb)
Set background RGB color.
Definition X3DOM.cpp:127
dolfin::X3DOMParameters::set_diffuse_color
void set_diffuse_color(std::array< double, 3 > rgb)
Set the RGB color of the object.
Definition X3DOM.cpp:94
dolfin::X3DOMParameters::get_specular_color
std::array< double, 3 > get_specular_color() const
Get the RGB specular color.
Definition X3DOM.cpp:122
dolfin::X3DOMParameters::set_x3d_stats
void set_x3d_stats(bool show)
Turn X3D 'statistics' window on/off.
Definition X3DOM.cpp:193
dolfin::X3DOMParameters::get_ambient_intensity
double get_ambient_intensity() const
Get the ambient lighting intensity.
Definition X3DOM.cpp:144
dolfin::X3DOMParameters::get_menu_display
bool get_menu_display() const
Get the menu display state.
Definition X3DOM.cpp:208
dolfin::X3DOMParameters::set_representation
void set_representation(Representation representation)
Definition X3DOM.cpp:79
dolfin::X3DOMParameters::set_shininess
void set_shininess(double shininess)
Set the surface shininess of the object.
Definition X3DOM.cpp:149
dolfin::X3DOMParameters::set_transparency
void set_transparency(double transparency)
Set the transparency (0-1).
Definition X3DOM.cpp:160
dolfin::X3DOMParameters::set_ambient_intensity
void set_ambient_intensity(double intensity)
Set the ambient lighting intensity.
Definition X3DOM.cpp:138
dolfin::X3DOMParameters::set_color_map
void set_color_map(const std::vector< double > &color_data)
Definition X3DOM.cpp:171
dolfin::X3DOMParameters::set_specular_color
void set_specular_color(std::array< double, 3 > rgb)
Set the RGB specular color.
Definition X3DOM.cpp:116
dolfin::X3DOMParameters::get_x3d_stats
bool get_x3d_stats() const
Get the state of the 'statistics' window.
Definition X3DOM.cpp:198
dolfin::X3DOMParameters::get_diffuse_color
std::array< double, 3 > get_diffuse_color() const
Get the RGB diffuse color of the object.
Definition X3DOM.cpp:100
dolfin::X3DOMParameters::get_transparency
double get_transparency() const
Get the transparency (0-1).
Definition X3DOM.cpp:166
dolfin::X3DOMParameters::set_emissive_color
void set_emissive_color(std::array< double, 3 > rgb)
Set the RGB emissive color.
Definition X3DOM.cpp:105
dolfin::X3DOMParameters::get_shininess
double get_shininess() const
Set the surface shininess of the object.
Definition X3DOM.cpp:155
dolfin::X3DOMParameters::get_background_color
std::array< double, 3 > get_background_color() const
Get background RGB color.
Definition X3DOM.cpp:133
dolfin::X3DOMParameters::X3DOMParameters
X3DOMParameters()
Constructor (with default parameter settings).
Definition X3DOM.cpp:62
dolfin::X3DOMParameters::get_color_map_array
boost::multi_array< float, 2 > get_color_map_array() const
Get the color map as a boost::multi_array (256x3).
Definition X3DOM.cpp:188
dolfin::X3DOMParameters::get_color_map
std::vector< double > get_color_map() const
Definition X3DOM.cpp:178
dolfin::X3DOMParameters::set_menu_display
void set_menu_display(bool show)
Toggle menu option.
Definition X3DOM.cpp:203
dolfin::X3DOMParameters::get_representation
Representation get_representation() const
Definition X3DOM.cpp:84
dolfin::X3DOM
Definition X3DOM.h:194
dolfin::X3DOM::build_x3dom_tree
static void build_x3dom_tree(pugi::xml_document &xml_doc, const Mesh &mesh, const X3DOMParameters &parameters=X3DOMParameters())
Build X3DOM pugixml tree for a Mesh.
Definition X3DOM.cpp:296
dolfin::X3DOM::html
static std::string html(const Mesh &mesh, X3DOMParameters parameters=X3DOMParameters())
Return HTML5 string with embedded X3D for a Mesh.
Definition X3DOM.cpp:268
dolfin::X3DOM::str
static std::string str(const Mesh &mesh, X3DOMParameters parameters=X3DOMParameters())
Return X3D string for a Mesh.
Definition X3DOM.cpp:239
dolfin
Definition adapt.h:30
dolfin::parameters
GlobalParameters parameters
The global parameter database.
Definition GlobalParameters.cpp:32