protected static int ToPointsBuffer
        (
            Rhino.Geometry.Point point,
            out VertexFormatBits vertexFormatBits,
            out VertexBuffer vb, out int vertexCount,
            out IndexBuffer ib
        )
        {
            int pointsCount = 0;

            if (point.Location.IsValid)
            {
                pointsCount = 1;
                vertexCount = 1;

                vertexFormatBits = VertexFormatBits.Position;
                vb = new VertexBuffer(pointsCount * VertexPosition.GetSizeInFloats());
                vb.Map(pointsCount * VertexPosition.GetSizeInFloats());
                using (var vstream = vb.GetVertexStreamPosition())
                {
                    vstream.AddVertex(new VertexPosition(RawEncoder.ToHost(point.Location)));
                }
                vb.Unmap();

                ib = IndexPointsBuffer(pointsCount);
            }
            else
            {
                vertexFormatBits = 0;
                vb = null; vertexCount = 0;
                ib = null;
            }

            return(pointsCount);
        }
        public static int GetVertexSize(VertexFormatBits format)
        {
            switch (format)
            {
            case VertexFormatBits.Position: return(VertexPosition.GetSizeInFloats());

            case VertexFormatBits.PositionColored:
                return(VertexPositionColored.GetSizeInFloats());

            case VertexFormatBits.PositionNormal:
                return(VertexPositionNormal.GetSizeInFloats());

            case VertexFormatBits.PositionNormalColored:
                return(VertexPositionNormalColored.GetSizeInFloats());

            default: break;
            }
            return(VertexPosition.GetSizeInFloats());
        }
        protected static int ToPolylineBuffer
        (
            Rhino.Geometry.Polyline polyline,
            out VertexFormatBits vertexFormatBits,
            out VertexBuffer vb, out int vertexCount,
            out IndexBuffer ib
        )
        {
            int linesCount = 0;

            if (polyline.SegmentCount > 0)
            {
                linesCount  = polyline.SegmentCount;
                vertexCount = polyline.Count;

                vertexFormatBits = VertexFormatBits.Position;
                vb = new VertexBuffer(vertexCount * VertexPosition.GetSizeInFloats());
                vb.Map(vertexCount * VertexPosition.GetSizeInFloats());
                using (var vstream = vb.GetVertexStreamPosition())
                {
                    foreach (var v in polyline)
                    {
                        vstream.AddVertex(new VertexPosition(RawEncoder.ToHost(v)));
                    }
                }
                vb.Unmap();

                ib = IndexLinesBuffer(vertexCount);
            }
            else
            {
                vertexFormatBits = 0;
                vb = null; vertexCount = 0;
                ib = null;
            }

            return(linesCount);
        }
 public static bool HasVertexColors(VertexFormatBits vertexFormatBits) => (((int)vertexFormatBits) & 4) != 0;
 public static bool HasVertexNormals(VertexFormatBits vertexFormatBits) => (((int)vertexFormatBits) & 2) != 0;
        protected static int ToPointsBuffer
        (
            Rhino.Geometry.PointCloud pointCloud,
            Primitive.Part part,
            out VertexFormatBits vertexFormatBits,
            out VertexBuffer vb, out int vertexCount,
            out IndexBuffer ib
        )
        {
            int pointsCount = part.VertexCount;
            int normalCount = pointCloud.ContainsNormals ? pointsCount : 0;
            int colorsCount = pointCloud.ContainsColors  ? pointsCount : 0;

            bool hasPoints  = pointsCount > 0;
            bool hasNormals = normalCount == pointsCount;
            bool hasColors  = colorsCount == pointsCount;

            if (hasPoints)
            {
                if (hasNormals)
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormalColored;
                        vb = new VertexBuffer(pointsCount * VertexPositionNormalColored.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionNormalColored.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionNormalColored())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                var c     = new ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A);
                                vstream.AddVertex(new VertexPositionNormalColored(RawEncoder.ToHost(point.Location), RawEncoder.ToHost(point.Normal), c));
                            }
                        }

                        vb.Unmap();
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormal;
                        vb = new VertexBuffer(pointsCount * VertexPositionNormal.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionNormal.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionNormal())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                vstream.AddVertex(new VertexPositionNormal(RawEncoder.ToHost(point.Location), RawEncoder.ToHost(point.Normal)));
                            }
                        }

                        vb.Unmap();
                    }
                }
                else
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionColored;
                        vb = new VertexBuffer(pointsCount * VertexPositionColored.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionColored.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionColored())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                var c     = new ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A);
                                vstream.AddVertex(new VertexPositionColored(RawEncoder.ToHost(point.Location), c));
                            }
                        }

                        vb.Unmap();
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.Position;
                        vb = new VertexBuffer(pointsCount * VertexPosition.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPosition.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPosition())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                vstream.AddVertex(new VertexPosition(RawEncoder.ToHost(point.Location)));
                            }
                        }

                        vb.Unmap();
                    }
                }

                ib = IndexPointsBuffer(pointsCount);
            }
            else
            {
                vertexFormatBits = 0;
                vb = null;
                ib = null;
            }

            vertexCount = pointsCount;
            return(pointsCount);
        }
        protected static VertexBuffer ToVertexBuffer
        (
            Rhino.Geometry.Mesh mesh,
            Primitive.Part part,
            out VertexFormatBits vertexFormatBits,
            System.Drawing.Color color = default
        )
        {
            int verticesCount = part.EndVertexIndex - part.StartVertexIndex;
            int normalCount   = mesh.Normals.Count == mesh.Vertices.Count ? verticesCount : 0;
            int colorsCount   = color.IsEmpty ? (mesh.VertexColors.Count == mesh.Vertices.Count ? verticesCount : 0) : verticesCount;

            bool hasVertices = verticesCount > 0;
            bool hasNormals  = normalCount > 0;
            bool hasColors   = colorsCount > 0;

            if (hasVertices)
            {
                var vertices = mesh.Vertices;
                if (hasNormals)
                {
                    var normals = mesh.Normals;
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormalColored;
                        var colors = mesh.VertexColors;
                        var vb     = new VertexBuffer(verticesCount * VertexPositionNormalColored.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionNormalColored.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionNormalColored())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                var  c = !color.IsEmpty ? color : colors[v];
                                uint T = Math.Max(1, 255u - c.A);
                                stream.AddVertex(new VertexPositionNormalColored(RawEncoder.ToHost(vertices[v]), RawEncoder.ToHost(normals[v]), new ColorWithTransparency(c.R, c.G, c.B, T)));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormal;
                        var vb = new VertexBuffer(verticesCount * VertexPositionNormal.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionNormal.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionNormal())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                stream.AddVertex(new VertexPositionNormal(RawEncoder.ToHost(vertices[v]), RawEncoder.ToHost(normals[v])));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                }
                else
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionColored;
                        var colors = mesh.VertexColors;
                        var vb     = new VertexBuffer(verticesCount * VertexPositionColored.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionColored.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionColored())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                var  c = !color.IsEmpty ? color : colors[v];
                                uint T = Math.Max(1, 255u - c.A);
                                stream.AddVertex(new VertexPositionColored(RawEncoder.ToHost(vertices[v]), new ColorWithTransparency(c.R, c.G, c.B, T)));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.Position;
                        var vb = new VertexBuffer(verticesCount * VertexPosition.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPosition.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPosition())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                stream.AddVertex(new VertexPosition(RawEncoder.ToHost(vertices[v])));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                }
            }

            vertexFormatBits = 0;
            return(null);
        }