Tulip  5.6.0
Large graphs analysis and drawing
graph_display/graph_display.cpp

This example gives a minimalistic approach of importing a graph and displaying it with Tulip OpenGL rendering engine In this example, we are making a standalone program that will load a graph file passed as argument to the program and display it into a new window.

The code contains a step-by-step explanation of the process, please refer to classes documentation for more information about their usage.

#include <tulip/PluginLoaderTxt.h>
#include <tulip/PluginLibraryLoader.h>
#include <tulip/GlMainWidget.h>
#include <tulip/MouseInteractors.h>
#include <tulip/TlpQtTools.h>
#include <tulip/LayoutProperty.h>
#include <tulip/SizeProperty.h>
#include <tulip/StringProperty.h>
#include <tulip/DoubleProperty.h>
#include <tulip/IntegerProperty.h>
#include <tulip/TulipViewSettings.h>
#include <tulip/GlGraphComposite.h>
#include <tulip/GlGraphRenderingParameters.h>
#include <QApplication>
#include <QString>
#include <iostream>
using namespace tlp;
using namespace std;
void addChildren(Graph *graph, node root, int depth, int degree) {
if (depth > 0) {
for (int i = 0; i < degree; ++i) {
node child = graph->addNode();
graph->addEdge(root, child);
addChildren(graph, child, depth - 1, degree);
}
}
}
Graph *createCompleteTree(int depth, int degree) {
Graph *graph = newGraph();
node root = graph->addNode();
addChildren(graph, root, depth, degree);
return graph;
}
// That function sets some visual properties on a complete tree whose depth equals 5
void setTreeVisualProperties(Graph *tree) {
// First compute a layout, we use the Bubble Tree algorithm
LayoutProperty *viewLayout = tree->getProperty<LayoutProperty>("viewLayout");
std::string errMsg;
tree->applyPropertyAlgorithm("Bubble Tree", viewLayout, errMsg);
// Then apply Auto Sizing on the nodes
SizeProperty *viewSize = tree->getProperty<SizeProperty>("viewSize");
tree->applyPropertyAlgorithm("Auto Sizing", viewSize, errMsg);
// Labels the node with their id
StringProperty *viewLabel = tree->getProperty<StringProperty>("viewLabel");
for (auto n : tree->nodes()) {
viewLabel->setNodeValue(n, QStringToTlpString(QString::number(n.id)));
}
// Add a border to the nodes, keep the default color who is black
DoubleProperty *viewBorderWidth = tree->getProperty<DoubleProperty>("viewBorderWidth");
viewBorderWidth->setAllNodeValue(1);
// Build some maps to set shapes and colors according to the dag level of a node
std::vector<int> glyphsMap;
glyphsMap.push_back(tlp::NodeShape::Square);
glyphsMap.push_back(tlp::NodeShape::Circle);
glyphsMap.push_back(tlp::NodeShape::RoundedBox);
glyphsMap.push_back(tlp::NodeShape::Hexagon);
glyphsMap.push_back(tlp::NodeShape::Star);
glyphsMap.push_back(tlp::NodeShape::Ring);
std::vector<Color> colorsMap;
colorsMap.push_back(Color::Red);
colorsMap.push_back(Color::Azure);
colorsMap.push_back(Color::Lemon);
colorsMap.push_back(Color::SpringGreen);
colorsMap.push_back(Color::Apricot);
colorsMap.push_back(Color::Magenta);
// Compute the Dag Level metric, the value of each node will correspond
// to their layer id in the tree
DoubleProperty dagLevel(tree);
tree->applyPropertyAlgorithm("Dag Level", &dagLevel, errMsg);
// Sets different shapes and colors for each layer of the tree
IntegerProperty *viewShape = tree->getProperty<IntegerProperty>("viewShape");
ColorProperty *viewColor = tree->getProperty<ColorProperty>("viewColor");
for (auto n : tree->nodes()) {
viewShape->setNodeValue(n, glyphsMap[int(dagLevel.getNodeValue(n))]);
viewColor->setNodeValue(n, colorsMap[int(dagLevel.getNodeValue(n))]);
}
}
// That function sets some rendering parameters on the graph to visualize
void setGraphRenderingParameters(GlGraphComposite *glGraphComposite) {
GlGraphRenderingParameters *renderingParameters =
glGraphComposite->getRenderingParametersPointer();
// Activate the display of edge extremities (arrows by default)
renderingParameters->setViewArrow(true);
// No color interpolation for the edges
renderingParameters->setEdgeColorInterpolate(false);
// Size interpolation for the edges
renderingParameters->setEdgeSizeInterpolate(true);
// Scale labels to node sizes
renderingParameters->setLabelScaled(true);
}
int main(int argc, char **argv) {
// A QApplication must always be declared at the beginning of the main function if you intend to
// use the tulip-gui library
// This must be done before calling tlp::initTulipSoftware()
QApplication app(argc, argv);
// Initialize the library and load all plugins
Graph *g = nullptr;
if (QApplication::arguments().size() == 2) {
// Load the file passed as first argument into a graph.
// This method will select the default Tulip algorithm plugin (TLP)
QString filename = QApplication::arguments()[1];
if (!((filename.endsWith(".tlp")) || (filename.endsWith(".tlp.gz")))) {
cout << "File " << QStringToTlpString(filename)
<< " not compatible. Use a tlp file or a tlp.gz file" << endl;
exit(EXIT_FAILURE);
}
} else {
// If no arguments were given to the command, create a complete tree of depth 5
// and degree 2 for demo purpose
g = createCompleteTree(5, 2);
// Set some visual properties in order to visualize the tree
setTreeVisualProperties(g);
}
// Creates the main widget that will display our graph
GlMainWidget *mainWidget = new GlMainWidget(nullptr);
// Adds a layer to the scene
GlLayer *mainLayer = mainWidget->getScene()->createLayer("Main");
// Adds the graph to this layer
mainLayer->addGraph(g, "graph");
// Sets some rendering parameters on the graph to visualize
setGraphRenderingParameters(mainWidget->getScene()->getGlGraphComposite());
// Display the widget
mainWidget->show();
// Flush event loop in order to let paint events pass through in order for the scene to be
// initialized.
QApplication::processEvents();
// Center the camera and draw the graph
mainWidget->centerScene();
mainWidget->draw();
// Adds Zoom and pan navigation to the widget
mainWidget->installEventFilter(new MouseNKeysNavigator);
return app.exec();
}
tlp::GlLayer
A GlLayer is like an 2D drawing software layer system.
Definition: GlLayer.h:54
tlp::loadGraph
Graph * loadGraph(const std::string &filename, tlp::PluginProgress *progress=nullptr)
Loads a graph from a file (extension can be any of the Tulip supported input graph file format).
tlp::GlLayer::addGraph
void addGraph(tlp::Graph *graph, const std::string &name)
A convenient function that adds a graph to the layer.
tlp::LayoutProperty
A graph property that maps a tlp::Coord value to graph nodes and std::vector<tlp::Coord> for edges.
Definition: LayoutProperty.h:50
tlp::IntegerProperty
A graph property that maps an integer value to graph elements.
Definition: IntegerProperty.h:40
tlp::Graph
Definition: Graph.h:234
tlp::StringProperty
A graph property that maps a std::string value to graph elements.
Definition: StringProperty.h:36
tlp::initTulipSoftware
void initTulipSoftware(PluginLoader *loader=nullptr, bool removeDiscardedPlugins=false)
Sets up environment when creating an executable using Tulip libraries This method performs basic oper...
tlp::newGraph
Graph * newGraph()
Creates a new, empty graph.
tlp::Graph::nodes
virtual const std::vector< node > & nodes() const =0
Return a const reference on the vector of nodes of the graph It is the fastest way to access to nodes...
tlp::GlScene::getGlGraphComposite
GlGraphComposite * getGlGraphComposite()
Return the current GlGraphComposite used by the scene.
Definition: GlScene.h:463
tlp::GlGraphRenderingParameters::setEdgeColorInterpolate
void setEdgeColorInterpolate(const bool state)
Activate or deactivate interpolation of nodes colors along edge.
tlp::Graph::addEdge
virtual edge addEdge(const node source, const node target)=0
Adds a new edge in the graph This edge is also added in all the super-graph of the graph.
tlp::Graph::getProperty
virtual PropertyInterface * getProperty(const std::string &name) const =0
Gets an existing property. In DEBUG mode an assertion checks the existence of the property.
tlp::GlGraphRenderingParameters::setLabelScaled
void setLabelScaled(bool state)
tlp::ColorProperty
A graph property that maps a tlp::Color value to graph elements.
Definition: ColorProperty.h:36
tlp::SizeProperty
A graph property that maps a tlp::Size value to graph elements.
Definition: SizeProperty.h:39
tlp::GlMainWidget::getScene
tlp::GlScene * getScene()
Get the GlScene of this GlMainWidget You have to add yours GlLayer and GlEntity to this GlScene At th...
Definition: GlMainWidget.h:99
tlp::QStringToTlpString
std::string QStringToTlpString(const QString &toConvert)
Convert a QString to a Tulip UTF-8 encoded std::string.
Definition: TlpQtTools.h:49
tlp::AbstractProperty::setNodeValue
virtual void setNodeValue(const node n, typename tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue v)
Sets the value of a node and notify the observers of a modification.
Definition: AbstractProperty.cxx:96
tlp::DoubleProperty
A graph property that maps a double value to graph elements.
Definition: DoubleProperty.h:39
tlp::GlMainWidget::draw
void draw(bool graphChanged=true)
tlp::GlGraphComposite::getRenderingParametersPointer
GlGraphRenderingParameters * getRenderingParametersPointer()
Return a pointer on rendering parameters used for rendering.
tlp::Graph::applyPropertyAlgorithm
bool applyPropertyAlgorithm(const std::string &algorithm, PropertyInterface *result, std::string &errorMessage, DataSet *parameters=nullptr, PluginProgress *progress=nullptr)
Runs a plugin on the graph, whose result is a property.
tlp::GlGraphRenderingParameters::setViewArrow
void setViewArrow(const bool state)
Activate or deactivate displaying of arrows along the graph's edges.
tlp::GlGraphRenderingParameters::setEdgeSizeInterpolate
void setEdgeSizeInterpolate(const bool state)
Activate or deactivate interpolation of nodes colors along edge.
tlp
Definition: AbstractProperty.h:34
tlp::Graph::addNode
virtual node addNode()=0
Adds a new node in the graph and returns it. This node is also added in all the ancestor graphs.
tlp::GlGraphComposite
Class use to visualize graph in OpenGL Tulip engine.
Definition: GlGraphComposite.h:51
tlp::GlScene::createLayer
GlLayer * createLayer(const std::string &name)
Create a layer with the given name in the scene This layer is added to the layers list Now the scene ...
tlp::GlMainWidget
This widget provide a simple system to visualize data/graph with OpenGL 3D engine.
Definition: GlMainWidget.h:63
tlp::node
The node struct represents a node in a Graph object.
Definition: Node.h:40
tlp::GlMainWidget::centerScene
void centerScene(bool graphChanged=false, float zoomFactor=1.0)
Convenience function that calls center function on the current scene, applies a zoom (if needed) and ...
tlp::GlGraphRenderingParameters
That class defines all the parameters used by GlGraphComposite to render a graph.
Definition: GlGraphRenderingParameters.h:38