-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfruchterman_reingold.cpp
202 lines (153 loc) · 4.08 KB
/
fruchterman_reingold.cpp
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
#include "fruchterman_reingold.hpp"
#include <algorithm>
#include <cmath>
#include <iostream>
namespace nodesoup
{
FruchtermanReingold::FruchtermanReingold(const adj_list_t& aAdjList,double aK)
: m_AdjList(aAdjList)
, m_K(aK)
, m_KSquared(aK* aK)
, m_Temp(10 * sqrt(aAdjList.size()))
, m_Mvmts(m_AdjList.size())
, m_StartCircle(true)
, m_CurrIter(0), m_MaxIter(0)
{
}
void FruchtermanReingold::Start(bool aStartCircle)
{
m_Mvmts.resize(m_AdjList.size());
m_Positions.resize(m_AdjList.size());
m_CurrIter=0;
m_MaxIter=0;
m_StartCircle=aStartCircle;
}
void FruchtermanReingold::DoStep()
{
ImVec2 zero={0.0f,0.0f};
fill(m_Mvmts.begin(),m_Mvmts.end(), zero);
// Repulsion force between vertice pairs
for(vertex_id_t v_id=0; v_id<m_AdjList.size(); v_id++)
{
for(vertex_id_t other_id=v_id+1; other_id<m_AdjList.size(); other_id++)
{
if(v_id==other_id)
{
continue;
}
ImVec2 delta = m_Positions[v_id].m_Pos-m_Positions[other_id].m_Pos;
double distance = norm(delta);
// TODO: handle distance == 0.0
// > 1000.0: not worth computing
if(distance > 1000.0)
{
continue;
}
double repulsion = m_KSquared / distance;
m_Mvmts[v_id] += delta/distance * repulsion;
m_Mvmts[other_id] -= delta/distance * repulsion;
}
// Attraction force between edges
for(vertex_id_t adj_id:m_AdjList[v_id])
{
if(adj_id>v_id)
{
continue;
}
ImVec2 delta = m_Positions[v_id].m_Pos-m_Positions[adj_id].m_Pos;
float distance=norm(delta);
if(distance==0.0f)
{
continue;
}
double attraction = distance*distance / m_K;
m_Mvmts[v_id] -= delta / distance * attraction;
m_Mvmts[adj_id] += delta / distance * attraction;
}
}
// Max movement capped by current temperature
for(vertex_id_t v_id=0; v_id<m_AdjList.size(); v_id++)
{
if(!m_Positions[v_id].m_Fixed)
{
double mvmt_norm=norm(m_Mvmts[v_id]);
// < 1.0: not worth computing
if (mvmt_norm < 1.0)
{
continue;
}
double capped_mvmt_norm=std::min(mvmt_norm, m_Temp);
ImVec2 capped_mvmt=m_Mvmts[v_id] / mvmt_norm * capped_mvmt_norm;
m_Positions[v_id].m_Pos=m_Positions[v_id].m_Pos+capped_mvmt;
}
}
// Cool down fast until we reach 1.5, then stay at low temperature
if(m_Temp>0.1)
{
m_Temp*=0.85;
}
else
{
m_Temp=0.1;
}
}
void FruchtermanReingold::Step(int aStepSize,int aMaxStep,std::vector<NsPosition>& aPositions)
{
if(m_CurrIter>=aMaxStep && aMaxStep>0)
{
for(int k=0;k<aPositions.size();++k)
{
aPositions[k].m_Fixed=m_Positions[k].m_Fixed;
aPositions[k].m_Pos=m_Positions[k].m_Pos;
}
return;
}
if (!m_CurrIter)
{
SetInitPositions();
}
for(int k=0;k<aStepSize && m_CurrIter<m_MaxIter;++k)
{
DoStep();
m_CurrIter++;
}
if(m_CurrIter>=m_MaxIter)
{
m_CurrIter=1;
m_MaxIter++;
for(int k=0;k<aPositions.size();++k)
{
aPositions[k].m_Fixed=m_Positions[k].m_Fixed;
aPositions[k].m_Pos=m_Positions[k].m_Pos;
}
}
}
void FruchtermanReingold::SetInitPositions()
{
nodesoup::SetInitPositions(m_StartCircle,m_Positions);
}
void FruchtermanReingold::SetK(double aK) noexcept
{
m_K=aK;
m_KSquared=aK*aK;
}
void FruchtermanReingold::MovePos(vertex_id_t aVertexId,const ImVec2& aDisp,bool aRecalculate)
{
// TODO: assert aVertexId en rango
if(aRecalculate)
{
if(aDisp.x==kInvalidPos && aDisp.y==kInvalidPos)
{
m_Positions[aVertexId].m_Fixed=!m_Positions[aVertexId].m_Fixed;
}
m_CurrIter=1;
m_MaxIter=0;
return;
}
m_Positions[aVertexId].m_Pos+=aDisp;
if(sq_norm(aDisp)>0.0f)
{
m_Positions[aVertexId].m_Fixed=true;
}
}
}