public static List <Polygon> ConvertMesh3DTo2DPolygons(XbimShapeTriangulation vMesh3D, bool isClosed)
        {
            if (vMesh3D == null)
            {
                return(new List <Polygon>());
            }

            var finalPolygons = new List <Polygon>();
            var vPoint2DCol   = new List <Point2D>();
            var positions     = new List <float[]>();
            var indices       = new List <int>();

            vMesh3D.ToPointsWithNormalsAndIndices(out positions, out indices);

            var sameSurfaceIndices = new Dictionary <double, List <int> >();

            for (var x = 0; x <= indices.Count - 1; x += 3)
            {
                var pos1 = positions[indices[x]];
                var pos2 = positions[indices[x + 1]];
                var pos3 = positions[indices[x + 2]];
                if (pos1[2] == pos2[2] && pos2[2] == pos3[2])
                {
                    if (!sameSurfaceIndices.ContainsKey(pos1[2]))
                    {
                        sameSurfaceIndices.Add(pos1[2], new List <int>());
                    }
                    sameSurfaceIndices[pos1[2]].Add(indices[x]);
                    sameSurfaceIndices[pos1[2]].Add(indices[x + 1]);
                    sameSurfaceIndices[pos1[2]].Add(indices[x + 2]);
                }
            }

            for (var x = 0; x <= positions.Count - 1; x++)
            {
                vPoint2DCol.Add(new Point2D(positions[x][0], positions[x][1]));
            }

            var vNewIndices = new List <int>();
            var vNewPos     = new List <Point2D>();

            int             vIndex1;
            int             vIndex2;
            int             vIndex3;
            Triangle        vTriangle;
            List <Triangle> vListTriangles = new List <Triangle>();
            Point2D         p1, p2, p3;

            foreach (var sameSurface in sameSurfaceIndices)
            {
                var shapePolygon = new Polygon();
                for (var x = 0; x <= sameSurface.Value.Count - 1; x += 3)
                {
                    p1        = vPoint2DCol[sameSurface.Value[x]];
                    p2        = vPoint2DCol[sameSurface.Value[x + 1]];
                    p3        = vPoint2DCol[sameSurface.Value[x + 2]];
                    vTriangle = new Triangle(p1, p2, p3);
                    if (!vTriangle.IsValid)
                    {
                        continue;
                    }
                    vIndex1 = vNewPos.IndexOf(p1);
                    vIndex2 = vNewPos.IndexOf(p2);
                    vIndex3 = vNewPos.IndexOf(p3);
                    if (!vListTriangles.Contains(vTriangle))
                    {
                        vListTriangles.Add(vTriangle);
                        if (vIndex1 != -1)
                        {
                            vNewIndices.Add(vIndex1);
                        }
                        else
                        {
                            vNewIndices.Add(vNewPos.Count);
                            vNewPos.Add(p1);
                        }

                        if (vIndex2 != -1)
                        {
                            vNewIndices.Add(vIndex2);
                        }
                        else
                        {
                            vNewIndices.Add(vNewPos.Count);
                            vNewPos.Add(p2);
                        }

                        if (vIndex3 != -1)
                        {
                            vNewIndices.Add(vIndex3);
                        }
                        else
                        {
                            vNewIndices.Add(vNewPos.Count);
                            vNewPos.Add(p3);
                        }
                    }
                }
                /// lets try this feature

                var boundaryPath = EdgeHelpers.GetEdges(vNewIndices.ToArray()).FindBoundary().SortEdges();

                foreach (var pathStep in boundaryPath)
                {
                    shapePolygon.PolygonVertices.Add(vNewPos[pathStep.v1]);
                }
                finalPolygons.Add(shapePolygon);
            }


            return(finalPolygons);
        }
Ejemplo n.º 2
0
    private void GetModel(XbimShapeTriangulation shapeTriangulation, int styleId, int ifcProductLabel)
    {
        //生成模型
        GameObject go   = Instantiate(prefab);
        Mesh       mesh = new Mesh();

        go.GetComponent <MeshFilter>().mesh = mesh;
        List <XbimPoint3D> xbimPoint3Ds = new List <XbimPoint3D>();

        xbimPoint3Ds = (List <XbimPoint3D>)shapeTriangulation.Vertices;

        List <Vector3> vector3s = new List <Vector3>();
        List <int>     indices  = new List <int>();
        List <Vector2> uvs      = new List <Vector2>();


        List <float[]> positions = new List <float[]>();
        List <Vector3> vertices  = new List <Vector3>();
        List <Vector3> normals   = new List <Vector3>();

        foreach (var points in xbimPoint3Ds)
        {
            vector3s.Add(new Vector3((float)points.X, (float)points.Y, (float)points.Z));
        }
        foreach (var face in (List <XbimFaceTriangulation>)shapeTriangulation.Faces)
        {
            foreach (var indice in face.Indices)
            {
                indices.Add(indice);
            }
            foreach (var uv in face.Normals)
            {
                //normals.Add(new Vector3((float)uv.Normal.X, (float)uv.Normal.Y, (float)uv.Normal.Z));
                uvs.Add(new Vector2(uv.U, uv.V));
            }
        }


        shapeTriangulation.ToPointsWithNormalsAndIndices(out positions, out indices);
        foreach (var position in positions)
        {
            vertices.Add(new Vector3(position[0] / scale, position[1] / scale, position[2] / scale));
            normals.Add(new Vector3(position[3], position[4], position[5]));
        }



        mesh.vertices = vertices.ToArray();
        //mesh.vertices = vector3s.ToArray();
        //mesh.uv = uvs.ToArray();
        mesh.triangles = indices.ToArray();
        //mesh.Optimize();
        //mesh.RecalculateNormals();
        mesh.normals = normals.ToArray();

        //赋予材质
        Material material = new Material(Shader.Find("CrossSection/OnePlaneBSP"));
        Vector4  color    = new Vector4();

        styleDictionary.TryGetValue(styleId, out color);
        material.color = color;
        go.GetComponent <Renderer>().material = material;
        //设置父子关系
        GameObject parent;

        productDictionary.TryGetValue(ifcProductLabel, out parent);
        go.transform.SetParent(parent.transform);
        go.AddComponent <PlaneCuttingController>();
    }