forked from uyras/partsEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustermachine.cpp
146 lines (128 loc) · 3.63 KB
/
clustermachine.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
#include "clustermachine.h"
ClusterMachine::ClusterMachine(PartArray *sys, double interactionRange):
sys(sys),
interactionRange(interactionRange),
lastSize(0)
{
initNeightbours();
}
ClusterMachine::cluster ClusterMachine::max()
{
findClusters();
cluster maxCluster; unsigned max=0;
for (const cluster& c : _clusters){
if (c.size()>max){
maxCluster = c;
max = c.size();
}
}
return maxCluster;
}
ClusterMachine::cluster ClusterMachine::min()
{
findClusters();
cluster minCluster = _clusters.at(0); unsigned min=minCluster.size();
for (const cluster& c : _clusters){
if (c.size()<min){
minCluster = c;
min = c.size();
}
}
return minCluster;
}
ClusterMachine::clusterArray& ClusterMachine::all()
{
findClusters();
return _clusters;
}
double ClusterMachine::averageSize()
{
findClusters();
double avSize = 0;
for (const cluster& c: _clusters)
avSize+=c.size();
avSize /= (double)_clusters.size();
return avSize;
}
double ClusterMachine::maximalSize()
{
findClusters();
unsigned max=0;
for (const cluster& c : _clusters){
if (c.size()>max)
max = c.size();
}
return max;
}
double ClusterMachine::minimalSize()
{
findClusters();
unsigned min=_clusters.at(0).size();
for (const cluster& c : _clusters){
if (c.size()<min)
min = c.size();
}
return min;
}
void ClusterMachine::initNeightbours()
{
if (lastSize!=sys->size()){
neighbours.clear();
for (Part* p1 : sys->parts){
for (Part* p2 : sys->parts){
if (isNeightbours(p1,p2))
neighbours[p1->Id()].push_front(p2);
}
}
lastSize = sys->size();
}
}
bool ClusterMachine::isConnected(const Part *p1, const Part *p2) const
{
if (p1==p2) return false;
return p2->interact(p1->pos).scalar(p1->m)>0;
}
bool ClusterMachine::isNeightbours(const Part *a, const Part *b) const
{
if (a==b) return false;
if (interactionRange==0) return true;
else return a->pos.space(b->pos)<=interactionRange;
}
void ClusterMachine::findClusters()
{
if (sys->state!=oldState){
initNeightbours();
_clusters.clear();
unordered_set<unsigned> unWalked; //флаг,учавствует (или учавствовала) ли частица в кластере
for (Part* temp : sys->parts){
unWalked.insert(temp->Id());
}
Part* temp;
temp = sys->getById(*unWalked.begin());
while (temp !=0){
cluster currentCluster;
currentCluster.push_back(temp);
unWalked.erase(temp->Id());//помечаем как пройденную
walkNeighbours(temp,currentCluster, unWalked);
_clusters.push_back(currentCluster);
//получаем еще не пройденную частицу
if (unWalked.size()>0)
temp = sys->getById(*unWalked.begin());
else
temp=0;
}
}
}
void ClusterMachine::walkNeighbours(Part *part, ClusterMachine::cluster ¤tCluster, unordered_set<unsigned> &unWalked)
{
try{
for(Part* temp: neighbours.at(part->Id())){
bool walked = (unWalked.find(temp->Id())==unWalked.end());
if (!walked && isConnected(part,temp)){
unWalked.erase(temp->Id());
currentCluster.push_back(temp);
this->walkNeighbours(temp,currentCluster, unWalked);
}
}
} catch (std::out_of_range& oor){(void)oor;};
}