protected static IndexBuffer ToWireframeBuffer(Rhino.Geometry.Mesh mesh, out int linesCount)
        {
            linesCount = (mesh.Faces.Count * 3) + mesh.Faces.QuadCount;
            if (linesCount > 0)
            {
                var ib = new IndexBuffer(linesCount * IndexLine.GetSizeInShortInts());
                ib.Map(linesCount * IndexLine.GetSizeInShortInts());

                using (var istream = ib.GetIndexStreamLine())
                {
                    foreach (var face in mesh.Faces)
                    {
                        istream.AddLine(new IndexLine(face.A, face.B));
                        istream.AddLine(new IndexLine(face.B, face.C));
                        istream.AddLine(new IndexLine(face.C, face.D));
                        if (face.IsQuad)
                        {
                            istream.AddLine(new IndexLine(face.D, face.A));
                        }
                    }
                }

                ib.Unmap();
                return(ib);
            }

            return(null);
        }
        public static RenderData DrawEdges(List <Elements.Geometry.Line> edges)
        {
            var numVertices      = edges.Count * 2;
            var numPrimitives    = edges.Count;
            var pType            = PrimitiveType.LineList;
            var numIndices       = GetPrimitiveSize(pType) * numPrimitives;
            var vertexFormatBits = VertexFormatBits.Position;
            var vertexFormat     = new VertexFormat(vertexFormatBits);
            var vBuffer          = new VertexBuffer(GetVertexSize(vertexFormatBits) * numVertices);
            var iBuffer          = new IndexBuffer(numIndices);

            vBuffer.Map(GetVertexSize(vertexFormatBits) * numVertices);
            iBuffer.Map(numIndices);

            var vertices = new List <VertexPosition>();
            var lines    = new List <IndexLine>();

            var index = 0;

            foreach (var e in edges)
            {
                vertices.Add(new VertexPosition(e.Start.ToXYZFeet()));
                vertices.Add(new VertexPosition(e.End.ToXYZFeet()));
                lines.Add(new IndexLine(index, index + 1));
                index += 2;
            }
            var p = vBuffer.GetVertexStreamPosition();

            p.AddVertices(vertices);
            var iPos = iBuffer.GetIndexStreamLine();

            iPos.AddLines(lines);

            vBuffer.Unmap();
            iBuffer.Unmap();

            var effect = new EffectInstance(vertexFormatBits);

            var renderData = new RenderData()
            {
                VertexBuffer   = vBuffer,
                VertexCount    = numVertices,
                IndexBuffer    = iBuffer,
                IndexCount     = numIndices,
                VertexFormat   = vertexFormat,
                Effect         = effect,
                PrimitiveType  = pType,
                PrimitiveCount = numPrimitives
            };

            DrawContext.FlushBuffer(vBuffer, numVertices, iBuffer, numIndices, vertexFormat, effect, pType, 0, numPrimitives);

            return(renderData);
        }
        static IndexBuffer IndexLinesBuffer(int pointsCount)
        {
            Debug.Assert(pointsCount <= VertexThreshold);

            if (indexLinesBuffer == null)
            {
                indexLinesBuffer = new IndexBuffer(VertexThreshold * IndexLine.GetSizeInShortInts());
                indexLinesBuffer.Map(VertexThreshold * IndexLine.GetSizeInShortInts());
                using (var istream = indexLinesBuffer.GetIndexStreamLine())
                {
                    for (int vi = 0; vi < VertexThreshold - 1; ++vi)
                    {
                        istream.AddLine(new IndexLine(vi, vi + 1));
                    }
                }
                indexLinesBuffer.Unmap();
            }

            Debug.Assert(indexLinesBuffer.IsValid());
            return(indexLinesBuffer);
        }
        protected static IndexBuffer ToEdgeBuffer
        (
            Rhino.Geometry.Mesh mesh,
            Primitive.Part part,
            out int linesCount
        )
        {
            if (part.VertexCount != mesh.Vertices.Count)
            {
                if (part.VertexCount > 0)
                {
                    linesCount = -part.VertexCount;
                    return(IndexPointsBuffer(part.VertexCount));
                }

                linesCount = 0;
            }
            else
            {
                var edgeIndices = new List <IndexPair>();
                if (mesh.Ngons.Count > 0)
                {
                    foreach (var ngon in mesh.Ngons)
                    {
                        var boundary = ngon.BoundaryVertexIndexList();
                        if ((boundary?.Length ?? 0) > 1)
                        {
                            for (int b = 0; b < boundary.Length - 1; ++b)
                            {
                                edgeIndices.Add(new IndexPair((int)boundary[b], (int)boundary[b + 1]));
                            }

                            edgeIndices.Add(new IndexPair((int)boundary[boundary.Length - 1], (int)boundary[0]));
                        }
                    }
                }
                else
                {
                    var vertices  = mesh.TopologyVertices;
                    var edges     = mesh.TopologyEdges;
                    var edgeCount = edges.Count;
                    for (int e = 0; e < edgeCount; ++e)
                    {
                        if (edges.IsEdgeUnwelded(e) || edges.GetConnectedFaces(e).Length < 2)
                        {
                            var pair = edges.GetTopologyVertices(e);
                            pair.I = vertices.MeshVertexIndices(pair.I)[0];
                            pair.J = vertices.MeshVertexIndices(pair.J)[0];
                            edgeIndices.Add(pair);
                        }
                    }
                }

                linesCount = edgeIndices.Count;
                if (linesCount > 0)
                {
                    var ib = new IndexBuffer(linesCount * IndexLine.GetSizeInShortInts());
                    ib.Map(linesCount * IndexLine.GetSizeInShortInts());
                    using (var istream = ib.GetIndexStreamLine())
                    {
                        foreach (var edge in edgeIndices)
                        {
                            Debug.Assert(0 <= edge.I && edge.I < part.VertexCount);
                            Debug.Assert(0 <= edge.J && edge.J < part.VertexCount);
                            istream.AddLine(new IndexLine(edge.I, edge.J));
                        }
                    }
                    ib.Unmap();

                    return(ib);
                }
            }

            return(null);
        }