private static void GenerateFace(OctreeNode <NodeParms> node, MeshBuilder_IntegerVertex mb, ref Int16Triple[] tempArray, BitMap3d bmp) { InitVoxelPositionForNodeRange(node.XMin, node.XMax, node.YMin, node.YMax, node.ZMin, node.ZMax, ref tempArray); //需要找到该节点的端点位置 byte cubeConfig = 0; for (int pi = 0; pi < 8; pi++) { if (InRange(bmp, tempArray[pi].X, tempArray[pi].Y, tempArray[pi].Z) && IsWhite(bmp, tempArray[pi].X, tempArray[pi].Y, tempArray[pi].Z)) { cubeConfig |= PointIndexToFlag[pi]; } } int index = 0; while (MCTable.TriTable[cubeConfig, index] != -1) { int ei1 = MCTable.TriTable[cubeConfig, index]; int ei2 = MCTable.TriTable[cubeConfig, index + 1]; int ei3 = MCTable.TriTable[cubeConfig, index + 2]; Int16Triple p1 = GetIntersetedPointAtEdge(node, ei1, OctreeTable.NormalTypeIdToNormal[node.Parms.NormalTypeId], node.Parms.D); Int16Triple p2 = GetIntersetedPointAtEdge(node, ei2, OctreeTable.NormalTypeIdToNormal[node.Parms.NormalTypeId], node.Parms.D); Int16Triple p3 = GetIntersetedPointAtEdge(node, ei3, OctreeTable.NormalTypeIdToNormal[node.Parms.NormalTypeId], node.Parms.D); mb.AddTriangle(p1, p2, p3); index += 3; } }//对非叶子节点的超体元的抽取需要参考MCTable求取被截断边的信息
public void AddTriangle(Int16Triple p0, Int16Triple p1, Int16Triple p2) { int p0i; int p1i; int p2i; int index = 0; bool hasValue; hasValue = hashMap.GetHashValue(p0.X, p0.Y, p0.Z, ref index); if (!hasValue) { p0i = mesh.AddVertex(new Point3d(p0.X, p0.Y, p0.Z)); hashMap.SetHashValue(p0.X, p0.Y, p0.Z, p0i); } else { p0i = index; } hasValue = hashMap.GetHashValue(p1.X, p1.Y, p1.Z, ref index); if (!hasValue) { p1i = mesh.AddVertex(new Point3d(p1.X, p1.Y, p1.Z)); hashMap.SetHashValue(p1.X, p1.Y, p1.Z, p1i); } else { p1i = index; } hasValue = hashMap.GetHashValue(p2.X, p2.Y, p2.Z, ref index); if (!hasValue) { p2i = mesh.AddVertex(new Point3d(p2.X, p2.Y, p2.Z)); hashMap.SetHashValue(p2.X, p2.Y, p2.Z, p2i); } else { p2i = index; } if (p0i != p1i && p0i != p2i && p1i != p2i) { Triangle t = new Triangle(p0i, p1i, p2i); mesh.AddFace(t); } }
}//对非叶子节点的超体元的抽取需要参考MCTable求取被截断边的信息 private static void GenerateFaceLeaf(OctreeNode <NodeParms> node, MeshBuilder_IntegerVertex mb, ref Int16Triple[] tempArray, BitMap3d bmp) { for (int k = 0; k < 8; k++) { tempArray[k].X = node.XMin + PointIndexToPointDelta[k].X; tempArray[k].Y = node.YMin + PointIndexToPointDelta[k].Y; tempArray[k].Z = node.ZMin + PointIndexToPointDelta[k].Z; } byte value = node.Parms.Config; int index = 0; while (SMCTable.TableFat[value, index] != -1) { Int16Triple t0 = tempArray[SMCTable.TableFat[value, index]]; Int16Triple t1 = tempArray[SMCTable.TableFat[value, index + 1]]; Int16Triple t2 = tempArray[SMCTable.TableFat[value, index + 2]]; mb.AddTriangle(t0, t1, t2); index += 3; } }//对叶子节点的单位体元的抽取和SMC算法中的抽取一致
public static Mesh GenerateSurface(BitMap3d bmp) { int width = bmp.width; int height = bmp.height; int depth = bmp.depth; Int16Triple[] tempArray = new Int16Triple[8]; #region CreateTree RegionOctree <NodeParms> otree = new RegionOctree <NodeParms>(width, height, depth); Queue <OctreeNode <NodeParms> > nodequeue = new Queue <OctreeNode <NodeParms> >(); for (int k = 0; k < depth - 1; k++) { for (int j = 0; j < height - 1; j++) { for (int i = 0; i < width - 1; i++) { byte value = 0; for (int pi = 0; pi < 8; pi++) { tempArray[pi].X = i + PointIndexToPointDelta[pi].X; tempArray[pi].Y = j + PointIndexToPointDelta[pi].Y; tempArray[pi].Z = k + PointIndexToPointDelta[pi].Z; if (InRange(bmp, tempArray[pi].X, tempArray[pi].Y, tempArray[pi].Z) && IsWhite(bmp, tempArray[pi].X, tempArray[pi].Y, tempArray[pi].Z)) { value |= PointIndexToFlag[pi]; } } if (value != 0 && value != 255) { OctreeNode <NodeParms> leafnode = otree.CreateToLeafNode(i, j, k); leafnode.Parms = new NodeParms(); leafnode.Parms.Config = value; leafnode.Parms.NormalTypeId = OctreeTable.ConfigToNormalTypeId[value]; leafnode.Parms.D = CaculateDFromNormalAndCoord(i, j, k, value); nodequeue.Enqueue(leafnode.Parent); } } } } #endregion #region Shrink while (nodequeue.Count != 0) { OctreeNode <NodeParms> node = nodequeue.Dequeue(); byte normalType = OctreeTable.NormalNotSimple; int D = int.MinValue; if (CanMergeNode(node, ref normalType, ref D)) { node.Parms = new NodeParms(); //node.Parms.Config = GetConfigFromChildren(node.Children); node.Parms.NormalTypeId = normalType; node.Parms.D = D; nodequeue.Enqueue(node.Parent); } } #endregion #region ExtractTriangles MeshBuilder_IntegerVertex mb = new MeshBuilder_IntegerVertex(width + 1, height + 1, depth + 1); nodequeue.Enqueue(otree.Root); while (nodequeue.Count != 0) { OctreeNode <NodeParms> node = nodequeue.Dequeue(); if (node.Parms == null) { for (int i = 0; i < 8; i++) { if (node.Children[i] != null) { nodequeue.Enqueue(node.Children[i]); } } } else { if (node.Parms.NormalTypeId != OctreeTable.NormalNotSimple) { if (node.IsLeaf()) { GenerateFaceLeaf(node, mb, ref tempArray, bmp); } else { GenerateFace(node, mb, ref tempArray, bmp); } } else { if (node.IsLeaf()) { GenerateFaceLeaf(node, mb, ref tempArray, bmp); } else { for (int i = 0; i < 8; i++) { if (node.Children[i] != null) { nodequeue.Enqueue(node.Children[i]); } } } } } }//采用层次遍历寻找需要抽取三角片的节点 #endregion return(mb.GetMesh()); }
private static Int16Triple GetIntersetedPointAtEdge(OctreeNode <NodeParms> node, int edgeIndex, Int16Triple normal, int d) { int x = 0, y = 0, z = 0; switch (edgeIndex) { case 0: { x = node.XMin; y = node.YMax + 1; return(new Int16Triple(x, y, (d - normal.X * x - normal.Y * y) / normal.Z)); } case 2: { x = node.XMin; y = node.YMin; return(new Int16Triple(x, y, (d - normal.X * x - normal.Y * y) / normal.Z)); } case 4: { x = node.XMax + 1; y = node.YMax + 1; return(new Int16Triple(x, y, (d - normal.X * x - normal.Y * y) / normal.Z)); } case 6: { x = node.XMax + 1; y = node.YMin; return(new Int16Triple(x, y, (d - normal.X * x - normal.Y * y) / normal.Z)); } case 8: { y = node.YMax + 1; z = node.ZMax + 1; return(new Int16Triple((d - normal.Y * y - normal.Z * z) / normal.X, y, z)); } case 9: { y = node.YMax + 1; z = node.ZMin; return(new Int16Triple((d - normal.Y * y - normal.Z * z) / normal.X, y, z)); } case 10: { y = node.YMin; z = node.ZMin; return(new Int16Triple((d - normal.Y * y - normal.Z * z) / normal.X, y, z)); } case 11: { y = node.YMin; z = node.ZMax + 1; return(new Int16Triple((d - normal.Y * y - normal.Z * z) / normal.X, y, z)); } case 1: { x = node.XMin; z = node.ZMin; return(new Int16Triple(x, (d - normal.X * x - normal.Z * z) / normal.Y, z)); } case 3: { x = node.XMin; z = node.ZMax + 1; return(new Int16Triple(x, (d - normal.X * x - normal.Z * z) / normal.Y, z)); } case 5: { x = node.XMax + 1; z = node.ZMin; return(new Int16Triple(x, (d - normal.X * x - normal.Z * z) / normal.Y, z)); } case 7: { x = node.XMax + 1; z = node.ZMax + 1; return(new Int16Triple(x, (d - normal.X * x - normal.Z * z) / normal.Y, z)); } default: throw new Exception(); } }