Debian Codegraph @

src/InsertGenerator3D.cc

raw ◀ ▶
1 Debian patch touch this file
1 /////////////////////////////////////////////////////////////
2 // //
3 // Copyright (c) 2007-2017 by The University of Queensland //
4 // Centre for Geoscience Computing //
5 // http://earth.uq.edu.au/centre-geoscience-computing //
6 // //
7 // Primary Business: Brisbane, Queensland, Australia //
8 // Licensed under the Open Software License version 3.0 //
9 // http://www.apache.org/licenses/LICENSE-2.0 //
10 // //
11 /////////////////////////////////////////////////////////////
12
13 #include "InsertGenerator3D.h"
14 #include <boost/python/overloads.hpp>
15
16 // --- System includes ---
•17 #include <sys/time.h>
18 #include <cmath>
19
20 using std::ceil;
21 using std::sqrt;
22
23 // --- project includes ---
24 #include "sphere_fitting/Sphere3DFitter.h"
25
26
27 InsertGenerator3D::InsertGenerator3D()
28 {}
29
30 /*!
31 Constructor
32
33 \param rmin minimum particle radius
34 \param rmax maximum particle radius
35 \param ntries max. nr. of tries to insert particle
36 \param max_iter maximum iterations within the iterative solvers
37 \param prec max. error in iterative solvers
38 */
39 InsertGenerator3D::InsertGenerator3D(double rmin,double rmax,int tries,int max_iter,double prec)
40 {
41 m_rmin=rmin;
42 m_rmax=rmax;
43 m_max_tries=tries;
44 m_max_iter=max_iter;
45 m_prec=prec;
46 }
47
48 /*!
49 Constructor
50
51 \param rmin minimum particle radius
52 \param rmax maximum particle radius
53 \param ntries max. nr. of tries to insert particle
54 \param max_iter maximum iterations within the iterative solvers
55 \param prec max. error in iterative solvers
56 \param seed if true, initialize random number generator via time
57 */
58 InsertGenerator3D::InsertGenerator3D(double rmin,double rmax,int tries,int max_iter,double prec, bool seed)
59 {
60 if (seed){
61 struct timeval tv;
62 gettimeofday(&tv,NULL);
63 int random_seed=tv.tv_usec;
64 srand(random_seed);
65 }
66 m_rmin=rmin;
67 m_rmax=rmax;
68 m_max_tries=tries;
69 m_max_iter=max_iter;
70 m_prec=prec;
71 m_next_tag=0;
72 }
73
74 /*!
75 Destructor
76 */
77 InsertGenerator3D::~InsertGenerator3D()
78 {}
79
80
81 /*!
82 toggle between old (pre rev.84, ansiotropic) and new (rev 84 onwards) seeding behaviour
83
84 \param old if true -> old behaviour, if false -> new behaviour
85 */
86 void InsertGenerator3D::setOldSeeding(bool old)
87 {
88 m_old_seeding=old;
89 }
90
91 /*!
92 Set tag for the next particles to be generated. Persists until changed.
93
94 \param tag the tag
95 */
96 void InsertGenerator3D::setNextTag(int tag)
97 {
98 m_next_tag=tag;
99 }
100
101 void InsertGenerator3D::generatePacking3 (AVolume3D* vol,MNTable3D* ntable,int gid) {
102 int tag=-1;
103 generatePacking4(vol,ntable,gid,tag);
104 }
105
106 void InsertGenerator3D::generatePacking4 (AVolume3D* vol,MNTable3D* ntable,int gid, int tag=-1) {
107 ShapeList* list = (ShapeList*)0;
108 generatePacking(vol,ntable,gid,tag,list);
109 }
110 /*!
111 generate packing with at most a given total volume of the inserted particles
112
113 \param vol a pointer to the volume in which the packing is generated
114 \param ntable a pointer to the neighbour table used
115 \param gid particle group id
116 \param tag the tagid of particles to be inserted
117 \param maxvol maximum cumulative volume of inserted particles (may not be reached)
118 */
119 void InsertGenerator3D::generatePackingMaxVolume(AVolume3D* vol,MNTable3D* ntable,int gid, int tag, double maxvol)
120 {
121 this->seedParticles(vol,ntable,gid,tag);
122 this->fillIn(vol,ntable,gid,tag,maxvol);
123 }
124
125 /*!
126 generate packing
127
128 \param vol a pointer to the volume in which the packing is generated
129 \param ntable a pointer to the neighbour table used
130 \param gid particle group id
131 \param tag the tagid of particles to be inserted
132 \param list the list of shapes to be inserted
133 */
134 void InsertGenerator3D::generatePacking (AVolume3D* vol,MNTable3D* ntable,int gid, int tag=-1, ShapeList* list = (ShapeList*)0)
135 {
136 if(list == (ShapeList*)0) {
137 if(tag==-1){
138 this->seedParticles(vol,ntable,gid,(int)m_next_tag);
139 this->fillIn(vol,ntable,gid,(int)m_next_tag);
140 } else {
141 this->seedParticles(vol,ntable,gid,tag);
142 this->fillIn(vol,ntable,gid,tag);
143 }
144 } else {
145 if(tag==-1){
146 this->seedParticles(vol,ntable,gid,(int)m_next_tag,list);
147 this->fillIn(vol,ntable,gid,(int)m_next_tag,list);
148 } else {
149 this->seedParticles(vol,ntable,gid,tag,list);
150 this->fillIn(vol,ntable,gid,tag,list);
151 }
152 }
153 }
154
155 /*!
156 seed the area with particles
157
158 \param vol a pointer to the packing volume
159 \param ntable a pointer to the neighbour table used
160 \param gid particle group id
161 \param tag the tag for the generated particles
162 */
163 void InsertGenerator3D::seedParticles(AVolume3D* vol,MNTable3D* ntable,int gid, int tag)
164 {
165 // get bounding box
166 std::cout << "InsertGenerator3D::seedParticles" << std::endl;
167 pair<Vector3,Vector3> bbx=vol->getBoundingBox();
168 std::cout << "bbx: " << bbx.first << " - " << bbx.second << std::endl;
169 double dx=(bbx.second.X()-bbx.first.X());
170 double dy=(bbx.second.Y()-bbx.first.Y());
171 double dz=(bbx.second.Z()-bbx.first.Z());
172 // get index limits for seeding
173 int imax=int(ceil(dx/(m_rmax*2.0)));
174 int jmax=int(ceil(dy/(m_rmax*sqrt(3.0))));
175 int kmax=int(ceil(dz/(m_rmax*2.0*sqrt(2.0/3.0))));
176 // seed positions
177 for(int i=0;i<=imax;i++){
178 for(int j=0;j<=jmax;j++){
179 for(int k=0;k<=kmax;k++){
180 // get position
181 // double px=bbx.first.X()+(double(i)+0.5*double(j%2)-0.25)*m_rmax*2.0;
182 // double py=bbx.first.Y()+(double(j)+(double(k%2)-0.5)/3.0)*sqrt(3.0)*m_rmax;
183 // double pz=bbx.first.Z()+(double(k)*2.0*sqrt(2.0/3.0))*m_rmax;
184 double px=bbx.first.X()+((double(i)+0.5*double(j%2)+0.5*double(k%2))*m_rmax*2.0)+m_rmax+1e-5;
185 double py=bbx.first.Y()+((double(j)+double(k%2)/3.0)*sqrt(3.0)*m_rmax)+m_rmax+1e-5;
186 double pz=bbx.first.Z()+((double(k)*2.0*sqrt(2.0/3.0))*m_rmax)+m_rmax+1e-5;
187
188 // get dist to egde
189 double dex=(bbx.second.X()-px) < (px-bbx.first.X()) ? bbx.second.X()-px : px-bbx.first.X();
190 double dey=(bbx.second.Y()-py) < (py-bbx.first.Y()) ? bbx.second.Y()-py : py-bbx.first.Y();
191 double dez=(bbx.second.Z()-pz) < (pz-bbx.first.Z()) ? bbx.second.Z()-pz : pz-bbx.first.Z();
192
193 double de=(dex<dey) ? dex : dey;
194 de = (de < dez) ? de : dez;
195
196 // check max rad.
197 if(de>m_rmin){
198 // calc random radius
199 double r;
200 double jitter;
201 if(de<m_rmax) {
202 if(m_old_seeding){
203 r=m_rmin+((de-m_rmin)*((double)(rand())/(double)(RAND_MAX)));
204 jitter=0.0;
205 } else {
206 r=m_rmin+(((de-m_rmin)/2.0)*((double)(rand())/(double)(RAND_MAX)));
207 jitter=de-r;
208 }
209 } else {
210 if(m_old_seeding){
211 r=m_rmin+((m_rmax-m_rmin)*((double)(rand())/(double)(RAND_MAX)));
212 jitter=0.0;
213 } else {
214 r=m_rmin+(((m_rmax-m_rmin)/2.0)*((double)(rand())/(double)(RAND_MAX)));
215 jitter=m_rmax-r;
216 }
217 }
218 //std::cout << "pos: " << Vector3(px,py,pz) << " r: " << r << std::endl;
219 // jitter position
220 double dx=jitter*(2.0*((double)(rand())/(double)(RAND_MAX))-1.0);
221 double dy=jitter*(2.0*((double)(rand())/(double)(RAND_MAX))-1.0);
222 double dz=jitter*(2.0*((double)(rand())/(double)(RAND_MAX))-1.0);
223 Sphere S(Vector3(px+dx,py+dy,pz+dz),r);
224 bool fit=vol->isIn(S) && ntable->checkInsertable(S,gid);
225 if(fit){
226 S.setTag(tag);
227 ntable->insertChecked(S,gid);
228 //std::cout << "inserted" << std::endl;
229 } // else {
230 // std::cout << "isIn " << vol->isIn(S) << " insertable: " << ntable->checkInsertable(S,gid) << std::endl;
231 // }
232 }
233 }
234 }
235 }
236
237 }
238 /*!
239 seed the area with particles
240
241 \param vol a pointer to the packing volume
242 \param ntable a pointer to the neighbour table used
243 \param gid particle group id
244 \param tag the tag for the generated particles
245 */
246 void InsertGenerator3D::seedParticles(AVolume3D* vol,MNTable3D* ntable,int gid, int tag, ShapeList* sList)
247 {
248 // get bounding box
249 std::cout << "InsertGenerator3D::seedParticles" << std::endl;
250 pair<Vector3,Vector3> bbx=vol->getBoundingBox();
251 std::cout << "bbx: " << bbx.first << " - " << bbx.second << std::endl;
252 double dx=(bbx.second.X()-bbx.first.X());
253 double dy=(bbx.second.Y()-bbx.first.Y());
254 double dz=(bbx.second.Z()-bbx.first.Z());
255 // get index limits for seeding
256 int imax=int(ceil(dx/(m_rmax*2.0)));
257 int jmax=int(ceil(dy/(m_rmax*sqrt(3.0))));
258 int kmax=int(ceil(dz/(m_rmax*2.0*sqrt(2.0/3.0))));
259 // seed positions
260 for(int i=0;i<=imax;i++){
261 for(int j=0;j<=jmax;j++){
262 for(int k=0;k<=kmax;k++){
263 // get position
264 // double px=bbx.first.X()+(double(i)+0.5*double(j%2)-0.25)*m_rmax*2.0;
265 // double py=bbx.first.Y()+(double(j)+(double(k%2)-0.5)/3.0)*sqrt(3.0)*m_rmax;
266 // double pz=bbx.first.Z()+(double(k)*2.0*sqrt(2.0/3.0))*m_rmax;
267 double px=bbx.first.X()+((double(i)+0.5*double(j%2)+0.5*double(k%2))*m_rmax*2.0)+m_rmax+1e-5;
268 double py=bbx.first.Y()+((double(j)+double(k%2)/3.0)*sqrt(3.0)*m_rmax)+m_rmax+1e-5;
269 double pz=bbx.first.Z()+((double(k)*2.0*sqrt(2.0/3.0))*m_rmax)+m_rmax+1e-5;
270
271 // get dist to egde
272 double dex=(bbx.second.X()-px) < (px-bbx.first.X()) ? bbx.second.X()-px : px-bbx.first.X();
273 double dey=(bbx.second.Y()-py) < (py-bbx.first.Y()) ? bbx.second.Y()-py : py-bbx.first.Y();
274 double dez=(bbx.second.Z()-pz) < (pz-bbx.first.Z()) ? bbx.second.Z()-pz : pz-bbx.first.Z();
275
276 double de=(dex<dey) ? dex : dey;
277 de = (de < dez) ? de : dez;
278
279 // check max rad.
280 if(de>m_rmin){
281 // calc random radius
282 double r;
283 if(de<m_rmax) {
284 r=m_rmin+((de-m_rmin)*((double)(rand())/(double)(RAND_MAX)));
285 } else {
286 r=m_rmin+((m_rmax-m_rmin)*((double)(rand())/(double)(RAND_MAX)));
287 }
288 //std::cout << "pos: " << Vector3(px,py,pz) << " r: " << r << std::endl;
289 Sphere S(Vector3(px,py,pz),r);
290 bool fit=vol->isIn(S) && ntable->checkInsertable(S,gid);
291 if(fit){
292 // A sphere fits, so insert a "shape"
293 //S.setTag(tag);
294 sList->insertShape(Vector3(px,py,pz),r,ntable,tag,gid);
295 //ntable->insertChecked(S,gid);
296 //std::cout << "inserted" << std::endl;
297 } // else {
298 // std::cout << "isIn " << vol->isIn(S) << " insertable: " << ntable->checkInsertable(S,gid) << std::endl;
299 // }
300 }
301 }
302 }
303 }
304
305 }
306
307
308 /*!
309 Fill in particles
310
311 \param vol a pointer to the packing volume
312 \param ntable a pointer to the neighbour table used
313 \param gid particle group id
314 */
315 void InsertGenerator3D::fillIn(AVolume3D* vol,MNTable3D* ntable,int gid, int tag)
316 {
317 Sphere nsph;
318
319 int total_tries=0;
320 int count_insert=0;
321
322 int nvol=vol->getNumberSubVolumes();
323 // for(int ivol=0;ivol<nvol;ivol++){
324 int countfail=0; // number of failed attempts since last successfull insertion
325 int last_fail_count=0; // number of failed attempts since last 100
326 while(countfail<m_max_tries){
327 bool findfit=false;
328 Vector3 P=vol->getAPoint(0); // get random point within volume
329 multimap<double,const Sphere*> close_particles=ntable->getSpheresClosestTo(P,4); // get 3 nearest spheres
330 map<double,const AGeometricObject*> close_lines=vol->getClosestObjects(P,4); // get 2 nearest planes
331
332 map<double,const AGeometricObject*> geomap;
333 geomap.insert(close_particles.begin(),close_particles.end());
334 geomap.insert(close_lines.begin(),close_lines.end());
335
336 if(geomap.size()>=4){
337 map<double,const AGeometricObject*>::iterator iter=geomap.begin();
338 const AGeometricObject* GO1=iter->second;iter++;
339 const AGeometricObject* GO2=iter->second;iter++;
340 const AGeometricObject* GO3=iter->second;iter++;
341 const AGeometricObject* GO4=iter->second;
342 nsph=FitSphere3D(GO1,GO2,GO3,GO4,P,m_max_iter,m_prec);
343 findfit=true;
344 }
345
346 if(findfit){
347 // check if within radius range
348 bool is_radius=(m_rmin<nsph.Radius()) && (m_rmax>nsph.Radius());
349
350 // check inside volume, intersections ...
351 bool fit=vol->isIn(nsph); // && ntable->checkInsertable(nsph,gid);
352
353 if(fit && is_radius){ // acceptable particle
354 nsph.setTag(tag);
355 if(ntable->insertChecked(nsph,gid)){; // insert
356 count_insert++;
357 total_tries+=countfail;
358 last_fail_count+=countfail;
359 if((count_insert%100)==0) {
360 std::cout << "inserted " << count_insert << " at avg. " << double(last_fail_count)*0.01 << std::endl;
361 last_fail_count=0;
362 }
363 countfail=0; // reset failure counter
364 } else countfail++;
365 } else {
366 countfail++;
367 }
368 } else countfail++;
369 }
370 // }
371 std::cout << "total tries: " << total_tries << std::endl;
372 }
373
374 /*!
375 Fill in particles
376
377 \param vol a pointer to the packing volume
378 \param ntable a pointer to the neighbour table used
379 \param gid particle group id
380 \param tag the tag of the new particles
381 \param maxvol maximum particle volume
382 */
383 void InsertGenerator3D::fillIn(AVolume3D* vol,MNTable3D* ntable,int gid, int tag, double maxvol)
384 {
385
386 // get vol. of particles already in ntable
387 double tvol=ntable->getSumVolume(gid);
388 std::cout << "particle volume: " << tvol << std::endl;
389
390 Sphere nsph;
391
392 int total_tries=0;
393 int count_insert=0;
394
395 int nvol=vol->getNumberSubVolumes();
396 for(int ivol=0;ivol<nvol;ivol++){
397 int countfail=0; // number of failed attempts since last successfull insertion
398 int last_fail_count=0; // number of failed attempts since last 100
399 while((countfail<m_max_tries) && (tvol<maxvol)){
400 bool findfit=false;
401 Vector3 P=vol->getAPoint(ivol); // get random point within volume
402 multimap<double,const Sphere*> close_particles=ntable->getSpheresClosestTo(P,4); // get 3 nearest spheres
403 map<double,const AGeometricObject*> close_lines=vol->getClosestObjects(P,3); // get 2 nearest planes
404
405 map<double,const AGeometricObject*> geomap;
406 geomap.insert(close_particles.begin(),close_particles.end());
407 geomap.insert(close_lines.begin(),close_lines.end());
408
409 if(geomap.size()>=4){
410 map<double,const AGeometricObject*>::iterator iter=geomap.begin();
411 const AGeometricObject* GO1=iter->second;iter++;
412 const AGeometricObject* GO2=iter->second;iter++;
413 const AGeometricObject* GO3=iter->second;iter++;
414 const AGeometricObject* GO4=iter->second;
415 nsph=FitSphere3D(GO1,GO2,GO3,GO4,P,m_max_iter,m_prec);
416 findfit=true;
417 }
418
419 if(findfit){
420 // check if within radius range
421 bool is_radius=(m_rmin<nsph.Radius()) && (m_rmax>nsph.Radius());
422
423 // check inside volume, intersections ...
424 bool fit=vol->isIn(nsph) && ntable->checkInsertable(nsph,gid);
425
426 if(fit && is_radius){ // acceptable particle
427 nsph.setTag(tag);
428 ntable->insertChecked(nsph,gid); // insert
429 count_insert++;
430 double rad=nsph.Radius();
431 tvol+=(4.0/3.0)*3.1415926*rad*rad*rad; // V=4/3*pi*r^3
432 total_tries+=countfail;
433 last_fail_count+=countfail;
434 if((count_insert%100)==0) {
435 std::cout << "inserted " << count_insert << " with volume " << tvol << " at avg. " << double(last_fail_count)*0.01 << std::endl;
436 last_fail_count=0;
437 }
438 //if(countfail>m_max_tries/10) std::cout << countfail << " tries" << std::endl;
439 countfail=0; // reset failure counter
440 } else {
441 countfail++;
442 }
443 } else countfail++;
444 }
445 }
446 std::cout << "total tries: " << total_tries << std::endl;
447 }
448
449
450
451 /*!
452 Fill in particles
453
454 \param vol a pointer to the packing volume
455 \param ntable a pointer to the neighbour table used
456 \param gid particle group id
457 */
458 void InsertGenerator3D::fillIn(AVolume3D* vol,MNTable3D* ntable,int gid, int tag, ShapeList *list)
459 {
460 Sphere nsph;
461
462 int total_tries=0;
463 int count_insert=0;
464
465 int nvol=vol->getNumberSubVolumes();
466 for(int ivol=0;ivol<nvol;ivol++){
467 int countfail=0; // number of failed attempts since last successfull insertion
468 int last_fail_count=0; // number of failed attempts since last 100
469 while(countfail<m_max_tries){
470 bool findfit=false;
471 Vector3 P=vol->getAPoint(ivol); // get random point within volume
472 multimap<double,const Sphere*> close_particles=ntable->getSpheresClosestTo(P,4); // get 3 nearest spheres
473 map<double,const AGeometricObject*> close_lines=vol->getClosestObjects(P,3); // get 2 nearest planes
474
475 map<double,const AGeometricObject*> geomap;
476 geomap.insert(close_particles.begin(),close_particles.end());
477 geomap.insert(close_lines.begin(),close_lines.end());
478
479 if(geomap.size()>=4){
480 map<double,const AGeometricObject*>::iterator iter=geomap.begin();
481 const AGeometricObject* GO1=iter->second;iter++;
482 const AGeometricObject* GO2=iter->second;iter++;
483 const AGeometricObject* GO3=iter->second;iter++;
484 const AGeometricObject* GO4=iter->second;
485 nsph=FitSphere3D(GO1,GO2,GO3,GO4,P,m_max_iter,m_prec);
486 findfit=true;
487 }
488
489 if(findfit){
490 // check if within radius range
491 bool is_radius=(m_rmin<nsph.Radius()) && (m_rmax>nsph.Radius());
492
493 // check inside volume, intersections ...
494 bool fit=vol->isIn(nsph) && ntable->checkInsertable(nsph,gid);
495
496 if(fit && is_radius){ // acceptable particle
497 nsph.setTag(tag);
498 // ntable->insertChecked(nsph,gid); // insert
499 list->insertShape(nsph.Center(),nsph.Radius(),ntable,tag,gid);
500 count_insert++;
501 total_tries+=countfail;
502 last_fail_count+=countfail;
503 if((count_insert%100)==0) {
504 std::cout << "inserted " << count_insert << " at avg. " << double(last_fail_count)*0.01 << std::endl;
505 last_fail_count=0;
506 }
507 //if(countfail>m_max_tries/10) std::cout << countfail << " tries" << std::endl;
508 countfail=0; // reset failure counter
509 } else {
510 countfail++;
511 }
512 } else countfail++;
513 }
514 }
515 std::cout << "total tries: " << total_tries << std::endl;
516 }
517
518 ostream& operator << (ostream& ost,const InsertGenerator3D& T)
519 {
520 return ost;
521 }
522
523