コード例 #1
0
            public static Vector3 randomOnRoad(Vector3 me, float radius)
            {
                float   t;
                Vector3 o = new Vector3(me.X, me.Y, me.Z);

                o.X += 2 * radius * ((float)RandGen.NextDouble()) - radius;
                o.Y += 2 * radius * ((float)RandGen.NextDouble()) - radius;

                unsafe
                {
                    Function.Call(Hash.GET_GROUND_Z_FOR_3D_COORD, o.X, o.Y, 10000f, &t);
                    //Function.Call(Hash.GET_CLOSEST_ROAD, 1986.5509f, 3802.58447f, 32.2808743f, 1.0f /*default: 1f*/, 1 /*default: 1*/, &x1, &y1, &za, &x2, &y2, false /*default: False*/);
                    if (t == 0)
                    {
                        return(new Vector3(0, 0, 0));
                    }

                    bool onR = Function.Call <bool>(Hash.IS_POINT_ON_ROAD, o.X, o.Y, t, 0);

                    o.Z = t;

                    if (onR)
                    {
                        return(o);
                    }
                }
                return(new Vector3(0, 0, 0));
            }
コード例 #2
0
        /**************************************************
        * Mathemagic here
        **************************************************/

        private double GaussianLuck()
        {
            // Box-Muller transform
            // See: https://stackoverflow.com/a/218600/149900
            double u1            = 1.0 - RandGen.NextDouble();                          // uniform(0,1] random doubles. subtraction necessary to prevent getting u1=0.0, which will blow up Math.Log
            double u2            = RandGen.NextDouble();                                // no such problem for u2. [0,1) is perfectly okay for Math.Sin
            double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(TAU * u2); // gaussian distrib around 0
            double randNormal    = (double)Config.GaussianLuckStdDev * randStdNormal;   // stretch horizontally to get wanted curve
            double newLuckValue  = randNormal / 1000.0;

            Monitor.Log($"Gaussian NewLuck: {newLuckValue}", LogLevel.Trace);
            return(newLuckValue);
        }
コード例 #3
0
        public HelperTCell(ITissue parent, int mpLen)
            : base(CellType.Helper, parent)
        {
            Activators = new List <Address>();

            // generate a random molecular pattern
            // NOTE: pattern vectors should be normalized
            var pat = new double[mpLen];

            for (int i = 0; i < mpLen; i++)
            {
                pat[i] = RandGen.NextDouble();
            }
            Pattern = new MolecularPattern(pat);
        }
コード例 #4
0
        /// <summary>
        /// Return an iterator of points in 3D euclidean space representing an uniform
        /// distribution on points on a unit sphere.
        /// </summary>
        /// <remarks>
        /// Based on the paper `Spherical Fibonacci Mapping, Benjamin Keinert et al.
        /// Journal ACM Transactions on Graphics, Volume 34 Issue 6, November 2015`.
        /// </remarks>
        public static IEnumerable <Vec3> Fibonnaci(int number, double jitter)
        {
            // Multiplication cheaper than division
            double oneDivN = 1.0 / (double)number;

            for (uint ind = 0; ind < number; ind++)
            {
                // Keep only decimal part of x
                // double x = InvGoldenRatio * ind;
                double x   = InvGoldenRatio * ind + jitter * RandGen.NextDouble();
                double phi = TWOPI * (x - Math.Truncate(x));

                double theta = Math.Acos(1.0 - (2.0 * (double)ind + 1.0 + jitter * RandGen.NextDouble()) * oneDivN);

                yield return(Geometry.SphericalToEuclidean(phi, theta));
            }
        }
コード例 #5
0
    void Start()
    {
        // BUILDING  ---  ---  BUILDING  ---  ---  BUILDING
        System.DateTime previousTime = System.DateTime.Now;

        var points = new List <Vec3>();
        int n      = seed;

        RandGen.Init(seed);


        switch (usedGenerator)
        {
        case Generator.Halton:
            points = SphereSampler.Halton(pointNumber, seed, bases[0], bases[1]).ToList();
            break;

        case Generator.Uniform:
            for (int i = 0; i < pointNumber; i++)
            {
                double phi   = 2.0 * System.Math.PI * RandGen.NextDouble();
                double theta = System.Math.Acos(2.0 * RandGen.NextDouble() - 1.0);

                points.Add(Geometry.SphericalToEuclidean(phi, theta));
                n++;
            }
            break;

        case Generator.Fibonnaci:
            // points = SphereSampler.Fibonnaci(pointNumber, 0.8).ToList();
            points = SphereSampler.Fibonnaci(pointNumber).ToList();
            break;

        case Generator.Poisson:
            points = SphereSampler.Poisson(pointNumber, 0.1f).ToList();
            Debug.Log(points.Count);
            break;
        }


        System.TimeSpan delta = System.DateTime.Now - previousTime;
        Debug.Log(string.Format("BUILDING *** {0} secondes OU {1} milliseconds *** BUILDING",
                                delta.TotalSeconds, delta.TotalMilliseconds));
        Debug.Log("Total generated points: " + points.Count);


        if (triangulate)
        {
            // INIT  ---  ---  INIT  ---  ---  INIT
            previousTime   = System.DateTime.Now;
            sphereMeshUsed = new SphericalMesh <int, int>(points.Select(x => Geometry.StereographicProjection(x)).ToArray(), false);
            delta          = System.DateTime.Now - previousTime;
            Debug.Log("***");
            Debug.Log(string.Format("*** INIT *** {0} secondes OU {1} milliseconds *** INIT",
                                    delta.TotalSeconds, delta.TotalMilliseconds));

            // TRIANGULATION  ---  ---  TRIANGULATION  ---  ---  TRIANGULATION
            previousTime = System.DateTime.Now;
            sphereMeshUsed.Construct();
            delta = System.DateTime.Now - previousTime;
            Debug.Log("***");
            Debug.Log(string.Format("*** TRIANGULATION *** {0} secondes OU {1} milliseconds *** TRIANGULATION",
                                    delta.TotalSeconds, delta.TotalMilliseconds));


            // // START DEBUGTOOLS
            // // START DEBUGTOOLS
            // // START DEBUGTOOLS

            // var test = radius * sphereMeshUsed.RightMostEdge.Rot.Origin;
            // if (triangulationOnSphere)
            // {
            //     test = radius * Geometry.InvStereographicProjection(sphereMeshUsed.RightMostEdge.Rot.Origin);
            // }
            // var newGo = GameObject.Instantiate(shape);
            // newGo.transform.SetParent(transform);
            // newGo.transform.position = test.AsVector3();
            // newGo.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            // // Color
            // var meshR = newGo.GetComponent<MeshRenderer>();
            // if (meshR != null)
            // {
            //     meshR.materials[0].color = Color.blue;
            // }

            // var test2 = radius * sphereMeshUsed.RightMostEdge.Destination;
            // if (triangulationOnSphere)
            // {
            //     test2 = radius * Geometry.InvStereographicProjection(sphereMeshUsed.RightMostEdge.Destination);
            // }
            // var newGo2 = GameObject.Instantiate(shape);
            // newGo2.transform.SetParent(transform);
            // newGo2.transform.position = test2.AsVector3();
            // newGo2.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            // // Color
            // var meshR2 = newGo2.GetComponent<MeshRenderer>();
            // if (meshR2 != null)
            // {
            //     meshR2.materials[0].color = Color.red;
            // }

            // int i = 0;
            // foreach (QuadEdge<int> edge in baseEdge.RightEdges())
            // {
            //     var test3 = radius * edge.Destination;
            //     if (triangulationOnSphere)
            //     {
            //         test3 = radius * Geometry.InvStereographicProjection(edge.Destination);
            //     }
            //     var newGo3 = GameObject.Instantiate(shape);
            //     newGo3.transform.SetParent(transform);
            //     newGo3.transform.position = test3.AsVector3();
            //     newGo3.transform.localScale = new Vector3(4.0f, 4.0f, 4.0f);
            //     // Color
            //     var meshR3 = newGo3.GetComponent<MeshRenderer>();
            //     if (meshR3 != null)
            //     {
            //         meshR3.materials[0].color = Color.yellow;
            //     }

            //     if (i >= 0)
            //     {
            //         break;
            //     }
            //     i++;
            // }

            // // END DEBUGTOOLS
            // // END DEBUGTOOLS
            // // END DEBUGTOOLS



            // DRAWING  ---  ---  DRAWING  ---  ---  DRAWING
            List <Vec3> triangles;


            if (triangulationOnSphere)
            {
                triangles = sphereMeshUsed.Triangles().ForEach(x => Geometry.InvStereographicProjection(x) * radius).ToList();
            }
            else
            {
                // Remove first triangles
                triangles = sphereMeshUsed.Triangles().ForEach(x => x * radius).ToList();
            }

            TriangleDrawer.DrawLine(triangles, transform, mat, Color.black, lineScale);
            TriangleDrawer.DrawFace(triangles, transform, mat, gradient);
            TriangleDrawer.DrawPoints(triangles, transform, shape, Color.red, scale);

            // Remove first face (reconstructed hole)
            // List<Face<int, int>> faces = sphereMeshUsed.Faces(FaceConfig.Voronoi, radius).Collection().Skip(1).ToList();
            List <Face <int, int> > faces = sphereMeshUsed.Faces(FaceConfig.Voronoi, radius).ToList();


            // // START DEBUGTOOLS
            // // START DEBUGTOOLS
            // // START DEBUGTOOLS

            // int i = 0;
            // foreach (QuadEdge<int> edge in sphereMeshUsed.RightMostEdge.Oprev.Sym.Lnext.Oprev.FaceLeftEdges())
            // // foreach (QuadEdge<int> edge in sphereMeshUsed.RightMostEdge.Oprev.Rprev.EdgesFrom())
            // {
            //     var test3 = edge.Destination;
            //     // test3 = radius * test3;
            //     // if (triangulationOnSphere)
            //     // {
            //     //     test3 = radius * Geometry.InvStereographicProjection(edge.Destination);
            //     // }
            //     var newGo3 = GameObject.Instantiate(shape);
            //     newGo3.transform.SetParent(transform);
            //     newGo3.transform.position = test3.AsVector3();
            //     newGo3.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            //     // Color
            //     var meshR3 = newGo3.GetComponent<MeshRenderer>();
            //     if (meshR3 != null)
            //     {
            //         meshR3.materials[0].color = Color.yellow;
            //     }

            //     if (i >= 1)
            //     {
            //         // Add fake point created
            //         break;
            //     }
            //     else
            //     {
            //         test3 = Geometry.CircumCenter3D(Geometry.InvStereographicProjection(edge.RotSym.Origin),
            //                                         Geometry.InvStereographicProjection(edge.RotSym.Destination),
            //                                         Geometry.InvStereographicProjection(edge.RotSym.Oprev.Destination));

            //         Vec3 a = Geometry.InvStereographicProjection(edge.RotSym.Origin);
            //         Vec3 b = Geometry.InvStereographicProjection(edge.RotSym.Destination);
            //         Vec3 c  = Geometry.InvStereographicProjection(edge.RotSym.Oprev.Destination);

            //         Debug.Log(a);
            //         Debug.Log(b);
            //         Debug.Log(c);

            //         Vec3 ca = c - a;
            //         Vec3 ba = b - a;

            //         Vec3 baca = Vec3.Cross(ba, ca);
            //         Debug.Log(baca.SquaredMagnitude);
            //         Debug.Log(test3);



            //         var newGo31 = GameObject.Instantiate(shape);
            //         newGo31.transform.SetParent(transform);
            //         newGo31.transform.position = (radius * Geometry.InvStereographicProjection(edge.RotSym.Origin)).AsVector3();
            //         newGo31.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            //         // Color
            //         var meshR31 = newGo31.GetComponent<MeshRenderer>();
            //         if (meshR31 != null)
            //         {
            //             meshR31.materials[0].color = Color.grey;
            //         }

            //         var newGo32 = GameObject.Instantiate(shape);
            //         newGo32.transform.SetParent(transform);
            //         newGo32.transform.position = (radius * Geometry.InvStereographicProjection(edge.RotSym.Destination)).AsVector3();
            //         newGo32.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            //         // Color
            //         var meshR32 = newGo32.GetComponent<MeshRenderer>();
            //         if (meshR32 != null)
            //         {
            //             meshR32.materials[0].color = Color.grey;
            //         }

            //         var newGo33 = GameObject.Instantiate(shape);
            //         newGo33.transform.SetParent(transform);
            //         newGo33.transform.position = (radius * Geometry.InvStereographicProjection(edge.RotSym.Oprev.Destination)).AsVector3();
            //         newGo33.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
            //         // Color
            //         var meshR33 = newGo33.GetComponent<MeshRenderer>();
            //         if (meshR33 != null)
            //         {
            //             meshR33.materials[0].color = Color.grey;
            //         }

            //         double invDistanceScaled = radius / test3.Magnitude;
            //         test3 *= invDistanceScaled;

            //         var newGo34 = GameObject.Instantiate(shape);
            //         newGo34.transform.SetParent(transform);
            //         newGo34.transform.position = test3.AsVector3();
            //         newGo34.transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
            //         // Color
            //         var meshR34 = newGo34.GetComponent<MeshRenderer>();
            //         if (meshR34 != null)
            //         {
            //             meshR34.materials[0].color = Color.green;
            //         }
            //     }


            //     // if (!Geometry.Ccw(edge.Destination, edge.Lprev.Destination, edge.Lprev.Origin))
            //     // {
            //     //     Debug.Log("FOUND");
            //     //     if (meshR3 != null)
            //     //     {
            //     //         meshR3.materials[0].color = Color.magenta;
            //     //     }
            //     // }
            //     i++;
            // }

            // // END DEBUGTOOLS
            // // END DEBUGTOOLS
            // // END DEBUGTOOLS


            if (triangulationOnSphere)
            {
                float nbCells   = (float)faces.Count;
                int   indcolor2 = 0;
                foreach (Face <int, int> face in faces)
                {
                    var color = gradient.Evaluate(indcolor2 / nbCells);

                    face.DrawFace(transform, mat, color, scale: radius);
                    face.DrawLine(transform, mat, Color.white, lineScale, loop: true);
                    face.DrawPoints(transform, shape, mat, Color.blue, 0.6f * scale);

                    indcolor2++;

                    // if (indcolor2 > 11)
                    // {
                    //     break;
                    // }
                }
            }
        }
        else
        {
            // DRAWING  ---  ---  DRAWING  ---  ---  DRAWING
            points = points.Select(x => x * radius).ToList();
            int ptId = 0;
            foreach (Vec3 point in points)
            {
                var newGo = GameObject.Instantiate(shape);
                newGo.name = string.Format("Fibonnaci Sphere {0}", ptId.ToString());
                newGo.transform.SetParent(transform);
                newGo.transform.position   = point.AsVector3();
                newGo.transform.localScale = new Vector3(scale, scale, scale);
                // Color
                var meshR = newGo.GetComponent <MeshRenderer>();
                if (meshR != null)
                {
                    meshR.materials[0].color = Color.black;
                }

                ptId++;
            }
        }
    }