Example #1
0
        // Return a random point on the triangle given a random generator.
        public Vector RandomPoint(PseudoDES rand)
        {
            double r1 = rand.RandomDouble();
            double r2 = rand.RandomDouble();

            if (r1 + r2 > 1.0)
            {
                r1 = 1.0 - r1;
                r2 = 1.0 - r2;
            }
            return(A + r1 * AtoB + r2 * AtoC);
        }
Example #2
0
        public Vector RandomPoint()
        {
            double az  = pd.RandomDouble();
            double phi = 2.0 * Math.PI * pd.RandomDouble();

            double ax = WireRadius * Math.Cos(phi);
            double ay = WireRadius * Math.Sin(phi);

            return(new Vector(Start.x + ax * perpX.x + ay * perpY.x + az * Axis.x,
                              Start.y + ax * perpX.y + ay * perpY.y + az * Axis.y,
                              Start.z + ax * perpX.z + ay * perpY.z + az * Axis.z));
        }
Example #3
0
        public Vector RandomPoint()
        {
            double cosTheta = cos1 + (cos2 - cos1) * pd.RandomDouble();
            double sinTheta = Math.Sqrt(1.0 - cosTheta * cosTheta);
            double phi      = 2.0 * Math.PI * pd.RandomDouble();

            double ax = Radius * Math.Cos(phi) * sinTheta;
            double ay = Radius * Math.Sin(phi) * sinTheta;
            double az = Radius * cosTheta;

            return(new Vector(Center.x + ax * perpX.x + ay * perpY.x + az * Axis.x,
                              Center.y + ax * perpX.y + ay * perpY.y + az * Axis.y,
                              Center.z + ax * perpX.z + ay * perpY.z + az * Axis.z));
        }
Example #4
0
        public Vector RandomPoint()
        {
            // Here's the math. Theta and phi are random angles.
            // unitRadial = cos(theta)*perpX + sin(theta)*perpY
            // result = center + Radius*unitRadial + WireRadius*(cos(phi)*unitRadial+sin(phi)*Axis)
            double theta = 2.0 * Math.PI * pd.RandomDouble();
            double phi   = 2.0 * Math.PI * pd.RandomDouble();

            double r1 = Radius + WireRadius * Math.Cos(phi);
            double ax = r1 * Math.Cos(theta);
            double ay = r1 * Math.Sin(theta);
            double az = WireRadius * Math.Sin(phi);

            return(new Vector(Center.x + ax * perpX.x + ay * perpY.x + az * Axis.x,
                              Center.y + ax * perpX.y + ay * perpY.y + az * Axis.y,
                              Center.z + ax * perpX.z + ay * perpY.z + az * Axis.z));
        }
Example #5
0
        static Particle PlaceParticleRandomly(IShape[] elements)
        {
            int iElement = 0;

            if (elements.Length > 1)
            {
                iElement = (int)Math.Floor(pdes.RandomDouble() * elements.Length);
            }
            return(new Particle(elements[iElement].RandomPoint(), iElement));
        }
Example #6
0
        public Vector RandomPoint()
        {
            double theta = 2.0 * Math.PI * pd.RandomDouble();
            double c     = Radius * Math.Cos(theta);
            double s     = Radius * Math.Sin(theta);

            return(new Vector(Center.x + c * perpX.x + s * perpY.x,
                              Center.y + c * perpX.y + s * perpY.y,
                              Center.z + c * perpX.z + s * perpY.z));
        }
Example #7
0
        public Vector RandomPoint()
        {
            double area = TotalArea * rand.RandomDouble();
            int    idx  = Array.BinarySearch(cummulativeAreas, area);

            if (idx < 0)
            {
                idx = ~idx;
            }
            return(triangles[idx].RandomPoint(rand));
        }
Example #8
0
        static void Main(string[] args)
        {
            bool allPassed = true;

            if (!ReadArgs(args))
            {
                return;
            }

            if (!haveSeed)
            {
                seed = (uint)DateTime.UtcNow.Ticks;
            }
            PseudoDES pdes = new PseudoDES(0, seed);

            Console.WriteLine("Random seed = {0}", seed);
            if (nVectors < 11)
            {
                nVectors = 11;
            }
            Console.WriteLine("Vectors = {0}", nVectors);

            // Run the PseudoDES test
            if (pdes.Test())
            {
                Console.WriteLine("PseudoDES test passed");
            }
            else
            {
                Console.WriteLine("PseudoDES test FAILED");
                allPassed = false;
            }

            Vector[] vecs = new Vector[nVectors];
            vecs[0] = new Vector(1.0, 0.0, 0.0);
            vecs[1] = new Vector(0.0, 1.0, 0.0);
            vecs[2] = new Vector(0.0, 0.0, 1.0);
            vecs[3] = new Vector(1.0, 0.0, 0.0);
            vecs[4] = new Vector(0.0, 1.0, 0.0);
            vecs[5] = new Vector(0.0, 0.0, 1.0);
            vecs[6] = new Vector(1e-15, 1e-11, 1.0);
            vecs[7] = new Vector(1e-11, 1e-15, -1.0);
            vecs[8] = new Vector(1e-15, 1e-11, 1.01);
            vecs[9] = new Vector(1e-11, 1e-15, -1.01);
            // Fill the rest with random points.
            for (int i = 10; i < vecs.Length; i++)
            {
                vecs[i] = new Vector(pdes.RandomDouble(), pdes.RandomDouble(), pdes.RandomDouble());
            }

            // Test the vector operations.
            bool vectorsPassed = true;

            for (int i = 0; i < vecs.Length; i++)
            {
                Vector   v      = vecs[i];
                bool     passed = true;
                Vector[] perps  = v.GetPerpendiculars();
                // All three vectors are perpendicular.
                passed = IsSmall(Vector.Dot(perps[0], perps[1]), 1e-13, "p0.p1");
                passed = passed && IsSmall(Vector.Dot(perps[0], v), 1e-13, "p0.v");
                passed = passed && IsSmall(Vector.Dot(v, perps[1]), 1e-13, "v.p1");
                // The computed vectors have length 1.
                passed = passed && IsSmall(Vector.Dot(perps[0], perps[0]) - 1.0, 1e-13, "p0.p0 - 1");
                passed = passed && IsSmall(Vector.Dot(perps[1], perps[1]) - 1.0, 1e-13, "p1.p1 - 1");
                // The vectors form a right-handed system.
                double len   = Math.Sqrt(Vector.Dot(v, v));
                Vector cross = Vector.Cross(perps[0], perps[1]);
                passed = passed && IsSmall(Vector.Dot(cross, v) / len - 1.0, 1e-13, "(p0xp1.v)/sqrt(v.v) - 1");

                if (!passed)
                {
                    Console.WriteLine("Vector test FAILED for i={0}", i);
                    vectorsPassed = false;
                    Console.WriteLine("  v  = {0}", v);
                    Console.WriteLine("  p0 = {0}", perps[0]);
                    Console.WriteLine("  p1 = {0}", perps[1]);
                }
            }
            if (vectorsPassed)
            {
                Console.WriteLine("Vector test passed");
            }
            else
            {
                Console.WriteLine("Vector test FAILED");
                allPassed = false;
            }

            if (allPassed)
            {
                Console.WriteLine("All unit tests passed");
            }
            else
            {
                Console.WriteLine("Some unit tests FAILED");
            }
        }