Пример #1
0
        public unsafe DisplacementVector GetBilinearSample(float x, float y)
        {
            int x0, y0;

            if (x >= width - 1)
            {
                x  = width - 1;
                x0 = width - 2;
            }
            else
            {
                if (x < 0)
                {
                    x = 0;
                }
                x0 = (int)x;
            }

            if (y >= height - 1)
            {
                y  = height - 1;
                y0 = height - 2;
            }
            else
            {
                if (y < 0)
                {
                    y = 0;
                }
                y0 = (int)y;
            }

            float factorX = x - x0;

            DisplacementVector *
                tl = GetPointAddressUnchecked(x0, y0),
                bl = tl + width;

            DisplacementVector
                t = DisplacementVector.Lerp(*tl, *(tl + 1), factorX),
                b = DisplacementVector.Lerp(*bl, *(bl + 1), factorX);

            return(DisplacementVector.Lerp(t, b, y - y0));
        }
Пример #2
0
        public unsafe DisplacementMesh Resize(Size size)
        {
            DisplacementMesh ret = new DisplacementMesh(size);

            float xfactor = (float)width / size.Width;
            float yfactor = (float)height / size.Height;

            for (int y = 0; y < size.Height; ++y)
            {
                DisplacementVector *ptr = ret.GetPointAddressUnchecked(0, y);
                float srcy = y * yfactor;;
                for (int x = 0; x < size.Width; ++x)
                {
                    float srcx           = x * xfactor;
                    DisplacementVector v = GetBilinearSample(srcx, srcy);
                    ptr->X = v.X / xfactor;
                    ptr->Y = v.Y / yfactor;
                    ++ptr;
                }
            }
            return(ret);
        }
Пример #3
0
 public static DisplacementVector Lerp(DisplacementVector lhs, DisplacementVector rhs, float factor)
 {
     return(new DisplacementVector(
                lhs.X * (1 - factor) + rhs.X * factor,
                lhs.Y * (1 - factor) + rhs.Y * factor));
 }
Пример #4
0
        public Rectangle SetPoint(int pi, int pj, float nx, float ny)
        {
            int istart = pi - 1,
                iend   = pi + 2,
                jstart = pj - 1,
                jend   = pj + 2;

            if (istart < 0)
            {
                istart = 0;
            }
            else if (iend > handlesx)
            {
                iend = handlesx;
            }
            if (jstart < 0)
            {
                jstart = 0;
            }
            else if (jend > handlesy)
            {
                jend = handlesy;
            }

            int l = mesh.Width,
                t = mesh.Height,
                r = 0,
                b = 0;

            for (int j = jstart; j < jend; ++j)
            {
                for (int i = istart; i < iend; ++i)
                {
                    float bx = i + points[i, j].X;
                    float by = j + points[i, j].Y;

                    int x = (int)(bx * xspacing);
                    int y = (int)(by * yspacing);

                    if (x < l)
                    {
                        l = x;
                    }
                    if (x > r)
                    {
                        r = x;
                    }

                    if (y < t)
                    {
                        t = y;
                    }
                    if (y > b)
                    {
                        b = y;
                    }

                    if (i > pi && nx + pi > bx)
                    {
                        nx = bx - pi - delx;
                    }
                    else if (i < pi && nx + pi < bx)
                    {
                        nx = bx - pi + delx;
                    }

                    if (j > pj && ny + pj > by)
                    {
                        ny = by - pj - dely;
                    }
                    else if (j < pj && ny + pj < by)
                    {
                        ny = by - pj + dely;
                    }
                }
            }

            if (l < 0)
            {
                l = 0;
            }
            if (r > mesh.Width)
            {
                r = mesh.Width;
            }
            if (t < 0)
            {
                t = 0;
            }
            if (b > mesh.Height)
            {
                b = mesh.Height;
            }

            if (nx + pi < 0)
            {
                nx = -pi;
            }
            else if (nx + pi > width)
            {
                nx = width - pi;
            }
            if (ny + pj < 0)
            {
                ny = -pj;
            }
            else if (ny + pj > height)
            {
                ny = height - pj;
            }

            points[pi, pj] = new DisplacementVector(nx, ny);

            QueuePointUpdate(pi, pj);

            return(Rectangle.FromLTRB(l, t, r, b));
        }