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
|
/**
*
* 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 "SOMMapElement.h"
#include <SOMMap.h>
#include <tulip/GlRect.h>
#include <tulip/GlCircle.h>
#include <tulip/ColorProperty.h>
#include <tulip/GlBoundingBoxSceneVisitor.h>
#define DEGTORAD(x) (M_PI/180.f) * x
using namespace tlp;
SOMMapElement::SOMMapElement(tlp::Coord position, tlp::Size size, SOMMap *map,
tlp::ColorProperty *colorProperty) :
som(map), position(position), size(size) {
buildMainComposite(position, size, map);
if (colorProperty)
updateColors(colorProperty);
computeNodeAreaSize();
}
SOMMapElement::~SOMMapElement() {
reset(true);
}
void SOMMapElement::setData(SOMMap* map, tlp::ColorProperty* colorProperty) {
som = map;
reset(true);
nodesMap.clear();
buildMainComposite(position, size, som);
if (colorProperty)
updateColors(colorProperty);
computeNodeAreaSize();
}
float SOMMapElement::computeMaximizedRadiusForHexagone(unsigned int width,
unsigned int height, tlp::Size& size) {
float ry = (4 * size.getH()) / (3 * height + 1) / 2;
float rx = (size.getW() / (cos(DEGTORAD(30)) * width)) / 2;
//Take the min
return rx > ry ? ry : rx;
}
void SOMMapElement::buildMainComposite(tlp::Coord basePos, tlp::Size gridSize,
SOMMap* map) {
reset(true);
SOMMap::SOMMapConnectivity conn = map->getConnectivity();
tlp::node n;
std::ostringstream oss;
oss.str("");
if (conn == SOMMap::six) {
//Compute the best radius
float r = computeMaximizedRadiusForHexagone(map->getWidth(),
map->getHeight(), gridSize);
float h = r / 2;
float ri = cos(DEGTORAD(30)) * r;
float top = basePos.getY() + gridSize.getH();
for (unsigned int i = 0; i < map->getHeight(); ++i) {
for (unsigned int j = 0; j < map->getWidth(); ++j) {
float x = (j * ri * 2) + ri;
float y = ((i + 1) * ((2 * r) - h)) - h;
Coord center(basePos.getX() + x, top - y, 0);
if (i % 2 != 0) {
center.setX(center.getX() + ri);
}
n = map->getNodeAt(j, i);
Color c = Color(255, 255, 255, 0);
tlp::GlCircle *hex = new tlp::GlCircle(center, r, c, c, true, false,
M_PI / 2.0f, 6);
oss.str("");
oss << j << "," << i;
addGlEntity(hex, oss.str());
nodesMap[n] = hex;
}
}
}
else {
Coord elementSize(gridSize.getW() / map->getWidth(), gridSize.getH()
/ map->getHeight(), 0);
for (unsigned int i = 0; i < map->getHeight(); ++i) {
for (unsigned int j = 0; j < map->getWidth(); ++j) {
Coord topLeft(j * elementSize.getX(), (map->getHeight() - i)
* elementSize.getY(), 0);
topLeft += basePos;
Coord bottomRight(topLeft.getX() + elementSize.getX(), topLeft.getY()
- elementSize.getY(), 0);
assert(topLeft.getX() < bottomRight.getX() && topLeft.getY()> bottomRight.getY());
tlp::GlRect *rec = NULL;
n = map->getNodeAt(j, i);
Color c = Color(255, 255, 255, 0);
rec = new tlp::GlRect(topLeft, bottomRight, c, c);
oss.str("");
oss << j << "," << i;
addGlEntity(rec, oss.str());
nodesMap[n] = rec;
}
}
}
}
void SOMMapElement::updateColors(ColorProperty *newColor) {
node n;
SOMMap::SOMMapConnectivity connect = som->getConnectivity();
forEach(n,som->getNodes()) {
if (connect == SOMMap::six) {
GlCircle *hex = (GlCircle*) nodesMap[n];
hex->setFillColor(newColor->getNodeValue(n));
}
else {
GlRect *rect = (GlRect*) nodesMap[n];
rect->setBottomRightColor(newColor->getNodeValue(n));
rect->setTopLeftColor(newColor->getNodeValue(n));
}
}
}
tlp::Coord SOMMapElement::getTopLeftPositionForElement(unsigned int x,
unsigned int y) {
Coord pos;
if (som->getConnectivity() == SOMMap::six) {
float r = computeMaximizedRadiusForHexagone(som->getWidth(),
som->getHeight(), size);
float h = r / 2;
float ri = cos(DEGTORAD(30)) * r;
if (y % 2 == 0)
pos.setX(x * ri * 2);
else
pos.setX(ri * (x * 2 + 1));
float ys = ((y + 1) * ((2 * r) - h)) - r;
pos.setX(position.getX() + pos.getX());
float top = position.getY() + size.getH();
pos.setY(top - ys);
}
else {
pos.set(x * (size.getW() / som->getWidth()), (som->getHeight() - y)
* (size.getH() / som->getHeight()), 0);
pos += position;
}
return pos;
}
tlp::Size SOMMapElement::getNodeAreaSize() {
return nodeAreaSize;
}
void SOMMapElement::computeNodeAreaSize() {
if (som->getConnectivity() == SOMMap::six) {
//Compute the best radius
float r = computeMaximizedRadiusForHexagone(som->getWidth(),
som->getHeight(), size);
float ri = cos(DEGTORAD(30)) * r;
nodeAreaSize = Size(2 * ri, r, 0);
}
else {
nodeAreaSize.set(size.getW() / som->getWidth(), size.getH()
/ som->getHeight(), 0);
}
}
|