fixed force calculation in elastic model. Still need to figure out what connexions we show

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3690 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2010-10-22 20:47:08 +00:00
parent b7b8cfe860
commit c4b5f41446
9 changed files with 941 additions and 1014 deletions

View file

@ -1,210 +1,207 @@
/****************************************************************************
**
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the example classes of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech gives you certain
** additional rights. These rights are described in the Trolltech GPL
** Exception version 1.0, which can be found at
** http://www.trolltech.com/products/qt/gplexception/ and in the file
** GPL_EXCEPTION.txt in this package.
**
** In addition, as a special exception, Trolltech, as the sole copyright
** holder for Qt Designer, grants users of the Qt/Eclipse Integration
** plug-in the right for the Qt/Eclipse Integration to link to
** functionality provided by Qt Designer and its related libraries.
**
** Trolltech reserves all rights not expressly granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "graphwidget.h"
#include "edge.h"
#include "arrow.h"
#include "node.h"
#include <retroshare/rspeers.h>
#include <iostream>
#include <QDebug>
#include <QGraphicsScene>
#include <QWheelEvent>
#include <math.h>
GraphWidget::GraphWidget(QWidget *parent)
:QGraphicsView(parent), timerId(0)
{
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
#if 0
void fourn(double data[],unsigned long nn[],unsigned long ndim,int isign)
{
int i1,i2,i3,i2rev,i3rev,ip1,ip2,ip3,ifp1,ifp2;
int ibit,idim,k1,k2,n,nprev,nrem,ntot;
double tempi,tempr;
double theta,wi,wpi,wpr,wr,wtemp;
ntot=1;
for (idim=1;idim<=ndim;idim++)
ntot *= nn[idim];
nprev=1;
for (idim=ndim;idim>=1;idim--) {
n=nn[idim];
nrem=ntot/(n*nprev);
ip1=nprev << 1;
ip2=ip1*n;
ip3=ip2*nrem;
i2rev=1;
for (i2=1;i2<=ip2;i2+=ip1) {
if (i2 < i2rev) {
for (i1=i2;i1<=i2+ip1-2;i1+=2) {
for (i3=i1;i3<=ip3;i3+=ip2) {
i3rev=i2rev+i3-i2;
SWAP(data[i3],data[i3rev]);
SWAP(data[i3+1],data[i3rev+1]);
}
}
}
ibit=ip2 >> 1;
while (ibit >= ip1 && i2rev > ibit) {
i2rev -= ibit;
ibit >>= 1;
}
i2rev += ibit;
}
ifp1=ip1;
while (ifp1 < ip2) {
ifp2=ifp1 << 1;
theta=isign*6.28318530717959/(ifp2/ip1);
wtemp=sin(0.5*theta);
wpr = -2.0*wtemp*wtemp;
wpi=sin(theta);
wr=1.0;
wi=0.0;
for (i3=1;i3<=ifp1;i3+=ip1) {
for (i1=i3;i1<=i3+ip1-2;i1+=2) {
for (i2=i1;i2<=ip3;i2+=ifp2) {
k1=i2;
k2=k1+ifp1;
tempr=wr*data[k2]-wi*data[k2+1];
tempi=wr*data[k2+1]+wi*data[k2];
data[k2]=data[k1]-tempr;
data[k2+1]=data[k1+1]-tempi;
data[k1] += tempr;
data[k1+1] += tempi;
}
}
wr=(wtemp=wr)*wpr-wi*wpi+wr;
wi=wi*wpr+wtemp*wpi+wi;
}
ifp1=ifp2;
}
nprev *= n;
}
}
#undef SWAP
GraphWidget::GraphWidget(QWidget *)
: timerId(0)
{
QGraphicsScene *scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
//scene->setSceneRect(-200, -200, 400, 400);
scene->setSceneRect(-200, -200, 1000, 1000);
setScene(scene);
centerNode = new Node(this, 1, "You");
scene->addItem(centerNode);
centerNode->setPos(0, 0);
#endif
centerNode = NULL;
setCacheMode(CacheBackground);
setViewportUpdateMode(BoundingRectViewportUpdate);
setRenderHint(QPainter::Antialiasing);
setTransformationAnchor(AnchorUnderMouse);
setResizeAnchor(AnchorViewCenter);
scale(0.8, 0.8);
//setMinimumSize(400, 400);
//setMinimumSize(1000, 1000);
scale(qreal(0.8), qreal(0.8));
setMinimumSize(400, 400);
setWindowTitle(tr("Elastic Nodes"));
clearGraph();
}
bool GraphWidget::clearGraph()
void GraphWidget::clearGraph()
{
QGraphicsScene *oldscene = scene();
QGraphicsScene *oldscene = scene();
QGraphicsScene *scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
//scene->setSceneRect(-200, -200, 400, 400);
scene->setSceneRect(-200, -200, 1000, 1000);
setScene(scene);
QGraphicsScene *scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
scene->setSceneRect(-200, -200, 1000, 1000);
setScene(scene);
centerNode = new Node(this, 1, rsPeers->getGPGOwnId(), "You");
scene->addItem(centerNode);
centerNode->setPos(0, 0);
// scene->addItem(centerNode);
// centerNode->setPos(0, 0);
if (oldscene)
{
delete oldscene;
}
if (oldscene != NULL)
{
delete oldscene;
}
nodeMap.clear();
edgeList.clear();
arrowList.clear();
return true;
_edges.clear();
_nodes.clear();
}
void GraphWidget::addNode(uint32_t type, std::string id, std::string name)
GraphWidget::NodeId GraphWidget::addNode(const std::string& node_string,uint32_t flags)
{
Node *node = new Node(this, type, id, name);
/* store node */
nodeMap[id] = node;
Node *node = new Node(node_string,flags,this);
_nodes.push_back(node) ;
scene()->addItem(node);
node->setPos(-50 + qrand() % 100 , -50 + qrand() % 100);
node->setPos(-50+rand()%1000/53.0f, -50+rand()%1000/53.0f);
return _nodes.size()-1 ;
}
void GraphWidget::addEdge(std::string id1, std::string id2)
GraphWidget::EdgeId GraphWidget::addEdge(NodeId n1,NodeId n2)
{
std::map<std::string, Node *>::iterator it;
Node *n1 = NULL;
Node *n2 = NULL;
if (id1 == "")
{
n1 = centerNode;
}
else
{
it = nodeMap.find(id1);
if (it != nodeMap.end())
{
n1 = it->second;
}
}
it = nodeMap.find(id2);
if (it != nodeMap.end())
{
n2 = it->second;
}
if ((n1) && (n2))
{
Edge *edge = new Edge(n1, n2);
scene()->addItem(edge);
edgeList.push_back(edge);
}
Edge *edge = new Edge(_nodes[n1],_nodes[n2]);
scene()->addItem(edge);
return 0 ;
}
void GraphWidget::addArrow(std::string id1, std::string id2)
{
std::map<std::string, Node *>::iterator it;
Node *n1 = NULL;
Node *n2 = NULL;
if (id1 == "")
{
n1 = centerNode;
}
else
{
it = nodeMap.find(id1);
if (it != nodeMap.end())
{
n1 = it->second;
}
}
it = nodeMap.find(id2);
if (it != nodeMap.end())
{
n2 = it->second;
}
if ((n1) && (n2))
{
Arrow *arrow = new Arrow(n1, n2);
scene()->addItem(arrow);
arrowList.push_back(arrow);
}
}
void GraphWidget::itemMoved()
{
if (!timerId)
timerId = startTimer(1000 / 1);
timerId = startTimer(1000 / 25);
}
void GraphWidget::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Up:
centerNode->moveBy(0, -20);
break;
case Qt::Key_Down:
centerNode->moveBy(0, 20);
break;
case Qt::Key_Left:
centerNode->moveBy(-20, 0);
break;
case Qt::Key_Right:
centerNode->moveBy(20, 0);
break;
// case Qt::Key_Up:
// centerNode->moveBy(0, -20);
// break;
// case Qt::Key_Down:
// centerNode->moveBy(0, 20);
// break;
// case Qt::Key_Left:
// centerNode->moveBy(-20, 0);
// break;
// case Qt::Key_Right:
// centerNode->moveBy(20, 0);
// break;
case Qt::Key_Plus:
scaleView(1.2);
scaleView(qreal(1.2));
break;
case Qt::Key_Minus:
scaleView(1 / 1.2);
scaleView(1 / qreal(1.2));
break;
case Qt::Key_Space:
case Qt::Key_Enter:
@ -218,22 +215,95 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
}
}
static void convolveWithGaussian(double *forceMap,int S,int s)
{
static double *bf = NULL ;
if(bf == NULL)
{
bf = new double[S*S*2] ;
for(int i=0;i<S;++i)
for(int j=0;j<S;++j)
{
int x = (i<S/2)?i:(S-i) ;
int y = (j<S/2)?j:(S-j) ;
int l=2*(x*x+y*y);
bf[2*(i+S*j)] = log(sqrtf(0.1 + x*x+y*y)); // linear -> derivative is constant
bf[2*(i+S*j)+1] = 0 ;
}
unsigned long nn[2] = {S,S};
fourn(&bf[-1],&nn[-1],2,1) ;
}
unsigned long nn[2] = {S,S};
fourn(&forceMap[-1],&nn[-1],2,1) ;
for(int i=0;i<S;++i)
for(int j=0;j<S;++j)
{
float a = forceMap[2*(i+S*j)]*bf[2*(i+S*j)] - forceMap[2*(i+S*j)+1]*bf[2*(i+S*j)+1] ;
float b = forceMap[2*(i+S*j)]*bf[2*(i+S*j)+1] + forceMap[2*(i+S*j)+1]*bf[2*(i+S*j)] ;
forceMap[2*(i+S*j)] = a ;
forceMap[2*(i+S*j)+1] = b ;
}
fourn(&forceMap[-1],&nn[-1],2,-1) ;
for(int i=0;i<S*S*2;++i)
forceMap[i] /= S*S;
}
void GraphWidget::timerEvent(QTimerEvent *event)
{
if(!isVisible())
return;
Q_UNUSED(event);
QList<Node *> nodes;
foreach (QGraphicsItem *item, scene()->items()) {
if (Node *node = qgraphicsitem_cast<Node *>(item))
nodes << node;
}
static const int S = 256 ;
static double *forceMap = new double[2*S*S] ;
foreach (Node *node, nodes)
node->calculateForces();
memset(forceMap,0,2*S*S*sizeof(double)) ;
QRectF R(scene()->sceneRect()) ;
foreach (Node *node, _nodes)
{
QPointF pos = node->mapToScene(QPointF(0,0)) ;
float x = S*(pos.x()-R.left())/R.width() ;
float y = S*(pos.y()- R.top())/R.height() ;
int i=(int)floor(x) ;
int j=(int)floor(y) ;
float di = x-i ;
float dj = y-j ;
if( i>=0 && i<S-1 && j>=0 && j<S-1)
{
forceMap[2*(i +S*(j ))] += (1-di)*(1-dj) ;
forceMap[2*(i+1+S*(j ))] += di *(1-dj) ;
forceMap[2*(i +S*(j+1))] += (1-di)*dj ;
forceMap[2*(i+1+S*(j+1))] += di *dj ;
}
}
// compute convolution with 1/omega kernel.
convolveWithGaussian(forceMap,S,20) ;
static int toto=0 ;
static float speedf=1.0f;
foreach (Node *node, _nodes)
{
QPointF pos = node->mapToScene(QPointF(0,0)) ;
float x = S*(pos.x()-R.left())/R.width() ;
float y = S*(pos.y()-R.top())/R.height() ;
node->calculateForces(forceMap,R.width(),R.height(),S,S,x,y,speedf);
}
bool itemsMoved = false;
foreach (Node *node, nodes) {
foreach (Node *node, _nodes) {
if (node->advance())
itemsMoved = true;
}
@ -271,19 +341,19 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
painter->drawRect(sceneRect);
// Text
// QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
// sceneRect.width() - 4, sceneRect.height() - 4);
// QString message(tr("Click and drag the nodes around, and zoom with the mouse "
// "wheel or the '+' and '-' keys"));
//
// QFont font = painter->font();
// font.setBold(true);
// font.setPointSize(14);
// painter->setFont(font);
// painter->setPen(Qt::lightGray);
// painter->drawText(textRect.translated(2, 2), message);
// painter->setPen(Qt::black);
// painter->drawText(textRect, message);
QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
sceneRect.width() - 4, sceneRect.height() - 4);
QString message(tr("Click and drag the nodes around, and zoom with the mouse "
"wheel or the '+' and '-' keys"));
QFont font = painter->font();
font.setBold(true);
font.setPointSize(14);
painter->setFont(font);
painter->setPen(Qt::lightGray);
painter->drawText(textRect.translated(2, 2), message);
painter->setPen(Qt::black);
painter->drawText(textRect, message);
}
void GraphWidget::scaleView(qreal scaleFactor)