diff options
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r-- | OpenSim/Framework/Util.cs | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 8e4a953..c103c5c 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -262,10 +262,7 @@ namespace OpenSim.Framework | |||
262 | /// <returns>The distance between the two vectors</returns> | 262 | /// <returns>The distance between the two vectors</returns> |
263 | public static double GetDistanceTo(Vector3 a, Vector3 b) | 263 | public static double GetDistanceTo(Vector3 a, Vector3 b) |
264 | { | 264 | { |
265 | float dx = a.X - b.X; | 265 | return Vector3.Distance(a,b); |
266 | float dy = a.Y - b.Y; | ||
267 | float dz = a.Z - b.Z; | ||
268 | return Math.Sqrt(dx * dx + dy * dy + dz * dz); | ||
269 | } | 266 | } |
270 | 267 | ||
271 | /// <summary> | 268 | /// <summary> |
@@ -277,10 +274,7 @@ namespace OpenSim.Framework | |||
277 | /// <returns></returns> | 274 | /// <returns></returns> |
278 | public static bool DistanceLessThan(Vector3 a, Vector3 b, double amount) | 275 | public static bool DistanceLessThan(Vector3 a, Vector3 b, double amount) |
279 | { | 276 | { |
280 | float dx = a.X - b.X; | 277 | return Vector3.DistanceSquared(a,b) < (amount * amount); |
281 | float dy = a.Y - b.Y; | ||
282 | float dz = a.Z - b.Z; | ||
283 | return (dx*dx + dy*dy + dz*dz) < (amount*amount); | ||
284 | } | 278 | } |
285 | 279 | ||
286 | /// <summary> | 280 | /// <summary> |
@@ -381,15 +375,17 @@ namespace OpenSim.Framework | |||
381 | get { return randomClass; } | 375 | get { return randomClass; } |
382 | } | 376 | } |
383 | 377 | ||
378 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
384 | public static ulong UIntsToLong(uint X, uint Y) | 379 | public static ulong UIntsToLong(uint X, uint Y) |
385 | { | 380 | { |
386 | return Utils.UIntsToLong(X, Y); | 381 | return ((ulong)X << 32) | (ulong)Y; |
387 | } | 382 | } |
388 | 383 | ||
389 | // Regions are identified with a 'handle' made up of its world coordinates packed into a ulong. | 384 | // Regions are identified with a 'handle' made up of its world coordinates packed into a ulong. |
390 | // Region handles are based on the coordinate of the region corner with lower X and Y | 385 | // Region handles are based on the coordinate of the region corner with lower X and Y |
391 | // var regions need more work than this to get that right corner from a generic world position | 386 | // var regions need more work than this to get that right corner from a generic world position |
392 | // this corner must be on a grid point | 387 | // this corner must be on a grid point |
388 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
393 | public static ulong RegionWorldLocToHandle(uint X, uint Y) | 389 | public static ulong RegionWorldLocToHandle(uint X, uint Y) |
394 | { | 390 | { |
395 | ulong handle = X & 0xffffff00; // make sure it matchs grid coord points. | 391 | ulong handle = X & 0xffffff00; // make sure it matchs grid coord points. |
@@ -398,6 +394,7 @@ namespace OpenSim.Framework | |||
398 | return handle; | 394 | return handle; |
399 | } | 395 | } |
400 | 396 | ||
397 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
401 | public static ulong RegionGridLocToHandle(uint X, uint Y) | 398 | public static ulong RegionGridLocToHandle(uint X, uint Y) |
402 | { | 399 | { |
403 | ulong handle = X; | 400 | ulong handle = X; |
@@ -406,12 +403,14 @@ namespace OpenSim.Framework | |||
406 | return handle; | 403 | return handle; |
407 | } | 404 | } |
408 | 405 | ||
406 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
409 | public static void RegionHandleToWorldLoc(ulong handle, out uint X, out uint Y) | 407 | public static void RegionHandleToWorldLoc(ulong handle, out uint X, out uint Y) |
410 | { | 408 | { |
411 | X = (uint)(handle >> 32); | 409 | X = (uint)(handle >> 32); |
412 | Y = (uint)(handle & 0xfffffffful); | 410 | Y = (uint)(handle & 0xfffffffful); |
413 | } | 411 | } |
414 | 412 | ||
413 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
415 | public static void RegionHandleToRegionLoc(ulong handle, out uint X, out uint Y) | 414 | public static void RegionHandleToRegionLoc(ulong handle, out uint X, out uint Y) |
416 | { | 415 | { |
417 | X = (uint)(handle >> 40) & 0x00ffffffu; // bring from higher half, divide by 256 and clean | 416 | X = (uint)(handle >> 40) & 0x00ffffffu; // bring from higher half, divide by 256 and clean |
@@ -421,12 +420,14 @@ namespace OpenSim.Framework | |||
421 | 420 | ||
422 | // A region location can be 'world coordinates' (meters) or 'region grid coordinates' | 421 | // A region location can be 'world coordinates' (meters) or 'region grid coordinates' |
423 | // grid coordinates have a fixed step of 256m as defined by viewers | 422 | // grid coordinates have a fixed step of 256m as defined by viewers |
423 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
424 | public static uint WorldToRegionLoc(uint worldCoord) | 424 | public static uint WorldToRegionLoc(uint worldCoord) |
425 | { | 425 | { |
426 | return worldCoord >> 8; | 426 | return worldCoord >> 8; |
427 | } | 427 | } |
428 | 428 | ||
429 | // Convert a region's 'region grid coordinate' to its 'world coordinate'. | 429 | // Convert a region's 'region grid coordinate' to its 'world coordinate'. |
430 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
430 | public static uint RegionToWorldLoc(uint regionCoord) | 431 | public static uint RegionToWorldLoc(uint regionCoord) |
431 | { | 432 | { |
432 | return regionCoord << 8; | 433 | return regionCoord << 8; |
@@ -576,14 +577,15 @@ namespace OpenSim.Framework | |||
576 | } | 577 | } |
577 | 578 | ||
578 | // Clamp the maximum magnitude of a vector | 579 | // Clamp the maximum magnitude of a vector |
580 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
579 | public static Vector3 ClampV(Vector3 x, float max) | 581 | public static Vector3 ClampV(Vector3 x, float max) |
580 | { | 582 | { |
581 | float lenSq = x.LengthSquared(); | 583 | float lenSq = x.LengthSquared(); |
582 | if (lenSq > (max * max)) | 584 | if (lenSq > (max * max)) |
583 | { | 585 | { |
584 | x = x / x.Length() * max; | 586 | lenSq = max / (float)Math.Sqrt(lenSq); |
587 | x = x * lenSq; | ||
585 | } | 588 | } |
586 | |||
587 | return x; | 589 | return x; |
588 | } | 590 | } |
589 | 591 | ||
@@ -826,8 +828,8 @@ namespace OpenSim.Framework | |||
826 | 828 | ||
827 | private static byte[] ComputeMD5Hash(string data, Encoding encoding) | 829 | private static byte[] ComputeMD5Hash(string data, Encoding encoding) |
828 | { | 830 | { |
829 | MD5 md5 = MD5.Create(); | 831 | using(MD5 md5 = MD5.Create()) |
830 | return md5.ComputeHash(encoding.GetBytes(data)); | 832 | return md5.ComputeHash(encoding.GetBytes(data)); |
831 | } | 833 | } |
832 | 834 | ||
833 | /// <summary> | 835 | /// <summary> |
@@ -1915,7 +1917,9 @@ namespace OpenSim.Framework | |||
1915 | string ru = String.Empty; | 1917 | string ru = String.Empty; |
1916 | 1918 | ||
1917 | if (Environment.OSVersion.Platform == PlatformID.Unix) | 1919 | if (Environment.OSVersion.Platform == PlatformID.Unix) |
1918 | ru = "Unix/Mono"; | 1920 | { |
1921 | ru = "Unix/Mono"; | ||
1922 | } | ||
1919 | else | 1923 | else |
1920 | if (Environment.OSVersion.Platform == PlatformID.MacOSX) | 1924 | if (Environment.OSVersion.Platform == PlatformID.MacOSX) |
1921 | ru = "OSX/Mono"; | 1925 | ru = "OSX/Mono"; |
@@ -3025,16 +3029,36 @@ namespace OpenSim.Framework | |||
3025 | return tcA - tcB; | 3029 | return tcA - tcB; |
3026 | } | 3030 | } |
3027 | 3031 | ||
3032 | public static long GetPhysicalMemUse() | ||
3033 | { | ||
3034 | return System.Diagnostics.Process.GetCurrentProcess().WorkingSet64; | ||
3035 | } | ||
3036 | |||
3028 | // returns a timestamp in ms as double | 3037 | // returns a timestamp in ms as double |
3029 | // using the time resolution avaiable to StopWatch | 3038 | // using the time resolution avaiable to StopWatch |
3039 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
3030 | public static double GetTimeStamp() | 3040 | public static double GetTimeStamp() |
3031 | { | 3041 | { |
3032 | return (double)Stopwatch.GetTimestamp() * TimeStampClockPeriod; | 3042 | return Stopwatch.GetTimestamp() * TimeStampClockPeriod; |
3033 | } | 3043 | } |
3034 | 3044 | ||
3045 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
3035 | public static double GetTimeStampMS() | 3046 | public static double GetTimeStampMS() |
3036 | { | 3047 | { |
3037 | return (double)Stopwatch.GetTimestamp() * TimeStampClockPeriodMS; | 3048 | return Stopwatch.GetTimestamp() * TimeStampClockPeriodMS; |
3049 | } | ||
3050 | |||
3051 | // doing math in ticks is usefull to avoid loss of resolution | ||
3052 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
3053 | public static long GetTimeStampTicks() | ||
3054 | { | ||
3055 | return Stopwatch.GetTimestamp(); | ||
3056 | } | ||
3057 | |||
3058 | [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] | ||
3059 | public static double TimeStampTicksToMS(long ticks) | ||
3060 | { | ||
3061 | return ticks * TimeStampClockPeriodMS; | ||
3038 | } | 3062 | } |
3039 | 3063 | ||
3040 | /// <summary> | 3064 | /// <summary> |