public bool GenerateOctree(MeshData mesh) { if (mesh == null) { return(false); } // Create a list of triangles from the list of faces in the model List <Triangle> triangles = new List <Triangle>(); for (int i = 0; i < mesh.Tris.Length; i++) { Tri face = mesh.Tris[i]; Triangle tri = new Triangle(); tri.v0 = mesh.Vertices[face.P1.Vertex]; tri.v1 = mesh.Vertices[face.P2.Vertex]; tri.v2 = mesh.Vertices[face.P3.Vertex]; triangles.Add(tri); } // Determine the axis-aligned bounding box for the triangles Vector3 center; CreateUniformBoundingBox(triangles, out MeshBounds, out VoxelBounds, out center, out SideLength); { SmallestVoxelSideLength = SideLength; for (int i = 1; i < m_maxLevels; i++) { SmallestVoxelSideLength *= 0.5; } VoxelSize = new Vector3i(); VoxelSize.X = (Int32)Math.Pow(2, m_maxLevels); VoxelSize.Y = (Int32)Math.Pow(2, m_maxLevels); VoxelSize.Z = (Int32)Math.Pow(2, m_maxLevels); } m_root = new VoxelizingOctreeCell(this, null, center, SideLength, 0); m_root.Root = m_root; m_root.Triangles = new List <Triangle>(triangles); m_root.Status = CellStatus.IntersectingBounds; m_root.RecursiveSubdivide(m_maxLevels - 1); WorldVoxelOffset = new Vector3i( 0 - Root.VoxelBounds.MinX, 0 - Root.VoxelBounds.MinY, 0 - Root.VoxelBounds.MinZ); return(true); }
// Takes an array of points and returns an array of triangles. // The points form an arbitrary polygon. static Tri[] Triangulate(MeshPoint[] ps) { List <Tri> ts = new List <Tri>(); if (ps.Length < 3) { throw new Exception("Invalid shape! Must have >2 points"); } MeshPoint lastButOne = ps[1]; MeshPoint lastButTwo = ps[0]; for (int i = 2; i < ps.Length; i++) { Tri t = new Tri(lastButTwo, lastButOne, ps[i]); lastButOne = ps[i]; lastButTwo = ps[i - 1]; ts.Add(t); } return(ts.ToArray()); }
public bool Open(string meshFile, WindingOrder order) { if (Context != null) { Context.Dispose(); Context = null; } Context = new VoxelizationContext(); try { IImporter importer = IOFactory.ImporterFactory(meshFile); Context.CurrentMeshFile = meshFile; Context.CurrentMesh = importer.Load(meshFile); } catch (IOException) { Logger.DisplayError("Unable to open the file: " + meshFile); return(false); } Context.Octree = new VoxelizingOctree(Input.OctreeLevels); Context.Octree.GenerateOctree(Context.CurrentMesh); Vector4[] orignalVertices = new Vector4[Context.CurrentMesh.Vertices.Length]; for (int i = 0; i < orignalVertices.Length; i++) { orignalVertices[i] = new Vector4(Context.CurrentMesh.Vertices[i], 1); } int[] originalIndicies = new int[Context.CurrentMesh.Tris.Length * 3]; if (order == WindingOrder.CounterClockwise) { for (int i = 0; i < Context.CurrentMesh.Tris.Length; i++) { Tri t = Context.CurrentMesh.Tris[i]; int index = i * 3; originalIndicies[index + 0] = t.P1.Vertex; originalIndicies[index + 1] = t.P2.Vertex; originalIndicies[index + 2] = t.P3.Vertex; } } else if (order == WindingOrder.Clockwise) { for (int i = 0; i < Context.CurrentMesh.Tris.Length; i++) { Tri t = Context.CurrentMesh.Tris[i]; int index = i * 3; originalIndicies[index + 0] = t.P1.Vertex; originalIndicies[index + 2] = t.P2.Vertex; originalIndicies[index + 1] = t.P3.Vertex; } } if (Context.OriginalMesh != null) { Context.OriginalMesh.Dispose(); Context.OriginalMesh = null; } Mesh mesh = new Mesh(); mesh.Indicies = originalIndicies; mesh.Vertices = orignalVertices; Context.OriginalMesh = new RenderableMesh(mesh, true); return(true); }