public static string RunSequential(List <IPolygon> polygons) { var pool = new TrianglePool(); var predicates = new RobustPredicates(); var config = new Configuration(); config.Predicates = () => predicates; config.TrianglePool = () => pool.Restart(); var mesher = new GenericMesher(config); var result = new MeshResult(); foreach (var poly in polygons) { var mesh = mesher.Triangulate(poly); ProcessMesh(mesh, result); } pool.Clear(); //Console.WriteLine("Total number of triangles processed: {0}", result.NumberOfTriangles); var sequential = $"Total number of triangles processed: {result.NumberOfTriangles}"; if (result.Invalid > 0) { //Console.WriteLine(" Number of invalid triangulations: {0}", result.Invalid); sequential += $"Number of invalid triangulations: {result.Invalid}"; } return(sequential); }
private static void ProcessMesh(IMesh mesh, MeshResult result) { result.NumberOfTriangles += mesh.Triangles.Count; if (!MeshValidator.IsConsistent((Mesh)mesh)) { result.Invalid += 1; } }
public static string RunParallel(List <IPolygon> polygons) { var queue = new ConcurrentQueue <IPolygon>(polygons); int concurrencyLevel = Environment.ProcessorCount; var tasks = new Task <MeshResult> [concurrencyLevel]; for (int i = 0; i < concurrencyLevel; i++) { tasks[i] = Task.Run(() => { // Each task has it's own triangle pool and predicates instance. var pool = new TrianglePool(); var predicates = new RobustPredicates(); var config = new Configuration(); // The factory methods return the above instances. config.Predicates = () => predicates; config.TrianglePool = () => pool.Restart(); IPolygon poly; var mesher = new GenericMesher(config); var result = new MeshResult(); while (queue.Count > 0) { if (queue.TryDequeue(out poly)) { var mesh = mesher.Triangulate(poly); ProcessMesh(mesh, result); } } pool.Clear(); return(result); }); } Task.WaitAll(tasks); int numberOfTriangles = 0; int invalid = 0; for (int i = 0; i < concurrencyLevel; i++) { var result = tasks[i].Result; numberOfTriangles += result.NumberOfTriangles; invalid += result.Invalid; } string parallel = $"Total number of triangles processed: {numberOfTriangles}"; //Console.WriteLine("Total number of triangles processed: {0}", numberOfTriangles); if (invalid > 0) { //Console.WriteLine(" Number of invalid triangulations: {0}", invalid); parallel += $"\tNumber of invalid triangulations: {invalid}"; } return(parallel); }