Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation |
previous class next class | frames no frames |
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286 00287 00288 00289 00290 00291 00292 00293 00294 00295 00296 00297 00298 00299 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 00316 00317 00318 00319 00320 00321 00322 00323 00324 00325 00326 00327 00328 00329 00330 00331 00332 00333 00334 00335 00336 00337 00338 00339 00340 00341 00342 00343 00344 00345 00346 00347 00348 00349 00350 00351 00352 00353 00354 00355 00356 00357 00358 00359 00360 00361 00362 00363 00364 00365 00366 00367 00368 00369 00370 00371 00372 00373 00374 00375 00376 00377 00378 00379 00380 00381 00382 00383 00384 00385 00386 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 00397 00398 00399 00400 00401 00402 00403 00404 00405 00406 00407 00408 00409 00410 00411 00412 00413 00414 00415 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432 00433 00434 00435 00436 00437 00438 00439 |
// ============================================================================ // The base class for the diffusion based source simulation. // // This class simulates the 2D diffusion at a timepoint. Since diffusion will // continue until it reachs the same density, we only simulate the density // distribution at a time point that is specified by the diffusuin range, PropaRange. // We use grid cell based diffusion to calculate the source density that is // stored in a byte array. 0~255 corresponds to 0 to initial density. // It uses linear interpolation to find density at any point. The current class // propagates only on the x-y plan. It assumes the same density in the vertical // direction in the vertical valid range. It's possible to propagate in 3D. But // we need large array to store the densities. // ============================================================================ class PropaSource extends AmbientSource abstract placeable; struct Cell { var vector Location; var float Density; }; // The resolution -- the grid cell's size in meter. var() float CellSize; // The initial density at the source in percent. var() float IniDensity; // The environment density in percent. var() float EnvDensity; // The maximum diffusion range in meter. var() float PropaRange; // The valid range in vertical direction in meter var() Range VertRange; var float uuCellSize; var float uuPropaRange; var Range uuVertRange; // The decline factor represents how the source propagates... var float decline; // The factor used to convert byte (0~255) to density (0~IniDensity) var float byteDens; // The width of the density array var int width; // The center (the source) of the density array var int center; // The directions we will propagate var vector dirs[8]; // The array stores the hits infor in the 8 dircetions var byte hits[8]; // The density array var array<byte> densities; // The frontier cells used in propagation calculation var array<Cell> frontier; var int frLength; // The valid range of the density array (density>0) var RangeVector validRange; // boolean indicates whether we have tailored the density array to store only // the density within the valid range. var bool bReleased; // For debug purpose var bool bDebug; // Prepare the parameters and then call Propagation() to calculat densities. // It then tailored the density array to save memory. event PreBeginPlay() { local int i; local Cell theCell; local float CalculationTime; Super.PreBeginPlay(); center = int(PropaRange/CellSize); width = 2*center + 1; decline = (IniDensity-EnvDensity)/uuPropaRange; byteDens = 255/(IniDensity-EnvDensity); Clock(CalculationTime); // init densities array for (i=0;i<width*width;i++) densities[i] = 0; frontier[0] = theCell; frontier[0].Location = Location; frontier[0].Density = IniDensity; validRange.X.Min = Location.X; validRange.X.Max = Location.X; validRange.Y.Min = Location.Y; validRange.Y.Max = Location.Y; frLength = 1; setDensity(frontier[0]); Propagation(); releaseDens(); UnClock(CalculationTime); log("Calculate Density for "$ Name $" [Size:"@densities.length@"byte Time:"@CalculationTime@"ms]"); } // for testing purpose function PostBeginPlay() { //local vector loc; //local string str; if (bDebug) dumpDensities(); // Testing the densities //loc.z = Location.z; //loc.y = validRange.Y.Min; //while (loc.y<=validRange.Y.Max) { // str = ""; // loc.x = validRange.X.Min; // while (loc.x<=validRange.X.Max) { // str = str@getDensity(loc)*ByteDens; // loc.x += uuCellSize/4; // } // log(str); // loc.y += uuCellSize/4; //} } function dumpDensities() { local int i, j, w, h; local string str; log(Name@"The densities:"); h = int((validRange.Y.Max-validRange.Y.Min)/uuCellSize)+1; w = int((validRange.X.Max-validRange.X.Min)/uuCellSize)+1; for (i=0; i<h; i++) { str = " "; for (j=0; j<w; j++) str = str@densities[j+i*w]; log(Name@str); } } function ConvertParam(USARConverter converter) { if (converter==None) { uuCellSize = CellSize; uuPropaRange = PropaRange; uuVertRange = VertRange; } else { uuCellSize = converter.LengthToUU(CellSize); uuPropaRange = converter.LengthToUU(PropaRange); uuVertRange.Min = converter.LengthToUU(VertRange.Min); uuVertRange.Max = converter.LengthToUU(VertRange.Max); } } // Tailored density array to save memory function releaseDens() { local int pos, step, len1, len2; local float y; //clear dirty memory frontier.remove(0,frontier.length); // trim densities array len1 = int((Location.Y + uuPropaRange - validRange.Y.Max)/uuCellSize)*width; // bottom len2 = int((Location.X + uuPropaRange - validRange.X.Max)/uuCellSize); // right pos = densities.length - len1 - len2; densities.remove(pos,len1+len2); len1 = int((validRange.X.Min - Location.X + uuPropaRange)/uuCellSize); // left step = int((validRange.X.Max - validRange.X.Min)/uuCellSize)+1; for (y = validRange.Y.Max; y>validRange.Y.Min; y-=uuCellSize) { pos -= step + len1 + len2; densities.remove(pos,len1+len2); } len2 = int((validRange.Y.Min - Location.Y + uuPropaRange)/uuCellSize)*width; densities.remove(0,len2+len1); // we are now using the tailored array bReleased = True; width = step; } function setDensity(Cell theCell) { local int x, y; x = int((theCell.Location.X - Location.X)/uuCellSize)+center; y = int((theCell.Location.Y - Location.Y)/uuCellSize)+center; densities[x+y*width] = byte((theCell.Density-EnvDensity)*byteDens); if (theCell.Location.X > validRange.X.Max) validRange.X.Max = theCell.Location.X; else if (theCell.Location.X < validRange.X.Min) validRange.X.Min = theCell.Location.X; if (theCell.Location.Y > validRange.Y.Max) validRange.Y.Max = theCell.Location.Y; else if (theCell.Location.Y < validRange.Y.Min) validRange.Y.Min = theCell.Location.Y; } function setDensity2(vector loc, float density) { local int x, y; x = int((loc.X - Location.X)/uuCellSize)+center; y = int((loc.Y - Location.Y)/uuCellSize)+center; densities[x+y*width] = byte((density-EnvDensity)*byteDens); if (loc.X > validRange.X.Max) validRange.X.Max = loc.X; else if (loc.X < validRange.X.Min) validRange.X.Min = loc.X; if (loc.Y > validRange.Y.Max) validRange.Y.Max = loc.Y; else if (loc.Y < validRange.Y.Min) validRange.Y.Min = loc.Y; } // If bReleased=false, we directly return the density. This case only happens // during the propagation computation. For sensor query, bReleased should be // true, and we will check the validation and return the interpolation data. function float getDensity(Vector loc) { local int x, y, x0, y0, i; local float dx, dy, den, den2, halfCell; local Vector ref; local array<Vector> refs; if (!bReleased) { x = int((loc.X - Location.X)/uuCellSize)+center; y = int((loc.Y - Location.Y)/uuCellSize)+center; if (x<0 || x>=width || y<0 || y>=width) return EnvDensity; else return densities[x+y*width]/byteDens+EnvDensity; } halfCell = uuCellSize/2; if (loc.X<validRange.X.Min-halfCell || loc.X>validRange.X.Max+halfCell || loc.Y<validRange.Y.Min-halfCell || loc.Y>validRange.Y.Max+halfCell || loc.Z<Location.z+uuVertRange.Min || loc.Z>Location.z+uuVertRange.Max) return EnvDensity; x0 = int((loc.X - validRange.X.Min)/uuCellSize+0.5); y0 = int((loc.Y - validRange.Y.Min)/uuCellSize+0.5); dx = loc.X - validRange.X.Min - x0*uuCellSize; dy = loc.Y - validRange.Y.Min - y0*uuCellSize; //log(dx@dy@loc); x = x0; y = y0; ref.X = validRange.X.Min + x*uuCellSize; ref.Y = validRange.Y.Min + y*uuCellSize; ref.z = loc.z; if (FastTrace(ref,loc)) { ref.Z = densities[x+y*width]; refs[i++] = ref; } x = x + sign(dx); if (x>=0 && x<width) { ref.X = validRange.X.Min + x*uuCellSize; ref.Y = validRange.Y.Min + y*uuCellSize; ref.z = loc.z; if (FastTrace(ref,loc) && sign(dx)!=0) { ref.Z = densities[x+y*width]; refs[i++] = ref; } y = y + sign(dy); if (y>=0 && y<width) { ref.X = validRange.X.Min + x*uuCellSize; ref.Y = validRange.Y.Min + y*uuCellSize; ref.z = loc.z; if (FastTrace(ref,loc) && sign(dx)!=0 && sign(dy)!=0) { ref.Z = densities[x+y*width]; refs[i++] = ref; } } } x = x0; if (y>=0 && y<width) { ref.X = validRange.X.Min + x*uuCellSize; ref.Y = validRange.Y.Min + y*uuCellSize; ref.Z = loc.z; if (FastTrace(ref,loc) && sign(dy)!=0) { ref.Z = densities[x+y*width]; refs[i++] = ref; } } switch(refs.length) { case 1: //log("["$1$"]"); return refs[0].z/byteDens+EnvDensity; case 2: //log("["$2$"]"@refs[0]@refs[1]); den = ((refs[1].x-refs[0].x)*(loc.x-refs[0].x)+(refs[1].y-refs[0].y)*(loc.y-refs[0].y))/ ((refs[1].x-refs[0].x)*(refs[1].x-refs[0].x)+(refs[1].y-refs[0].y)*(refs[1].y-refs[0].y)); den = den * (refs[1].Z-refs[0].Z) + refs[0].Z; return den/byteDens+EnvDensity; case 3: //log("["$3$"]"@refs[0]@refs[1]@refs[2]); den = ((refs[0].y-refs[2].y)*(refs[1].z-refs[2].z) - (refs[1].y-refs[2].y)*(refs[0].z-refs[2].z)) * (loc.x - refs[2].x) - ((refs[0].x-refs[2].x)*(refs[1].z-refs[2].z) - (refs[1].x-refs[2].x)*(refs[0].z-refs[2].z)) * (loc.y - refs[2].y); den = -den/((refs[0].x-refs[2].x)*(refs[1].y-refs[2].y) - (refs[1].x-refs[2].x)*(refs[0].y-refs[2].y)); den = den + refs[2].z; return den/byteDens+EnvDensity; case 4: //log("["$4$"]"@refs[0]@refs[1]@refs[2]@refs[3]); den = ((refs[0].y-refs[2].y)*(refs[1].z-refs[2].z) - (refs[1].y-refs[2].y)*(refs[0].z-refs[2].z)) * (loc.x - refs[2].x) - ((refs[0].x-refs[2].x)*(refs[1].z-refs[2].z) - (refs[1].x-refs[2].x)*(refs[0].z-refs[2].z)) * (loc.y - refs[2].y); den = -den/((refs[0].x-refs[2].x)*(refs[1].y-refs[2].y) - (refs[1].x-refs[2].x)*(refs[0].y-refs[2].y)); den = den + refs[2].z; den2 = ((refs[0].y-refs[2].y)*(refs[3].z-refs[2].z) - (refs[3].y-refs[2].y)*(refs[0].z-refs[2].z)) * (loc.x - refs[2].x) - ((refs[0].x-refs[2].x)*(refs[3].z-refs[2].z) - (refs[3].x-refs[2].x)*(refs[0].z-refs[2].z)) * (loc.y - refs[2].y); den2 = -den2/((refs[0].x-refs[2].x)*(refs[3].y-refs[2].y) - (refs[3].x-refs[2].x)*(refs[0].y-refs[2].y)); den2 = den2 + refs[2].z; den += den2; den2 = ((refs[0].y-refs[1].y)*(refs[3].z-refs[1].z) - (refs[3].y-refs[1].y)*(refs[0].z-refs[1].z)) * (loc.x - refs[1].x) - ((refs[0].x-refs[1].x)*(refs[3].z-refs[1].z) - (refs[3].x-refs[1].x)*(refs[0].z-refs[1].z)) * (loc.y - refs[1].y); den2 = -den2/((refs[0].x-refs[1].x)*(refs[3].y-refs[1].y) - (refs[3].x-refs[1].x)*(refs[0].y-refs[1].y)); den2 = den2 + refs[1].z; den += den2; den2 = ((refs[2].y-refs[1].y)*(refs[3].z-refs[1].z) - (refs[3].y-refs[1].y)*(refs[2].z-refs[1].z)) * (loc.x - refs[1].x) - ((refs[2].x-refs[1].x)*(refs[3].z-refs[1].z) - (refs[3].x-refs[1].x)*(refs[2].z-refs[1].z)) * (loc.y - refs[1].y); den2 = -den2/((refs[2].x-refs[1].x)*(refs[3].y-refs[1].y) - (refs[3].x-refs[1].x)*(refs[2].y-refs[1].y)); den2 = den2 + refs[1].z; den += den2; return den/4/byteDens+EnvDensity; default: return densities[x0+y0*width]/byteDens+EnvDensity; } } function float sign(float f) { if (f>0) return 1; else if (f<0) return -1; else return 0; } // check the hit situation in the 8 directions function checkHits(vector loc, float range) { local int i; for (i=0;i<8;i++) { if (FastTrace(loc + range*dirs[i], loc)) hits[i]=0; else hits[i]=1; } } // The decline function. We can override this function to simulate different // propagation style. function float calcDens(float iniDens, float length) { return iniDens - decline*length; } // The propagation simulation function function Propagation() { local int i, j; local array<Cell> myFrt; local Cell theCell; local int myFrtLen; local vector myLoc; myFrtLen = 0; for (i=0; i<frLength; i++) { checkHits(frontier[i].Location, uuCellSize); if (bDebug) log(Name@">>"@frontier[i].Location@frontier[i].Density); for (j=0; j<8; j++) { myLoc = frontier[i].Location + dirs[j]*uuCellSize; if (getDensity(myLoc)==0) { if (hits[j]==0) { myFrt[myFrtLen] = theCell; myFrt[myFrtLen].Location = myLoc; if (j%2==0) myFrt[myFrtLen].Density = calcDens(frontier[i].Density,uuCellSize); else myFrt[myFrtLen].Density = calcDens(frontier[i].Density,uuCellSize*1.414); setDensity(myFrt[myFrtLen]); if (bDebug) log(Name@" "@myFrt[myFrtLen].Location@myFrt[myFrtLen].Density); myFrtLen += 1; } } } } frLength = 0; for (i=0; i<myFrtLen; i++) { if (Abs(myFrt[i].Location.X - Location.X)<uuPropaRange && Abs(myFrt[i].Location.Y - Location.Y)<uuPropaRange) { frontier[i] = theCell; frontier[i].Location = myFrt[i].Location; frontier[i].Density = myFrt[i].Density; frLength += 1; } } if (frLength>0) Propagation(); } defaultproperties { dirs(0)=(X= 1,Y= 0,Z=0) dirs(1)=(X= 1,Y= 1,Z=0) dirs(2)=(X= 0,Y= 1,Z=0) dirs(3)=(X= 1,Y=-1,Z=0) dirs(4)=(X= 0,Y=-1,Z=0) dirs(5)=(X=-1,Y=-1,Z=0) dirs(6)=(X=-1,Y= 0,Z=0) dirs(7)=(X=-1,Y= 1,Z=0) bDebug=False SourceType="propagation" CellSize=0.5 IniDensity=100 EnvDensity=0 PropaRange=20 VertRange=(Min=-1,Max=1) } |
Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation |
previous class next class | frames no frames |