Debian Codegraph @

src/InsertGenerator3D.cc

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