public static void SubdivideHexahedron(Hexahedron hex) { // 0: centroid // 1-6: face centroids // 7-18: edge midpoints // 19-26: original hexahedron vertices Vector3[] CachedVertices = new Vector3[27]; // 0 Vector3 sum = Vector3.zero; for (int i = 0; i < 8; i++) { sum += hex.Vertices[i]; } CachedVertices[0] = sum / 8; // 1-6 for (int i = 0; i < 6; i++) { sum = Vector3.zero; for (int j = 0; j < 4; j++) { sum += hex.Vertices[Lookups.HexahedronFaces[i, j]]; } CachedVertices[1 + i] = sum / 4; } // 7-18 for (int i = 0; i < 12; i++) { sum = Vector3.zero; for (int j = 0; j < 2; j++) { sum += hex.Vertices[Lookups.HexahedronEdgePairs[i, j]]; } CachedVertices[7 + i] = sum / 2; } // 19-26 for (int i = 0; i < 8; i++) { CachedVertices[19 + i] = hex.Vertices[i]; } Hexahedron[] SubdividedHexahedra = new Hexahedron[9]; for (int i = 0; i < 8; i++) { SubdividedHexahedra[i] = new Hexahedron(); SubdividedHexahedra[i].Vertices = new Vector3[8]; SubdividedHexahedra[i].IsLeaf = true; for (int j = 0; j < 8; j++) { SubdividedHexahedra[i].Vertices[j] = CachedVertices[Lookups.HexahedronSubHexahedra[i, j]]; } } hex.Children = SubdividedHexahedra; hex.IsLeaf = false; }
public static List <Hexahedron> GenerateSubdividedHexahedronList(Hexahedron hex, int depth) { RecursiveSubdivideHexahedron(hex, depth); List <Hexahedron> Leaves = new List <Hexahedron>(); PopulateSubdividedHexahedronList(hex, Leaves); return(Leaves); }
public static void PopulateSubdividedHexahedronList(Hexahedron hex, List <Hexahedron> listToPopulate) { if (hex.IsLeaf) { listToPopulate.Add(hex); } else { for (int i = 0; i < 8; i++) { PopulateSubdividedHexahedronList(hex.Children[i], listToPopulate); } } }
public static void RecursiveSubdivideHexahedron(Hexahedron hex, int n) { if (n <= 0) { return; } else { SubdivideHexahedron(hex); for (int i = 0; i < 8; i++) { RecursiveSubdivideHexahedron(hex.Children[i], n - 1); } } }
public static Hexahedron[] ExtractHexahedra(Node node) { // step 1: get all the faces of the tetrahedron Face[] faces = new Face[4]; for (int i = 0; i < 4; i++) { faces[i] = new Face(); faces[i].Vertices = new Vector3[3]; Vector3 sum = Vector3.zero; for (int j = 0; j < 3; j++) { faces[i].Vertices[j] = node.Vertices[Lookups.TetrahedronFaces[i, j]]; sum += faces[i].Vertices[j]; } faces[i].Centroid = sum / 3; } // step 2: calculate centroid of tetrahedron Vector3 centroid = Vector3.zero; for (int i = 0; i < 4; i++) { centroid += node.Vertices[i]; } centroid /= 4; // step 3: each combination of faces is a new hexehedron Hexahedron[] hexahedra = new Hexahedron[4]; for (int i = 0; i < 4; i++) { hexahedra[i] = new Hexahedron(); hexahedra[i].Vertices = new Vector3[8]; hexahedra[i].Vertices[0] = centroid; Face[] hexfaces = new Face[3]; hexahedra[i].BaseFaces = faces; for (int j = 0; j < 3; j++) { // calculate centroid of each face. hexfaces[j] = faces[Lookups.TetrahedronFaces[i, j]]; } for (int j = 0; j < 3; j++) { hexahedra[i].Vertices[j + 1] = hexfaces[j].Centroid; // for each face combination, find the midpoint of the shared edge Face face1 = hexfaces[Lookups.FaceCombinations[j, 0]]; Face face2 = hexfaces[Lookups.FaceCombinations[j, 1]]; int currentPoint = 0; Vector3[] sharedPoints = new Vector3[2]; for (int z = 0; z < 3; z++) { for (int ii = 0; ii < 3; ii++) { if (face1.Vertices[z] == face2.Vertices[ii] && !(currentPoint >= 2)) { sharedPoints[currentPoint] = face1.Vertices[z]; currentPoint++; } } } hexahedra[i].Vertices[j + 4] = Utility.FindMidpoint(sharedPoints[0], sharedPoints[1]); } // find shared point between all the faces for (int j = 0; j < 3; j++) { for (int z = 0; z < 3; z++) { for (int ii = 0; ii < 3; ii++) { if (hexfaces[0].Vertices[j] == hexfaces[1].Vertices[z] && hexfaces[0].Vertices[j] == hexfaces[2].Vertices[ii]) { hexahedra[i].Vertices[7] = hexfaces[0].Vertices[j]; } } } } Vector3[] CachedVertices = new Vector3[12]; for (int j = 0; j < 8; j++) { CachedVertices[j] = hexahedra[i].Vertices[Lookups.HexahedronVertexReorder[j]]; } hexahedra[i].Vertices = CachedVertices; } return(hexahedra); }