/// <summary>
        ///
        /// </summary>
        /// <param name="parallel"></param>
        /// <returns></returns>
        public static GridField2d <Vec2d> GetGradient(this GridField2d <double> field, bool parallel = false)
        {
            var result = GridField2d.Vec2d.Create(field);

            GetGradient(field, result, parallel);
            return(result);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="parallel"></param>
        /// <returns></returns>
        public static GridField2d <double> GetCurl(this GridField2d <Vec2d> field, bool parallel = false)
        {
            var result = GridField2d.Double.Create(field);

            GetCurl(field, result, parallel);
            return(result);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="parallel"></param>
        /// <returns></returns>
        public static GridField2d <Vec2d> GetLaplacian(this GridField2d <Vec2d> field, bool parallel = false)
        {
            var result = GridField2d.Vec2d.Create(field);

            GetLaplacian(field, result, parallel);
            return(result);
        }
Beispiel #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="field"></param>
 /// <param name="path"></param>
 /// <param name="mapper"></param>
 public static void SaveAsImage <T>(GridField2d <T> field, string path, Func <T, Color> mapper)
     where T : struct
 {
     using (Bitmap bmp = new Bitmap(field.CountX, field.CountY, PixelFormat.Format32bppArgb))
     {
         WriteToImage(field, bmp, mapper);
         bmp.Save(path);
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        /// <param name="parallel"></param>
        public static void GetLaplacian(this GridField2d <double> field, double[] result, bool parallel = false)
        {
            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;

                (var dx, var dy) = field.Scale;
                dx = 1.0 / (dx * dx);
                dy = 1.0 / (dy * dy);

                (int di, int dj) = field.GetBoundaryOffsets();
                (int i, int j)   = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }

                    double tx0 = (i == 0) ? vals[index + di] : vals[index - 1];
                    double tx1 = (i == nx - 1) ? vals[index - di] : vals[index + 1];

                    double ty0 = (j == 0) ? vals[index + dj] : vals[index - nx];
                    double ty1 = (j == ny - 1) ? vals[index - dj] : vals[index + nx];

                    double t = vals[index] * 2.0;
                    result[index] = (tx0 + tx1 - t) * dx + (ty0 + ty1 - t) * dy;
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        public static void GetCurl(this GridField2d <Vec2d> field, double[] result, bool parallel = false)
        {
            // implementation reference
            // http://www.math.harvard.edu/archive/21a_spring_09/PDF/13-05-curl-and-divergence.pdf

            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;

                (var tx, var ty) = (0.5 / field.Scale);
                (int di, int dj) = field.GetBoundaryOffsets();
                (int i, int j)   = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }

                    Vec2d tx0 = (i == 0) ? vals[index + di] : vals[index - 1];
                    Vec2d tx1 = (i == nx - 1) ? vals[index - di] : vals[index + 1];

                    Vec2d ty0 = (j == 0) ? vals[index + dj] : vals[index - nx];
                    Vec2d ty1 = (j == ny - 1) ? vals[index - dj] : vals[index + nx];

                    result[index] = (tx1.Y - tx0.Y) * tx - (ty1.X - ty0.X) * ty;
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        /// <param name="parallel"></param>
        public static void GetGradient(this GridField2d <double> field, Vec2d[] result, bool parallel = false)
        {
            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;

                (var dx, var dy) = (0.5 / field.Scale);
                (int di, int dj) = field.GetBoundaryOffsets();
                (int i, int j)   = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }

                    double tx0 = (i == 0) ? vals[index + di] : vals[index - 1];
                    double tx1 = (i == nx - 1) ? vals[index - di] : vals[index + 1];

                    double ty0 = (j == 0) ? vals[index + dj] : vals[index - nx];
                    double ty1 = (j == ny - 1) ? vals[index - dj] : vals[index + nx];

                    result[index] = new Vec2d((tx1 - tx0) * dx, (ty1 - ty0) * dy);
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// Calculates L1 (Manhattan) geodesic distance via Dijksta's algorithm as detailed in 
        /// http://www.numerical-tours.com/matlab/fastmarching_0_implementing/
        /// </summary>
        /// <param name="cost"></param>
        /// <param name="sources"></param>
        /// <param name="result"></param>
        public static void GeodesicDistanceL1(GridField2d<double> field, double[] cost, IEnumerable<int> sources, IEnumerable<int> exclude = null)
        {
            // TODO handle additonal wrap modes

            var dists = field.Values;
            int nx = field.CountX;
            int ny = field.CountY;

            (double dx, double dy) = Vec2d.Abs(field.Scale);

            var queue = new PriorityQueue<double, int>();
            dists.Set(double.PositiveInfinity);

            // enqueue sources
            foreach (int i in sources)
            {
                dists[i] = 0.0;
                queue.Insert(0.0, i);
            }

            // exclude
            if (exclude != null)
            {
                foreach (int i in exclude)
                    dists[i] = 0.0;
            }

            // breadth first search from sources
            while (queue.Count > 0)
            {
                (var d0, int i0) = queue.RemoveMin();
                if (dists[i0] < d0) continue; // skip if lower value has been assigned

                (int x0, int y0) = field.IndicesAt(i0);

                // -x
                if (x0 > 0)
                    TryUpdate(d0 + dx * cost[i0 - 1], i0 - 1);

                // +x
                if (x0 < nx - 1)
                    TryUpdate(d0 + dx * cost[i0 + 1], i0 + 1);

                // -y
                if (y0 > 0)
                    TryUpdate(d0 + dy * cost[i0 - nx], i0 - nx);

                // +y
                if (y0 < ny - 1)
                    TryUpdate(d0 + dy * cost[i0 + nx], i0 + nx);

                // add to queue if less than current min
                void TryUpdate(double distance, int index)
                {
                    if (distance < dists[index])
                    {
                        dists[index] = distance;
                        queue.Insert(distance, index);
                    }
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Calculates L1 (Manhattan) geodesic distance via Dijksta's algorithm as detailed in 
        /// http://www.numerical-tours.com/matlab/fastmarching_0_implementing/
        /// </summary>
        /// <param name="cost"></param>
        /// <param name="sources"></param>
        /// <param name="field"></param>
        public static void GeodesicDistanceL1(GridField2d<double> field, IEnumerable<int> sources, IEnumerable<int> exclude = null)
        {
            // TODO handle additonal wrap modes

            var dists = field.Values;
            int nx = field.CountX;
            int ny = field.CountY;

            (double dx, double dy) = Vec2d.Abs(field.Scale);

            var queue = new Queue<int>();
            dists.Set(double.PositiveInfinity);

            // enqueue sources
            foreach (int i in sources)
            {
                dists[i] = 0.0;
                queue.Enqueue(i);
            }

            // exclude
            if (exclude != null)
            {
                foreach (int i in exclude)
                    dists[i] = 0.0;
            }

            // breadth first search from sources
            while (queue.Count > 0)
            {
                int i0 = queue.Dequeue();
                var d0 = dists[i0];

                (int x0, int y0) = field.IndicesAt(i0);

                // -x
                if (x0 > 0)
                    TryUpdate(d0 + dx, i0 - 1);

                // +x
                if (x0 < nx - 1)
                    TryUpdate(d0 + dx, i0 + 1);

                // -y
                if (y0 > 0)
                    TryUpdate(d0 + dy, i0 - nx);

                // +y
                if (y0 < ny - 1)
                    TryUpdate(d0 + dy, i0 + nx);

                // add to queue if less than current min
                void TryUpdate(double distance, int index)
                {
                    if (distance < dists[index])
                    {
                        dists[index] = distance;
                        queue.Enqueue(index);
                    }
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="cost"></param>
        /// <param name="sources"></param>
        /// <param name="exclude"></param>
        public static void GeodesicDistanceL2(GridField2d<double> field, double[] cost, IEnumerable<int> sources, IEnumerable<int> exclude = null)
        {
            // TODO handle additonal wrap modes

            var dists = field.Values;
            var nx = field.CountX;
            var ny = field.CountY;

            (var dx, var dy) = Vec2d.Abs(field.Scale);
            var eikonal = new Eikonal2d(dx, dy);

            var queue = new PriorityQueue<double, int>();
            dists.Set(double.PositiveInfinity);

            // enqueue sources
            foreach (int i in sources)
            {
                dists[i] = 0.0;
                queue.Insert(0.0, i);
            }

            // exclude
            if (exclude != null)
            {
                foreach (var i in exclude)
                    dists[i] = 0.0;
            }

            // breadth first search from sources
            while (queue.Count > 0)
            {
                (double d0, int i0) = queue.RemoveMin();
                if (dists[i0] < d0) continue; // skip if lower value has been assigned

                (int x0, int y0) = field.IndicesAt(i0);

                if (x0 > 0) X(i0 - 1);
                if (x0 < nx - 1) X(i0 + 1);

                if (y0 > 0) Y(i0 - nx);
                if (y0 < ny - 1) Y(i0 + nx);

                // process x neighbor
                void X(int index)
                {
                    var d1 = dists[index];
                    if (d1 < d0) return; // no backtracking

                    double d2;
                    double minY = GetMinY(index); // will return infinity if neither neighbor has been visited
                    
                    if (minY > double.MaxValue || !eikonal.Evaluate(d0, minY, cost[index], out d2))
                        d2 = d0 + dx * cost[index];
                  
                    // add to queue if less than current min
                    if (d2 < d1)
                    {
                        dists[index] = d2;
                        queue.Insert(d2, index);
                    }
                }

                // process y neighbor
                void Y(int index)
                {
                    var d1 = dists[index];
                    if (d1 < d0) return; // no backtracking

                    double d2;
                    double minX = GetMinX(index); // will return infinity if neither neighbor has been visited

                    if (minX > double.MaxValue || !eikonal.Evaluate(minX, d0, cost[index], out d2))
                        d2 = d0 + dy * cost[index];

                    // add to queue if less than current min
                    if (d2 < d1)
                    {
                        dists[index] = d2;
                        queue.Insert(d2, index);
                    }
                }

                // returns the minimum adjacent value in the x
                double GetMinX(int index)
                {
                    if (x0 == 0) return dists[index + 1];
                    else if (x0 == nx - 1) return dists[index - 1];
                    return Math.Min(dists[index - 1], dists[index + 1]);
                }

                // returns the minimum adjacent value in the y
                double GetMinY(int index)
                {
                    if (y0 == 0) return dists[index + nx];
                    else if (y0 == ny - 1) return dists[index - nx];
                    return Math.Min(dists[index - nx], dists[index + nx]);
                }
            }
        }
Beispiel #11
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="field"></param>
 /// <param name="mapper"></param>
 /// <param name="image"></param>
 public static void ReadFromImage <T>(Bitmap image, GridField2d <T> field, Func <Color, T> mapper)
     where T : struct
 {
     ReadFromImage(image, field.Values, 0, mapper);
 }
Beispiel #12
0
 /// <summary>
 /// Writes the given field to an existing image.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="field"></param>
 /// <param name="mapper"></param>
 /// <param name="image"></param>
 public static void WriteToImage <T>(GridField2d <T> field, Bitmap image, Func <T, Color> mapper)
     where T : struct
 {
     WriteToImage(field.Values, 0, image, mapper);
 }
Beispiel #13
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="point"></param>
 /// <param name="amount"></param>
 public static void IncrementAt(this GridField2d <Vec2d> field, GridPoint2d point, Vec2d amount)
 {
     FieldUtil.IncrementAt(field, point.Corners, point.Weights, amount);
 }
Beispiel #14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="point"></param>
 /// <param name="value"></param>
 public static void SetAt(this GridField2d <Vec2d> field, GridPoint2d point, Vec2d value)
 {
     FieldUtil.SetAt(field, point.Corners, point.Weights, value);
 }
Beispiel #15
0
        /*
         * /// <summary>
         * /// Adds the Laplacian of the field to the deltas array.
         * /// http://en.wikipedia.org/wiki/Discrete_Laplace_operator
         * /// </summary>
         * /// <param name="field"></param>
         * /// <param name="deltas"></param>
         * /// <param name="rate"></param>
         * /// <param name="parallel"></param>
         * public static void Diffuse(HeVertexScalarField field, double[] deltas, double rate, bool parallel = false)
         * {
         *  var vals = field.Values;
         *  var verts = field.Vertices;
         *
         *  Action<Tuple<int, int>> func = range =>
         *  {
         *      for (int i = range.Item1; i < range.Item2; i++)
         *      {
         *          double sum = 0.0;
         *          int n = 0;
         *
         *          foreach (var he in verts[i].IncomingHalfedges)
         *          {
         *              sum += vals[he.Start.Index];
         *              n++;
         *          }
         *
         *          deltas[i] += (sum / n - vals[i]) * rate;
         *      }
         *  };
         *
         *  if (parallel)
         *      Parallel.ForEach(Partitioner.Create(0, field.Count), func);
         *  else
         *      func(Tuple.Create(0, field.Count));
         * }
         *
         *
         * /// <summary>
         * /// Adds the Laplacian of the field to the deltas array.
         * /// The Laplacian is calculated with a user-defined weighting scheme.
         * /// http://www.cs.princeton.edu/courses/archive/fall10/cos526/papers/sorkine05.pdf
         * /// http://www.igl.ethz.ch/projects/Laplacian-mesh-processing/Laplacian-mesh-optimization/lmo.pdf
         * /// </summary>
         * /// <param name="field"></param>
         * /// <param name="deltas"></param>
         * /// <param name="rate"></param>
         * /// <param name="hedgeWeights"></param>
         * /// <param name="parallel"></param>
         * public static void Diffuse(HeVertexScalarField field, double[] deltas, double rate, IReadOnlyList<double> hedgeWeights, bool parallel = false)
         * {
         *  var vals = field.Values;
         *  var verts = field.Vertices;
         *
         *  Action<Tuple<int, int>> func = range =>
         *  {
         *      for (int i = range.Item1; i < range.Item2; i++)
         *      {
         *          double value = vals[i];
         *          double sum = 0.0;
         *
         *          foreach (var he in verts[i].OutgoingHalfedges)
         *              sum += (vals[he.End.Index] - value) * hedgeWeights[he.Index];
         *
         *          deltas[i] += sum * rate;
         *      }
         *  };
         *
         *  if (parallel)
         *      Parallel.ForEach(Partitioner.Create(0, field.Count), func);
         *  else
         *      func(Tuple.Create(0, field.Count));
         * }
         */


        /// <summary>
        /// http://micsymposium.org/mics_2011_proceedings/mics2011_submission_30.pdf
        /// </summary>
        /// <param name="field"></param>
        /// <param name="deltas"></param>
        /// <param name="slope"></param>
        /// <param name="rate"></param>
        /// <param name="parallel"></param>
        public static void ErodeThermal(GridField2d <double> field, double[] deltas, double slope, double rate, bool parallel = false)
        {
            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;

                (double dx, double dy) = field.Scale;
                dx = 1.0 / Math.Abs(dx);
                dy = 1.0 / Math.Abs(dy);

                (int di, int dj) = field.GetBoundaryOffsets();
                (int i, int j)   = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }

                    double value = vals[index];
                    double sum = 0.0;
                    double m, md;

                    //-x
                    m  = ((i == 0) ? vals[index + di] : vals[index - 1]) - value;
                    m *= dx;
                    md = Math.Abs(m) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    //+x
                    m  = ((i == nx - 1) ? vals[index - di] : vals[index + 1]) - value;
                    m *= dx;
                    md = Math.Abs(m) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    //-y
                    m  = ((j == 0) ? vals[index + dj] : vals[index - nx]) - value;
                    m *= dy;
                    md = Math.Abs(m) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    //+y
                    m  = ((j == ny - 1) ? vals[index - dj] : vals[index + nx]) - value;
                    m *= dy;
                    md = Math.Abs(m) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    deltas[index] += sum * rate;
                }
            }
        }