Ejemplo n.º 1
0
    private void StartConvexHull()
    {
        //Get random points in 3d space
        //HashSet<Vector3> points_Unity = TestAlgorithmsHelpMethods.GenerateRandomPoints3D(seed: 0, halfCubeSize: 1f, numberOfPoints: 50);

        //Get random points on a sphere
        HashSet <Vector3> points_Unity = TestAlgorithmsHelpMethods.GenerateRandomPointsOnSphere(seed: 0, radius: 1f, numberOfPoints: 50);

        //Generate points we can display
        foreach (Vector3 p in points_Unity)
        {
            GameObject newPoint = Instantiate(pointObj, p, Quaternion.identity);

            newPoint.SetActive(true);

            allPoints.Add(newPoint);
        }

        //Standardize the data
        //To MyVector3
        HashSet <MyVector3> points = new HashSet <MyVector3>(points_Unity.Select(x => x.ToMyVector3()));

        //Normalize
        normalizer = new Normalizer3(new List <MyVector3>(points));

        HashSet <MyVector3> points_normalized = normalizer.Normalize(points);


        VisualizeIterativeConvexHull visualizeThisAlgorithm = GetComponent <VisualizeIterativeConvexHull>();

        visualizeThisAlgorithm.StartVisualizer(points_normalized);
    }
Ejemplo n.º 2
0
    //Generates points, delaunay triangulation, and voronoi diagram
    public void Generate()
    {
        if (radius <= 0f)
        {
            radius = 0.01f;
        }
        if (numberOfPoints < 4)
        {
            numberOfPoints = 4;
        }

        //Get random points in 3d space
        points_Unity = TestAlgorithmsHelpMethods.GenerateRandomPointsOnSphere(seed, radius, numberOfPoints);

        //To MyVector3
        HashSet <MyVector3> points = new HashSet <MyVector3>(points_Unity.Select(x => x.ToMyVector3()));

        //Normalize
        Normalizer3 normalizer = new Normalizer3(new List <MyVector3>(points));

        HashSet <MyVector3> points_normalized = normalizer.Normalize(points);


        System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();


        //
        // Generate the convex hull, which is the same as the Delaunay triangulation of points on the sphere
        //

        //Iterative algorithm
        timer.Start();

        HalfEdgeData3 convexHull_normalized = _ConvexHull.Iterative_3D(points_normalized, removeUnwantedTriangles: false, normalizer);

        timer.Stop();

        Debug.Log($"Generated a 3d convex hull in {timer.ElapsedMilliseconds / 1000f} seconds");

        if (convexHull_normalized == null)
        {
            return;
        }



        //
        // Generate the voronoi diagram from the delaunay triangulation
        //
        timer.Restart();

        HashSet <VoronoiCell3> voronoiCells_normalized = _Voronoi.Delaunay3DToVoronoi(convexHull_normalized);

        timer.Stop();

        Debug.Log($"Generated a 3d voronoi diagram in {timer.ElapsedMilliseconds / 1000f} seconds");

        if (voronoiCells_normalized == null)
        {
            return;
        }


        //
        // Display
        //

        //Delaunay
        HalfEdgeData3 convexHull = normalizer.UnNormalize(convexHull_normalized);

        MyMesh myMesh = convexHull.ConvertToMyMesh("convex hull aka delaunay triangulation", MyMesh.MeshStyle.HardEdges);

        delaunayMesh = myMesh.ConvertToUnityMesh(generateNormals: false);


        //Voronoi
        HashSet <VoronoiCell3> voronoiCells = normalizer.UnNormalize(voronoiCells_normalized);

        //Generate a mesh for each separate cell
        voronoiCellsMeshes = GenerateVoronoiCellsMeshes(voronoiCells);

        //Generate a single mesh for all cells where each vertex has a color belonging to that cell
        //Now we can display the mesh with an unlit shader where each vertex is associated with a color belonging to that cell
        //The problem is that the voronoi cell is not a flat surface on the mesh
        //But it looks flat if we are using an unlit shader
        Mesh oneMesh = GenerateAndDisplaySingleMesh(voronoiCellsMeshes);

        if (meshFilter != null)
        {
            meshFilter.mesh = oneMesh;
        }
    }