Advertisement
NPSF3000

CullingSystemThreaded

Dec 27th, 2011
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7.  
  8. //  Very simple, very stupid - but demonstrates a point about paying attention to real world data, not theoretical n^2
  9. namespace CullingSystemThreaded
  10. {
  11.     class Program
  12.     {
  13.  
  14.         static int pointCount = 1000;
  15.  
  16.  
  17.         struct Vector3
  18.         {
  19.             public float x;
  20.             public float y;
  21.             public float z;
  22.  
  23.             public Vector3(float x, float y, float z)
  24.             {
  25.                 this.x = x;
  26.                 this.y = y;
  27.                 this.z = z;
  28.             }
  29.             public Vector3(float randomDistance)
  30.             {
  31.                 this.x = (float)rnd.NextDouble() * randomDistance;
  32.                 this.y = (float)rnd.NextDouble() * randomDistance;
  33.                 this.z = (float)rnd.NextDouble() * randomDistance;
  34.             }
  35.  
  36.             public static Vector3 operator -(Vector3 a, Vector3 b)
  37.             {
  38.                 return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  39.             }
  40.  
  41.             public float sqrMagnitude()
  42.             {
  43.                 return x * x + y * y + z * z;
  44.             }
  45.         }
  46.        
  47.         struct CullingData
  48.         {
  49.             public Vector3 position;
  50.             public float sqrSight;
  51.  
  52.             public CullingData(Vector3 position, float sight)
  53.             {
  54.                 this.position = position;
  55.                 this.sqrSight = sight * sight;
  56.             }
  57.  
  58.         }
  59.         static Random rnd = new Random();
  60.  
  61.         static void Main(string[] args)
  62.         {
  63.             while (true)
  64.             {
  65.                 Console.ReadLine();
  66.  
  67.                 var data = new CullingData[pointCount];
  68.  
  69.                 for (int i = 0; i < pointCount; i++)
  70.                     data[i] = new CullingData(new Vector3(1000), rnd.Next(50, 100));
  71.  
  72.                 var sw = Stopwatch.StartNew();
  73.  
  74.                 var pairs = new List<CullingData[]>();
  75.  
  76.                 Parallel.ForEach(data, point =>
  77.                 {
  78.                     foreach (var opoint in data)
  79.                         if ((point.position - opoint.position).sqrMagnitude() < point.sqrSight) lock (pairs) { pairs.Add(new[] { point, opoint }); };
  80.                 });
  81.  
  82.                 sw.Stop();
  83.  
  84.                 Console.WriteLine("Time Elapsed: {0}ms, pairs found: " + pairs.Count, sw.ElapsedTicks*1000f /Stopwatch.Frequency);
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement