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);
        }
Exemple #2
0
        // A helper function, analogous to ProcessFaces.
        private void ProcessEdges(RenderingPassBufferStorage bufferStorage)
        {
            List <IList <XYZ> > edges = bufferStorage.EdgeXYZs;

            if (edges.Count == 0)
            {
                return;
            }

            // Edges are encoded as line segment primitives whose vertices contain only position information.
            bufferStorage.FormatBits = VertexFormatBits.Position;

            int        edgeVertexBufferSizeInFloats = VertexPosition.GetSizeInFloats() * bufferStorage.VertexBufferCount;
            List <int> numVerticesInEdgesBefore     = new List <int>();

            numVerticesInEdgesBefore.Add(0);

            bufferStorage.VertexBuffer = new VertexBuffer(edgeVertexBufferSizeInFloats);
            bufferStorage.VertexBuffer.Map(edgeVertexBufferSizeInFloats);
            {
                VertexStreamPosition vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPosition();
                foreach (IList <XYZ> xyzs in edges)
                {
                    foreach (XYZ vertex in xyzs)
                    {
                        vertexStream.AddVertex(new VertexPosition(vertex + m_offset));
                    }

                    numVerticesInEdgesBefore.Add(numVerticesInEdgesBefore.Last() + xyzs.Count);
                }
            }
            bufferStorage.VertexBuffer.Unmap();

            int edgeNumber = 0;

            bufferStorage.IndexBufferCount = bufferStorage.PrimitiveCount * IndexLine.GetSizeInShortInts();
            int indexBufferSizeInShortInts = 1 * bufferStorage.IndexBufferCount;

            bufferStorage.IndexBuffer = new IndexBuffer(indexBufferSizeInShortInts);
            bufferStorage.IndexBuffer.Map(indexBufferSizeInShortInts);
            {
                IndexStreamLine indexStream = bufferStorage.IndexBuffer.GetIndexStreamLine();
                foreach (IList <XYZ> xyzs in edges)
                {
                    int startIndex = numVerticesInEdgesBefore[edgeNumber];
                    for (int i = 1; i < xyzs.Count; i++)
                    {
                        // Add two indices that define a line segment.
                        indexStream.AddLine(new IndexLine((int)(startIndex + i - 1),
                                                          (int)(startIndex + i)));
                    }
                    edgeNumber++;
                }
            }
            bufferStorage.IndexBuffer.Unmap();


            bufferStorage.VertexFormat   = new VertexFormat(bufferStorage.FormatBits);
            bufferStorage.EffectInstance = new EffectInstance(bufferStorage.FormatBits);
        }
        public static int GetPrimitiveSize(PrimitiveType primitive)
        {
            switch (primitive)
            {
            case PrimitiveType.LineList: return(IndexLine.GetSizeInShortInts());

            case PrimitiveType.PointList: return(IndexPoint.GetSizeInShortInts());

            case PrimitiveType.TriangleList: return(IndexTriangle.GetSizeInShortInts());

            default: break;
            }
            return(IndexTriangle.GetSizeInShortInts());
        }
        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);
        }