Esempio n. 1
0
        public static MeshGeometry3D WriteTriangles(IIfcProduct ifcElement, Xbim3DModelContext context, XbimMatrix3D wcsTransformation)
        {
            MeshBuilder meshBuilder = new MeshBuilder(false, false);

            // var allTriangles = new List<Triangles>();
            // foreach (XbimShapeInstance instance in context.ShapeInstancesOf(ifcElement).Where(x => x.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded))
            foreach (XbimShapeInstance instance in context.ShapeInstancesOf(ifcElement).Where(x => x.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded))
            {
                XbimShapeGeometry geometry = context.ShapeGeometry(instance);
                var data = ((IXbimShapeGeometryData)geometry).ShapeData;
                using (var stream = new MemoryStream(data))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        XbimShapeTriangulation mesh = reader.ReadShapeTriangulation();
                        mesh = mesh.Transform(instance.Transformation);
                        // WCS transforms
                        mesh = mesh.Transform(wcsTransformation);

                        foreach (XbimFaceTriangulation face in mesh.Faces)
                        {
                            var j = 0;
                            for (var i = 0; i < face.TriangleCount; i++)
                            {
                                int k      = i + j;
                                var point1 = new Point3D {
                                    X = mesh.Vertices[face.Indices[k]].X, Y = mesh.Vertices[face.Indices[k]].Y, Z = mesh.Vertices[face.Indices[k]].Z
                                };
                                j++;
                                k = i + j;
                                var point2 = new Point3D {
                                    X = mesh.Vertices[face.Indices[k]].X, Y = mesh.Vertices[face.Indices[k]].Y, Z = mesh.Vertices[face.Indices[k]].Z
                                };
                                j++;
                                k = i + j;
                                var point3 = new Point3D {
                                    X = mesh.Vertices[face.Indices[k]].X, Y = mesh.Vertices[face.Indices[k]].Y, Z = mesh.Vertices[face.Indices[k]].Z
                                };

                                meshBuilder.AddTriangle(point1, point2, point3);
                            }
                        }
                    }
                }
            }
            return(meshBuilder.ToMesh());
            // return allTriangles;
        }
Esempio n. 2
0
        private static void getgeometry(XbimShapeInstance shape, Xbim3DModelContext m_context, int entityLabel, int number_of_shapes)
        {
            XbimShapeTriangulation mesh = null;

            var geometry = m_context.ShapeGeometry(shape);


            Console.WriteLine($"{"\n"}{GetIndent(11)}{"--Geometry Type: " + geometry.Format}");


            var ms = new MemoryStream(((IXbimShapeGeometryData)geometry).ShapeData);
            var br = new BinaryReader(ms);

            mesh = br.ReadShapeTriangulation();
            mesh = mesh.Transform(((XbimShapeInstance)shape).Transformation);

            var facesfound = mesh.Faces.ToList();


            var number_of_faces = facesfound.Count();

            Console.WriteLine($"{"\n"}{GetIndent(11)}{"  -----No. of faces on the shape #" + shape.IfcProductLabel + ": " + number_of_faces}");

            //used for an ID for each face
            int face_index = 0;
            //used for the total number of triangles
            int number_of_triangles = 0;

            //write the Faces element with its count
            xmlWriter.WriteStartElement("Faces");
            xmlWriter.WriteAttributeString("NumFaces", number_of_faces.ToString());

            foreach (XbimFaceTriangulation f in facesfound)
            {
                number_of_triangles = f.TriangleCount;
                Console.WriteLine($"{"\n"}{GetIndent(13)}{"  -----Triangle count on face: " + f.GetType() + " :mesh is  " + number_of_triangles}");



                face_index++;

                composetrianglesets(f, mesh, entityLabel, facesfound.Count(), face_index, number_of_triangles, number_of_shapes);
            }

            //this ends the faces element in the xml file
            xmlWriter.WriteEndElement();



            //Console.WriteLine($"{"\n"}{GetIndent(13)}{" -----Vertices of the shape: #" + shape.IfcProductLabel}");
            //foreach (var v in mesh.Vertices.ToList())
            //{
            //    Console.WriteLine($"{GetIndent(13)}{" --vertex_" + mesh.Vertices.ToList().IndexOf(v) + " : " + Math.Round((double)v.X, 2) + " | " + Math.Round((double)v.Y, 2) + " | " + Math.Round((double)v.Z, 2)}");

            //}

            Console.WriteLine("\n");
        }
Esempio n. 3
0
        private static void getgeometry(XbimShapeInstance shape, Xbim3DModelContext m_context, int entityLabel, int number_OF_Walls)
        {
            XbimShapeTriangulation mesh = null;

            var geometry = m_context.ShapeGeometry(shape);

            //Console.WriteLine("====" + geometry.GetType());
            Console.WriteLine($"{"\n"}{GetIndent(11)}{"--Geometry Type: " + geometry.Format}");


            var ms = new MemoryStream(((IXbimShapeGeometryData)geometry).ShapeData);
            var br = new BinaryReader(ms);

            mesh = br.ReadShapeTriangulation();
            mesh = mesh.Transform(((XbimShapeInstance)shape).Transformation);

            var facesfound = mesh.Faces.ToList();



            Console.WriteLine($"{"\n"}{GetIndent(11)}{"  -----No. of faces on the shape #" + shape.IfcProductLabel + ": " + facesfound.Count()}");
            int FaceNum        = 0;
            int Triangle_Count = 0;

            //xmlWriter.WriteElementString("Faces", facesfound.Count().ToString());
            xmlWriter.WriteStartElement("Faces");
            xmlWriter.WriteAttributeString("NumFaces", facesfound.Count().ToString());

            foreach (XbimFaceTriangulation f in facesfound)
            {
                Triangle_Count = f.TriangleCount;
                Console.WriteLine($"{"\n"}{GetIndent(13)}{"  -----Triangle count on face: " + f.GetType() + " :mesh is  " +Triangle_Count}");


                //foreach (var fi in f.Indices)
                //{
                //    Console.WriteLine($"{GetIndent(13)}{" -> " + fi}");

                //}
                FaceNum++;

                composetrianglesets(f, mesh, entityLabel, facesfound.Count(), FaceNum, Triangle_Count, number_OF_Walls);
            }
            //this is for the faces NUmber
            xmlWriter.WriteEndElement();
            // xmlWriter.WriteWhitespace("\n");
            //Console.WriteLine($"{"\n"}{GetIndent(13)}{" -----Vertices of the shape: #" + shape.IfcProductLabel}");
            //foreach (var v in mesh.Vertices.ToList())
            //{
            //    Console.WriteLine($"{GetIndent(13)}{" --vertex_" + mesh.Vertices.ToList().IndexOf(v) + " : " + Math.Round((double)v.X, 2) + " | " + Math.Round((double)v.Y, 2) + " | " + Math.Round((double)v.Z, 2)}");

            //}

            Console.WriteLine("\n");
        }
Esempio n. 4
0
        private static void getgeometry(XbimShapeInstance shape, Xbim3DModelContext m_context)
        {
            XbimShapeTriangulation mesh = null;

            var geometry = m_context.ShapeGeometry(shape);

            //Console.WriteLine("====" + geometry.GetType());
            Console.WriteLine($"{"\n"}{GetIndent(11)}{"--Geometry Type: " + geometry.Format}");


            var ms = new MemoryStream(((IXbimShapeGeometryData)geometry).ShapeData);
            var br = new BinaryReader(ms);

            mesh = br.ReadShapeTriangulation();
            mesh = mesh.Transform(((XbimShapeInstance)shape).Transformation);

            var facesfound = mesh.Faces.ToList();

            Console.WriteLine($"{"\n"}{GetIndent(11)}{"  -----No. of faces on the shape #" + shape.IfcProductLabel + ": " + facesfound.Count()}");

            foreach (XbimFaceTriangulation f in facesfound)
            {
                Console.WriteLine($"{"\n"}{GetIndent(13)}{"  -----Triangle count on face: " + f.GetType() + " :mesh is  " + f.TriangleCount}");
                //foreach (var fi in f.Indices)
                //{
                //    Console.WriteLine($"{GetIndent(13)}{" -> " + fi}");

                //}
                composetrianglesets(f, mesh);
            }

            //Console.WriteLine($"{"\n"}{GetIndent(13)}{" -----Vertices of the shape: #" + shape.IfcProductLabel}");
            //foreach (var v in mesh.Vertices.ToList())
            //{
            //    Console.WriteLine($"{GetIndent(13)}{" --vertex_" + mesh.Vertices.ToList().IndexOf(v) + " : " + Math.Round((double)v.X, 2) + " | " + Math.Round((double)v.Y, 2) + " | " + Math.Round((double)v.Z, 2)}");

            //}

            Console.WriteLine("\n");
        }
Esempio n. 5
0
    public BimTriangulation(XbimShapeTriangulation triangulation, float scale, Vector3 offset, XbimMatrix3D matrix = new XbimMatrix3D(), bool bMatrix = false)
    {
        if (bMatrix)
        {
            bimTriangulation = triangulation.Transform(matrix);
        }
        else
        {
            bimTriangulation = triangulation;
        }

        vertices = new List <Vector3>();
        normals  = new List <Vector3>();

        bimTriangulation.ToPointsWithNormalsAndIndices(out List <float[]> points, out List <int> indices);
        triangles = indices;
        foreach (var p in points)
        {
            //原版减去了一个offset,但是我发现不计算offset所有偏移都是正确的
            //var vertice = new Vector3(p[0], p[1], p[2]) / scale - offset;
            var vertice = new Vector3(p[0], p[1], p[2]) / scale;
            var normal  = new Vector3(p[3], p[4], p[5]);
            vertices.Add(vertice);
            normals.Add(normal);
        }

        /*
         * foreach (var v in bimTriangulation.Vertices)
         *  vertices.Add(new Vector3((float)v.X, (float)v.Y, (float)v.Z) / scale - offset);
         * foreach(var f in bimTriangulation.Faces)
         * {
         *  triangles.AddRange(f.Indices);
         *  foreach(var n in f.Normals)
         *  {
         *      normals.Add(new Vector3((float)n.Normal.X, (float)n.Normal.Y, (float)n.Normal.Z));
         *  }
         * }
         */
    }
    public MyBimTriangulation(XbimShapeTriangulation triangulation, Vector3 offsite, float scale, XbimMatrix3D matrix = default, bool bMatrix = false)
    {
        if (bMatrix)
        {
            bimTriangulation = triangulation.Transform(matrix);
        }
        else
        {
            bimTriangulation = triangulation;
        }

        vertices = new List <Vector3>();
        normals  = new List <Vector3>();

        bimTriangulation.ToPointsWithNormalsAndIndices(out List <float[]> points, out List <int> indices);

        triangles = indices;
        foreach (var p in points)
        {
            var vertice = new Vector3(p[0], p[1], p[2]) / scale - offsite;
            var normal  = new Vector3(p[3], p[4], p[5]);
            vertices.Add(vertice);
            normals.Add(normal);
        }
        //foreach (var v in bimTriangulation.Vertices)
        //{
        //    vertices.Add(new Vector3((float)v.X, (float)v.Y, (float)v.Z) / scale - offsite);
        //}
        //foreach (var f in bimTriangulation.Faces)
        //{
        //    triangles.AddRange(f.Indices);
        //    foreach (var n in f.Normals)
        //    {
        //        var nor = new Vector3((float)n.Normal.X, (float)n.Normal.Y, (float)n.Normal.Z);
        //        normals.Add(nor);
        //    }
        //}
    }
Esempio n. 7
0
    public BimTriangulation(XbimShapeTriangulation shapeTriangulation, Vector3 offsite, float scale, XbimMatrix3D xbimMatrix = default, bool shapeRepetite = false)
    {
        if (shapeRepetite)
        {
            this.shapeTriangulation = shapeTriangulation.Transform(xbimMatrix);
        }
        else
        {
            this.shapeTriangulation = shapeTriangulation;
        }

        vertices = new List <Vector3>();
        normals  = new List <Vector3>();

        this.shapeTriangulation.ToPointsWithNormalsAndIndices(out List <float[]> points, out List <int> indices);
        triangles = indices;
        foreach (var p in points)
        {
            Vector3 vertice = new Vector3(p[0], p[1], p[2]) / scale - offsite;
            Vector3 normal  = new Vector3(p[3], p[4], p[5]);
            vertices.Add(vertice);
            normals.Add(normal);
        }
    }
Esempio n. 8
0
        public static List <Point3D> GetFootPrintNonBREP(IIfcProduct ifcElement, Xbim3DModelContext context)
        {
            var resultList = new List <Point3D>();

            foreach (XbimShapeInstance instance in context.ShapeInstancesOf(ifcElement))
            {
                // Get the IFC Representation of the Footprint of the instance
                XbimShapeGeometry geometry = context.ShapeGeometry(instance);

                var data = ((IXbimShapeGeometryData)geometry).ShapeData;
                using (var stream = new MemoryStream(data))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        XbimShapeTriangulation mesh = reader.ReadShapeTriangulation();
                        mesh = mesh.Transform(instance.Transformation);

                        // find the minimal z coordinate
                        double minZ = 10000;
                        foreach (var vertex in mesh.Vertices)
                        {
                            if (vertex.Z <= minZ)
                            {
                                minZ = vertex.Z;
                            }
                        }
                        List <IVertex> points = new List <IVertex>();

                        foreach (var vertex in mesh.Vertices)
                        {
                            if (vertex.Z != minZ)
                            {
                                continue;
                            }

                            points.Add(new DefaultVertex {
                                Position = new[] { vertex.X, vertex.Y }
                            });
                        }

                        if (points.Count <= 2)
                        {
                            return(null);
                        }

                        // Compute ConvexHull
                        var cH = ConvexHull.Create(points);

                        foreach (var item in cH.Points)
                        {
                            var point = new Point3D()
                            {
                                X = item.Position[0], Y = item.Position[1], Z = minZ
                            };
                            bool duplicate = false;
                            foreach (var result in resultList)
                            {
                                if (result == point)
                                {
                                    duplicate = true;
                                    break;
                                }
                            }
                            if (!duplicate)
                            {
                                resultList.Add(point);
                            }
                        }
                    }
                }
            }
            return(resultList);
        }