private void populateMatrix(List <Face> faces, List <Face>[,,] matrix, Extent size) { Trace.TraceInformation("Partitioning Faces."); int xLength = matrix.GetLength(0); int yLength = matrix.GetLength(1); int zLength = matrix.GetLength(2); // World to cube ratios double xOffset = 0 - size.XMin; double xRatio = xLength / (size.XSize); double yOffset = 0 - size.YMin; double yRatio = yLength / (size.YSize); double zOffset = 0 - size.ZMin; double zRatio = zLength / (size.ZSize); // Initialize matrix SpatialUtilities.EnumerateSpace(xLength, yLength, zLength, (x, y, z) => matrix[x, y, z] = new List <Face>()); foreach (var face in faces) { List <int> used = new List <int>(); for (int i = 0; i < 3; i++) { Vertex vertex = VertexList[face.VertexIndexList[i] - 1]; int x = (int)Math.Floor((vertex.X + xOffset) * xRatio); int y = (int)Math.Floor((vertex.Y + yOffset) * yRatio); int z = (int)Math.Floor((vertex.Z + zOffset) * zRatio); if (x == xLength) { x--; } if (y == yLength) { y--; } if (z == zLength) { z--; } int hash = x * 10000 + y * 100 + z; if (!used.Contains(hash)) { matrix[x, y, z].Add(face); used.Add(hash); } } } // Crop all the cubes SpatialUtilities.EnumerateSpace(xLength, yLength, zLength, (x, y, z) => CropCube(matrix[x, y, z], x, y, z, matrix, size)); }
public void GenerateCubes(string outputPath, SlicingOptions options) { CubeMetadata metadata = new CubeMetadata(size) { WorldBounds = ObjInstance.Size, VirtualWorldBounds = options.ForceCubicalCubes ? ObjInstance.CubicalSize : ObjInstance.Size, VertexCount = ObjInstance.VertexList.Count }; // Configure texture slicing metadata if (options.RequiresTextureProcessing()) { metadata.TextureSetSize = new Vector2(options.TextureSliceX, options.TextureSliceY); } else { metadata.TextureSetSize = new Vector2(1, 1); } // Create texture if there is one if (!string.IsNullOrEmpty(options.Texture)) { Texture t = new Texture(this.ObjInstance, options.Texture); options.TextureInstance = t; } // Generate the data if (options.Debug) { SpatialUtilities.EnumerateSpace(metadata.TextureSetSize, (x, y) => { var vertexCounts = GenerateCubesForTextureTileAsync(outputPath, new Vector2(x, y), options, new CancellationToken()).Result; foreach (var cube in vertexCounts.Keys) { metadata.CubeExists[cube.X, cube.Y, cube.Z] = vertexCounts[cube] > 0; } }); } else { SpatialUtilities.EnumerateSpaceParallel(metadata.TextureSetSize, (x, y) => { var vertexCounts = GenerateCubesForTextureTileAsync(outputPath, new Vector2(x, y), options, new CancellationToken()).Result; foreach (var cube in vertexCounts.Keys) { metadata.CubeExists[cube.X, cube.Y, cube.Z] = vertexCounts[cube] > 0; } }); } // Write out some json metadata string metadataPath = Path.Combine(outputPath, "metadata.json"); if (File.Exists(metadataPath)) { File.Delete(metadataPath); } string metadataString = JsonConvert.SerializeObject(metadata); File.WriteAllText(metadataPath, metadataString); }