示例#1
0
        /***************************************************/

        public static List <MeshFace> MeshFaces(BH.oM.Geometry.Mesh mesh, ISurfaceProperty property = null, string name = null)
        {
            List <MeshFace> meshFaces = new List <MeshFace>();

            foreach (Face face in mesh.Faces)
            {
                List <Node> nodes = new List <Node>();
                nodes.Add(Create.Node(mesh.Vertices[face.A]));
                nodes.Add(Create.Node(mesh.Vertices[face.B]));
                nodes.Add(Create.Node(mesh.Vertices[face.C]));

                if (BH.Engine.Geometry.Query.IsQuad(face))
                {
                    nodes.Add(Create.Node(mesh.Vertices[face.D]));
                }
                MeshFace mf = new MeshFace()
                {
                    Property = property, Nodes = nodes
                };

                if (name != null)
                {
                    mf.Name = name;
                }

                meshFaces.Add(mf);
            }

            return(meshFaces);
        }
示例#2
0
        /***************************************************/
        /**** Public Methods                            ****/
        /***************************************************/

        public static FEMesh FEMesh(BH.oM.Geometry.Mesh mesh, ISurfaceProperty property = null, string name = null)
        {
            FEMesh feMesh = new FEMesh();

            feMesh.Nodes = mesh.Vertices.Select(x => Node(x)).ToList();

            foreach (Face face in mesh.Faces)
            {
                FEMeshFace feFace = new FEMeshFace();
                feFace.NodeListIndices.Add(face.A);
                feFace.NodeListIndices.Add(face.B);
                feFace.NodeListIndices.Add(face.C);

                if (face.IsQuad())
                {
                    feFace.NodeListIndices.Add(face.D);
                }

                feMesh.MeshFaces.Add(feFace);
            }

            if (property != null)
            {
                feMesh.Property = property;
            }

            if (name != null)
            {
                feMesh.Name = name;
            }

            return(feMesh);
        }
示例#3
0
文件: Mesh.cs 项目: BHoM/XML_Toolkit
        public static List <KML.Polygon> ToKML(this BHG.Mesh mesh, GeoReference geoReference)
        {
            mesh = mesh.Rotate(geoReference.Reference, BHG.Vector.ZAxis, geoReference.NorthVector.SignedAngle(BHG.Vector.YAxis, BHG.Vector.ZAxis));
            List <KML.Polygon> polygons = new List <KML.Polygon>();

            foreach (BHG.Face face in mesh.Faces)
            {
                List <double>    coords = new List <double>();
                List <BHG.Point> points = new List <BHG.Point> {
                    mesh.Vertices[face.A].DeepClone(),
                    mesh.Vertices[face.B].DeepClone(),
                    mesh.Vertices[face.C].DeepClone(),
                    mesh.Vertices[face.A].DeepClone()
                };

                if (face.D > -1)
                {
                    points.Insert(3, mesh.Vertices[face.D].DeepClone());
                }

                foreach (BHG.Point p in points)
                {
                    BHG.Point kmlpoint = p.ToLatLon(geoReference);
                    coords.Add(kmlpoint.X);
                    coords.Add(kmlpoint.Y);
                    coords.Add(kmlpoint.Z);
                }

                KML.Polygon polygon = new KML.Polygon();
                polygon.AltitudeMode = geoReference.AltitudeMode.ToKML();
                polygon.OuterBoundaryIs.LinearRing.Coordinates = coords.ToArray();
                polygons.Add(polygon);
            }
            return(polygons);
        }
示例#4
0
        public static SpeckleMesh ToSpeckle(this BHG.Mesh bhomMesh, Color?colour = null)
        {
            double[] vertices = bhomMesh.Vertices.ToFlatArray();
            int[]    faces    = bhomMesh.Faces.SelectMany(face =>
            {
                if (face.D != -1)
                {
                    return new int[] { 1, face.A, face.B, face.C, face.D }
                }
                ;
                return(new int[] { 0, face.A, face.B, face.C });
            }).ToArray();

            Color col = System.Drawing.Color.FromArgb(255, 100, 100, 100);

            if (colour != null)
            {
                col = (Color)colour;
            }

            int[] colors = Enumerable.Repeat(col.ToArgb(), vertices.Count()).ToArray();

            SpeckleMesh speckleMesh = new SpeckleMesh(vertices, faces, colors, null);

            return(speckleMesh);
        }
示例#5
0
        /***************************************************/
        /**** Public Methods                            ****/
        /***************************************************/

        public static BH.oM.Adapters.TDRepo.TDR_Mesh ToTDRepo(BH.oM.Geometry.Mesh mesh)
        {
            var faces = mesh.Faces.Select(face =>
                                          new BH.oM.Adapters.TDRepo.TDR_Face(new int[] { face.A, face.B, face.C, face.D })
                                          );

            var points = mesh.Vertices.Select(vertex =>
                                              new BH.oM.Adapters.TDRepo.TDR_Point(vertex.X, vertex.Y, vertex.Z)
                                              );

            return(new BH.oM.Adapters.TDRepo.TDR_Mesh("Mesh", points.ToArray(), faces.ToArray()));
        }
示例#6
0
        public static BH.oM.Graphics.RenderMesh IRenderMesh(this IObject obj, RenderMeshOptions renderMeshOptions = null)
        {
            if (obj == null)
            {
                return(null);
            }

            renderMeshOptions = renderMeshOptions ?? new RenderMeshOptions();

            RenderMesh renderMesh = null;

            // See if there is a custom BHoM mesh representation for this BHoMObject, before attempting the RenderMesh computation.
            if (obj is IBHoMObject)
            {
                if (Query.TryGetRendermesh(obj as IBHoMObject, out renderMesh))
                {
                    return(renderMesh);
                }
            }

            if (obj is BH.oM.Graphics.RenderMesh)
            {
                return(obj as BH.oM.Graphics.RenderMesh);
            }

            BH.oM.Geometry.Mesh mesh = obj as BH.oM.Geometry.Mesh;
            if (mesh != null)
            {
                return(mesh.ToRenderMesh());
            }

            // If obj is of type IGeometry, we still need to compute its geometrical representation.
            // E.g. A BH.oM.Geometry.Point can only be represented with a Sphere, a Pixel, a Voxel, etc.
            IGeometry geomRepr = IGeometricalRepresentation(obj, renderMeshOptions.RepresentationOptions);

            if (geomRepr != null)
            {
                renderMesh = RenderMesh(geomRepr as dynamic, renderMeshOptions);
            }

            if (renderMesh == null)
            {
                throw new Exception($"Could not compute the {nameof(BH.oM.Graphics.RenderMesh)} of {obj.GetType().Name}.");
            }

            return(renderMesh);
        }
示例#7
0
        /***************************************************/
        /**** Public Methods  - Mesh                    ****/
        /***************************************************/

        public static RHG.Mesh ToRhino(this BHG.Mesh mesh)
        {
            if (mesh == null)
            {
                return(null);
            }

            List <RHG.Point3d>  rVertices = mesh.Vertices.Select(x => x.ToRhino()).ToList();
            List <BHG.Face>     faces     = mesh.Faces;
            List <RHG.MeshFace> rFaces    = new List <RHG.MeshFace>();
            int nbVertices = rVertices.Count;

            for (int i = 0; i < faces.Count; i++)
            {
                BHG.Face face = faces[i];
                if (face.IsQuad())
                {
                    if (face.A < nbVertices && face.B < nbVertices && face.C < nbVertices && face.D < nbVertices)
                    {
                        rFaces.Add(new RHG.MeshFace(face.A, face.B, face.C, face.D));
                    }
                    else
                    {
                        Reflection.Compute.RecordWarning("Mesh face [" + face.A + ", " + face.B + ", " + face.C + ", " + face.D + "] could not be created due to corresponding vertices missing");
                    }
                }
                else
                {
                    if (face.A < nbVertices && face.B < nbVertices && face.C < nbVertices)
                    {
                        rFaces.Add(new RHG.MeshFace(face.A, face.B, face.C));
                    }
                    else
                    {
                        Reflection.Compute.RecordWarning("Mesh face [" + face.A + ", " + face.B + ", " + face.C + "] could not be created due to corresponding vertices missing");
                    }
                }
            }
            RHG.Mesh rMesh = new RHG.Mesh();
            rMesh.Faces.AddFaces(rFaces);
            rMesh.Vertices.AddVertices(rVertices);
            return(rMesh);
        }
示例#8
0
        /***************************************************/
        /**** Private methods                           ****/
        /***************************************************/

        private static string WriteBIMFile(List <IObject> objectsToWrite, string directory = null, string fileName = null, RenderMeshOptions renderMeshOptions = null)
        {
            // --------------------------------------------- //
            //                    Set-up                     //
            // --------------------------------------------- //

            directory = directory ?? Path.Combine("C:\\temp", "BIMFileFormat");

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            fileName = fileName ?? Guid.NewGuid().ToString();
            string bimFilePath = Path.Combine(directory, fileName + ".bim");

            renderMeshOptions = renderMeshOptions ?? new RenderMeshOptions();

            // --------------------------------------------- //
            //             Compute representation            //
            // --------------------------------------------- //

            List <Mesh> representationMeshes = new List <Mesh>();
            List <Tuple <IObject, Mesh> > objsAndRepresentations = new List <Tuple <IObject, Mesh> >();

            IBHoMObject bHoMObject = null;

            for (int i = 0; i < objectsToWrite.Count; i++)
            {
                IObject obj = objectsToWrite[i];

                Mesh meshRepresentation = null;

                // See if there is a custom BHoM mesh representation for that BHoMObject.
                bHoMObject = obj as IBHoMObject;
                RenderMesh renderMesh = null;

                if (bHoMObject != null)
                {
                    bHoMObject.TryGetRendermesh(out renderMesh);
                }

                if (renderMesh == null && meshRepresentation == null)
                {
                    renderMesh = BH.Engine.Representation.Compute.IRenderMesh(obj, renderMeshOptions);
                }

                if (renderMesh != null) //convert to Mesh
                {
                    meshRepresentation = new Mesh()
                    {
                        Faces = renderMesh.Faces, Vertices = renderMesh.Vertices.Select(v => new oM.Geometry.Point()
                        {
                            X = v.Point.X, Y = v.Point.Y, Z = v.Point.Z
                        }).ToList()
                    }
                }
                ;

                representationMeshes.Add(meshRepresentation);

                if (bHoMObject != null)
                {
                    // Add/update the RenderMesh in the BHoMObject
                    bHoMObject.ISetRendermesh(meshRepresentation);
                    obj = bHoMObject;
                }

                objsAndRepresentations.Add(new Tuple <IObject, Mesh>(obj, meshRepresentation));
            }


            // --------------------------------------------- //
            //                File preparation               //
            // --------------------------------------------- //

            BIMDataExporter exporter = new BIMDataExporter();

            // Prepare default material
            TDR_Material defaultMat = new TDR_Material()
            {
                MaterialArray = new List <float> {
                    1f, 1f, 1f, 1f
                }
            };
            int defaultMatIdx = exporter.AddMaterial(defaultMat.MaterialArray);

            // Prepare transformation matrix
            List <float> transfMatrix = new List <float>
            {
                1, 0, 0, 2,
                0, 1, 0, 2,
                0, 0, 1, 2,
                0, 0, 0, 1
            };

            // Prepare root node
            int rootNodeIdx = exporter.AddNode("root", -1, null);

            // Process the meshes
            for (int i = 0; i < objsAndRepresentations.Count; i++)
            {
                BH.oM.Geometry.Mesh m = representationMeshes[i];
                Tuple <IObject, BH.oM.Geometry.Mesh> objAndRepr = objsAndRepresentations[i];

                // Check if a colour has been specified in the BHoMObject's Fragment
                bHoMObject = objAndRepr.Item1 as IBHoMObject;
                int customMatIdx = defaultMatIdx;
                if (bHoMObject != null)
                {
                    Color?colour = bHoMObject.FindFragment <ColourFragment>()?.Colour;

                    if (colour != null)
                    {
                        Color col = (Color)colour;

                        float r = (float)col.R / 255;
                        float g = (float)col.G / 255;
                        float b = (float)col.B / 255;
                        float a = (float)col.A / 255;

                        TDR_Material customMat = new TDR_Material()
                        {
                            MaterialArray = new List <float> {
                                r, g, b, a
                            }
                        };
                        customMatIdx = exporter.AddMaterial(customMat.MaterialArray);
                    }
                }

                // Convert object representation mesh to a
                Geometry geometry = BH.Adapter.TDrepo.Convert.MeshToGeometry(objAndRepr.Item2, customMatIdx);

                // Add metadata
                Dictionary <string, RepoVariant> metadata = new Dictionary <string, RepoVariant>();

                // Serialize the object
                string serialisedBHoMData = BH.Engine.Serialiser.Convert.ToJson(objAndRepr.Item1);

                // Flatten the JSON in a Dictionary. Nested properties names are concatenated with fullstops.
                Dictionary <string, object> flattenedObj = BH.Engine.Adapters.TDRepo.Compute.FlattenJsonToDictionary(serialisedBHoMData);

                // For each entry in the flattened object, add a metadata with the value.
                flattenedObj.ToList().ForEach(
                    kv =>
                {
                    if (kv.Value is int)
                    {
                        metadata.Add(kv.Key, RepoVariant.Int((int)kv.Value));
                    }
                    else if (kv.Value is double)
                    {
                        metadata.Add(kv.Key, RepoVariant.Double((double)kv.Value));
                    }
                    else if (kv.Value is bool)
                    {
                        metadata.Add(kv.Key, RepoVariant.Boolean((bool)kv.Value));
                    }
                    else
                    {
                        metadata.Add(kv.Key, RepoVariant.String(kv.Value?.ToString()));
                    }
                }
                    );

                metadata.Add("ZippedBHoM", RepoVariant.String(BH.Engine.Serialiser.Convert.ToZip(serialisedBHoMData)));

                //metadata.Add("Area", RepoVariant.Int(1));
                //metadata.Add("Boolean Test", RepoVariant.Boolean(true));
                //metadata.Add("Double", RepoVariant.Double(1.3242524));

                // Add node to exporter.
                exporter.AddNode("mesh" + i, rootNodeIdx, transfMatrix, geometry, metadata);
            }

            exporter.ExportToFile(bimFilePath);

            return(bimFilePath);
        }
示例#9
0
        public static global::Topologic.Topology TopologyByGeometry(BH.oM.Geometry.IGeometry geometry, double tolerance = 0.0001)
        {
            BH.oM.Geometry.Point bhomPoint = geometry as BH.oM.Geometry.Point;
            if (bhomPoint != null)
            {
                return(Create.VertexByPoint(bhomPoint));
            }

            // Handle polyline and polycurve first
            BH.oM.Geometry.Polyline bhomPolyline = geometry as BH.oM.Geometry.Polyline;
            if (bhomPolyline != null)
            {
                if (bhomPolyline.ControlPoints.Count < 2)
                {
                    throw new Exception("An invalid polyline with fewer than 2 control points is given.");
                }
                else if (bhomPolyline.ControlPoints.Count == 2)
                {
                    BH.oM.Geometry.Line bhomLine = BH.Engine.Geometry.Create.Line(bhomPolyline.ControlPoints[0], bhomPolyline.ControlPoints[1]);
                    return(Create.EdgeByLine(bhomLine));
                }
                else
                {
                    return(Create.WireByPolyLine(bhomPolyline));
                }
            }

            BH.oM.Geometry.PolyCurve bhomPolyCurve = geometry as BH.oM.Geometry.PolyCurve;
            if (bhomPolyCurve != null)
            {
                if (bhomPolyCurve.Curves.Count == 0)
                {
                    throw new Exception("An invalid polycurve with no curve is given.");
                }
                else if (bhomPolyCurve.Curves.Count == 1)
                {
                    BH.oM.Geometry.ICurve bhomACurve = bhomPolyCurve.Curves[0];
                    return(Create.EdgeByCurve(bhomACurve));
                }
                else
                {
                    return(Create.WireByPolyCurve(bhomPolyCurve));
                }
            }

            // Then curve
            BH.oM.Geometry.ICurve bhomCurve = geometry as BH.oM.Geometry.ICurve;
            if (bhomCurve != null)
            {
                return(Create.EdgeByCurve(bhomCurve));
            }

            // Do polysurface first.
            BH.oM.Geometry.PolySurface bhomPolySurface = geometry as BH.oM.Geometry.PolySurface;
            if (bhomPolySurface != null)
            {
                return(Create.ShellByPolySurface(bhomPolySurface, tolerance));
            }

            // Then surface
            BH.oM.Geometry.ISurface bhomSurface = geometry as BH.oM.Geometry.ISurface;
            if (bhomSurface != null)
            {
                return(Create.FaceBySurface(bhomSurface));
            }

            BH.oM.Geometry.ISolid bhomSolid = geometry as BH.oM.Geometry.ISolid;
            if (bhomSolid != null)
            {
                return(Create.CellBySolid(bhomSolid, tolerance));
            }

            BH.oM.Geometry.BoundingBox bhomBoundingBox = geometry as BH.oM.Geometry.BoundingBox;
            if (bhomBoundingBox != null)
            {
                return(Create.CellByBoundingBox(bhomBoundingBox));
            }

            BH.oM.Geometry.CompositeGeometry bhomCompositeGeometry = geometry as BH.oM.Geometry.CompositeGeometry;
            if (bhomCompositeGeometry != null)
            {
                return(Create.ClusterByCompositeGeometry(bhomCompositeGeometry, tolerance));
            }

            BH.oM.Geometry.Mesh bhomMesh = geometry as BH.oM.Geometry.Mesh;
            if (bhomMesh != null)
            {
                return(Create.TopologyByMesh(bhomMesh));
            }

            throw new NotImplementedException("This BHoM geometry is not yet supported.");
        }
示例#10
0
        /***************************************************/
        /**** Public Methods  - Mesh                    ****/
        /***************************************************/

        public static void RenderMeshes(BHG.Mesh mesh, Rhino.Display.DisplayPipeline pipeline, DisplayMaterial material)
        {
            RHG.Mesh rMesh = mesh.ToRhino();
            pipeline.DrawMeshShaded(rMesh, material);
        }
示例#11
0
        /***************************************************/
        /**** Public Methods  - Mesh                    ****/
        /***************************************************/

        public static void RenderWires(BHG.Mesh mesh, Rhino.Display.DisplayPipeline pipeline, Color bhColour)
        {
            RHG.Mesh rMesh = mesh.ToRhino();
            pipeline.DrawMeshWires(rMesh, bhColour);
        }