public static Mesh ToNative(this SpeckleMesh mesh)
        {
            Mesh m = new Mesh();

            m.Vertices.AddVertices(mesh.Vertices.ToPoints());

            int i = 0;

            while (i < mesh.Faces.Count)
            {
                if (mesh.Faces[i] == 0)
                { // triangle
                    m.Faces.AddFace(new MeshFace(mesh.Faces[i + 1], mesh.Faces[i + 2], mesh.Faces[i + 3]));
                    i += 4;
                }
                else
                { // quad
                    m.Faces.AddFace(new MeshFace(mesh.Faces[i + 1], mesh.Faces[i + 2], mesh.Faces[i + 3], mesh.Faces[i + 4]));
                    i += 5;
                }
            }

            m.VertexColors.AppendColors(mesh.Colors.Select(c => System.Drawing.Color.FromArgb(( int )c)).ToArray());

            if (mesh.TextureCoordinates != null)
            {
                for (int j = 0; j < mesh.TextureCoordinates.Count; j += 2)
                {
                    m.TextureCoordinates.Add(mesh.TextureCoordinates[j], mesh.TextureCoordinates[j + 1]);
                }
            }

            m.UserDictionary.ReplaceContentsWith(mesh.Properties.ToNative());
            return(m);
        }
예제 #2
0
        public static Mesh ToNative(this SpeckleMesh mesh)
        {
            var points = mesh.Vertices.ToPoints();
            List <IndexGroup> faces = new List <IndexGroup>();
            int i = 0;

            while (i < mesh.Faces.Count)
            {
                if (mesh.Faces[i] == 0)
                { // triangle
                    var ig = IndexGroup.ByIndices((uint)mesh.Faces[i + 1], (uint)mesh.Faces[i + 2], (uint)mesh.Faces[i + 3]);
                    faces.Add(ig);
                    i += 4;
                }
                else
                { // quad
                    var ig = IndexGroup.ByIndices((uint)mesh.Faces[i + 1], (uint)mesh.Faces[i + 2], (uint)mesh.Faces[i + 3], (uint)mesh.Faces[i + 4]);
                    faces.Add(ig);
                    i += 5;
                }
            }

            var dsMesh = Mesh.ByPointsFaceIndices(points, faces);

            if (mesh.Properties != null)
            {
                dsMesh.Tags.AddTag(speckleKey, mesh.Properties);
            }
            return(dsMesh);
        }
예제 #3
0
        private static List <int[]> FaceEdgePairs(this SpeckleMesh mesh)
        {
            var allEdgePairs   = new List <int[]>();
            var edgePairCounts = new Dictionary <int, int>();

            var i = 0;

            do
            {
                var numInFace = (mesh.Faces[i] == 0) ? 3 : 4;
                if ((i + numInFace) < mesh.Faces.Count())
                {
                    i++;
                    for (var v = 0; v < numInFace; v++)
                    {
                        var pair       = (new int[] { mesh.Faces[i + v], mesh.Faces[((v + 1) == numInFace) ? i : i + v + 1] }).OrderBy(n => n).ToArray();
                        var foundIndex = allEdgePairs.FindIndex(ep => EqualPair(ep, pair));
                        if (foundIndex >= 0)
                        {
                            edgePairCounts[foundIndex]++;
                        }
                        else
                        {
                            allEdgePairs.Add(pair);
                            edgePairCounts.Add(allEdgePairs.IndexOf(pair), 1);
                        }
                    }
                }
                i += numInFace;
            } while (i < mesh.Faces.Count());

            var edgePairIndices = edgePairCounts.Where(kvp => kvp.Value == 1).Select(kvp => kvp.Key).ToList();

            return(edgePairIndices.Select(pi => allEdgePairs[pi]).ToList());
        }
예제 #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
        // Meshes
        public static SpeckleMesh ToSpeckle(this Mesh mesh)
        {
            var vertices      = mesh.VertexPositions.ToFlatArray();
            var defaultColour = System.Drawing.Color.FromArgb(255, 100, 100, 100);

            var faces = mesh.FaceIndices.SelectMany(f =>
            {
                if (f.Count == 4)
                {
                    return(new int[5] {
                        1, (int)f.A, (int)f.B, (int)f.C, (int)f.D
                    });
                }
                else
                {
                    return(new int[4] {
                        0, (int)f.A, (int)f.B, (int)f.C
                    });
                }
            })
                        .ToArray();

            var colors = Enumerable.Repeat(defaultColour.ToArgb(), vertices.Count()).ToArray();
            //double[] textureCoords;

            //if (SpeckleRhinoConverter.AddMeshTextureCoordinates)
            //{
            //  textureCoords = mesh.TextureCoordinates.Select(pt => pt).ToFlatArray();
            //  return new SpeckleMesh(vertices, faces, Colors, textureCoords, properties: mesh.UserDictionary.ToSpeckle());
            //}

            var speckleMesh = new SpeckleMesh(vertices, faces, colors, null, appId, mesh.GetSpeckleProperties());

            return(speckleMesh);
        }
예제 #6
0
        //This is to cater for situations where the mesh has duplicate points; these are when the same combination of x/y/z values are repeated
        //in the vertices collection
        public static void Consolidate(this SpeckleMesh mesh)
        {
            var vPts = Enumerable.Range(0, mesh.NumVertices()).Select(v => new Point3D(mesh.Vertices[v * 3], mesh.Vertices[(v * 3) + 1], mesh.Vertices[(v * 3) + 2])).ToList();

            //This algorithm is O(N^2) at the moment
            var indexConsolidateMappings = new Dictionary <int, int>();
            var newPts = new List <Point3D>();

            for (var i = 0; i < vPts.Count(); i++)
            {
                var found = false;
                for (int j = 0; j < newPts.Count(); j++)
                {
                    if (vPts[i].Equals(newPts[j], SpeckleStructuralClasses.Helper.PointComparisonEpsilon))
                    {
                        indexConsolidateMappings.Add(i, j);
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    var newIndex = newPts.Count();
                    newPts.Add(vPts[i]);
                    indexConsolidateMappings.Add(i, newIndex);
                }
            }

            var newFaces = mesh.Faces.ToList();
            var f        = 0;

            do
            {
                var numInFace = (newFaces[f] == 0) ? 3 : 4;
                if ((f + numInFace) < newFaces.Count())
                {
                    f++;
                    for (var v = 0; v < numInFace; v++)
                    {
                        if (indexConsolidateMappings.ContainsKey(newFaces[f + v]))
                        {
                            newFaces[f + v] = indexConsolidateMappings[newFaces[f + v]];
                        }
                    }
                }
                f += numInFace;
            } while (f < newFaces.Count());

            mesh.Faces    = newFaces;
            mesh.Vertices = newPts.SelectMany(p => new[] { p.X, p.Y, p.Z }).ToList();
        }
    public static GameObject ToNative(this SpeckleMesh mesh)
        {
            GameObject go = BaseMeshObject();
            
            go.GetComponent<UnitySpeckleObjectData>().Id = mesh._id;
            go.GetComponent<UnitySpeckleObjectData>().speckleObject = mesh;
        

        var newMesh = go.GetComponent<MeshFilter>().mesh;

            var vertices = mesh.Vertices.ToPoints();
            newMesh.vertices = vertices;

            List<int> tris = new List<int>();

            int i = 0;
            while (i < mesh.Faces.Count)
            {
                if (mesh.Faces[i] == 0)
                {
                    //Triangle
                    tris.Add(mesh.Faces[i+1]);
                    tris.Add(mesh.Faces[i+3]);
                    tris.Add(mesh.Faces[i+2]);
                    i += 4;
                } else {
                    //Quads
                    tris.Add(mesh.Faces[i + 1]);
                    tris.Add(mesh.Faces[i + 3]);
                    tris.Add(mesh.Faces[i + 2]);

                    tris.Add(mesh.Faces[i + 3]);
                    tris.Add(mesh.Faces[i + 1]);
                    tris.Add(mesh.Faces[i + 4]);

                    i += 5;
            }
        }
        newMesh.triangles = tris.ToArray();
        newMesh.RecalculateNormals();
        newMesh.RecalculateTangents();

        //Add mesh collider
        MeshCollider mc = go.AddComponent<MeshCollider>();
        mc.sharedMesh = newMesh;

        return go;
        
    }
예제 #8
0
        public static string ToNative(this SpeckleMesh inputObject)
        {
            var convertedObject = new Structural2DElementMesh();

            foreach (var p in convertedObject.GetType().GetProperties().Where(p => p.CanWrite))
            {
                var inputProperty = inputObject.GetType().GetProperty(p.Name);
                if (inputProperty != null)
                {
                    p.SetValue(convertedObject, inputProperty.GetValue(inputObject));
                }
            }

            return(convertedObject.ToNative());
        }
예제 #9
0
    public static GameObject ToNative(this SpeckleMesh mesh)
    {
        GameObject go = BaseObejct();

        go.GetComponent <UnitySpeckleObjectData>().Id = mesh._id;
        //go.GetComponent<UnitySpeckleObjectData>().LayerName = (string)mesh.Properties["LayerName"];

        var newMesh = go.GetComponent <MeshFilter>().mesh;

        var vertices = mesh.Vertices.ToPoints();

        newMesh.vertices = vertices;

        List <int> tris = new List <int>();

        int i = 0;

        while (i < mesh.Faces.Count)
        {
            if (mesh.Faces[i] == 0)
            {
                //Triangle
                tris.Add(mesh.Faces[i + 1]);
                tris.Add(mesh.Faces[i + 3]);
                tris.Add(mesh.Faces[i + 2]);
                i += 4;
            }
            else
            {
                //Quads
                tris.Add(mesh.Faces[i + 1]);
                tris.Add(mesh.Faces[i + 3]);
                tris.Add(mesh.Faces[i + 2]);

                tris.Add(mesh.Faces[i + 3]);
                tris.Add(mesh.Faces[i + 1]);
                tris.Add(mesh.Faces[i + 4]);

                i += 5;
            }
        }
        newMesh.triangles = tris.ToArray();
        newMesh.RecalculateNormals();
        newMesh.RecalculateTangents();

        return(go);
    }
예제 #10
0
        // Insipred by
        // https://github.com/DynamoDS/DynamoRevit/blob/master/src/Libraries/RevitNodes/GeometryConversion/ProtoToRevitMesh.cs
        public static IList <GeometryObject> ToNative(this SpeckleMesh mesh)
        {
            TessellatedShapeBuilderTarget   target   = TessellatedShapeBuilderTarget.Mesh;
            TessellatedShapeBuilderFallback fallback = TessellatedShapeBuilderFallback.Salvage;

            var tsb = new TessellatedShapeBuilder()
            {
                Fallback = fallback, Target = target, GraphicsStyleId = ElementId.InvalidElementId
            };

            tsb.OpenConnectedFaceSet(false);

            var vertices = mesh.Vertices.ToPoints();

            int i = 0;

            while (i < mesh.Faces.Count)
            {
                var points = new List <XYZ>();

                if (mesh.Faces[i] == 0)
                { // triangle
                    points = new List <XYZ> {
                        vertices[mesh.Faces[i + 1]], vertices[mesh.Faces[i + 2]], vertices[mesh.Faces[i + 3]]
                    };
                    i += 4;
                }
                else
                { // quad
                    points = new List <XYZ> {
                        vertices[mesh.Faces[i + 1]], vertices[mesh.Faces[i + 2]], vertices[mesh.Faces[i + 3]], vertices[mesh.Faces[i + 4]]
                    };
                    i += 5;
                }

                var face = new TessellatedFace(points, ElementId.InvalidElementId);
                tsb.AddFace(face);
            }


            tsb.CloseConnectedFaceSet();
            tsb.Build();
            var result = tsb.GetBuildResult();

            return(result.GetGeometricalObjects());
        }
예제 #11
0
        public static SpeckleMesh ToSpeckle(this BH.oM.Graphics.RenderMesh renderMesh)
        {
            double[] vertices = renderMesh.Vertices.Select(v => v.Point).ToFlatArray();
            int[]    faces    = renderMesh.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();
            var defaultColour = System.Drawing.Color.FromArgb(255, 100, 100, 100);
            var colors        = Enumerable.Repeat(defaultColour.ToArgb(), vertices.Count()).ToArray();

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

            return(speckleMesh);
        }
예제 #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="speckleMesh"></param>
        /// <returns></returns>
        public static SpeckleUnityMesh ToNative(this SpeckleMesh speckleMesh)
        {
            if (speckleMesh.Vertices.Count == 0 || speckleMesh.Faces.Count == 0)
            {
                return(null);
            }

            speckleMesh.Scale(scaleFactor);

            //convert speckleMesh.Faces into triangle array
            List <int> tris = new List <int> ();
            int        i    = 0;

            while (i < speckleMesh.Faces.Count)
            {
                if (speckleMesh.Faces[i] == 0)
                {
                    //Triangles
                    tris.Add(speckleMesh.Faces[i + 1]);
                    tris.Add(speckleMesh.Faces[i + 3]);
                    tris.Add(speckleMesh.Faces[i + 2]);
                    i += 4;
                }
                else
                {
                    //Quads to triangles
                    tris.Add(speckleMesh.Faces[i + 1]);
                    tris.Add(speckleMesh.Faces[i + 3]);
                    tris.Add(speckleMesh.Faces[i + 2]);

                    tris.Add(speckleMesh.Faces[i + 3]);
                    tris.Add(speckleMesh.Faces[i + 1]);
                    tris.Add(speckleMesh.Faces[i + 4]);

                    i += 5;
                }
            }

            return(new SpeckleUnityMesh(speckleMesh.Type, speckleMesh.Vertices.ToPoints(), tris.ToArray()));
        }
예제 #13
0
        public static int NumFaces(this SpeckleMesh mesh)
        {
            if (mesh.Vertices == null || mesh.Vertices.Count() == 0 || mesh.Faces == null || mesh.Faces.Count() == 0)
            {
                return(0);
            }
            var remainingNumPointsInFace = 0;
            var numFaces = 0;

            for (var i = 0; i < mesh.Faces.Count(); i++)
            {
                if (remainingNumPointsInFace == 0)
                {
                    numFaces++;
                    remainingNumPointsInFace = mesh.Faces[i] + 3;
                }
                else
                {
                    remainingNumPointsInFace--;
                }
            }
            return(numFaces);
        }
예제 #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="brep"></param>
        /// <returns></returns>
        public static SpeckleUnityMesh ToNative(this SpeckleBrep brep)
        {
            SpeckleMesh speckleMesh = brep.DisplayValue;

            return(speckleMesh.ToNative());
        }
예제 #15
0
        public static List <int[]> Edges(this SpeckleMesh mesh)
        {
            var edgePairs = mesh.FaceEdgePairs();
            var vPts      = Enumerable.Range(0, mesh.Vertices.Count() / 3).Select(i => new Point3D(mesh.Vertices[i * 3], mesh.Vertices[(i * 3) + 1], mesh.Vertices[(i * 3) + 2])).ToList();

            //Cap the number of attempts at finding connecting lines to no more than the number of lines overall
            var iterationCount = 0;
            var maxIterations  = edgePairs.Count();

            var loops = new List <List <int> >();

            var remainingEdgePairs = edgePairs.ToList();

            do
            {
                var currIndex = remainingEdgePairs.First()[1];
                var endIndex  = remainingEdgePairs.First()[0];

                var loop = new List <int>()
                {
                    currIndex
                };

                remainingEdgePairs = remainingEdgePairs.Skip(1).ToList();
                var error = false;

                do
                {
                    for (var i = 0; i < 2; i++)
                    {
                        if (FindNextLoopEndIndex(currIndex, remainingEdgePairs, out var nextLoopEndIndex, out var connectingEdgePairIndex))
                        {
                            currIndex = nextLoopEndIndex.Value; //Move the end of the loop along this newly-found line to its end
                            loop.Add(currIndex);
                            remainingEdgePairs.RemoveAt(connectingEdgePairIndex.Value);
                        }
                        iterationCount++;
                    }
                } while (remainingEdgePairs.Count() > 0 && currIndex != endIndex && !error && iterationCount < maxIterations);

                if (!error && loop.Count() > 0)
                {
                    loops.Add(loop);
                }
            } while (remainingEdgePairs.Count() > 0 && iterationCount < maxIterations);

            var lengthsOfLoops = new List <double>();

            foreach (var l in loops)
            {
                double length = 0;
                for (var i = 0; i < l.Count(); i++)
                {
                    var j = ((i + 1) < l.Count()) ? i + 1 : 0;

                    length += (new Line3D(vPts[l[i]], vPts[l[j]])).Length;
                }
                lengthsOfLoops.Add(length);
            }

            //Assumption: the longest length loop is the outer loop
            //Sort by loop length
            var ordered = lengthsOfLoops
                          .Select((x, i) => new KeyValuePair <double, int>(x, i))
                          .OrderBy(x => x.Key)
                          .Select(x => x.Value)
                          .Reverse();

            var sortedEdgeConnectivities = new List <int[]>();

            foreach (var i in ordered)
            {
                sortedEdgeConnectivities.Add(loops[i].ToArray());;
            }

            return(sortedEdgeConnectivities);
        }
예제 #16
0
 public static int NumVertices(this SpeckleMesh mesh) => (mesh.Vertices == null) ? 0 : (int)(mesh.Vertices.Count() / 3);