Example #1
0
 /// <summary>
 /// Draws the specified bitmap at a specified point using the
 /// D2dGraphic's DPI instead of the bitmap's DPI</summary>
 /// <param name="bmp">The bitmap to render</param>
 /// <param name="point">Point structure that represents the location of the upper-left
 /// corner of the drawn bitmap</param>
 /// <param name="opacity">A value between 0.0f and 1.0f, inclusive, that specifies an opacity value
 /// to apply to the bitmap. This value is multiplied against the alpha values
 /// of the bitmap's contents.</param>
 public void DrawBitmap(D2dBitmap bmp, PointF point, float opacity = 1.0f)
 {
     SizeF bmpsize = bmp.PixelSize;
     var rect = new SharpDX.RectangleF(point.X, point.Y, bmpsize.Width, bmpsize.Height);
     m_renderTarget.DrawBitmap(bmp.NativeBitmap, rect, opacity, BitmapInterpolationMode.Linear, null);
 }
Example #2
0
 /// <summary>
 /// Paints the interior of the specified rounded rectangle</summary>
 /// <param name="roundedRect">The dimensions of the rounded rectangle to paint, in pixels</param>
 /// <param name="brush">The brush used to paint the interior of the rounded rectangle</param>
 public void FillRoundedRectangle(D2dRoundedRect roundedRect, D2dBrush brush)
 {
     var tmp = new RoundedRectangle();
     tmp.RadiusX = roundedRect.RadiusX;
     tmp.RadiusY = roundedRect.RadiusY;
     var r = new SharpDX.RectangleF(
         roundedRect.Rect.Left,
         roundedRect.Rect.Top,
         roundedRect.Rect.Right,
         roundedRect.Rect.Bottom);
     tmp.Rect = r;
     m_renderTarget.FillRoundedRectangle(ref tmp, brush.NativeBrush);
 }
Example #3
0
        private async void DrawKDTree(RenderTarget renderTarget2d, RectangleF view, int zoomLevel)
        {
            if (substations == null || substations.Count == 0 || !draw)
            {
                return;
            }

            // renderTarget2d.Clear(new Color4(0,0,0,0));

            if (_bmp == null)
            {
                float min = float.MaxValue;
                float max = float.MinValue;
                foreach (MM_Substation sub in substations)
                {
                    var pu = float.IsNaN(sub.Average_pU) ? 1 : sub.Average_pU;

                    if (pu < min)
                    {
                        min = pu;
                    }
                    if (pu > max)
                    {
                        max = pu;
                    }
                }

                var range = Math.Max(Math.Abs(1 - min), Math.Abs(max - 1));
                max = 1 + range;
                min = 1 - range;

                int height           = 64 * 2;
                int width            = 96 * 2;
                int stride           = width * sizeof(int);
                var bitmapProperties = new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));

                using (var tempStream = new DataStream(height * stride, true, true))
                {
                    for (int yPixel = 0; yPixel < height; yPixel++)

                    {
                        double yPosition = ((double)yPixel / height) * view.Height + view.Top;

                        for (int xPixel = 0; xPixel < width; xPixel++)
                        {
                            double xPosition = ((double)xPixel / width) * view.Width + view.Left;

                            int searchRange    = 1;
                            int maxSearchRange = 5;
                            var neighbors      = _kdTree.NearestNeighbors(new[] { xPosition, yPosition }, 25, zoomLevel);

                            while (!neighbors.MoveNext() && searchRange < maxSearchRange)
                            {
                                neighbors = _kdTree.NearestNeighbors(new[] { xPosition, yPosition }, 25, Math.Pow(zoomLevel, ++searchRange));
                            }
                            neighbors = _kdTree.NearestNeighbors(new[] { xPosition, yPosition }, 25, Math.Pow(zoomLevel, ++searchRange));
                            neighbors.MoveNext();
                            double totalWeight = 0;
                            double totalValue  = 0;
                            List <Tuple <double, MM_Substation> > subWeights = new List <Tuple <double, MM_Substation> >();

                            int numMatches = 0;
                            if (neighbors.CurrentDistance >= 0)
                            {
                                do
                                {
                                    var curDistance = neighbors.CurrentDistance / zoomLevel;
                                    var substation  = (MM_Substation)((IEnumerator)neighbors).Current;
                                    if (float.IsNaN(substation.Average_pU))
                                    {
                                        continue;
                                    }

                                    var weight = curDistance < double.Epsilon ? 1 : 1 / curDistance;
                                    totalWeight += weight;
                                    totalValue  += substation.Average_pU * weight;
                                    subWeights.Add(new Tuple <double, MM_Substation>(weight, substation));
                                    numMatches++;
                                }while (neighbors.MoveNext());
                            }

                            double ratio = 0;
                            if (numMatches > 0)
                            {
                                totalValue /= totalWeight;
                                ratio       = (totalValue - min) / (max - min);
                                if (numMatches <= 0)
                                {
                                    ratio = 0;
                                }
                            }

                            if (numMatches > 0)
                            {
                                byte B    = (byte)(ratio * byte.MaxValue);
                                byte G    = ratio > 0.4 && ratio < 0.6 ? (byte)(1 - (Math.Abs(ratio - 0.5) / 0.2) * byte.MaxValue) : (byte)0;
                                byte R    = (byte)(1 - ratio * byte.MaxValue);
                                byte A    = 255;
                                int  rgba = R | (G << 8) | (B << 16) | (A << 24);

                                tempStream.Write(rgba);
                            }
                            else
                            {
                                tempStream.Write(0);
                            }
                        }
                    }
                    tempStream.Position = 0;
                    _bmp = new SharpDX.Direct2D1.Bitmap(renderTarget2d, new Size2(width, height), tempStream, stride, bitmapProperties);
                }
            }
            if (_bmp != null && _surface != null)
            {
                var dest = new SharpDX.RectangleF(-_surface.RenderTarget2D.Transform.M31, -_surface.RenderTarget2D.Transform.M32, _surface.RenderTarget2D.Size.Width, _surface.RenderTarget2D.Size.Height);
                _surface.RenderTarget2D.DrawBitmap(_bmp, dest, 0.5f, BitmapInterpolationMode.Linear);
            }
        }
Example #4
0
 /// <summary>
 /// Paints the interior of the specified rectangle</summary>
 /// <param name="rect">Rectangle to paint, in pixels</param>
 /// <param name="brush">The brush used to paint the rectangle's interior</param>
 public void FillRectangle(RectangleF rect, D2dBrush brush)
 {
     var r = new SharpDX.RectangleF(rect.Left, rect.Top, rect.Right, rect.Bottom);
     m_renderTarget.FillRectangle(r, brush.NativeBrush);
 }
Example #5
0
 /// <summary>
 /// Paints the interior of the specified rectangle with a smooth color gradient</summary>
 /// <param name="rect">Rectangle to paint, in pixels</param>
 /// <param name="pt1">The point, in pixels, that color1 gets mapped to</param>
 /// <param name="pt2">The point, in pixels, that color2 gets mapped to</param>
 /// <param name="color1">The color to use at the first point</param>
 /// <param name="color2">The color to use at the second point</param>
 /// <remarks>Note that each color combination is used to create a brush that
 /// is cached, so you cannot use an unlimited number of color combinations.</remarks>
 public void FillRectangle(RectangleF rect, PointF pt1, PointF pt2, Color color1, Color color2)
 {
     var r = new SharpDX.RectangleF(rect.Left, rect.Top, rect.Right, rect.Bottom);
     var brush = GetCachedLinearGradientBrush(color1, color2);
     brush.StartPoint = new DrawingPointF(pt1.X, pt1.Y);
     brush.EndPoint = new DrawingPointF(pt2.X, pt2.Y);
     m_renderTarget.FillRectangle(r, brush);
 }
Example #6
0
        /// <summary>
        /// Draws a solid rectangle with the specified brush while using the alpha channel of the given
        /// bitmap to control the opacity of each pixel.</summary>
        /// <param name="opacityMask">The opacity mask to apply to the brush. The alpha value of
        /// each pixel in the region specified by sourceRectangle is multiplied with the alpha value
        /// of the brush after the brush has been mapped to the area defined by destinationRectangle.</param>
        /// <param name="brush">The brush used to paint the region of the render target specified by destinationRectangle.</param>
        /// <param name="destRect">The region of the render target to paint, in device-independent pixels.</param>
        /// <param name="sourceRect">The region of the bitmap to use as the opacity mask, in device-independent pixels.</param>
        public void FillOpacityMask(D2dBitmap opacityMask, D2dBrush brush, RectangleF destRect, RectangleF sourceRect)
        {
            var drect = new SharpDX.RectangleF(destRect.X, destRect.Y, destRect.Right, destRect.Bottom);
            var srect = new SharpDX.RectangleF(sourceRect.X, sourceRect.Y, sourceRect.Right, sourceRect.Bottom);

            m_renderTarget.FillOpacityMask(opacityMask.NativeBitmap, brush.NativeBrush,
                                           OpacityMaskContent.Graphics,
                                           drect, srect);
        }
Example #7
0
 /// <summary>
 /// Draws the outline of a rectangle that has the specified dimensions and stroke style</summary>
 /// <param name="rect">The dimensions of the rectangle to draw, in pixels</param>
 /// <param name="brush">The brush used to paint the rectangle's stroke</param>
 /// <param name="strokeWidth">A value greater than or equal to 0.0f that specifies the width 
 /// of the rectangle's stroke. The stroke is centered on the rectangle's outline.</param>
 /// <param name="strokeStyle">The style of stroke to paint or null to draw a solid line</param>
 public void DrawRectangle(RectangleF rect, D2dBrush brush, float strokeWidth = 1.0f, D2dStrokeStyle strokeStyle = null)
 {
     var r = new SharpDX.RectangleF(rect.Left, rect.Top, rect.Right, rect.Bottom);
     m_renderTarget.DrawRectangle(r, brush.NativeBrush, strokeWidth,
         strokeStyle != null ? strokeStyle.NativeStrokeStyle : null);
 }
Example #8
0
        /// <summary>
        /// Draws the specified bitmap after scaling it to the size of the specified rectangle</summary>
        /// <param name="bmp">The bitmap to render</param>
        /// <param name="destRect">The size and position, in pixels, in the D2dGraphics's coordinate
        /// space, of the area in which the bitmap is drawn. If the rectangle is not well-ordered,
        /// nothing is drawn, but the D2dGraphics does not enter an error state.</param>
        /// <param name="opacity">A value between 0.0f and 1.0f, inclusive, that specifies an opacity value        
        /// to apply to the bitmap. This value is multiplied against the alpha values
        /// of the bitmap's contents.</param>
        /// <param name="interpolationMode">The interpolation mode to use if the bitmap is scaled or rotated 
        /// by the drawing operation</param>
        /// <param name="sourceRect">The size and position, in pixels in the bitmap's coordinate space, of the area
        /// within the bitmap to draw</param>
        public void DrawBitmap(D2dBitmap bmp, RectangleF destRect, float opacity, 
            D2dBitmapInterpolationMode interpolationMode, RectangleF sourceRect)
        {
            var drect = new SharpDX.RectangleF(destRect.X, destRect.Y, destRect.Right, destRect.Bottom);
            var srect = new SharpDX.RectangleF(sourceRect.X, sourceRect.Y, sourceRect.Right, sourceRect.Bottom);

            m_renderTarget.DrawBitmap(bmp.NativeBitmap, drect, opacity,
                (BitmapInterpolationMode)interpolationMode, srect);
        }
Example #9
0
 /// <summary>
 /// Draws the specified bitmap after scaling it to the size of the specifiedrectangle</summary>
 /// <param name="bmp">The bitmap to render</param>
 /// <param name="destRect">The size and position, in pixels, in the D2dGraphics's coordinate
 /// space, of the area in which the bitmap is drawn. If the rectangle is not well-ordered,
 /// nothing is drawn, but the D2dGraphics does not enter an error state.</param>
 /// <param name="opacity">A value between 0.0f and 1.0f, inclusive, that specifies an opacity value        
 /// to apply to the bitmap. This value is multiplied against the alpha values
 /// of the bitmap's contents.</param>
 /// <param name="interpolationMode">The interpolation mode to use if the bitmap is scaled or rotated 
 /// by the drawing operation</param>
 public void DrawBitmap(D2dBitmap bmp, RectangleF destRect, float opacity = 1.0f, D2dBitmapInterpolationMode interpolationMode = D2dBitmapInterpolationMode.Linear)
 {
     var rect = new SharpDX.RectangleF(destRect.X, destRect.Y, destRect.Right, destRect.Bottom);
     m_renderTarget.DrawBitmap(bmp.NativeBitmap, rect, opacity, (BitmapInterpolationMode)interpolationMode, null);
 }
Example #10
0
 /// <summary>    
 /// Specifies a rectangle to which all subsequent drawing operations are clipped</summary>
 /// <remarks>The clipRect is transformed by the current world transform set on the render target.
 /// After the transform is applied to the clipRect that is passed in, the axis-aligned 
 /// bounding box for the clipRect is computed. For efficiency, the contents are clipped 
 /// to this axis-aligned bounding box and not to the original clipRect that is passed in.</remarks>    
 /// <param name="clipRect">The size and position of the clipping area, in pixels</param>
 /// <param name="antialiasMode">The antialiasing mode that is used to draw the edges of clip rects that have subpixel 
 /// boundaries, and to blend the clip with the scene contents. The blending is performed 
 /// once when the PopAxisAlignedClip method is called, and does not apply to each 
 /// primitive within the layer.</param>        
 public void PushAxisAlignedClip(RectangleF clipRect, D2dAntialiasMode antialiasMode)
 {
     m_clipStack.Push(clipRect);
     var rect = new SharpDX.RectangleF(clipRect.Left, clipRect.Top, clipRect.Right, clipRect.Bottom);
     m_renderTarget.PushAxisAlignedClip(rect, (AntialiasMode)antialiasMode);
 }