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
|
/**
*
* This file is part of Tulip (www.tulip-software.org)
*
* Authors: David Auber and the Tulip development Team
* from LaBRI, University of Bordeaux
*
* Tulip 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.
*
* Tulip 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 General Public License for more details.
*
*/
#include <tulip/ForEach.h>
#include <QThread>
#include <tulip/GlOffscreenRenderer.h>
#include <tulip/Gl2DRect.h>
#include <tulip/GlOffscreenRenderer.h>
#include <tulip/GlGraphRenderingParameters.h>
#include <tulip/GlGraphComposite.h>
#include <tulip/GlLabel.h>
#include <tulip/GlProgressBar.h>
#include <tulip/GlQuantitativeAxis.h>
#include <tulip/GlMainWidget.h>
#include <tulip/GlTextureManager.h>
#include "ScatterPlot2D.h"
using namespace std;
const float DEFAULT_AXIS_LENGTH = 1000.0f;
const unsigned int DEFAULT_NB_GRADS = 15;
template <typename T>
std::string getStringFromNumber(T number) {
std::ostringstream oss;
oss.precision(5);
oss << number;
return oss.str();
}
namespace tlp {
int ScatterPlot2D::overviewCpt(0);
static void setGraphView (GlGraphComposite *glGraph, bool displayEdges) {
GlGraphRenderingParameters param = glGraph->getRenderingParameters ();
param.setAntialiasing (true);
param.setViewNodeLabel (true);
param.setFontsType (2);
param.setSelectedNodesStencil(1);
param.setNodesStencil(0xFFFF);
param.setNodesLabelStencil(0xFFFF);
param.setDisplayEdges(displayEdges);
param.setEdgesStencil(0xFFFF);
param.setSelectedEdgesStencil(1);
param.setDisplayNodes(true);
param.setDisplayMetaNodes(true);
glGraph->setRenderingParameters (param);
}
ScatterPlot2D::ScatterPlot2D(Graph *graph, const string& xDim, const string& yDim, Coord blCorner, unsigned int size, const Color &backgroundColor, const Color &foregroundColor)
: xDim(xDim), yDim(yDim), blCorner(blCorner), size(size), graph(graph), xAxis(NULL), yAxis(NULL), overviewGen(false), backgroundColor(backgroundColor),
foregroundColor(foregroundColor), mapBackgroundColorToCoeff(false), displayEdges(false) {
glGraphComposite = new GlGraphComposite(graph);
scatterLayout = new LayoutProperty(graph);
GlGraphInputData *glGraphInputData = glGraphComposite->getInputData();
glGraphInputData->setElementLayout(scatterLayout);
setGraphView(glGraphComposite, displayEdges);
backgroundRect = new GlRect(Coord(blCorner.getX(), blCorner.getY() + size), Coord(blCorner.getX() + size, blCorner.getY()), backgroundColor, backgroundColor, true, false);
addGlEntity(backgroundRect, "background rect");
clickLabel = new GlLabel(Coord(blCorner.getX() + size / 2.0f, blCorner.getY() + size / 2.0f), Size(size, size / 4.0f), foregroundColor);
clickLabel->setText("Double Click to generate overview");
addGlEntity(clickLabel, "label");
computeBoundingBox();
overviewId = overviewCpt++;
textureName = xDim + "_" + yDim + " " + getStringFromNumber(overviewId);
}
ScatterPlot2D::~ScatterPlot2D() {
clean();
delete glGraphComposite;
delete scatterLayout;
GlTextureManager::getInst().deleteTexture(textureName);
}
void ScatterPlot2D::setSizeProperty(SizeProperty *sizeProperty) {
GlGraphInputData *glGraphInputData = glGraphComposite->getInputData();
glGraphInputData->setElementSize(sizeProperty);
}
void ScatterPlot2D::setBLCorner(const Coord &blCorner) {
GlComposite::translate(blCorner - this->blCorner);
this->blCorner = blCorner;
computeBoundingBox();
}
void ScatterPlot2D::generateOverview(GlMainWidget *glWidget, LayoutProperty *reverseLayout) {
clean();
clickLabel = NULL;
backgroundRect = NULL;
createAxis();
glProgressBar = new GlProgressBar(Coord(blCorner.getX() + size / 2.0f, blCorner.getY() + size / 2.0f), size, size, Color(0,0,255));
glProgressBar->setComment("Generating overview ...");
addGlEntity(glProgressBar, "progress bar");
computeScatterPlotLayout(glWidget, reverseLayout);
if (mapBackgroundColorToCoeff) {
Color startColor = zeroColor, endColor;
if (correlationCoeff < 0) {
endColor = minusOneColor;
}
else {
endColor = oneColor;
}
for (unsigned int i = 0; i < 4; ++i) {
backgroundColor[i] = static_cast<unsigned char>((double(startColor[i]) + (double(endColor[i])- double(startColor[i])) * abs(correlationCoeff)));
}
int bgV = backgroundColor.getV();
if (bgV < 128) {
foregroundColor = Color(255,255,255);
}
else {
foregroundColor = Color(0,0,0);
}
}
GlOffscreenRenderer *glOffscreenRenderer = GlOffscreenRenderer::getInstance();
glOffscreenRenderer->setViewPortSize(size, size);
glOffscreenRenderer->clearScene();
GlScene *scene = glOffscreenRenderer->getScene();
if (mapBackgroundColorToCoeff) {
GlLayer *backgroundLayer = scene->getLayer("Background");
Gl2DRect *background = new Gl2DRect(1.0f, 0.0f, 0.0f, 1.0f,"gaussian_tex_back", true);
backgroundLayer->addGlEntity(background,"background");
}
setGraphView(glGraphComposite, displayEdges);
glOffscreenRenderer->setSceneBackgroundColor(backgroundColor);
glOffscreenRenderer->addGraphCompositeToScene(glGraphComposite);
glOffscreenRenderer->addGlEntityToScene(xAxis);
glOffscreenRenderer->addGlEntityToScene(yAxis);
glOffscreenRenderer->renderScene(true);
GLuint textureId = glOffscreenRenderer->getGLTexture(true);
GlTextureManager::getInst().deleteTexture(textureName);
GlTextureManager::getInst().registerExternalTexture(textureName, textureId);
glOffscreenRenderer->clearScene();
deleteGlEntity(glProgressBar);
delete glProgressBar;
Gl2DRect *rectTextured = new Gl2DRect(blCorner.getY()+size, blCorner.getY(), blCorner.getX(), blCorner.getX() + size, textureName);
addGlEntity(rectTextured, textureName + " overview");
computeBoundingBox();
overviewGen = true;
}
void ScatterPlot2D::clean() {
delete xAxis;
xAxis = NULL;
delete yAxis;
yAxis = NULL;
reset(true);
}
void ScatterPlot2D::createAxis() {
assert(dynamic_cast<NumericProperty *>(graph->getProperty(xDim)));
assert(dynamic_cast<NumericProperty *>(graph->getProperty(yDim)));
NumericProperty* xProp = (NumericProperty *) graph->getProperty(xDim);
NumericProperty* yProp = (NumericProperty *) graph->getProperty(yDim);
xType = graph->getProperty(xDim)->getTypename();
yType = graph->getProperty(yDim)->getTypename();
double xMin, xMax, yMin, yMax;
xMin = xProp->getNodeDoubleMin(graph);
xMax = xProp->getNodeDoubleMax(graph);
yMin = yProp->getNodeDoubleMin(graph);
yMax = yProp->getNodeDoubleMax(graph);
xAxis = new GlQuantitativeAxis(xDim, Coord(0.0f, 0.0f, 0.0f), DEFAULT_AXIS_LENGTH, GlAxis::HORIZONTAL_AXIS, foregroundColor, true);
if (xType == "double") {
xAxis->setAxisParameters(xMin ,xMax, DEFAULT_NB_GRADS, GlAxis::LEFT_OR_BELOW, true);
}
else {
unsigned int step = static_cast<unsigned int>((xMax - xMin) / 20);
xAxis->setAxisParameters(static_cast<int>(xMin), static_cast<int>(xMax),
step ? step : 1, GlAxis::LEFT_OR_BELOW, true);
}
xAxis->setAxisGraduationsMaxLabelWidth(300.0f);
xAxis->addCaption(GlAxis::BELOW, 100.0f, false, 300.0f, 155.0f);
xAxis->updateAxis();
yAxis = new GlQuantitativeAxis(yDim, Coord(0.0f, 0.0f, 0.0f), DEFAULT_AXIS_LENGTH, GlAxis::VERTICAL_AXIS, foregroundColor, true);
if (yType == "double") {
yAxis->setAxisParameters(yMin ,yMax, DEFAULT_NB_GRADS, GlAxis::LEFT_OR_BELOW, true);
}
else {
unsigned int step = static_cast<unsigned int>((yMax - yMin) / 20);
yAxis->setAxisParameters(static_cast<int>(yMin) ,static_cast<int>(yMax),
step ? step : 1, GlAxis::LEFT_OR_BELOW, true);
}
yAxis->addCaption(GlAxis::LEFT, 100.0f, false, 300.0f, 155.0f);
yAxis->updateAxis();
if(xAxis->getCaptionHeight()>yAxis->getCaptionHeight())
xAxis->setCaptionHeight(yAxis->getCaptionHeight(),false);
else
yAxis->setCaptionHeight(xAxis->getCaptionHeight(),false);
}
void ScatterPlot2D::computeScatterPlotLayout(GlMainWidget *glWidget, LayoutProperty *reverseLayout) {
double sumxiyi = 0.0, sumxi = 0.0, sumyi = 0.0, sumxi2 = 0.0, sumyi2 = 0.0;
unsigned int nbGraphNodes = graph->numberOfNodes();
node n;
currentStep = 0;
maxStep = nbGraphNodes;
drawStep = maxStep / 20;
if(!drawStep)
drawStep=1;
assert(dynamic_cast<NumericProperty *>(graph->getProperty(xDim)));
assert(dynamic_cast<NumericProperty *>(graph->getProperty(yDim)));
NumericProperty* xProp = (NumericProperty *) graph->getProperty(xDim);
NumericProperty* yProp = (NumericProperty *) graph->getProperty(yDim);
forEach(n, graph->getNodes()) {
Coord nodeCoord;
double xValue, yValue;
xValue = xProp->getNodeDoubleValue(n);
sumxi += xValue;
sumxi2 += (xValue * xValue);
yValue = yProp->getNodeDoubleValue(n);
sumyi += yValue;
sumyi2 += (yValue * yValue);
sumxiyi += (xValue * yValue);
if (reverseLayout == NULL) {
Coord xValueAxisCoord = xAxis->getAxisPointCoordForValue(xValue);
Coord yValueAxisCoord = yAxis->getAxisPointCoordForValue(yValue);
nodeCoord = Coord(xValueAxisCoord.getX(), yValueAxisCoord.getY(), 0.0f);
}
else {
Coord nodeCoordReverse = reverseLayout->getNodeValue(n);
nodeCoord = Coord(nodeCoordReverse.getY(), nodeCoordReverse.getX(), 0.0f);
}
scatterLayout->setNodeValue(n, nodeCoord);
++currentStep;
if (glWidget != NULL && currentStep % drawStep == 0) {
glProgressBar->progress(currentStep, maxStep);
glWidget->draw();
}
}
double numerator = sumxiyi - (1./nbGraphNodes) * sumxi * sumyi;
double denominator = sqrt(sumxi2 - (1./nbGraphNodes) * (sumxi * sumxi)) * sqrt(sumyi2 - (1./nbGraphNodes) * (sumyi * sumyi));
if (denominator == 0) {
correlationCoeff = 0;
}
else {
correlationCoeff = numerator / denominator;
}
}
Coord ScatterPlot2D::getOverviewCenter() const {
return Coord(blCorner.getX() + size / 2.0f, blCorner.getY() + size / 2.0f, 0.0f);
}
void ScatterPlot2D::setUniformBackgroundColor(const Color &backgroundColor) {
this->backgroundColor = backgroundColor;
mapBackgroundColorToCoeff = false;
if (backgroundRect != NULL) {
backgroundRect->setTopLeftColor(backgroundColor);
backgroundRect->setBottomRightColor(backgroundColor);
}
}
void ScatterPlot2D::mapBackgroundColorToCorrelCoeff(const bool mapBackgroundColor, const Color &minusOneColor, const Color &zeroColor, const Color &oneColor) {
mapBackgroundColorToCoeff = mapBackgroundColor;
this->minusOneColor = minusOneColor;
this->zeroColor = zeroColor;
this->oneColor = oneColor;
}
void ScatterPlot2D::setForegroundColor(const Color &foregroundColor) {
this->foregroundColor = foregroundColor;
if (clickLabel != NULL) {
clickLabel->setColor(foregroundColor);
}
}
}
|