Example #1
0
        /// <summary>
        /// This figures out which tiles each polygon intersects
        /// </summary>
        /// <param name="size">The size of the grid</param>
        /// <param name="pixelsX">How many tiles along X</param>
        /// <param name="pixelsY">How many tiles along Y</param>
        /// <param name="triangles">The triangles should be shifted to cover the grid, Z is ignored</param>
        /// <returns>
        /// [index of triangle][array of tiles it covers]
        /// </returns>
        private static OverlayResult[][] GetIntersections(Size size, int pixelsX, int pixelsY, Point[][] polygons)
        {
            OverlayResult[][] retVal = new OverlayResult[polygons.Length][];

            // Convert each pixel into a rectangle
            Tuple <Rect, int, int>[] tiles = GetTiles(size, pixelsX, pixelsY);

            for (int cntr = 0; cntr < polygons.Length; cntr++)
            {
                // See which tiles this triangle intersects (and how much of each tile)
                retVal[cntr] = IntersectTiles(polygons[cntr], tiles);
            }

            // Exit Function
            return(retVal);
        }
        internal static void CreateNeurons(out Neuron_SensorPosition[] neuronsR, out Neuron_SensorPosition[] neuronsG, out Neuron_SensorPosition[] neuronsB, out OverlayResult[][] overlayR, out OverlayResult[][] overlayG, out OverlayResult[][] overlayB, out int pixelWidthHeight, ShipPartDNA dna, ItemOptions itemOptions, double neuronDensity)
        {
            const int MINPIXELWIDTH = 16;

            #region Calculate counts

            // Figure out how many neurons to make
            //NOTE: This radius isn't taking SCALE into account.  The other neural parts do this as well, so the neural density properties can be more consistent
            double radius = (dna.Scale.X + dna.Scale.Y + dna.Scale.Z) / (3d * 2d);		// xyz should all be the same anyway
            double area = Math.Pow(radius, itemOptions.Sensor_NeuronGrowthExponent);

            int neuronCount = Convert.ToInt32(Math.Ceiling(neuronDensity * area));
            if (neuronCount == 0)
            {
                neuronCount = 1;
            }

            // Figure out how many pixels to make
            pixelWidthHeight = neuronCount / 9;     // dividing by 3 to get the number of neurons in a single plate.  divide that by 3, because that's a good ratio of neuron cells to pixels
            if (pixelWidthHeight < MINPIXELWIDTH)
            {
                pixelWidthHeight = MINPIXELWIDTH;
            }

            #endregion

            #region Neurons

            // Place them evenly in a sphere
            //NOTE: An interesting side effect of calling this for each generation is that the parent may not have been perfectly evenly spaced, but calling this
            //again will slightly refine the positions
            Vector3D[][] positions = GetNeuronPositions(dna.Neurons, neuronCount, 3, radius);

            // Create neurons
            neuronsR = positions[0].Select(o => new Neuron_SensorPosition(o.ToPoint(), true)).ToArray();
            neuronsG = positions[1].Select(o => new Neuron_SensorPosition(o.ToPoint(), true)).ToArray();
            neuronsB = positions[2].Select(o => new Neuron_SensorPosition(o.ToPoint(), true)).ToArray();

            #endregion

            #region Polygons around neurons

            // Figure out which pixels each neuron intersects with
            VoronoiResult2D[] voronoi = new VoronoiResult2D[3];
            voronoi[0] = Math2D.CapVoronoiCircle(Math2D.GetVoronoi(positions[0].Select(o => new Point(o.X, o.Y)).ToArray(), true));
            voronoi[1] = Math2D.CapVoronoiCircle(Math2D.GetVoronoi(positions[1].Select(o => new Point(o.X, o.Y)).ToArray(), true));
            voronoi[2] = Math2D.CapVoronoiCircle(Math2D.GetVoronoi(positions[2].Select(o => new Point(o.X, o.Y)).ToArray(), true));

            #region Figure out the extremes

            Point[] allEdgePoints = voronoi.SelectMany(o => o.EdgePoints).ToArray();

            Point min = new Point(allEdgePoints.Min(o => o.X), allEdgePoints.Min(o => o.Y));
            Point max = new Point(allEdgePoints.Max(o => o.X), allEdgePoints.Max(o => o.Y));

            double width = max.X - min.X;
            double height = max.Y - min.Y;

            // Enlarge a bit
            min.X -= width * .05d;
            min.Y -= height * .05d;
            max.X += width * .05d;
            max.Y += height * .05d;

            width = max.X - min.X;
            height = max.Y - min.Y;

            Vector offset = new Vector(-min.X, -min.Y);

            #endregion

            //  Figure out which pixels each polygon overlaps
            overlayR = GetIntersections(new Size(width, height), pixelWidthHeight, pixelWidthHeight, GetPolygons(voronoi[0], offset));
            overlayG = GetIntersections(new Size(width, height), pixelWidthHeight, pixelWidthHeight, GetPolygons(voronoi[1], offset));
            overlayB = GetIntersections(new Size(width, height), pixelWidthHeight, pixelWidthHeight, GetPolygons(voronoi[2], offset));

            #endregion
        }
        internal static byte[] GetColor(OverlayResult[] pixels, IBitmapCustom bitmap)
        {
            Tuple<byte[], double>[] colors = new Tuple<byte[], double>[pixels.Length];

            for (int cntr = 0; cntr < pixels.Length; cntr++)
            {
                colors[cntr] = Tuple.Create(bitmap.GetColor_Byte(pixels[cntr].X, pixels[cntr].Y), pixels[cntr].Percent);
            }

            return UtilityWPF.AverageColors(colors);
        }
        /// <summary>
        /// This figures out which tiles each polygon intersects
        /// </summary>
        /// <param name="size">The size of the grid</param>
        /// <param name="pixelsX">How many tiles along X</param>
        /// <param name="pixelsY">How many tiles along Y</param>
        /// <param name="triangles">The triangles should be shifted to cover the grid, Z is ignored</param>
        /// <returns>
        /// [index of triangle][array of tiles it covers]
        /// </returns>
        private static OverlayResult[][] GetIntersections(Size size, int pixelsX, int pixelsY, Point[][] polygons)
        {
            OverlayResult[][] retVal = new OverlayResult[polygons.Length][];

            // Convert each pixel into a rectangle
            Tuple<Rect, int, int>[] tiles = GetTiles(size, pixelsX, pixelsY);

            for (int cntr = 0; cntr < polygons.Length; cntr++)
            {
                // See which tiles this triangle intersects (and how much of each tile)
                retVal[cntr] = IntersectTiles(polygons[cntr], tiles);
            }

            // Exit Function
            return retVal;
        }
            /// <summary>
            /// This figures out which tiles each triangle intersects
            /// </summary>
            /// <param name="size">The size of the grid</param>
            /// <param name="pixelsX">How many tiles along X</param>
            /// <param name="pixelsY">How many tiles along Y</param>
            /// <param name="triangles">The triangles should be shifted to cover the grid, Z is ignored</param>
            /// <returns>
            /// [index of triangle][array of tiles it covers]
            /// </returns>
            public static OverlayResult[][] GetIntersections(out Rect[] pixelRects, Size size, int pixelsX, int pixelsY, ITriangle[] triangles)
            {
                OverlayResult[][] retVal = new OverlayResult[triangles.Length][];

                // Convert each pixel into a rectangle
                Tuple<Rect, int, int>[] tiles = GetTiles(size, pixelsX, pixelsY);

                for (int cntr = 0; cntr < triangles.Length; cntr++)
                {
                    // See which tiles this triangle intersects (and how much of each tile)
                    retVal[cntr] = IntersectTiles(triangles[cntr], tiles);
                }

                // Exit Function
                pixelRects = tiles.Select(o => o.Item1).ToArray();
                return retVal;
            }
Example #6
0
 internal void CloseOverlayReference(OverlayResult overlayDataResult)
 {
     Close?.Invoke(overlayDataResult);
 }
Example #7
0
 public void Close(OverlayResult overlayDataResult)
 {
     _overlayService.Detach(overlayDataResult);
 }