Beispiel #1
0
            /// <summary>
            /// Returns the noise value at the given coordinates.
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <returns></returns>
            public static double ValueAt(double x, double y)
            {
                // separate whole and fractional components
                x = SlurMath.Fract(x, out int i);
                y = SlurMath.Fract(y, out int j);

                // wrap to perm table
                i &= 255;
                j &= 255;

                // calculate noise contributions from each corner
                double n00 = GradDot(ToIndex(i, j), x, y);
                double n10 = GradDot(ToIndex(i + 1, j), x - 1.0, y);
                double n01 = GradDot(ToIndex(i, j + 1), x, y - 1.0);
                double n11 = GradDot(ToIndex(i + 1, j + 1), x - 1.0, y - 1.0);

                // eased values for x and y
                x = SlurMath.HermiteC2(x);
                y = SlurMath.HermiteC2(y);

                // bilinear interpolation
                return(SlurMath.Lerp(
                           SlurMath.Lerp(n00, n10, x),
                           SlurMath.Lerp(n01, n11, x),
                           y));
            }
Beispiel #2
0
        /// <summary>
        /// Returns the noise value at the given coordinates
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        /// <returns></returns>
        public static double ValueAt(double x, double y, double z)
        {
            // separate whole and fractional components
            x = SlurMath.Fract(x, out int i);
            y = SlurMath.Fract(y, out int j);
            z = SlurMath.Fract(z, out int k);

            // calculate noise contributions from each corner
            double n000 = GradDot(ToIndex(i, j, k), x, y, z);
            double n100 = GradDot(ToIndex(i + 1, j, k), x - 1.0, y, z);
            double n010 = GradDot(ToIndex(i, j + 1, k), x, y - 1.0, z);
            double n110 = GradDot(ToIndex(i + 1, j + 1, k), x - 1.0, y - 1.0, z);

            double n001 = GradDot(ToIndex(i, j, k + 1), x, y, z - 1.0);
            double n101 = GradDot(ToIndex(i + 1, j, k + 1), x - 1.0, y, z - 1.0);
            double n011 = GradDot(ToIndex(i, j + 1, k + 1), x, y - 1.0, z - 1.0);
            double n111 = GradDot(ToIndex(i + 1, j + 1, k + 1), x - 1.0, y - 1.0, z - 1.0);

            // eased values for each dimension
            x = SlurMath.HermiteC2(x);
            y = SlurMath.HermiteC2(y);
            z = SlurMath.HermiteC2(z);

            // trilinear interpolation
            return(SlurMath.Lerp(
                       SlurMath.Lerp(
                           SlurMath.Lerp(n000, n100, x),
                           SlurMath.Lerp(n010, n110, x),
                           y),
                       SlurMath.Lerp(
                           SlurMath.Lerp(n001, n101, x),
                           SlurMath.Lerp(n011, n111, x),
                           y),
                       z));
        }
        /// <inheritdoc />
        protected sealed override double ValueAtLinear(Vec3d point)
        {
            point = ToGridSpace(point);
            double u = SlurMath.Fract(point.X, out int i0);
            double v = SlurMath.Fract(point.Y, out int j0);
            double w = SlurMath.Fract(point.Z, out int k0);

            int i1 = WrapX(i0 + 1);
            int j1 = WrapY(j0 + 1) * CountX;
            int k1 = WrapZ(k0 + 1) * CountXY;

            i0 = WrapX(i0);
            j0 = WrapY(j0) * CountX;
            k0 = WrapZ(k0) * CountXY;

            var vals = Values;

            return(SlurMath.Lerp(
                       SlurMath.Lerp(
                           SlurMath.Lerp(vals[i0 + j0 + k0], vals[i1 + j0 + k0], u),
                           SlurMath.Lerp(vals[i0 + j1 + k0], vals[i1 + j1 + k0], u),
                           v),
                       SlurMath.Lerp(
                           SlurMath.Lerp(vals[i0 + j0 + k1], vals[i1 + j0 + k1], u),
                           SlurMath.Lerp(vals[i0 + j1 + k1], vals[i1 + j1 + k1], u),
                           v),
                       w));
        }
        /// <inheritdoc />
        protected sealed override double ValueAtLinearUnsafe(Vec3d point)
        {
            point = ToGridSpace(point);
            double u = SlurMath.Fract(point.X, out int i0);
            double v = SlurMath.Fract(point.Y, out int j0);
            double w = SlurMath.Fract(point.Z, out int k0);

            j0 *= CountX;
            k0 *= CountXY;
            int i1 = i0 + 1;
            int j1 = j0 + CountX;
            int k1 = k0 + CountXY;

            var vals = Values;

            return(SlurMath.Lerp(
                       SlurMath.Lerp(
                           SlurMath.Lerp(vals[i0 + j0 + k0], vals[i1 + j0 + k0], u),
                           SlurMath.Lerp(vals[i0 + j1 + k0], vals[i1 + j1 + k0], u),
                           v),
                       SlurMath.Lerp(
                           SlurMath.Lerp(vals[i0 + j0 + k1], vals[i1 + j0 + k1], u),
                           SlurMath.Lerp(vals[i0 + j1 + k1], vals[i1 + j1 + k1], u),
                           v),
                       w));
        }
Beispiel #5
0
        /// <inheritdoc />
        protected sealed override double ValueAtLinearUnsafe(Vector3d point)
        {
            (var u, var v, var w) = Vector3d.Fract(ToGridSpace(point), out Vector3i whole);

            var x0 = whole.X;
            var y0 = whole.Y * CountX;
            var z0 = whole.Z * CountXY;

            int x1 = x0 + 1;
            int y1 = y0 + CountX;
            int z1 = z0 + CountXY;

            var vals = Values;

            return(SlurMath.Lerp(
                       SlurMath.Lerp(
                           SlurMath.Lerp(vals[x0 + y0 + z0], vals[x1 + y0 + z0], u),
                           SlurMath.Lerp(vals[x0 + y1 + z0], vals[x1 + y1 + z0], u),
                           v),
                       SlurMath.Lerp(
                           SlurMath.Lerp(vals[x0 + y0 + z1], vals[x1 + y0 + z1], u),
                           SlurMath.Lerp(vals[x0 + y1 + z1], vals[x1 + y1 + z1], u),
                           v),
                       w));
        }
Beispiel #6
0
        /// <inheritdoc />
        protected sealed override double ValueAtLinear(Vector3d point)
        {
            (var u, var v, var w) = Vector3d.Fract(ToGridSpace(point), out Vector3i whole);

            var x0 = WrapX(whole.X);
            var y0 = WrapY(whole.Y) * CountX;
            var z0 = WrapZ(whole.Z) * CountXY;

            int x1 = WrapX(whole.X + 1);
            int y1 = WrapY(whole.Y + 1) * CountX;
            int z1 = WrapZ(whole.Z + 1) * CountXY;

            var vals = Values;

            return(SlurMath.Lerp(
                       SlurMath.Lerp(
                           SlurMath.Lerp(vals[x0 + y0 + z0], vals[x1 + y0 + z0], u),
                           SlurMath.Lerp(vals[x0 + y1 + z0], vals[x1 + y1 + z0], u),
                           v),
                       SlurMath.Lerp(
                           SlurMath.Lerp(vals[x0 + y0 + z1], vals[x1 + y0 + z1], u),
                           SlurMath.Lerp(vals[x0 + y1 + z1], vals[x1 + y1 + z1], u),
                           v),
                       w));
        }
        /// <inheritdoc />
        protected sealed override double ValueAtLinearUnsafe(Vector2d point)
        {
            (var u, var v) = Vector2d.Fract(ToGridSpace(point), out Vector2i whole);

            var x0 = whole.X;
            var y0 = whole.Y * CountX;

            var x1 = x0 + 1;
            var y1 = y0 + CountX;

            var vals = Values;

            return(SlurMath.Lerp(
                       SlurMath.Lerp(vals[x0 + y0], vals[x1 + y0], u),
                       SlurMath.Lerp(vals[x0 + y1], vals[x1 + y1], u),
                       v));
        }
        /// <inheritdoc />
        protected sealed override double ValueAtLinear(Vector2d point)
        {
            (var u, var v) = Vector2d.Fract(ToGridSpace(point), out Vector2i whole);

            var x0 = WrapX(whole.X);
            var y0 = WrapY(whole.Y) * CountX;

            int x1 = WrapX(whole.X + 1);
            int y1 = WrapY(whole.Y + 1) * CountX;

            var vals = Values;

            return(SlurMath.Lerp(
                       SlurMath.Lerp(vals[x0 + y0], vals[x1 + y0], u),
                       SlurMath.Lerp(vals[x0 + y1], vals[x1 + y1], u),
                       v));
        }
Beispiel #9
0
        /// <inheritdoc/>
        /// <summary>
        ///
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        protected sealed override double ValueAtLinearUnchecked(Vec2d point)
        {
            point = ToGridSpace(point);
            double u = SlurMath.Fract(point.X, out int i0);
            double v = SlurMath.Fract(point.Y, out int j0);

            j0 *= CountX;
            int i1 = i0 + 1;
            int j1 = j0 + CountX;

            var vals = Values;

            return(SlurMath.Lerp(
                       SlurMath.Lerp(vals[i0 + j0], vals[i1 + j0], u),
                       SlurMath.Lerp(vals[i0 + j1], vals[i1 + j1], u),
                       v));
        }
Beispiel #10
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List <Mesh>       mesh = new List <Mesh>();
            IField3d <double> f0   = null;
            IField3d <double> f1   = null;
            List <double>     t    = new List <double>();

            if (!DA.GetDataList(0, mesh))
            {
                return;
            }
            if (!DA.GetData(1, ref f0))
            {
                return;
            }
            if (!DA.GetData(2, ref f1))
            {
                return;
            }
            if (!DA.GetDataList(3, t))
            {
                return;
            }

            List <MeshField3d> fields = new List <MeshField3d>();

            for (int i = 0; i < t.Count; i++)
            {
                List <double> currentBlend = new List <double>();
                var           currentField = MeshField3d.Double.Create(mesh[i].ToHeMesh());

                foreach (Point3d p in mesh[i].Vertices)
                {
                    double val = SlurMath.Lerp(f0.ValueAt(p), f1.ValueAt(p), t[i]);
                    currentBlend.Add(val);
                    currentField.Set(currentBlend);
                }

                fields.Add(currentField);
            }
            ;

            DA.SetDataList(0, fields);
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List <Mesh>       mesh = new List <Mesh>();
            IField3d <double> f0   = null;
            IField3d <double> f1   = null;
            List <double>     t    = new List <double>();

            if (!DA.GetDataList(0, mesh))
            {
                return;
            }
            if (!DA.GetData(1, ref f0))
            {
                return;
            }
            if (!DA.GetData(2, ref f1))
            {
                return;
            }
            if (!DA.GetDataList(3, t))
            {
                return;
            }

            List <MeshField3d> fields = new List <MeshField3d>();

            for (int i = 0; i < t.Count; i++)
            {
                var      currentField = MeshField3d.Double.Create(mesh[i].ToHeMesh());
                double[] currentBlend = new double[currentField.Mesh.Vertices.Count];

                Parallel.For(0, currentField.Mesh.Vertices.Count, j =>
                {
                    currentBlend[j] = SlurMath.Lerp(f0.ValueAt(currentField.Mesh.Vertices[j].Position),
                                                    f1.ValueAt(currentField.Mesh.Vertices[j].Position), t[i]);
                });

                currentField.Set(currentBlend);
                fields.Add(currentField);
            }

            DA.SetDataList(0, fields);
        }
 /// <summary>
 /// Returns a linearly interpolated value at the given parameter along the halfedge.
 /// </summary>
 public static double Lerp <V, E>(this IHalfedge <V, E> hedge, Func <V, double> getValue, double t)
     where V : IHeVertex <V, E>
     where E : IHalfedge <V, E>
 {
     return(SlurMath.Lerp(getValue(hedge.Start), getValue(hedge.End), t));
 }
Beispiel #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="weight"></param>
        /// <returns></returns>
        public float ToScale(float weight)
        {
            var t = SlurMath.Saturate(SlurMath.Normalize(weight, _weight0, _weight1));

            return(SlurMath.Lerp(_scale0, _scale1, t));
        }