-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleMethod.h
More file actions
239 lines (215 loc) · 5.76 KB
/
ParticleMethod.h
File metadata and controls
239 lines (215 loc) · 5.76 KB
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
#ifndef _PARTICLEMETHOD_H
#define _PARTICLEMETHOD_H
#include "ColonizationMethod.h"
class Particle: public Node
{
void configure() {
energy=1.0;
isAlive=true;
distance=10000;
}
public:
float energy;
int nearest; //index of nearest particle
float distance;//distance to nearest particle
bool isAlive;//if false, particle will be deleted
int id;
Particle():Node() {
configure();
}
Particle(Point3d *p):Node(p) {
configure();
}
};
class ParticleMethod:public GenerationMethod
{
vector < Particle > particles;
vector < Particle > particlesCopy;
vector <int> reached;
vector <Node *> nodes;
//int seed;
//int points;//starting points
//float D;
float e;
float crownRadius;
void setDefault() {
// points=1000;
// seed=42;//spr 40
// D=0.2;
e=1;
crownRadius=5.0;
}
void createParticles (Point3d * crownCenter)
{
int pts=params->points;
particles.clear();
while (pts) {
#define MRAND (((random() % 2000) - 1000) / (float) 1000) * crownRadius
float rx = MRAND + crownCenter->x;
float ry = MRAND + crownCenter->y;
float rz = MRAND + crownCenter->z;
#undef MRAND
Point3d a (rx, ry, rz);
if (a.getDistance (crownCenter) <= crownRadius) {
Particle p(&a);
Node *n=new Node(&a);
p.id=particles.size();
particles.push_back(p);
nodes.push_back(n);
pts--;
}
}
}
void colonize() {
#define _MAX(a,b) ((a)>(b)?(a):(b))
const float combine_dist=_MAX(params->cd,params->D+0.1);//0.5;
const float target_dist=0.3;
Point3d target(0,0,0);
int round=0;
reached.clear();
while(particles.size()>1) {
fprintf(stderr,"round=%d\n",round++);
//getchar();
//find nearest neighbour
for(unsigned int i=0; i<particles.size(); i++) {
Particle *a=&particles[i];
a->distance=10000;
for(unsigned int j=0; j<particles.size(); j++) {
if(i==j) continue;
Particle *b=&particles[j];
float newDistance=a->point.getDistance(&b->point);
//printf("newDistance(%d,%d) = %f\n",i,j,newDistance);
if(newDistance<a->distance) {
a->nearest=j;
a->distance=newDistance;
}
}
}
//fprintf(stderr,"particle.size()=%d\n",particles.size());
//for(unsigned int i=0; i<particles.size(); i++)
//{
// fprintf(stderr,"%d - %d %f\n",i,particles[i].nearest,particles[i].distance);
//}
//return;
//dla kazdej czastki s
unsigned int oldsize=particles.size();
for(unsigned int i=0; i<oldsize; i++) {
Particle *s=&particles[i];
Particle *n=&particles[s->nearest];
//
//fprintf(stderr,"\n\ti=%d, id=%d, nearest=%d, nearest_id=%d",i,s->id,s->nearest,n->id);
if(!s->isAlive) continue;
if(!n->isAlive) continue;
bool merged=false;
if(s->point.getDistance(&n->point)<=combine_dist) {
//scal dwie czastki
// fprintf(stderr,"merge (%d,%d)",i,s->nearest);
merged=true;
n->isAlive=false;
float x=(s->point.x+n->point.x)/2;
float y=(s->point.y+n->point.y)/2;
float z=(s->point.z+n->point.z)/2;
Node *node=new Node(x,y,z);
// fprintf(stderr," id's = %d,%d\n",n->id,s->id);
// fprintf(stderr,"nodes size = %d\n",nodes.size());
node->addChildren(nodes[n->id]);
node->addChildren(nodes[s->id]);
s->energy+=n->energy;
s->point=Point3d(x,y,z);
s->id=nodes.size();
nodes.push_back(node);
}
//sprawdz czy p osiagnal cel
if(s->point.getDistance(&target)<=target_dist) {
s->isAlive=false;
// fprintf(stderr,"target ");
reached.push_back(s->id);
} else { //move S
// fprintf(stderr,"move ");
s->isAlive=false;
Vector3d p(&s->point);
Vector3d sp(&s->point);
Vector3d sq(&n->point);
sp.mul(-1);
sq.mul(-1);
sp.normalize();
sq.normalize();
sq.mul(2);
sp.add(&sq);
sp.normalize();
sp.mul(params->D);
//Vector3d nb(&n->point); //neighbour
//Vector3d tv(&target);
//p.sub(&nb);
//q.sub(&tv);
//p.normalize();
//q.normalize();
//p.add(&q);
//p.mul(params->D);
//Point3d newPoint(sp.d[0],sp.d[1],sp.d[2]);
p.mul(0.6);
Point3d newPoint(p.d[0],p.d[1],p.d[2]);
// printf("z: \n");
// s->point.print();
// n->point.print();
//puts("robie");
//newPoint.print();
Particle newParticle(&newPoint);
newParticle.id=nodes.size();
//add child
Node *newNode=new Node(&newPoint);
newNode->addChildren(nodes[s->id]);
nodes.push_back(newNode);
particles.push_back(newParticle);
}
}
particlesCopy.clear();
//usun czastki oznaczone jako ~isAlive
for(unsigned int i=0; i<particles.size(); i++)
if(particles[i].isAlive)
particlesCopy.push_back(particles[i]);
particles.clear();
for(unsigned int i=0; i<particlesCopy.size(); i++)
particles.push_back(particlesCopy[i]);
//fprintf(stderr,"\nparticles size=%d\n",particles.size());
}
//podlacz wszystkie nody ktore osiagnely cen do jednego zrodla
Node *root=new Node(0,0,0);
printf("nodes.size= %u\n",(unsigned int)nodes.size());
printf("reached.size=%u\n",(unsigned int)reached.size());
for(unsigned int i=0; i<reached.size(); i++) {
int id=reached[i];
root->addChildren(nodes[id]);
}
nodes.push_back(root);
}
public:
MethodParameters *params;
ParticleMethod(Parameters *params) {
this->params=params->methodParams;
}
void init() {
setDefault();
srand(params->seed);
nodes.clear();
particles.clear();
particlesCopy.clear();
}
void generate() {
fprintf(stderr,"particle generate\n");
Point3d crownCenter(0,0,9);
createParticles(&crownCenter);
colonize ();
fprintf(stderr,"generate()");
fprintf(stderr,"nodes =%u \n",(unsigned int)nodes.size());
for(unsigned int i=0; i<nodes.size(); i++)
nodes[i]->print();
}
Node *getRoot() {
if(nodes.size()==0)
return NULL;
int last=nodes.size()-1;
return nodes[last];
}
};
#endif