public static int HashCode1of4(this V2d point, double epsilon)
        {
            var xi = (long)Math.Floor(point.X / epsilon);
            var yi = (long)Math.Floor(point.Y / epsilon);

            return(HashCode.Combine((int)(xi >> 1), (int)(yi >> 1)));
        }
        public static int HashCode1(this V2d point, V2d epsilon)
        {
            var xi = (long)Math.Floor(point.X / epsilon.X);
            var yi = (long)Math.Floor(point.Y / epsilon.Y);

            return(HashCode.Combine((int)xi, (int)yi));
        }
Beispiel #3
0
 public void Write(V2d value)
 {
     for (int i = 0; i < 2; i++)
     {
         base.Write(Conversion.HostToNetworkOrder(value[i]));
     }
 }
        static Volume <byte> Scaled(Volume <byte> source, V2d scaleFactor, ImageInterpolation ip = ImageInterpolation.Cubic)
        {
            var targetSize = (new V2d(0.5, 0.5) + scaleFactor * (V2d)source.Size.XY).ToV2l();
            var target     = new V3l(targetSize.X, targetSize.Y, source.Size.Z).CreateImageVolume <byte>();

            for (int c = 0; c < source.Size.Z; c++)
            {
                var targetMat = target.SubXYMatrixWindow(0);
                var sourceMat = source.SubXYMatrixWindow(0);
                switch (ip)
                {
                case ImageInterpolation.Near: targetMat.SetScaledNearest(sourceMat); break;

                case ImageInterpolation.Linear:
                    targetMat.SetScaledLinear(sourceMat, (s, a, b) => (byte)s.Lerp(a, b), (s, a, b) => (byte)s.Lerp(a, b));
                    break;

                case ImageInterpolation.Cubic: targetMat.SetScaledCubic(sourceMat); break;

                case ImageInterpolation.Lanczos: targetMat.SetScaledLanczos(sourceMat); break;

                default: throw new NotImplementedException();
                }
            }
            return(target);
        }
Beispiel #5
0
 public RayHit3d(double tMax)
 {
     T        = tMax;
     Point    = V3d.NaN;
     Coord    = V2d.NaN;
     BackSide = false;
     Part     = 0;
 }
        public static void ComputeCircumCircleSquared(
            V2d p0, V2d p1, V2d p2,
            out V2d center, out double radiusSquared)
        {
            double y01abs = System.Math.Abs(p0.Y - p1.Y);
            double y12abs = System.Math.Abs(p1.Y - p2.Y);

            if (y01abs < Constant <double> .PositiveTinyValue &&
                y12abs < Constant <double> .PositiveTinyValue)
            {
                center = V2d.NaN; radiusSquared = -1.0; return;
            }

            double xc, yc;

            if (y01abs < Constant <double> .PositiveTinyValue)
            {
                double m2   = (p1.X - p2.X) / (p2.Y - p1.Y);
                double m12x = 0.5 * (p1.X + p2.X);
                double m12y = 0.5 * (p1.Y + p2.Y);
                xc = 0.5 * (p1.X + p0.X);
                yc = m2 * (xc - m12x) + m12y;
            }
            else if (y12abs < Constant <double> .PositiveTinyValue)
            {
                double m1   = (p0.X - p1.X) / (p1.Y - p0.Y);
                double m01x = 0.5 * (p0.X + p1.X);
                double m01y = 0.5 * (p0.Y + p1.Y);
                xc = 0.5 * (p2.X + p1.X);
                yc = m1 * (xc - m01x) + m01y;
            }
            else
            {
                double m1   = (p0.X - p1.X) / (p1.Y - p0.Y);
                double m2   = (p1.X - p2.X) / (p2.Y - p1.Y);
                double m01x = 0.5 * (p0.X + p1.X);
                double m01y = 0.5 * (p0.Y + p1.Y);
                double m12x = 0.5 * (p1.X + p2.X);
                double m12y = 0.5 * (p1.Y + p2.Y);
                double m12  = m1 - m2;
                if (System.Math.Abs(m12) < Constant <double> .PositiveTinyValue)
                {
                    center = V2d.NaN; radiusSquared = -1.0; return;
                }
                xc = (m1 * m01x - m2 * m12x + m12y - m01y) / m12;
                if (y01abs > y12abs)
                {
                    yc = m1 * (xc - m01x) + m01y;
                }
                else
                {
                    yc = m2 * (xc - m12x) + m12y;
                }
            }
            center        = new V2d(xc, yc);
            radiusSquared = V2d.DistanceSquared(p0, center);
        }
        static Ray3d GetCameraRay(V2d uv, M44d invViewProj)
        {
            var deviceCoord = new V2d(uv.X * 2 - 1, -uv.Y * 2 + 1);

            var nearPoint = invViewProj.TransformPosProj(deviceCoord.XYO);
            var farPoint  = invViewProj.TransformPosProj(deviceCoord.XYI);

            return(new Ray3d(nearPoint, (farPoint - nearPoint).Normalized));
        }
        /// <summary>
        /// Build a texture coordinate transformation from the given parameters as specified in TextureTransform
        /// http://gun.teipir.gr/VRML-amgem/spec/part1/nodesRef.html#TextureTransform
        /// </summary>
        public static Trafo2d BuildVrmlTextureTrafo(V2d center, double rotation, V2d scale, V2d translation)
        {
            M33d C = M33d.Translation(center), Ci = M33d.Translation(-center);
            M33d R = M33d.Rotation(rotation), Ri = M33d.Rotation(-rotation);
            M33d S = M33d.Scale(scale), Si = M33d.Scale(1 / scale);
            M33d T = M33d.Translation(translation), Ti = M33d.Translation(-translation);

            return(new Trafo2d(
                       Ci * S * R * C * T,
                       Ti * Ci * Ri * Si * C));
        }
        public V2d MatrixColumnIndexerUnsafe()
        {
            V2d accum = V2d.Zero;

            for (int i = 0; i < count; i++)
            {
                accum += matrices[i].ColUnsafe(indices[i].X);
            }

            return(accum);
        }
        public V2d MatrixRowIndexerIf()
        {
            V2d accum = V2d.Zero;

            for (int i = 0; i < count; i++)
            {
                accum += matrices[i].RowIf(indices[i].X);
            }

            return(accum);
        }
Beispiel #11
0
        public bool IsDistanceToPointSmallerThan(V2d p, double maxDist)
        {
            //speed-up by first checking the bounding box
            var box = BoundingBox2d;

            box.EnlargeBy(maxDist);
            if (!box.Contains(p))
            {
                return(false);
            }
            return(GetDistanceToLine(p) <= maxDist);
        }
        public static void HashCodes4(this V2d point, double epsilon, int[] hca)
        {
            var xi = (long)Math.Floor(point.X / epsilon);
            var yi = (long)Math.Floor(point.Y / epsilon);

            int xh0 = (int)(xi >> 1), xh1 = xh0 - 1 + ((int)(xi & 1) << 1);
            int yh0 = (int)(yi >> 1), yh1 = yh0 - 1 + ((int)(yi & 1) << 1);

            hca[0] = HashCode.Combine(xh0, yh0);
            hca[1] = HashCode.Combine(xh1, yh0);
            hca[2] = HashCode.Combine(xh0, yh1);
            hca[3] = HashCode.Combine(xh1, yh1);
        }
Beispiel #13
0
        public Line(double rho, double theta)
        {
            double a = Math.Cos(theta);
            double b = Math.Sin(theta);

            double x0 = a * rho;
            double y0 = b * rho;

            StartPoint = new V2d(x0 + 1000 * -b, y0 + 1000 * a);
            EndPoint   = new V2d(x0 - 1000 * -b, y0 - 1000 * a);

            InternalLine = new Line2d(StartPoint, EndPoint);
        }
Beispiel #14
0
        public static void PolygonDemo(string dir)
        {
            // This code produces tiny tiff images that demonstrate the pixel-
            // level precision of SetMonotonePolygonFilledRaw.

            var tris = new[] {
                new { Tri = new V2d[] { new V2d(7.7, 2.5), new V2d(11.7, 2.5), new V2d(9.8, 0.8) }, Col = C3b.DarkGreen },
                new { Tri = new V2d[] { new V2d(11.7, 2.5), new V2d(7.7, 2.5), new V2d(9.5, 5.2) }, Col = C3b.Green },
                new { Tri = new V2d[] { new V2d(7, 4), new V2d(5, 6), new V2d(8, 7) }, Col = C3b.Green },
                new { Tri = new V2d[] { new V2d(1, 1), new V2d(2, 4), new V2d(6, 2) }, Col = C3b.Green },
                new { Tri = new V2d[] { new V2d(7, 4), new V2d(1, 6), new V2d(5, 6) }, Col = C3b.DarkGreen },
                new { Tri = new V2d[] { new V2d(7, 4), new V2d(8, 7), new V2d(9.5, 5.5) }, Col = C3b.DarkGreen },
                new { Tri = new V2d[] { new V2d(13.5, 5.5), new V2d(13.5, 7.5), new V2d(15.5, 5.5) }, Col = C3b.Green },
                new { Tri = new V2d[] { new V2d(15.5, 5.5), new V2d(13.5, 7.5), new V2d(15.5, 7.5) }, Col = C3b.DarkGreen },
                new { Tri = new V2d[] { new V2d(15, 0), new V2d(13.5, 1.5), new V2d(14.5, 2.5) }, Col = C3b.Green },
                new { Tri = new V2d[] { new V2d(14.5, 2.5), new V2d(13.5, 1.5), new V2d(14.5, 2.5) }, Col = C3b.Red },
                new { Tri = new V2d[] { new V2d(7.5, 0.5), new V2d(6.5, 1.5), new V2d(7.5, 1.5) }, Col = C3b.Red },
                new { Tri = new V2d[] { new V2d(6.3, 0.3), new V2d(5.3, 1.3), new V2d(6.3, 1.3) }, Col = C3b.Red },
                new { Tri = new V2d[] { new V2d(11.5, 4.5), new V2d(11.5, 6.5), new V2d(12.5, 5.5) }, Col = C3b.DarkGreen },
            };

            var triImg = new PixImage <byte>(16, 8, 3);
            var tMat   = triImg.GetMatrix <C3b>();

            Report.BeginTimed("polygon demo triangles");
            foreach (var t in tris)
            {
                tMat.SetMonotonePolygonFilledRaw(t.Tri, t.Col);
            }
            triImg.SaveAsImage(Path.Combine(dir, "polygon-triangles.tiff"));
            Report.End();

            var polyImg = new PixImage <byte>(10, 10, 3);

            var pMat = polyImg.GetMatrix <C3b>();

            V2d[] poly = new V2d[] {
                new V2d(4, 1),
                new V2d(1, 3),
                new V2d(2, 6),
                new V2d(2.1, 6.1),
                new V2d(5, 7),
                new V2d(8, 5),
                new V2d(9, 3),
            };
            Report.BeginTimed("polygon demo polygon");
            pMat.SetMonotonePolygonFilledRaw(poly, C3b.Red);
            polyImg.SaveAsImage(Path.Combine(dir, "polygon-polygon.tiff"));
            Report.End();
        }
        private static void GetRandomComplex(RandomSystem rnd, out Num.Complex c1, out ComplexD c2, bool withInf = true)
        {
            var type = rnd.UniformDouble();

            if (type < 0.1)
            {
                var v = rnd.UniformV2i(2);
                c1 = new Num.Complex(v.X, v.Y);
                c2 = new ComplexD(v.X, v.Y);
            }
            else if (type < 0.2 && withInf)
            {
                var i = rnd.UniformV2i(3);
                var v = new V2d(
                    (i.X == 0) ? 0 : ((i.X == 1) ? double.NegativeInfinity : double.PositiveInfinity),
                    (i.Y == 0) ? 0 : ((i.Y == 1) ? double.NegativeInfinity : double.PositiveInfinity)
                    );

                c1 = new Num.Complex(v.X, v.Y);
                c2 = new ComplexD(v.X, v.Y);
            }
            else
            {
                var v = (rnd.UniformV2d() - 0.5) * 100;
                if (type < 0.4)
                {
                    c1 = new Num.Complex(v.X, 0);
                    c2 = new ComplexD(v.X, 0);
                }
                else if (type < 0.5)
                {
                    c1 = new Num.Complex(0, v.Y);
                    c2 = new ComplexD(0, v.Y);
                }
                else
                {
                    c1 = new Num.Complex(v.X, v.Y);
                    c2 = new ComplexD(v.X, v.Y);
                }
            }
        }
Beispiel #16
0
        public V2d GetClosestPointOnLine(V2d p)
        {
            var d = P0 - P1;
            var l = d.LengthSquared;

            if (Fun.IsTiny(l))
            {
                return(P0);                     //it does not matter which of the two points we choose
            }
            var t = (P0.Dot(d) - p.Dot(d)) / l; //parametric distance from P0 to P1, where closest point to p is

            if (t <= 0)
            {
                return(P0);
            }
            if (t >= 1)
            {
                return(P1);
            }
            return(P0 - t * d);
        }
Beispiel #17
0
 public FastRay2d(Ray2d ray)
 {
     Ray      = ray;
     DirFlags = ray.Direction.DirFlags();
     InvDir   = 1.0 / ray.Direction;
 }
Beispiel #18
0
 public double RightValueOfDir(V2d v)
 {
     return(v.X * (P1.Y - P0.Y) + v.Y * (P0.X - P1.X));
 }
Beispiel #19
0
 public double RightValueOfPos(V2d p)
 {
     return((p.X - P0.X) * (P1.Y - P0.Y) + (p.Y - P0.Y) * (P0.X - P1.X));
 }
Beispiel #20
0
        public double GetDistanceToLine(V2d p)
        {
            var f = GetClosestPointOnLine(p);

            return((f - p).Length);
        }
Beispiel #21
0
 public double RightValueOfPos(V2d p)
 => (p.X - P0.X) * (P1.Y - P0.Y) + (p.Y - P0.Y) * (P0.X - P1.X);
Beispiel #22
0
 public double RightValueOfDir(V2d v) => v.X * (P1.Y - P0.Y) + v.Y * (P0.X - P1.X);
Beispiel #23
0
 /// <summary>
 /// Returns true if points a, b and c are collinear within eps.
 /// </summary>
 public static bool IsCollinear(V2d a, V2d b, V2d c, double eps = 1e-9)
 => Fun.ApproximateEquals((b.Y - a.Y) * (c.X - b.X), (c.Y - b.Y) * (b.X - a.X), eps);
Beispiel #24
0
 /// <summary>
 /// Returns true if points a, b and c are exactly collinear.
 /// </summary>
 public static bool IsCollinear(V2d a, V2d b, V2d c)
 => (b.Y - a.Y) * (c.X - b.X) == (c.Y - b.Y) * (b.X - a.X);
 /// <summary>
 /// Generates a uniform distributed random sample on the given triangle using
 /// two random series (seriesIndex,  seriesIndex + 1).
 /// </summary>
 public static V2d Triangle(V2d p0, V2d p1, V2d p2, IRandomSeries rnd, int seriesIndex)
 {
     return(Triangle(p0, p1, p2,
                     rnd.UniformDouble(seriesIndex),
                     rnd.UniformDouble(seriesIndex + 1)));
 }
        /// <summary>
        /// Generates a uniform distributed random sample on the given triangle using
        /// two random variables x1 and x2.
        /// </summary>
        public static V2d Triangle(V2d p0, V2d p1, V2d p2, double x1, double x2)
        {
            var x1sq = x1.Sqrt();

            return((1 - x1sq) * p0 + (x1sq * (1 - x2)) * p1 + (x1sq * x2) * p2);
        }
 public Ray3d Unproject(V2d xyOnNearPlane)
 {
     return(new Ray3d(V3d.Zero, new V3d(xyOnNearPlane, -m_box.Min.Z)));
 }
Beispiel #28
0
 public FastRay2d(V2d origin, V2d direction)
     : this(new Ray2d(origin, direction))
 {
 }
Beispiel #29
0
 protected SDPoint AsPoint(V2d point)
 {
     return(new SDPoint((int)point.X, (int)point.Y));
 }
 public void Write(V2d x)
 {
     Write(x.X); Write(x.Y);
 }