private static Brush GetTiledSamples(Point[] edgePoints, ISOMInput[] samples, SOMNode node, int tileWidth, int tileHeight, Action <DrawTileArgs> drawTile) { var dimensions = GetTileImagePositions(samples.Length); // The image tiles will be drawn spiraling out from the center. Order the list so that tiles closest to the node are first (so that // they are drawn closer to the center of the spiral) ISOMInput[] orderedSamples = samples. OrderBy(o => (o.Weights - node.Weights).LengthSquared). ToArray(); //int tilehalf_left = tileWidth / 2; //int tilehalf_top = tileHeight / 2; //int tilehalf_right = tileWidth - tilehalf_left; //int tilehalf_bot = tileHeight - tilehalf_top; int imageWidth = (dimensions.Item2.X - dimensions.Item1.X + 1) * tileWidth; int imageHeight = (dimensions.Item2.Y - dimensions.Item1.Y + 1) * tileHeight; //int offsetX = (Math.Abs(dimensions.Item1.X) * tileWidth) + tilehalf_left; //int offsetY = (Math.Abs(dimensions.Item1.Y) * tileHeight) + tilehalf_top; //TODO: Get the AABB of edgePoints. If the bitmap will be bigger than the aabb, then draw just enough to totally fill the polygon //NOTE: Copied from UtilityWPF.GetBitmap WriteableBitmap bitmap = new WriteableBitmap(imageWidth, imageHeight, UtilityWPF.DPI, UtilityWPF.DPI, PixelFormats.Pbgra32, null); // may want Bgra32 if performance is an issue int pixelWidth = bitmap.Format.BitsPerPixel / 8; int stride = bitmap.PixelWidth * pixelWidth; // this is the length of one row of pixels byte[] pixels = new byte[bitmap.PixelHeight * stride]; DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { for (int cntr = 0; cntr < orderedSamples.Length; cntr++) { int x = (dimensions.Item3[cntr].X - dimensions.Item1.X) * tileWidth; int y = (dimensions.Item3[cntr].Y - dimensions.Item1.Y) * tileWidth; DrawTileArgs args = new DrawTileArgs(orderedSamples[cntr], tileWidth, tileHeight, pixels, x, y, stride, pixelWidth); drawTile(args); } } bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), pixels, stride, 0); return(new ImageBrush(bitmap) { Stretch = Stretch.None, }); }
private static Brush GetTiledSamples(Point[] edgePoints, ISOMInput[] samples, SOMNode node, int tileWidth, int tileHeight, Action<DrawTileArgs> drawTile) { var dimensions = GetTileImagePositions(samples.Length); // The image tiles will be drawn spiraling out from the center. Order the list so that tiles closest to the node are first (so that // they are drawn closer to the center of the spiral) ISOMInput[] orderedSamples = samples. OrderBy(o => MathND.GetDistanceSquared(o.Weights, node.Weights)). ToArray(); //int tilehalf_left = tileWidth / 2; //int tilehalf_top = tileHeight / 2; //int tilehalf_right = tileWidth - tilehalf_left; //int tilehalf_bot = tileHeight - tilehalf_top; int imageWidth = (dimensions.Item2.X - dimensions.Item1.X + 1) * tileWidth; int imageHeight = (dimensions.Item2.Y - dimensions.Item1.Y + 1) * tileHeight; //int offsetX = (Math.Abs(dimensions.Item1.X) * tileWidth) + tilehalf_left; //int offsetY = (Math.Abs(dimensions.Item1.Y) * tileHeight) + tilehalf_top; //TODO: Get the AABB of edgePoints. If the bitmap will be bigger than the aabb, then draw just enough to totally fill the polygon //NOTE: Copied from UtilityWPF.GetBitmap WriteableBitmap bitmap = new WriteableBitmap(imageWidth, imageHeight, UtilityWPF.DPI, UtilityWPF.DPI, PixelFormats.Pbgra32, null); // may want Bgra32 if performance is an issue int pixelWidth = bitmap.Format.BitsPerPixel / 8; int stride = bitmap.PixelWidth * pixelWidth; // this is the length of one row of pixels byte[] pixels = new byte[bitmap.PixelHeight * stride]; DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { for (int cntr = 0; cntr < orderedSamples.Length; cntr++) { int x = (dimensions.Item3[cntr].X - dimensions.Item1.X) * tileWidth; int y = (dimensions.Item3[cntr].Y - dimensions.Item1.Y) * tileWidth; DrawTileArgs args = new DrawTileArgs(orderedSamples[cntr], tileWidth, tileHeight, pixels, x, y, stride, pixelWidth); drawTile(args); } #region DISCARD //int index = 0; //for (int y = 0; y < colorsHeight; y++) //{ // for (int x = 0; x < colorsWidth; x++) // { // int gray = (grayColors[index] * grayValueScale).ToInt_Round(); // if (gray < 0) gray = 0; // if (gray > 255) gray = 255; // byte grayByte = Convert.ToByte(gray); // Color color = Color.FromRgb(grayByte, grayByte, grayByte); // ctx.DrawRectangle(new SolidColorBrush(color), null, new Rect(x * scaleX, y * scaleY, scaleX, scaleY)); // index++; // } //} #endregion } bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), pixels, stride, 0); return new ImageBrush(bitmap) { Stretch = Stretch.None, }; }