/// <summary> /// Generates a structured mesh. /// </summary> /// <param name="bounds">Bounds of the mesh.</param> /// <param name="nx">Number of segments in x direction.</param> /// <param name="ny">Number of segments in y direction.</param> /// <returns>Mesh</returns> public static IMesh StructuredMesh(Rectangle bounds, int nx, int ny) { var polygon = new Polygon((nx + 1) * (ny + 1)); double x, y, dx, dy, left, bottom; dx = bounds.Width / nx; dy = bounds.Height / ny; left = bounds.Left; bottom = bounds.Bottom; int i, j, k, l, n = 0; // Add vertices. var points = new Vertex[(nx + 1) * (ny + 1)]; for (i = 0; i <= nx; i++) { x = left + i * dx; for (j = 0; j <= ny; j++) { y = bottom + j * dy; points[n++] = new Vertex(x, y); } } polygon.Points.AddRange(points); n = 0; // Set vertex hash and id. foreach (var v in points) { v.hash = v.id = n++; } // Add boundary segments. var segments = polygon.Segments; segments.Capacity = 2 * (nx + ny); Vertex a, b; for (j = 0; j < ny; j++) { // Left a = points[j]; b = points[j + 1]; segments.Add(new Segment(a, b, 1)); a.Label = b.Label = 1; // Right a = points[nx * (ny + 1) + j]; b = points[nx * (ny + 1) + (j + 1)]; segments.Add(new Segment(a, b, 1)); a.Label = b.Label = 1; } for (i = 0; i < nx; i++) { // Bottom a = points[(ny + 1) * i]; b = points[(ny + 1) * (i + 1)]; segments.Add(new Segment(a, b, 1)); a.Label = b.Label = 1; // Top a = points[ny + (ny + 1) * i]; b = points[ny + (ny + 1) * (i + 1)]; segments.Add(new Segment(a, b, 1)); a.Label = b.Label = 1; } // Add triangles. var triangles = new InputTriangle[2 * nx * ny]; n = 0; for (i = 0; i < nx; i++) { for (j = 0; j < ny; j++) { k = j + (ny + 1) * i; l = j + (ny + 1) * (i + 1); // Create 2 triangles in rectangle [k, l, l + 1, k + 1]. if ((i + j) % 2 == 0) { // Diagonal from bottom left to top right. triangles[n++] = new InputTriangle(k, l, l + 1); triangles[n++] = new InputTriangle(k, l + 1, k + 1); } else { // Diagonal from top left to bottom right. triangles[n++] = new InputTriangle(k, l, k + 1); triangles[n++] = new InputTriangle(l, l + 1, k + 1); } } } return(Converter.ToMesh(polygon, triangles)); }