Debian Codegraph @

src/InsertGenerator2D.cc

raw
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 "InsertGenerator2D.h"
14
15 // --- System includes ---
16 #include <cmath>
17 #include <cstdlib>
18 #include <sys/time.h>
19
20 using std::ceil;
21 using std::sqrt;
22
23 // --- project includes ---
24 #include "sphere_fitting/Sphere2DFitter.h"
25
26 InsertGenerator2D::InsertGenerator2D()
27 {}
28
29 /*!
30 Destructor
31 */
32 InsertGenerator2D::~InsertGenerator2D()
33 {}
34
35 /*!
36 Constructor
37
38 \param rmin minimum particle radius
39 \param rmax maximum particle radius
40 \param ntries max. nr. of tries to insert particle
41 \param max_iter maximum iterations within the iterative solvers
42 \param prec max. error in iterative solvers
43 */
44 InsertGenerator2D::InsertGenerator2D(double rmin,double rmax,int tries, int max_iter,double prec)
45 {
46 m_rmin=rmin;
47 m_rmax=rmax;
48 m_max_tries=tries;
49 m_max_iter=max_iter;
50 m_prec=prec;
51 m_old_seeding=false;
52 }
53
54 /*!
55 Constructor. Added random seed parameter
56
57 \param rmin minimum particle radius
58 \param rmax maximum particle radius
59 \param ntries max. nr. of tries to insert particle
60 \param max_iter maximum iterations within the iterative solvers
61 \param prec max. error in iterative solvers
62 \param seed if true, initialize random number generator via time
63 */
64 InsertGenerator2D::InsertGenerator2D(double rmin,double rmax,int tries, int max_iter,double prec, bool seed)
65 {
66 if(seed){
67 struct timeval tv;
68 gettimeofday(&tv,NULL);
69 int random_seed=tv.tv_usec;
70 srand(random_seed);
71 }
72 m_rmin=rmin;
73 m_rmax=rmax;
74 m_max_tries=tries;
75 m_max_iter=max_iter;
76 m_prec=prec;
77 m_old_seeding=false;
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 InsertGenerator2D::setOldSeeding(bool old)
86 {
87 m_old_seeding=old;
88 }
89
90 /*!
91 generate packing
92
93 \param vol a pointer to the volume in which the packing is generated
94 \param ntable a pointer to the neighbour table used
95 \param gid particle group id
96 \param tag the particle tag
97 */
98 void InsertGenerator2D::generatePacking (AVolume2D* vol,MNTable2D* ntable,int gid, int tag)
99 {
100 // use this-> to accomodate virtual seed/fill functions
101 this->seedParticles(vol,ntable,gid,tag);
102 this->fillIn(vol,ntable,gid,tag);
103 }
104
105 /*!
106 seed the area with particles
107
108 \param vol a pointer to the packing volume
109 \param ntable a pointer to the neighbour table used
110 \param gid particle group id
111 \param tag the particle tag
112 */
113 void InsertGenerator2D::seedParticles(AVolume2D* vol,MNTable2D* ntable,int gid, int tag)
114 {
115 // get bounding box
116 pair<Vector3,Vector3> bbx=vol->getBoundingBox();
117 double dx=(bbx.second.X()-bbx.first.X())-2.0*m_rmin;
118 double dy=(bbx.second.Y()-bbx.first.Y())-2.0*m_rmin;
119 // get index limits for seeding
120 int imax=int(ceil(dx/(m_rmax*2.0)));
121 int jmax=int(ceil(dy/(m_rmax*sqrt(3.0))));
122 // seed positions
123 for(int i=0;i<imax;i++){
124 for(int j=0;j<jmax;j++){
125 // get position
126 double px=bbx.first.X()+m_rmin+(double(i)+0.5*double(j%2))*m_rmax*2.0;
127 double py=bbx.first.Y()+m_rmin+double(j)*sqrt(3.0)*m_rmax;
128
129 // get dist to egde
130 double dex=(bbx.second.X()-px) < (px-bbx.first.X()) ? bbx.second.X()-px : px-bbx.first.X();
131 double dey=(bbx.second.Y()-py) < (py-bbx.first.Y()) ? bbx.second.Y()-py : py-bbx.first.Y();
132
133 double de=(dex<dey) ? dex : dey;
134
135 // check max rad.
136 if(de>m_rmin){
137 // calc random radius
138 double r;
139 double jitter;
140 if(de<m_rmax) {
141 if(m_old_seeding){
142 r=m_rmin+((de-m_rmin)*((double)(rand())/(double)(RAND_MAX)));
143 jitter=0.0;
144 } else {
145 r=m_rmin+(((de-m_rmin)/2.0)*((double)(rand())/(double)(RAND_MAX)));
146 jitter=de-r;
147 }
148 } else {
149 if(m_old_seeding){
150 r=m_rmin+((m_rmax-m_rmin)*((double)(rand())/(double)(RAND_MAX)));
151 jitter=0.0;
152 } else {
153 r=m_rmin+(((m_rmax-m_rmin)/2.0)*((double)(rand())/(double)(RAND_MAX)));
154 jitter=m_rmax-r;
155 }
156 }
157 r-=m_prec;
158 // jitter position
159 double dx=jitter*(2.0*((double)(rand())/(double)(RAND_MAX))-1.0);
160 double dy=jitter*(2.0*((double)(rand())/(double)(RAND_MAX))-1.0);
161 Sphere S(Vector3(px+dx,py+dy,0.0),r);
162 bool fit_in=vol->isIn(S);
163 bool fit_check=ntable->checkInsertable(S,gid);
164 if(fit_in && fit_check){
165 S.setTag(tag);
166 ntable->insertChecked(S,gid);
167 }
168 }
169 }
170 }
171
172 }
173
174 /*!
175 Fill in particles
176
177 \param vol a pointer to the packing volume
178 \param ntable a pointer to the neighbour table used
179 \param gid particle group id
180 \param tag the particle tag
181 */
182 void InsertGenerator2D::fillIn(AVolume2D* vol,MNTable2D* ntable,int gid, int tag)
183 {
184 Sphere nsph;
185
186 int total_tries=0;
187 int count_insert=0;
188
189 int nvol=vol->getNumberSubVolumes();
190 for(int ivol=0;ivol<nvol;ivol++){
191 int countfail=0; // number of failed attenpts since last successfull insertion
192 while(countfail<m_max_tries){
193 bool findfit=false;
194 Vector3 P=vol->getAPoint(ivol); // get random point within volume
195 multimap<double,const Sphere*> close_particles=ntable->getSpheresClosestTo(P,3); // get 3 nearest spheres
196 map<double,const Line2D*> close_lines=vol->getClosestPlanes(P,2); // get 2 nearest planes
197 map<double,const AGeometricObject*> close_objects=vol->getClosestObjects(P,2); // get 2 nearest "objects"
198
199
200 map<double,const AGeometricObject*> geomap;
201 geomap.insert(close_particles.begin(),close_particles.end());
202 geomap.insert(close_lines.begin(),close_lines.end());
203 geomap.insert(close_objects.begin(),close_objects.end());
204
205 if(geomap.size()>=3){
206 map<double,const AGeometricObject*>::iterator iter=geomap.begin();
207 const AGeometricObject* GO1=iter->second;iter++;
208 const AGeometricObject* GO2=iter->second;iter++;
209 const AGeometricObject* GO3=iter->second;
210 nsph=FitSphere2D(GO1,GO2,GO3,P,m_max_iter,m_prec);
211 findfit=true;
212 }
213
214 if(findfit){
215 // check if within radius range
216 bool is_radius=(m_rmin<nsph.Radius()) && (m_rmax>nsph.Radius());
217
218 // check inside volume, intersections ...
219 bool fit=vol->isIn(nsph) && ntable->checkInsertable(nsph,gid);
220
221 if(fit && is_radius){ // acceptable particle
222 nsph.setTag(tag);
223 ntable->insertChecked(nsph,gid); // insert
224 count_insert++;
225 if((count_insert%100)==0) std::cout << "inserted: " << count_insert << std::endl;
226 total_tries+=countfail;
227 if(countfail>m_max_tries/10) std::cout << countfail << " tries" << std::endl;
228 countfail=0; // reset failure counter
229 } else countfail++;
230 } else countfail++;
231 }
232 }
233 std::cout << "total tries: " << total_tries << std::endl;
234 }
235
236 ostream& operator << (ostream& ost,const InsertGenerator2D& T)
237 {
238 return ost;
239 }