예제 #1
0
        /// <summary>
        /// Visualises 2D data to a bitmap.
        /// </summary>
        /// <param name="bitmap">Bitmap.</param>
        /// <param name="volume">2D data.</param>
        /// <param name="volumeBoundingBox">Bounding box.</param>
        public static void Visualise2D(Bitmap bitmap, VolumeData2D volume, BoundingBox2D volumeBoundingBox)
        {
            var colors = SetupColors(volume.Cells.Count);

            // Access bitmap data
            int        width      = bitmap.Width;
            int        height     = bitmap.Height;
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            unsafe
            {
                // Get pointer to bitmap data
                byte *pointer = (byte *)bitmapData.Scan0;

                // Setup a point
                var point = new Vector2D();

                // For each pixel of a bitmap find a cell and color the pixel.
                for (int i = 0; i < height; i++)
                {
                    // Remap pixel to a position in 3D space.
                    point.Y = MathUtils.Remap(i, 0, height, volumeBoundingBox.Max.Y, volumeBoundingBox.Min.Y);

                    for (int j = 0; j < width; j++)
                    {
                        // Remap pixel to a position in 3D space
                        point.X = MathUtils.Remap(j, 0, width, volumeBoundingBox.Min.X, volumeBoundingBox.Max.X);

                        // For each cell check if it contains the point
                        for (int k = 0; k < volume.Cells.Count; k++)
                        {
                            if (volume.Cells[k].Contains(point))
                            {
                                // Color the pixel
                                pointer[0] = colors[k].B;
                                pointer[1] = colors[k].G;
                                pointer[2] = colors[k].R;
                                pointer[3] = colors[k].A;
                                break;
                            }
                        }

                        pointer += BytesPerPixel;
                    }
                }
            }

            bitmap.UnlockBits(bitmapData);
        }
예제 #2
0
        /// <summary>
        /// Test triangulation volumetric data in 2D.
        /// </summary>
        public static void TestFromTriangulationSingleTriangle()
        {
            var vertices = new List <Vertex>()
            {
                new Vertex(0, 0),
                new Vertex(1, 0),
                new Vertex(0, 1)
            };

            var triangles = new List <Triangle>()
            {
                new Triangle(0, 1, 2)
            };

            var volumeData = new VolumeData2D();

            volumeData.FromTriangulation(vertices, triangles);
        }
        /// <summary>
        /// Writes 2D data to a file.
        /// </summary>
        /// <param name="path">Path of the file.</param>
        /// <param name="volumeData">Data.</param>
        public void Write(string path, VolumeData2D volumeData)
        {
            StreamWriter writer = new StreamWriter(path);

            writer.WriteLine("# centroid.X" + separator + "centroid.Y" + separator + "triangleIndex" + separator + "voronoiIndex" + separator + "weight" + separator + "visited" + separator + "edgeIndices");

            var edgeList = new List <Edge2D>();

            foreach (var cell in volumeData.Cells)
            {
                writer.Write(cell.Centroid.X);
                writer.Write(separator);
                writer.Write(cell.Centroid.Y);
                writer.Write(separator);
                writer.Write(cell.TriangleIndex);
                writer.Write(separator);
                writer.Write(cell.VoronoiIndex);
                writer.Write(separator);
                writer.Write(cell.Weight);
                writer.Write(separator);
                writer.Write(cell.Visited);

                for (int i = 0; i < cell.Edges.Count; i++)
                {
                    writer.Write(separator);
                    writer.Write(edgeList.Count);

                    edgeList.Add(cell.Edges[i]);
                }

                writer.WriteLine();
            }

            writer.WriteLine("# normal.x" + separator + "normal.y" + separator + "c" + separator + "sourceIndex" + separator + "targetIndex");
            foreach (var edge in edgeList)
            {
                writer.Write(edge.Normal.X);
                writer.Write(separator);
                writer.Write(edge.Normal.Y);
                writer.Write(separator);
                writer.Write(edge.C);
                writer.Write(separator);

                int sourceIndex = -1;
                int targetIndex = -1;

                for (int i = 0; i < volumeData.Cells.Count; i++)
                {
                    if (volumeData.Cells[i] == edge.Source)
                    {
                        sourceIndex = i;
                    }

                    if (volumeData.Cells[i] == edge.Target)
                    {
                        targetIndex = i;
                    }

                    if (sourceIndex != -1 && targetIndex != -1)
                    {
                        break;
                    }
                }

                writer.Write(sourceIndex);
                writer.Write(separator);
                writer.Write(targetIndex);

                writer.WriteLine();
            }

            writer.Close();
        }