Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.Threading.Tasks;
- // Very simple, very stupid - but demonstrates a point about paying attention to real world data, not theoretical n^2
- namespace CullingSystemThreaded
- {
- class Program
- {
- static int pointCount = 1000;
- struct Vector3
- {
- public float x;
- public float y;
- public float z;
- public Vector3(float x, float y, float z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public Vector3(float randomDistance)
- {
- this.x = (float)rnd.NextDouble() * randomDistance;
- this.y = (float)rnd.NextDouble() * randomDistance;
- this.z = (float)rnd.NextDouble() * randomDistance;
- }
- public static Vector3 operator -(Vector3 a, Vector3 b)
- {
- return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
- }
- public float sqrMagnitude()
- {
- return x * x + y * y + z * z;
- }
- }
- struct CullingData
- {
- public Vector3 position;
- public float sqrSight;
- public CullingData(Vector3 position, float sight)
- {
- this.position = position;
- this.sqrSight = sight * sight;
- }
- }
- static Random rnd = new Random();
- static void Main(string[] args)
- {
- while (true)
- {
- Console.ReadLine();
- var data = new CullingData[pointCount];
- for (int i = 0; i < pointCount; i++)
- data[i] = new CullingData(new Vector3(1000), rnd.Next(50, 100));
- var sw = Stopwatch.StartNew();
- var pairs = new List<CullingData[]>();
- Parallel.ForEach(data, point =>
- {
- foreach (var opoint in data)
- if ((point.position - opoint.position).sqrMagnitude() < point.sqrSight) lock (pairs) { pairs.Add(new[] { point, opoint }); };
- });
- sw.Stop();
- Console.WriteLine("Time Elapsed: {0}ms, pairs found: " + pairs.Count, sw.ElapsedTicks*1000f /Stopwatch.Frequency);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement